From 3ba9feeeee77cda47705b601ed06a799ea2e5a3a Mon Sep 17 00:00:00 2001 From: Calvin Kim Date: Thu, 3 Aug 2023 15:21:11 +0900 Subject: [PATCH] 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. --- blockchain/merkle.go | 9 ++++----- btcutil/bloom/merkleblock.go | 5 +++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/blockchain/merkle.go b/blockchain/merkle.go index d7e567b2..b8e309ae 100644 --- a/blockchain/merkle.go +++ b/blockchain/merkle.go @@ -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++ } diff --git a/btcutil/bloom/merkleblock.go b/btcutil/bloom/merkleblock.go index 101a8f91..88dd274f 100644 --- a/btcutil/bloom/merkleblock.go +++ b/btcutil/bloom/merkleblock.go @@ -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