Skip to content

Commit 4089920

Browse files
committed
merkle root is the txid if there is only one
Signed-off-by: Darren Kellenschwiler <[email protected]>
1 parent 58361c8 commit 4089920

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

bump.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ func (bump *BUMP) String() (string, error) {
141141

142142
// CalculateRootGivenTxid calculates the root of the Merkle tree given a txid.
143143
func (bump *BUMP) CalculateRootGivenTxid(txid string) (string, error) {
144+
if len(bump.Path) < 2 {
145+
return txid, nil
146+
}
144147
// Find the index of the txid at the lowest level of the Merkle tree
145148
var index uint64
146149
txidFound := false
@@ -241,7 +244,7 @@ func NewBUMPFromMerkleTreeAndIndex(blockHeight uint64, merkleTree []*chainhash.H
241244
} else {
242245
sh := merkleTree[0].String()
243246
o := uint64(0)
244-
bump.Path[0][0] = leaf{Hash: &sh, Offset: &o, Txid: &t}
247+
bump.Path = [][]leaf{{leaf{Hash: &sh, Offset: &o, Txid: &t}}}
245248
}
246249

247250
return bump, nil

bump_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const (
1515
txidExample = `3ecead27a44d013ad1aae40038acbb1883ac9242406808bb4667c15b4f164eac`
1616
rootOfBlockTxExample = `1a1e779cd7dfc59f603b4e88842121001af822b2dc5d3b167ae66152e586a6b0`
1717
fakeMadeUpNum = 814435
18+
txidSmallBlock = `be7853d7685ed5b5ec61a0ca8e3f193a7b0632cb2db950528c2041ee5d7dd95d`
1819
)
1920

2021
var blockTxExample = []string{
@@ -28,6 +29,20 @@ var blockTxExample = []string{
2829
"397fe2ae4d1d24efcc868a02daae42d1b419289d9a1ded3a5fe771efcc1219d9",
2930
}
3031

32+
func TestNewBUMPFromMerkleTreeWithOnlyOneTxid(t *testing.T) {
33+
chainHashBlock := make([]*chainhash.Hash, 0)
34+
hash, err := chainhash.NewHashFromStr(txidSmallBlock)
35+
require.NoError(t, err)
36+
chainHashBlock = append(chainHashBlock, hash)
37+
merkles, err := BuildMerkleTreeStoreChainHash(chainHashBlock)
38+
require.NoError(t, err)
39+
bump, err := NewBUMPFromMerkleTreeAndIndex(fakeMadeUpNum, merkles, uint64(0))
40+
require.NoError(t, err)
41+
root, err := bump.CalculateRootGivenTxid(txidSmallBlock)
42+
require.NoError(t, err)
43+
require.Equal(t, txidSmallBlock, root)
44+
}
45+
3146
func TestNewBUMPFromMerkleTree(t *testing.T) {
3247
chainHashBlock := make([]*chainhash.Hash, 0)
3348
for _, txid := range blockTxExample {

0 commit comments

Comments
 (0)