merkletree: factor out collapsing logic

This commit is contained in:
Andrew Ayer 2023-02-16 20:36:37 -05:00
parent 1bde49894c
commit 1413b877f3
1 changed files with 3 additions and 0 deletions

View File

@ -44,7 +44,10 @@ 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++
tree.collapse()
}
func (tree *CollapsedTree) collapse() {
numNodes := calculateNumNodes(tree.size) numNodes := calculateNumNodes(tree.size)
for len(tree.nodes) > numNodes { 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]