diff --git a/merkletree/collapsed_tree.go b/merkletree/collapsed_tree.go index 09eadb7..ce60c96 100644 --- a/merkletree/collapsed_tree.go +++ b/merkletree/collapsed_tree.go @@ -25,10 +25,12 @@ func calculateNumNodes(size uint64) int { return bits.OnesCount64(size) } +// TODO: phase out this function func EmptyCollapsedTree() *CollapsedTree { return &CollapsedTree{nodes: []Hash{}, size: 0} } +// TODO: phase out this function func NewCollapsedTree(nodes []Hash, size uint64) (*CollapsedTree, error) { tree := new(CollapsedTree) if err := tree.Init(nodes, size); err != nil { @@ -37,23 +39,24 @@ func NewCollapsedTree(nodes []Hash, size uint64) (*CollapsedTree, error) { return tree, nil } -func CloneCollapsedTree(source *CollapsedTree) *CollapsedTree { - nodes := make([]Hash, len(source.nodes)) - copy(nodes, source.nodes) - return &CollapsedTree{nodes: nodes, size: source.size} -} - func (tree CollapsedTree) Equal(other CollapsedTree) bool { return tree.size == other.size && slices.Equal(tree.nodes, other.nodes) } +func (tree CollapsedTree) Clone() CollapsedTree { + return CollapsedTree{ + nodes: slices.Clone(tree.nodes), + size: tree.size, + } +} + func (tree *CollapsedTree) Add(hash Hash) { tree.nodes = append(tree.nodes, hash) tree.size++ tree.collapse() } -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) if other.size > maxSize {