Skip to content

Commit

Permalink
feat: add function which returns slice of txids contained within. (#80)
Browse files Browse the repository at this point in the history
* feat: add function which returns slice of txids contained within.

Signed-off-by: Darren Kellenschwiler <[email protected]>

* explain

Signed-off-by: Darren Kellenschwiler <[email protected]>

---------

Signed-off-by: Darren Kellenschwiler <[email protected]>
  • Loading branch information
sirdeggen authored Nov 21, 2023
1 parent 6f0ecba commit 8859782
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
13 changes: 13 additions & 0 deletions bump.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ func (bump *BUMP) Bytes() ([]byte, error) {
return bytes, nil
}

// String encodes a BUMP as a hex string.
func (bump *BUMP) String() (string, error) {
bytes, err := bump.Bytes()
if err != nil {
Expand All @@ -149,6 +150,18 @@ func (bump *BUMP) String() (string, error) {
return hex.EncodeToString(bytes), nil
}

// Txids returns the txids within the BUMP which the client is expecting.
// This allows a client to receive one BUMP for a whole block and it will know which txids it should update.
func (bump *BUMP) Txids() []string {
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 8859782

Please sign in to comment.