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

fix: working hash with initial version (backport #943) #945

Merged
merged 2 commits into from
May 8, 2024
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

### Bug Fixes

- [#943](https://github.com/cosmos/iavl/pull/943) Fix the `WorkingHash` with the `InitialVersion` option.
- [#909](https://github.com/cosmos/iavl/pull/909) Fix the reference node formats and delete legacy nodes.
- [#773](https://github.com/cosmos/iavl/pull/773) Fix memory leak in `Import`.
- [#795](https://github.com/cosmos/iavl/pull/795) Fix plugin used for buf generate.
Expand Down
2 changes: 1 addition & 1 deletion mutable_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (tree *MutableTree) Hash() []byte {

// WorkingHash returns the hash of the current working tree.
func (tree *MutableTree) WorkingHash() []byte {
return tree.ImmutableTree.Hash()
return tree.root.hashWithCount(tree.WorkingVersion())
}

func (tree *MutableTree) WorkingVersion() int64 {
Expand Down
34 changes: 34 additions & 0 deletions tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1885,3 +1885,37 @@ func TestReferenceRoot(t *testing.T) {
require.Equal(t, tree.root.GetKey(), (&NodeKey{version: 1, nonce: 3}).GetKey())
require.Equal(t, tree.root.key, []byte("key2"))
}

func TestWorkingHashWithInitialVersion(t *testing.T) {
db, err := dbm.NewDB("test", "memdb", "")
require.NoError(t, err)
defer db.Close()

initialVersion := int64(100)
tree := NewMutableTree(db, 0, false, log.NewNopLogger())
tree.SetInitialVersion(uint64(initialVersion))

v := tree.WorkingVersion()
require.Equal(t, initialVersion, v)

_, err = tree.Set([]byte("key1"), []byte("value1"))
require.NoError(t, err)

workingHash := tree.WorkingHash()
commitHash, _, err := tree.SaveVersion()
require.NoError(t, err)
require.Equal(t, commitHash, workingHash)

db, err = dbm.NewDB("test", "memdb", "")
require.NoError(t, err)

// without WorkingHash
tree = NewMutableTree(db, 0, false, log.NewNopLogger(), InitialVersionOption(uint64(initialVersion)))

_, err = tree.Set([]byte("key1"), []byte("value1"))
require.NoError(t, err)

commitHash1, _, err := tree.SaveVersion()
require.NoError(t, err)
require.Equal(t, commitHash1, commitHash)
}
Loading