Skip to content

Commit

Permalink
feat: add function which returns slice of txids contained within.
Browse files Browse the repository at this point in the history
Signed-off-by: Darren Kellenschwiler <[email protected]>
  • Loading branch information
sirdeggen committed Nov 21, 2023
1 parent 6f0ecba commit 1733c96
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
10 changes: 10 additions & 0 deletions bump.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,16 @@ func (bump *BUMP) String() (string, error) {
return hex.EncodeToString(bytes), nil
}

func (bump *BUMP) Txids() []string {

Check failure on line 152 in bump.go

View workflow job for this annotation

GitHub Actions / lint (1.17.x, ubuntu-latest)

exported: exported method BUMP.Txids should have comment or be unexported (revive)
txids := make([]string, 0)
for _, leaf := range bump.Path[0] {
if leaf.Txid != nil {
txids = append(txids, *leaf.Hash)
}
}
return txids
}

// CalculateRootGivenTxid calculates the root of the Merkle tree given a txid.
func (bump *BUMP) CalculateRootGivenTxid(txid string) (string, error) {
if len(bump.Path) == 1 {
Expand Down
16 changes: 16 additions & 0 deletions bump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,19 @@ func TestNotEnoughLeavesInHeight(t *testing.T) {
_, err := NewBUMPFromStr("01020101026d053ad9c0dec4f973cff9273394c41f3801e7d80e85964fe211d3fdee772f9100")
require.Error(t, err)
}

func TestTxids(t *testing.T) {
chainHashBlock := make([]*chainhash.Hash, 0)
for _, txid := range testnetBlockExample {
hash, err := chainhash.NewHashFromStr(txid)
require.NoError(t, err)
chainHashBlock = append(chainHashBlock, hash)
}
merkles, err := BuildMerkleTreeStoreChainHash(chainHashBlock)
require.NoError(t, err)

bump, err := NewBUMPFromMerkleTreeAndIndex(1575794, merkles, uint64(0))
require.NoError(t, err)
txids := bump.Txids()
require.Equal(t, []string{testnetBlockExample[0]}, txids)
}

0 comments on commit 1733c96

Please sign in to comment.