Skip to content

Commit

Permalink
support version in clear smt
Browse files Browse the repository at this point in the history
  • Loading branch information
liangxuuu committed Mar 12, 2023
1 parent 7e60c08 commit cda9094
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
2 changes: 1 addition & 1 deletion interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
21 changes: 16 additions & 5 deletions smt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}

Expand Down

0 comments on commit cda9094

Please sign in to comment.