Skip to content

Commit

Permalink
fix: working hash with initial version (cosmos#943)
Browse files Browse the repository at this point in the history
  • Loading branch information
cool-develope authored and roy-dydx committed May 9, 2024
1 parent b4bb6bd commit 1c8b8e7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion mutable_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,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 @@ -1920,3 +1920,37 @@ func TestReferenceRoot(t *testing.T) {
_, err = tree.Set([]byte("key1"), []byte("value2"))
require.NoError(t, err)
}

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 1c8b8e7

Please sign in to comment.