merkletree: add Append method

This commit is contained in:
Andrew Ayer 2023-02-18 21:14:36 -05:00
parent 1413b877f3
commit 007f24feee
1 changed files with 12 additions and 0 deletions

View File

@ -47,6 +47,18 @@ func (tree *CollapsedTree) Add(hash Hash) {
tree.collapse()
}
func (tree *CollapsedTree) Append(other *CollapsedTree) error {
maxSize := uint64(1) << bits.TrailingZeros64(tree.size)
if other.size > maxSize {
return fmt.Errorf("tree of size %d is too large to append to a tree of size %d (maximum size is %d)", other.size, tree.size, maxSize)
}
tree.nodes = append(tree.nodes, other.nodes...)
tree.size += other.size
tree.collapse()
return nil
}
func (tree *CollapsedTree) collapse() {
numNodes := calculateNumNodes(tree.size)
for len(tree.nodes) > numNodes {