Skip to content

Commit

Permalink
Additional renamings and CR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Aug 4, 2022
1 parent fa98ac2 commit 3c8db26
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 51 deletions.
16 changes: 8 additions & 8 deletions internal/trie/node/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

// EncodeAndHash returns the encoding of the node and
// the Merkle value of the node.
func (n *Node) EncodeAndHash() (encoding, hash []byte, err error) {
func (n *Node) EncodeAndHash() (encoding, merkleValue []byte, err error) {
if !n.Dirty && n.Encoding != nil && n.MerkleValue != nil {
return n.Encoding, n.MerkleValue, nil
}
Expand All @@ -37,8 +37,8 @@ func (n *Node) EncodeAndHash() (encoding, hash []byte, err error) {
if buffer.Len() < 32 {
n.MerkleValue = make([]byte, len(bufferBytes))
copy(n.MerkleValue, bufferBytes)
hash = n.MerkleValue // no need to copy
return encoding, hash, nil
merkleValue = n.MerkleValue // no need to copy
return encoding, merkleValue, nil
}

// Note: using the sync.Pool's buffer is useful here.
Expand All @@ -47,14 +47,14 @@ func (n *Node) EncodeAndHash() (encoding, hash []byte, err error) {
return nil, nil, err
}
n.MerkleValue = hashArray[:]
hash = n.MerkleValue // no need to copy
merkleValue = n.MerkleValue // no need to copy

return encoding, hash, nil
return encoding, merkleValue, nil
}

// EncodeAndHashRoot returns the encoding of the root node and
// the Merkle value of the root node (the hash of its encoding).
func (n *Node) EncodeAndHashRoot() (encoding, hash []byte, err error) {
func (n *Node) EncodeAndHashRoot() (encoding, merkleValue []byte, err error) {
if !n.Dirty && n.Encoding != nil && n.MerkleValue != nil {
return n.Encoding, n.MerkleValue, nil
}
Expand Down Expand Up @@ -82,7 +82,7 @@ func (n *Node) EncodeAndHashRoot() (encoding, hash []byte, err error) {
return nil, nil, err
}
n.MerkleValue = hashArray[:]
hash = n.MerkleValue // no need to copy
merkleValue = n.MerkleValue // no need to copy

return encoding, hash, nil
return encoding, merkleValue, nil
}
2 changes: 1 addition & 1 deletion internal/trie/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (n Node) StringNode() (stringNode *gotree.Node) {
stringNode.Appendf("Descendants: %d", n.Descendants)
}
stringNode.Appendf("Calculated encoding: " + bytesToString(n.Encoding))
stringNode.Appendf("Calculated digest: " + bytesToString(n.MerkleValue))
stringNode.Appendf("Merkle value: " + bytesToString(n.MerkleValue))

for i, child := range n.Children {
if child == nil {
Expand Down
24 changes: 12 additions & 12 deletions internal/trie/node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func Test_Node_String(t *testing.T) {
β”œβ”€β”€ Key: 0x0102
β”œβ”€β”€ Value: 0x0304
β”œβ”€β”€ Calculated encoding: nil
└── Calculated digest: nil`,
└── Merkle value: nil`,
},
"leaf with value higher than 1024": {
node: &Node{
Expand All @@ -42,7 +42,7 @@ func Test_Node_String(t *testing.T) {
β”œβ”€β”€ Key: 0x0102
β”œβ”€β”€ Value: 0x0000000000000000...0000000000000000
β”œβ”€β”€ Calculated encoding: nil
└── Calculated digest: nil`,
└── Merkle value: nil`,
},
"branch with value smaller than 1024": {
node: &Node{
Expand Down Expand Up @@ -70,15 +70,15 @@ func Test_Node_String(t *testing.T) {
β”œβ”€β”€ Value: 0x0304
β”œβ”€β”€ Descendants: 3
β”œβ”€β”€ Calculated encoding: nil
β”œβ”€β”€ Calculated digest: nil
β”œβ”€β”€ Merkle value: nil
β”œβ”€β”€ Child 3
| └── Leaf
| β”œβ”€β”€ Generation: 0
| β”œβ”€β”€ Dirty: false
| β”œβ”€β”€ Key: nil
| β”œβ”€β”€ Value: nil
| β”œβ”€β”€ Calculated encoding: nil
| └── Calculated digest: nil
| └── Merkle value: nil
β”œβ”€β”€ Child 7
| └── Branch
| β”œβ”€β”€ Generation: 0
Expand All @@ -87,23 +87,23 @@ func Test_Node_String(t *testing.T) {
| β”œβ”€β”€ Value: nil
| β”œβ”€β”€ Descendants: 1
| β”œβ”€β”€ Calculated encoding: nil
| β”œβ”€β”€ Calculated digest: nil
| β”œβ”€β”€ Merkle value: nil
| └── Child 0
| └── Leaf
| β”œβ”€β”€ Generation: 0
| β”œβ”€β”€ Dirty: false
| β”œβ”€β”€ Key: nil
| β”œβ”€β”€ Value: nil
| β”œβ”€β”€ Calculated encoding: nil
| └── Calculated digest: nil
| └── Merkle value: nil
└── Child 11
└── Leaf
β”œβ”€β”€ Generation: 0
β”œβ”€β”€ Dirty: false
β”œβ”€β”€ Key: nil
β”œβ”€β”€ Value: nil
β”œβ”€β”€ Calculated encoding: nil
└── Calculated digest: nil`,
└── Merkle value: nil`,
},
"branch with value higher than 1024": {
node: &Node{
Expand Down Expand Up @@ -131,15 +131,15 @@ func Test_Node_String(t *testing.T) {
β”œβ”€β”€ Value: 0x0000000000000000...0000000000000000
β”œβ”€β”€ Descendants: 3
β”œβ”€β”€ Calculated encoding: nil
β”œβ”€β”€ Calculated digest: nil
β”œβ”€β”€ Merkle value: nil
β”œβ”€β”€ Child 3
| └── Leaf
| β”œβ”€β”€ Generation: 0
| β”œβ”€β”€ Dirty: false
| β”œβ”€β”€ Key: nil
| β”œβ”€β”€ Value: nil
| β”œβ”€β”€ Calculated encoding: nil
| └── Calculated digest: nil
| └── Merkle value: nil
β”œβ”€β”€ Child 7
| └── Branch
| β”œβ”€β”€ Generation: 0
Expand All @@ -148,23 +148,23 @@ func Test_Node_String(t *testing.T) {
| β”œβ”€β”€ Value: nil
| β”œβ”€β”€ Descendants: 1
| β”œβ”€β”€ Calculated encoding: nil
| β”œβ”€β”€ Calculated digest: nil
| β”œβ”€β”€ Merkle value: nil
| └── Child 0
| └── Leaf
| β”œβ”€β”€ Generation: 0
| β”œβ”€β”€ Dirty: false
| β”œβ”€β”€ Key: nil
| β”œβ”€β”€ Value: nil
| β”œβ”€β”€ Calculated encoding: nil
| └── Calculated digest: nil
| └── Merkle value: nil
└── Child 11
└── Leaf
β”œβ”€β”€ Generation: 0
β”œβ”€β”€ Dirty: false
β”œβ”€β”€ Key: nil
β”œβ”€β”€ Value: nil
β”œβ”€β”€ Calculated encoding: nil
└── Calculated digest: nil`,
└── Merkle value: nil`,
},
}

Expand Down
46 changes: 23 additions & 23 deletions lib/trie/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ func (t *Trie) loadNode(db Database, n *Node) error {
continue
}

hash := child.MerkleValue
merkleValue := child.MerkleValue

if len(hash) == 0 {
if len(merkleValue) == 0 {
// node has already been loaded inline
// just set encoding + hash digest
_, _, err := child.EncodeAndHash()
Expand All @@ -133,25 +133,25 @@ func (t *Trie) loadNode(db Database, n *Node) error {
continue
}

encodedNode, err := db.Get(hash)
encodedNode, err := db.Get(merkleValue)
if err != nil {
return fmt.Errorf("cannot find child node key 0x%x in database: %w", hash, err)
return fmt.Errorf("cannot find child node key 0x%x in database: %w", merkleValue, err)
}

reader := bytes.NewReader(encodedNode)
decodedNode, err := node.Decode(reader)
if err != nil {
return fmt.Errorf("cannot decode node with hash 0x%x: %w", hash, err)
return fmt.Errorf("decoding node with Merkle value 0x%x: %w", merkleValue, err)
}

decodedNode.SetClean()
decodedNode.Encoding = encodedNode
decodedNode.MerkleValue = hash
decodedNode.MerkleValue = merkleValue
branch.Children[i] = decodedNode

err = t.loadNode(db, decodedNode)
if err != nil {
return fmt.Errorf("cannot load child at index %d with hash 0x%x: %w", i, hash, err)
return fmt.Errorf("loading child at index %d with Merkle value 0x%x: %w", i, merkleValue, err)
}

if decodedNode.Kind() == node.Branch {
Expand Down Expand Up @@ -291,24 +291,24 @@ func getFromDBAtNode(db chaindb.Database, n *Node, key []byte) (
}

// Child can be either inlined or a hash pointer.
childHash := child.MerkleValue
if len(childHash) == 0 && child.Kind() == node.Leaf {
childMerkleValue := child.MerkleValue
if len(childMerkleValue) == 0 && child.Kind() == node.Leaf {
return getFromDBAtNode(db, child, key[commonPrefixLength+1:])
}

encodedChild, err := db.Get(childHash)
encodedChild, err := db.Get(childMerkleValue)
if err != nil {
return nil, fmt.Errorf(
"cannot find child with hash 0x%x in database: %w",
childHash, err)
"finding child node with Merkle value 0x%x in database: %w",
childMerkleValue, err)
}

reader := bytes.NewReader(encodedChild)
decodedChild, err := node.Decode(reader)
if err != nil {
return nil, fmt.Errorf(
"cannot decode child node with hash 0x%x: %w",
childHash, err)
"decoding child node with Merkle value 0x%x: %w",
childMerkleValue, err)
}

return getFromDBAtNode(db, decodedChild, key[commonPrefixLength+1:])
Expand All @@ -332,23 +332,23 @@ func (t *Trie) writeDirtyNode(db chaindb.Batch, n *Node) (err error) {
return nil
}

var encoding, hash []byte
var encoding, merkleValue []byte
if n == t.root {
encoding, hash, err = n.EncodeAndHashRoot()
encoding, merkleValue, err = n.EncodeAndHashRoot()
} else {
encoding, hash, err = n.EncodeAndHash()
encoding, merkleValue, err = n.EncodeAndHash()
}
if err != nil {
return fmt.Errorf(
"cannot encode and hash node with hash 0x%x: %w",
"encoding and hashing node with Merkle value 0x%x: %w",
n.MerkleValue, err)
}

err = db.Put(hash, encoding)
err = db.Put(merkleValue, encoding)
if err != nil {
return fmt.Errorf(
"cannot put encoding of node with hash 0x%x in database: %w",
hash, err)
"putting encoding of node with Merkle value 0x%x in database: %w",
merkleValue, err)
}

if n.Kind() != node.Branch {
Expand All @@ -370,7 +370,7 @@ func (t *Trie) writeDirtyNode(db chaindb.Batch, n *Node) (err error) {

for _, childTrie := range t.childTries {
if err := childTrie.writeDirtyNode(db, childTrie.root); err != nil {
return fmt.Errorf("failed to write dirty node=0x%x to database: %w", childTrie.root.MerkleValue, err)
return fmt.Errorf("writing dirty node to database: %w", err)
}
}

Expand Down Expand Up @@ -405,7 +405,7 @@ func (t *Trie) getInsertedNodeHashesAtNode(n *Node, hashes map[common.Hash]struc
}
if err != nil {
return fmt.Errorf(
"cannot encode and hash node with hash 0x%x: %w",
"encoding and hashing node with Merkle value 0x%x: %w",
n.MerkleValue, err)
}

Expand Down
8 changes: 4 additions & 4 deletions lib/trie/print_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func Test_Trie_String(t *testing.T) {
β”œβ”€β”€ Key: 0x010203
β”œβ”€β”€ Value: 0x030405
β”œβ”€β”€ Calculated encoding: nil
└── Calculated digest: nil`,
└── Merkle value: nil`,
},
"branch root": {
trie: Trie{
Expand Down Expand Up @@ -63,23 +63,23 @@ func Test_Trie_String(t *testing.T) {
β”œβ”€β”€ Value: 0x0102
β”œβ”€β”€ Descendants: 2
β”œβ”€β”€ Calculated encoding: nil
β”œβ”€β”€ Calculated digest: nil
β”œβ”€β”€ Merkle value: nil
β”œβ”€β”€ Child 0
| └── Leaf
| β”œβ”€β”€ Generation: 2
| β”œβ”€β”€ Dirty: false
| β”œβ”€β”€ Key: 0x010203
| β”œβ”€β”€ Value: 0x030405
| β”œβ”€β”€ Calculated encoding: nil
| └── Calculated digest: nil
| └── Merkle value: nil
└── Child 3
└── Leaf
β”œβ”€β”€ Generation: 3
β”œβ”€β”€ Dirty: false
β”œβ”€β”€ Key: 0x010203
β”œβ”€β”€ Value: 0x030405
β”œβ”€β”€ Calculated encoding: nil
└── Calculated digest: nil`,
└── Merkle value: nil`,
},
}

Expand Down
3 changes: 0 additions & 3 deletions lib/trie/proof/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,6 @@ func loadProof(digestToEncoding map[string][]byte, n *node.Node) (err error) {
continue
}

// for inlined child nodes, the hash digest field is the
// encoding itself instead of the encoding hash digest, so we
// use the `merkleValue` variable name below to avoid confusion.
merkleValue := child.MerkleValue
encoding, ok := digestToEncoding[string(merkleValue)]
if !ok {
Expand Down

0 comments on commit 3c8db26

Please sign in to comment.