Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

relax the restriction of delete latest version #483

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Improvements

- [#483](https://github.com/cosmos/iavl/pull/483) Allow delete latest version.

## 0.18.0 (March 10, 2022)

### Breaking Changes
Expand Down
5 changes: 1 addition & 4 deletions mutable_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -566,13 +566,10 @@ func (tree *MutableTree) deleteVersion(version int64) error {
if version <= 0 {
return errors.New("version must be greater than 0")
}
if version == tree.version {
return errors.Errorf("cannot delete latest saved version (%d)", version)
}
if !tree.VersionExists(version) {
return errors.Wrap(ErrVersionDoesNotExist, "")
}
if err := tree.ndb.DeleteVersion(version, true); err != nil {
if err := tree.ndb.DeleteVersion(version, false); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure this works? You have to move every node in the merkle tree for the historical state at the prior height, to being in the live state section of node db

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure, only did basic unit test in cosmos-sdk side here:cosmos/cosmos-sdk#11361, I need more understanding on internals of iavl I think.
Do you think there is an efficient way to achieve this rollback feature?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we don't need to patch iavl at all, there's LoadVersionForOverwriting which is perfect for rollback state.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also check if it will be pruned correctly.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return err
}

Expand Down
4 changes: 2 additions & 2 deletions nodedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,8 @@ func (ndb *nodeDB) DeleteVersionsRange(fromVersion, toVersion int64) error {
defer ndb.mtx.Unlock()

latest := ndb.getLatestVersion()
if latest < toVersion {
return errors.Errorf("cannot delete latest saved version (%d)", latest)
if latest+1 < toVersion {
return errors.Errorf("cannot delete future versions [%d..%d)", latest+1, toVersion)
}

predecessor := ndb.getPreviousVersion(fromVersion)
Expand Down