From 8859782392d940f2f691bf2f22324f064a958b92 Mon Sep 17 00:00:00 2001 From: deggen Date: Tue, 21 Nov 2023 13:46:17 -0600 Subject: [PATCH] feat: add function which returns slice of txids contained within. (#80) * feat: add function which returns slice of txids contained within. Signed-off-by: Darren Kellenschwiler * explain Signed-off-by: Darren Kellenschwiler --------- Signed-off-by: Darren Kellenschwiler --- bump.go | 13 +++++++++++++ bump_test.go | 16 ++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/bump.go b/bump.go index 6e8093b..6eabc61 100644 --- a/bump.go +++ b/bump.go @@ -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 { @@ -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 { diff --git a/bump_test.go b/bump_test.go index 946ca6a..2d908a8 100644 --- a/bump_test.go +++ b/bump_test.go @@ -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) +}