30 lines
838 B
Go
30 lines
838 B
Go
|
package merkletrie
|
||
|
|
||
|
import "github.com/btcsuite/btcd/chaincfg/chainhash"
|
||
|
|
||
|
func hashMerkleBranches(left *chainhash.Hash, 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
|
||
|
}
|
||
|
|
||
|
func computeMerkleRoot(hashes []*chainhash.Hash) *chainhash.Hash {
|
||
|
if len(hashes) <= 0 {
|
||
|
return nil
|
||
|
}
|
||
|
for len(hashes) > 1 {
|
||
|
if (len(hashes) & 1) > 0 { // odd count
|
||
|
hashes = append(hashes, hashes[len(hashes)-1])
|
||
|
}
|
||
|
for i := 0; i < len(hashes); i += 2 { // TODO: parallelize this loop (or use a lib that does it)
|
||
|
hashes[i>>1] = hashMerkleBranches(hashes[i], hashes[i+1])
|
||
|
}
|
||
|
hashes = hashes[:len(hashes)>>1]
|
||
|
}
|
||
|
return hashes[0]
|
||
|
}
|