-
8000
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
blockchain: rolling merkle root #1421
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
39b1775
to
bd42fc6
Compare
bd42fc6
to
3a6c0e3
Compare
This commit adds an explicit assertion that the merkle root is properly computed when an odd number of leaves are present, and a left node must be hashed with itself to create a valid root.
3a6c0e3
to
c3ee68e
Compare
type RollingMerkleTree struct { | ||
// nodes contains all nodes in the tree. The nodes may correspond to | ||
// interior nodes if their children have already been pruned. | ||
nodes map[uint32]chainhash.Hash |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't this be better served as a slice? So we just index in rather than having the overhead of a map. We'd also gain memory locality for all the items as well.
merkleStoreTree := BuildMerkleTreeStore(block.Transactions(), false) | ||
merkleStoreRoot := merkleStoreTree[len(merkleStoreTree)-1] | ||
|
||
if calcMerkleRoot != *merkleStoreRoot { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
@jcvernaleo (as per #1530)
|
@cfromknecht mind rebasing so the actions can run? |
Superseded by #1979 |
I noticed BuildMerkleTreeStore is still in the codebase even though it's replaced with RollingMerkleTree and CalcMerkleRoot. Should we remove it from blockchain/merkle.go and blockchain/merkle_test.go? |
This PR adds a new
RollingMerkleTree
type for efficiently computing merkle roots. The leavesof the tree are iteratively pushed onto the tree, and any complete substrees are evaluated and
stored so that subsequent pushes may consume them when their sibling tree is complete. As a
result, this requires
O(log n)
additional memory be allocated. The currentBuildMerkleTreeStore
allocates an array of up to 4 times the number of leaves, which is used to store internal nodes and the root.
For 2000 leaves, the benchmarks show that this method is about 7% slower (~90 microseconds), but uses 99.04% less memory and 99.85% fewer allocations. These allocations reduce gc pressure, especially during IBD when the large slices of hashes currently used are almost immediately discarded.
alternative to #1377