merkletree: add Append method
This commit is contained in:
parent
1413b877f3
commit
007f24feee
|
@ -47,6 +47,18 @@ func (tree *CollapsedTree) Add(hash Hash) {
|
||||||
tree.collapse()
|
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() {
|
func (tree *CollapsedTree) collapse() {
|
||||||
numNodes := calculateNumNodes(tree.size)
|
numNodes := calculateNumNodes(tree.size)
|
||||||
for len(tree.nodes) > numNodes {
|
for len(tree.nodes) > numNodes {
|
||||||
|
|
Loading…
Reference in New Issue