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

return hash for empty trees in Hash() and SaveVersion() #304

Merged
merged 2 commits into from
Aug 8, 2020
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## 0.15.0-rc2

### Breaking Changes

- [\#304](https://github.com/cosmos/iavl/pull/304) Empty trees now return hashes rather than `nil`
from e.g. `Hash()`, `WorkingHash()`, and `SaveVersion()`, for conformance with RFC-6962.

## 0.15.0-rc1 (July 30, 2020)

The IAVL project has moved from https://github.com/tendermint/iavl to
Expand Down
3 changes: 2 additions & 1 deletion basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package iavl

import (
"bytes"
"encoding/hex"
mrand "math/rand"
"sort"
"testing"
Expand Down Expand Up @@ -429,7 +430,7 @@ func TestTreeProof(t *testing.T) {
db := db.NewMemDB()
tree, err := NewMutableTree(db, 100)
require.NoError(t, err)
assert.Equal(t, tree.Hash(), []byte(nil))
assert.Equal(t, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", hex.EncodeToString(tree.Hash()))

// should get false for proof with nil root
value, proof, err := tree.GetWithProof([]byte("foo"))
Expand Down
6 changes: 0 additions & 6 deletions immutable_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,18 +127,12 @@ func (t *ImmutableTree) Has(key []byte) bool {

// Hash returns the root hash.
func (t *ImmutableTree) Hash() []byte {
if t.root == nil {
return nil
}
hash, _ := t.root.hashWithCount()
return hash
}

// hashWithCount returns the root hash and hash count.
func (t *ImmutableTree) hashWithCount() ([]byte, int64) {
if t.root == nil {
return nil, 0
}
return t.root.hashWithCount()
}

Expand Down
5 changes: 1 addition & 4 deletions mutable_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,7 @@ func (tree *MutableTree) AvailableVersions() []int {
// Hash returns the hash of the latest saved version of the tree, as returned
// by SaveVersion. If no versions have been saved, Hash returns nil.
func (tree *MutableTree) Hash() []byte {
if tree.version > 0 {
return tree.lastSaved.Hash()
}
return nil
return tree.lastSaved.Hash()
}

// WorkingHash returns the hash of the current working tree.
Expand Down
5 changes: 5 additions & 0 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,12 @@ func (node *Node) _hash() []byte {

// Hash the node and its descendants recursively. This usually mutates all
// descendant nodes. Returns the node hash and number of nodes hashed.
// If the tree is empty (i.e. the node is nil), returns the hash of an empty input,
// to conform with RFC-6962.
func (node *Node) hashWithCount() ([]byte, int64) {
if node == nil {
return sha256.New().Sum(nil), 0
Copy link
Contributor

@alexanderbez alexanderbez Aug 7, 2020

Choose a reason for hiding this comment

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

Should this be nil or a byte representation of an empty string?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You mean the hash input? nil and []byte{} are equivalent.

}
if node.hash != nil {
return node.hash, 0
}
Expand Down
28 changes: 15 additions & 13 deletions tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,24 +301,24 @@ func TestVersionedEmptyTree(t *testing.T) {
require.NoError(err)

hash, v, err := tree.SaveVersion()
require.Nil(hash)
require.EqualValues(1, v)
require.NoError(err)
require.Equal("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", hex.EncodeToString(hash))
require.EqualValues(1, v)

hash, v, err = tree.SaveVersion()
require.Nil(hash)
require.EqualValues(2, v)
require.NoError(err)
require.Equal("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", hex.EncodeToString(hash))
require.EqualValues(2, v)

hash, v, err = tree.SaveVersion()
require.Nil(hash)
require.EqualValues(3, v)
require.NoError(err)
require.Equal("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", hex.EncodeToString(hash))
require.EqualValues(3, v)

hash, v, err = tree.SaveVersion()
require.Nil(hash)
require.EqualValues(4, v)
require.NoError(err)
require.Equal("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", hex.EncodeToString(hash))
require.EqualValues(4, v)

require.EqualValues(4, tree.Version())

Expand Down Expand Up @@ -1183,20 +1183,22 @@ func TestVersionedTreeHash(t *testing.T) {
tree, err := getTestTree(0)
require.NoError(err)

require.Nil(tree.Hash())
require.Equal("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", hex.EncodeToString(tree.Hash()))
tree.Set([]byte("I"), []byte("D"))
require.Nil(tree.Hash())
require.Equal("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", hex.EncodeToString(tree.Hash()))

hash1, _, _ := tree.SaveVersion()
hash1, _, err := tree.SaveVersion()
require.NoError(err)

tree.Set([]byte("I"), []byte("F"))
require.EqualValues(hash1, tree.Hash())

hash2, _, _ := tree.SaveVersion()
hash2, _, err := tree.SaveVersion()
require.NoError(err)

val, proof, err := tree.GetVersionedWithProof([]byte("I"), 2)
require.NoError(err)
require.EqualValues(val, []byte("F"))
require.EqualValues([]byte("F"), val)
require.NoError(proof.Verify(hash2))
require.NoError(proof.VerifyItem([]byte("I"), val))
}
Expand Down