diff --git a/interface.go b/interface.go index 8a9a69c..6390440 100644 --- a/interface.go +++ b/interface.go @@ -24,7 +24,7 @@ type ( LatestVersion() Version RecentVersion() Version Reset() - Clear() error + Clear(recentVersion *Version) error Commit(recentVersion *Version) (Version, error) Rollback(version Version) error } diff --git a/smt.go b/smt.go index 2f7d655..29e0026 100644 --- a/smt.go +++ b/smt.go @@ -739,11 +739,22 @@ func (tree *BNBSparseMerkleTree) Reset() { tree.rootSize = tree.lastSaveRootSize } -func (tree *BNBSparseMerkleTree) Clear() error { +func (tree *BNBSparseMerkleTree) Clear(recentVersion *Version) (err error) { + version := Version(0) + if recentVersion != nil { + version = *recentVersion + } tree.root = NewTreeNode(0, 0, tree.nilHashes, tree.hasher) - tree.version = Version(0) - tree.recentVersion = Version(0) - tree.journal = newJournal() + tree.rootSize = 0 + tree.lastSaveRoot = tree.root + tree.lastSaveRootSize = 0 + tree.version = version + tree.recentVersion = version + tree.journal.flush() + tree.dbCache, err = lru.New(tree.dbCacheSize) + if err != nil { + return err + } storageTreeNode := &StorageTreeNode{} tree.root = storageTreeNode.ToTreeNode(0, tree.nilHashes, tree.hasher) @@ -754,7 +765,7 @@ func (tree *BNBSparseMerkleTree) Clear() error { tree.rootSize += uint64(versionSize * len(tree.root.Children[i].Versions)) } } - _, err := tree.Commit(nil) + _, err = tree.Commit(nil) return err }