merkletree: make some code a bit more obvious and general

This commit is contained in:
Andrew Ayer 2023-02-16 20:36:13 -05:00
parent 935226b047
commit 1bde49894c
1 changed files with 3 additions and 3 deletions

View File

@ -44,12 +44,12 @@ func CloneCollapsedTree(source *CollapsedTree) *CollapsedTree {
func (tree *CollapsedTree) Add(hash Hash) { func (tree *CollapsedTree) Add(hash Hash) {
tree.nodes = append(tree.nodes, hash) tree.nodes = append(tree.nodes, hash)
tree.size++ tree.size++
size := tree.size
for size%2 == 0 { numNodes := calculateNumNodes(tree.size)
for len(tree.nodes) > numNodes {
left, right := tree.nodes[len(tree.nodes)-2], tree.nodes[len(tree.nodes)-1] left, right := tree.nodes[len(tree.nodes)-2], tree.nodes[len(tree.nodes)-1]
tree.nodes = tree.nodes[:len(tree.nodes)-2] tree.nodes = tree.nodes[:len(tree.nodes)-2]
tree.nodes = append(tree.nodes, HashChildren(left, right)) tree.nodes = append(tree.nodes, HashChildren(left, right))
size /= 2
} }
} }