Skip to content

Commit

Permalink
chore(lib/trie): unit tests full test coverage for trie.go
Browse files Browse the repository at this point in the history
- Fix additional problems found in production code
- Remove duplicate tests from `trie_endtoend_test.go`
- Generate needed mocks
- Export node's `HashDigest` field
  • Loading branch information
qdm12 committed Jan 27, 2022
1 parent 7b3f03a commit 9c89821
Show file tree
Hide file tree
Showing 14 changed files with 3,477 additions and 393 deletions.
4 changes: 2 additions & 2 deletions internal/trie/node/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type Branch struct {
// Dirty is true when the branch differs
// from the node stored in the database.
Dirty bool
hashDigest []byte
HashDigest []byte
Encoding []byte
// Generation is incremented on every trie Snapshot() call.
// Each node also contain a certain Generation number,
Expand Down Expand Up @@ -61,7 +61,7 @@ func (b *Branch) StringNode() (stringNode *gotree.Node) {
stringNode.Appendf("Key: " + bytesToString(b.Key))
stringNode.Appendf("Value: " + bytesToString(b.Value))
stringNode.Appendf("Calculated encoding: " + bytesToString(b.Encoding))
stringNode.Appendf("Calculated digest: " + bytesToString(b.hashDigest))
stringNode.Appendf("Calculated digest: " + bytesToString(b.HashDigest))

for i, child := range b.Children {
if child == nil {
Expand Down
12 changes: 6 additions & 6 deletions internal/trie/node/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ func (b *Branch) Copy(copyChildren bool) Node {
copy(cpy.Value, b.Value)
}

if b.hashDigest != nil {
cpy.hashDigest = make([]byte, len(b.hashDigest))
copy(cpy.hashDigest, b.hashDigest)
if b.HashDigest != nil {
cpy.HashDigest = make([]byte, len(b.HashDigest))
copy(cpy.HashDigest, b.HashDigest)
}

if b.Encoding != nil {
Expand Down Expand Up @@ -74,9 +74,9 @@ func (l *Leaf) Copy(_ bool) Node {
copy(cpy.Value, l.Value)
}

if l.hashDigest != nil {
cpy.hashDigest = make([]byte, len(l.hashDigest))
copy(cpy.hashDigest, l.hashDigest)
if l.HashDigest != nil {
cpy.HashDigest = make([]byte, len(l.HashDigest))
copy(cpy.HashDigest, l.HashDigest)
}

if l.Encoding != nil {
Expand Down
12 changes: 6 additions & 6 deletions internal/trie/node/copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func Test_Branch_Copy(t *testing.T) {
nil, nil, &Leaf{Key: []byte{9}},
},
Dirty: true,
hashDigest: []byte{5},
HashDigest: []byte{5},
Encoding: []byte{6},
},
expectedBranch: &Branch{
Expand All @@ -51,7 +51,7 @@ func Test_Branch_Copy(t *testing.T) {
nil, nil, &Leaf{Key: []byte{9}},
},
Dirty: true,
hashDigest: []byte{5},
HashDigest: []byte{5},
Encoding: []byte{6},
},
},
Expand Down Expand Up @@ -83,7 +83,7 @@ func Test_Branch_Copy(t *testing.T) {
assert.Equal(t, testCase.expectedBranch, branchCopy)
testForSliceModif(t, testCase.branch.Key, branchCopy.Key)
testForSliceModif(t, testCase.branch.Value, branchCopy.Value)
testForSliceModif(t, testCase.branch.hashDigest, branchCopy.hashDigest)
testForSliceModif(t, testCase.branch.HashDigest, branchCopy.HashDigest)
testForSliceModif(t, testCase.branch.Encoding, branchCopy.Encoding)

testCase.branch.Children[15] = &Leaf{Key: []byte("modified")}
Expand All @@ -108,14 +108,14 @@ func Test_Leaf_Copy(t *testing.T) {
Key: []byte{1, 2},
Value: []byte{3, 4},
Dirty: true,
hashDigest: []byte{5},
HashDigest: []byte{5},
Encoding: []byte{6},
},
expectedLeaf: &Leaf{
Key: []byte{1, 2},
Value: []byte{3, 4},
Dirty: true,
hashDigest: []byte{5},
HashDigest: []byte{5},
Encoding: []byte{6},
},
},
Expand All @@ -135,7 +135,7 @@ func Test_Leaf_Copy(t *testing.T) {
assert.Equal(t, testCase.expectedLeaf, leafCopy)
testForSliceModif(t, testCase.leaf.Key, leafCopy.Key)
testForSliceModif(t, testCase.leaf.Value, leafCopy.Value)
testForSliceModif(t, testCase.leaf.hashDigest, leafCopy.hashDigest)
testForSliceModif(t, testCase.leaf.HashDigest, leafCopy.HashDigest)
testForSliceModif(t, testCase.leaf.Encoding, leafCopy.Encoding)
})
}
Expand Down
2 changes: 1 addition & 1 deletion internal/trie/node/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func decodeBranch(reader io.Reader, header byte) (branch *Branch, err error) {
}

branch.Children[i] = &Leaf{
hashDigest: hash,
HashDigest: hash,
}
}

Expand Down
4 changes: 2 additions & 2 deletions internal/trie/node/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func Test_decodeBranch(t *testing.T) {
nil, nil, nil, nil, nil,
nil, nil, nil, nil, nil,
&Leaf{
hashDigest: []byte{1, 2, 3, 4, 5},
HashDigest: []byte{1, 2, 3, 4, 5},
},
},
Dirty: true,
Expand Down Expand Up @@ -208,7 +208,7 @@ func Test_decodeBranch(t *testing.T) {
nil, nil, nil, nil, nil,
nil, nil, nil, nil, nil,
&Leaf{
hashDigest: []byte{1, 2, 3, 4, 5},
HashDigest: []byte{1, 2, 3, 4, 5},
},
},
Dirty: true,
Expand Down
2 changes: 1 addition & 1 deletion internal/trie/node/encode_decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func Test_Branch_Encode_Decode(t *testing.T) {
Key: []byte{5},
Children: [16]Node{
&Leaf{
hashDigest: []byte{0x41, 0x9, 0x4, 0xa},
HashDigest: []byte{0x41, 0x9, 0x4, 0xa},
},
},
Dirty: true,
Expand Down
36 changes: 18 additions & 18 deletions internal/trie/node/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@ import (
// given to the branch. Note it does not copy them, so beware.
func (b *Branch) SetEncodingAndHash(enc, hash []byte) {
b.Encoding = enc
b.hashDigest = hash
b.HashDigest = hash
}

// GetHash returns the hash of the branch.
// Note it does not copy it, so modifying
// the returned hash will modify the hash
// of the branch.
func (b *Branch) GetHash() []byte {
return b.hashDigest
return b.HashDigest
}

// EncodeAndHash returns the encoding of the branch and
// the blake2b hash digest of the encoding of the branch.
// If the encoding is less than 32 bytes, the hash returned
// is the encoding and not the hash of the encoding.
func (b *Branch) EncodeAndHash() (encoding, hash []byte, err error) {
if !b.Dirty && b.Encoding != nil && b.hashDigest != nil {
return b.Encoding, b.hashDigest, nil
if !b.Dirty && b.Encoding != nil && b.HashDigest != nil {
return b.Encoding, b.HashDigest, nil
}

buffer := pools.EncodingBuffers.Get().(*bytes.Buffer)
Expand All @@ -50,9 +50,9 @@ func (b *Branch) EncodeAndHash() (encoding, hash []byte, err error) {
encoding = b.Encoding // no need to copy

if buffer.Len() < 32 {
b.hashDigest = make([]byte, len(bufferBytes))
copy(b.hashDigest, bufferBytes)
hash = b.hashDigest // no need to copy
b.HashDigest = make([]byte, len(bufferBytes))
copy(b.HashDigest, bufferBytes)
hash = b.HashDigest // no need to copy
return encoding, hash, nil
}

Expand All @@ -61,8 +61,8 @@ func (b *Branch) EncodeAndHash() (encoding, hash []byte, err error) {
if err != nil {
return nil, nil, err
}
b.hashDigest = hashArray[:]
hash = b.hashDigest // no need to copy
b.HashDigest = hashArray[:]
hash = b.HashDigest // no need to copy

return encoding, hash, nil
}
Expand All @@ -73,15 +73,15 @@ func (l *Leaf) SetEncodingAndHash(enc, hash []byte) {
l.encodingMu.Lock()
l.Encoding = enc
l.encodingMu.Unlock()
l.hashDigest = hash
l.HashDigest = hash
}

// GetHash returns the hash of the leaf.
// Note it does not copy it, so modifying
// the returned hash will modify the hash
// of the branch.
func (l *Leaf) GetHash() []byte {
return l.hashDigest
return l.HashDigest
}

// EncodeAndHash returns the encoding of the leaf and
Expand All @@ -90,9 +90,9 @@ func (l *Leaf) GetHash() []byte {
// is the encoding and not the hash of the encoding.
func (l *Leaf) EncodeAndHash() (encoding, hash []byte, err error) {
l.encodingMu.RLock()
if !l.IsDirty() && l.Encoding != nil && l.hashDigest != nil {
if !l.IsDirty() && l.Encoding != nil && l.HashDigest != nil {
l.encodingMu.RUnlock()
return l.Encoding, l.hashDigest, nil
return l.Encoding, l.HashDigest, nil
}
l.encodingMu.RUnlock()

Expand All @@ -116,9 +116,9 @@ func (l *Leaf) EncodeAndHash() (encoding, hash []byte, err error) {
encoding = l.Encoding // no need to copy

if len(bufferBytes) < 32 {
l.hashDigest = make([]byte, len(bufferBytes))
copy(l.hashDigest, bufferBytes)
hash = l.hashDigest // no need to copy
l.HashDigest = make([]byte, len(bufferBytes))
copy(l.HashDigest, bufferBytes)
hash = l.HashDigest // no need to copy
return encoding, hash, nil
}

Expand All @@ -128,8 +128,8 @@ func (l *Leaf) EncodeAndHash() (encoding, hash []byte, err error) {
return nil, nil, err
}

l.hashDigest = hashArray[:]
hash = l.hashDigest // no need to copy
l.HashDigest = hashArray[:]
hash = l.HashDigest // no need to copy

return encoding, hash, nil
}
Loading

0 comments on commit 9c89821

Please sign in to comment.