Skip to content

Commit

Permalink
fix: working hash with initial version (#943)
Browse files Browse the repository at this point in the history
(cherry picked from commit 70f77c2)

# Conflicts:
#	CHANGELOG.md
  • Loading branch information
cool-develope authored and mergify[bot] committed May 7, 2024
1 parent 6ca2f28 commit 77a71fd
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
32 changes: 32 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,38 @@
# Changelog

<<<<<<< HEAD
## Unreleased
=======
## v1.0.4 May 7, 2024

### Bug Fixes

- [#943](https://github.com/cosmos/iavl/pull/943) Fix the `WorkingHash` with the `InitialVersion` option.

## v1.0.3 April 8, 2024

### Bug Fixes

- [#928](https://github.com/cosmos/iavl/pull/928) Fix the reformatted root node issue.

## v1.0.2 March 16, 2024

### Bug Fixes

- [#911](https://github.com/cosmos/iavl/pull/911) Fix the reference root format from (prefix, version) to (prefix, version, nonce)

### Improvements

- [#911](https://github.com/cosmos/iavl/pull/911) Async pruning of legacy orphan nodes.

## v1.0.1 February 16, 2024

### Improvements

- [#876](https://github.com/cosmos/iavl/pull/876) Make pruning of legacy orphan nodes asynchronous.

## v1.0.0 (October 30, 2023)
>>>>>>> 70f77c2 (fix: working hash with initial version (#943))
### Improvements

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)
}

0 comments on commit 77a71fd

Please sign in to comment.