diff --git a/mutable_tree.go b/mutable_tree.go index b9a732063..5afc2c0ce 100644 --- a/mutable_tree.go +++ b/mutable_tree.go @@ -137,7 +137,9 @@ func (tree *MutableTree) Hash() []byte { // WorkingHash returns the hash of the current working tree. func (tree *MutableTree) WorkingHash() []byte { - return tree.root.hashWithCount(tree.WorkingVersion()) + // Use tree.version instead of tree.WorkingVersion() because tree.version + // is always the latest version + return tree.root.hashWithCount(tree.version + 1) } func (tree *MutableTree) WorkingVersion() int64 { diff --git a/tree_test.go b/tree_test.go index 13f570035..00158664f 100644 --- a/tree_test.go +++ b/tree_test.go @@ -1936,6 +1936,9 @@ func TestWorkingHashWithInitialVersion(t *testing.T) { _, err = tree.Set([]byte("key1"), []byte("value1")) require.NoError(t, err) + // WorkingHash sets the hashes in the nodes, SaveVersion doesn't recompute it. + // This test ensures WorkingHash and SaveVersion are consistent, using the + // same version number. workingHash := tree.WorkingHash() commitHash, _, err := tree.SaveVersion() require.NoError(t, err) @@ -1952,5 +1955,5 @@ func TestWorkingHashWithInitialVersion(t *testing.T) { commitHash1, _, err := tree.SaveVersion() require.NoError(t, err) - require.Equal(t, commitHash1, commitHash) + assert.Equal(t, hex.EncodeToString(commitHash1), hex.EncodeToString(commitHash)) }