blockchain, btcutil/bloom: BuildMerkleTreeStore returns chainhash.Hash

BuildMerkleTreeStore used to return a pointer, but it is changed to
return a chainhash.Hash directly.  This allows the compiler to make
optimizations in some cases and avoids a memory allocation.
This commit is contained in:
Calvin Kim 2023-08-03 15:21:11 +09:00
parent 52ede324be
commit 3ba9feeeee
2 changed files with 7 additions and 7 deletions

View file

@ -58,14 +58,13 @@ func nextPowerOfTwo(n int) int {
// HashMerkleBranches takes two hashes, treated as the left and right tree
// nodes, and returns the hash of their concatenation. This is a helper
// function used to aid in the generation of a merkle tree.
func HashMerkleBranches(left *chainhash.Hash, right *chainhash.Hash) *chainhash.Hash {
func HashMerkleBranches(left, right *chainhash.Hash) chainhash.Hash {
// Concatenate the left and right nodes.
var hash [chainhash.HashSize * 2]byte
copy(hash[:chainhash.HashSize], left[:])
copy(hash[chainhash.HashSize:], right[:])
newHash := chainhash.DoubleHashH(hash[:])
return &newHash
return chainhash.DoubleHashH(hash[:])
}
// BuildMerkleTreeStore creates a merkle tree from a slice of transactions,
@ -140,13 +139,13 @@ func BuildMerkleTreeStore(transactions []*btcutil.Tx, witness bool) []*chainhash
// hashing the concatenation of the left child with itself.
case merkles[i+1] == nil:
newHash := HashMerkleBranches(merkles[i], merkles[i])
merkles[offset] = newHash
merkles[offset] = &newHash
// The normal case sets the parent node to the double sha256
// of the concatentation of the left and right children.
default:
newHash := HashMerkleBranches(merkles[i], merkles[i+1])
merkles[offset] = newHash
merkles[offset] = &newHash
}
offset++
}

View file

@ -6,9 +6,9 @@ package bloom
import (
"github.com/btcsuite/btcd/blockchain"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcd/btcutil"
)
// merkleBlock is used to house intermediate information needed to generate a
@ -41,7 +41,8 @@ func (m *merkleBlock) calcHash(height, pos uint32) *chainhash.Hash {
} else {
right = left
}
return blockchain.HashMerkleBranches(left, right)
res := blockchain.HashMerkleBranches(left, right)
return &res
}
// traverseAndBuild builds a partial merkle tree using a recursive depth-first