diff --git a/go.mod b/go.mod index 000ecba..f022c5e 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/zecrey-labs/zecrey-smt -go 1.19 +go 1.20 require ( github.com/alicebob/miniredis/v2 v2.22.0 diff --git a/interface.go b/interface.go index 7e47b3c..8a9a69c 100644 --- a/interface.go +++ b/interface.go @@ -24,6 +24,7 @@ type ( LatestVersion() Version RecentVersion() Version Reset() + Clear() error Commit(recentVersion *Version) (Version, error) Rollback(version Version) error } diff --git a/smt.go b/smt.go index b99ad15..3df0faf 100644 --- a/smt.go +++ b/smt.go @@ -739,6 +739,24 @@ func (tree *BNBSparseMerkleTree) Reset() { tree.rootSize = tree.lastSaveRootSize } +func (tree *BNBSparseMerkleTree) Clear() error { + tree.root = NewTreeNode(0, 0, tree.nilHashes, tree.hasher) + tree.version = Version(0) + tree.recentVersion = Version(0) + + storageTreeNode := &StorageTreeNode{} + tree.root = storageTreeNode.ToTreeNode(0, tree.nilHashes, tree.hasher) + + tree.rootSize = tree.root.Size() + for i := 0; i < len(tree.root.Children); i++ { + if tree.root.Children[i] != nil { + tree.rootSize += uint64(versionSize * len(tree.root.Children[i].Versions)) + } + } + _, err := tree.Commit(nil) + return err +} + func (tree *BNBSparseMerkleTree) writeNode(db database.Batcher, fullNode *TreeNode, version Version, recentVersion *Version) (uint64, error) { changed := uint64(0) if fullNode.PreviousVersion() > tree.gcStatus.latestGCVersion {