merkletree.Append: fix appending to empty trees

This commit is contained in:
Andrew Ayer 2024-06-09 11:13:16 -04:00
parent cc98a06bcb
commit 759631f7e6
1 changed files with 5 additions and 3 deletions

View File

@ -54,10 +54,12 @@ func (tree *CollapsedTree) Add(hash Hash) {
} }
func (tree *CollapsedTree) Append(other *CollapsedTree) error { func (tree *CollapsedTree) Append(other *CollapsedTree) error {
if tree.size > 0 {
maxSize := uint64(1) << bits.TrailingZeros64(tree.size) maxSize := uint64(1) << bits.TrailingZeros64(tree.size)
if other.size > maxSize { 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) 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.nodes = append(tree.nodes, other.nodes...)
tree.size += other.size tree.size += other.size