@@ -12,7 +12,7 @@ import (
1212// GetMerkleProofForCoinbase returns a merkle proof for the coinbase transaction
1313func GetMerkleProofForCoinbase (subtrees []* Subtree ) ([]* chainhash.Hash , error ) {
1414 if len (subtrees ) == 0 {
15- return nil , fmt . Errorf ( "no subtrees available" )
15+ return nil , ErrNoSubtreesAvailable
1616 }
1717
1818 merkleProof , err := subtrees [0 ].GetMerkleProof (0 )
@@ -42,7 +42,7 @@ func GetMerkleProofForCoinbase(subtrees []*Subtree) ([]*chainhash.Hash, error) {
4242}
4343
4444// BuildMerkleTreeStoreFromBytes builds a merkle tree from the given nodes.
45- func BuildMerkleTreeStoreFromBytes (nodes []SubtreeNode ) (* []chainhash.Hash , error ) {
45+ func BuildMerkleTreeStoreFromBytes (nodes []Node ) (* []chainhash.Hash , error ) {
4646 if len (nodes ) == 0 {
4747 return & []chainhash.Hash {}, nil
4848 }
@@ -94,7 +94,7 @@ func BuildMerkleTreeStoreFromBytes(nodes []SubtreeNode) (*[]chainhash.Hash, erro
9494}
9595
9696// calcMerkles calculates the merkle hashes for the given nodes in the range
97- func calcMerkles (nodes []SubtreeNode , merkleFrom , merkleTo , nextPoT , length int , merkles []chainhash.Hash ) {
97+ func calcMerkles (nodes []Node , merkleFrom , merkleTo , nextPoT , length int , merkles []chainhash.Hash ) {
9898 var offset int
9999
100100 var currentMerkle chainhash.Hash
@@ -103,30 +103,37 @@ func calcMerkles(nodes []SubtreeNode, merkleFrom, merkleTo, nextPoT, length int,
103103
104104 for i := merkleFrom ; i < merkleTo ; i += 2 {
105105 offset = i / 2
106+ currentMerkle , currentMerkle1 = getMerklePair (nodes , merkles , i , nextPoT , length )
107+ merkles [offset ] = calcMerkle (currentMerkle , currentMerkle1 )
108+ }
109+ }
106110
107- if i < nextPoT {
108- if i >= length {
109- currentMerkle = chainhash.Hash {}
110- } else {
111- currentMerkle = nodes [i ].Hash
112- }
111+ // getMerklePair returns a pair of merkle hashes at the given index
112+ func getMerklePair (nodes []Node , merkles []chainhash.Hash , i , nextPoT , length int ) (chainhash.Hash , chainhash.Hash ) {
113+ var currentMerkle , currentMerkle1 chainhash.Hash
113114
114- if i + 1 >= length {
115- currentMerkle1 = chainhash.Hash {}
116- } else {
117- currentMerkle1 = nodes [i + 1 ].Hash
118- }
119- } else {
120- currentMerkle = merkles [i - nextPoT ]
121- currentMerkle1 = merkles [i - nextPoT + 1 ]
122- }
115+ if i < nextPoT {
116+ currentMerkle = getNodeHashAt (nodes , i , length )
117+ currentMerkle1 = getNodeHashAt (nodes , i + 1 , length )
118+ } else {
119+ currentMerkle = merkles [i - nextPoT ]
120+ currentMerkle1 = merkles [i - nextPoT + 1 ]
121+ }
123122
124- merkles [offset ] = calcMerkle (currentMerkle , currentMerkle1 )
123+ return currentMerkle , currentMerkle1
124+ }
125+
126+ // getNodeHashAt returns the hash at the given index, or an empty hash if out of bounds
127+ func getNodeHashAt (nodes []Node , index , length int ) chainhash.Hash {
128+ if index >= length {
129+ return chainhash.Hash {}
125130 }
131+
132+ return nodes [index ].Hash
126133}
127134
128135// calcMerkle calculates the parent node hash from the left and right child nodes
129- func calcMerkle (currentMerkle chainhash. Hash , currentMerkle1 chainhash.Hash ) [32 ]byte {
136+ func calcMerkle (currentMerkle , currentMerkle1 chainhash.Hash ) [32 ]byte {
130137 switch {
131138 // When there is no left child node, the parent is nil ("") too.
132139 case currentMerkle .Equal (chainhash.Hash {}):
0 commit comments