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 6d9205c
Show file tree
Hide file tree
Showing 14 changed files with 3,476 additions and 392 deletions.
2 changes: 1 addition & 1 deletion 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
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
}
40 changes: 20 additions & 20 deletions internal/trie/node/hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ func Test_Branch_SetEncodingAndHash(t *testing.T) {

branch := &Branch{
Encoding: []byte{2},
hashDigest: []byte{3},
HashDigest: []byte{3},
}
branch.SetEncodingAndHash([]byte{4}, []byte{5})

expectedBranch := &Branch{
Encoding: []byte{4},
hashDigest: []byte{5},
HashDigest: []byte{5},
}
assert.Equal(t, expectedBranch, branch)
}
Expand All @@ -29,7 +29,7 @@ func Test_Branch_GetHash(t *testing.T) {
t.Parallel()

branch := &Branch{
hashDigest: []byte{3},
HashDigest: []byte{3},
}
hash := branch.GetHash()

Expand All @@ -52,7 +52,7 @@ func Test_Branch_EncodeAndHash(t *testing.T) {
branch: &Branch{},
expectedBranch: &Branch{
Encoding: []byte{0x80, 0x0, 0x0},
hashDigest: []byte{0x80, 0x0, 0x0},
HashDigest: []byte{0x80, 0x0, 0x0},
},
encoding: []byte{0x80, 0x0, 0x0},
hash: []byte{0x80, 0x0, 0x0},
Expand All @@ -64,7 +64,7 @@ func Test_Branch_EncodeAndHash(t *testing.T) {
},
expectedBranch: &Branch{
Encoding: []byte{0xc1, 0x1, 0x0, 0x0, 0x4, 0x2},
hashDigest: []byte{0xc1, 0x1, 0x0, 0x0, 0x4, 0x2},
HashDigest: []byte{0xc1, 0x1, 0x0, 0x0, 0x4, 0x2},
},
encoding: []byte{0xc1, 0x1, 0x0, 0x0, 0x4, 0x2},
hash: []byte{0xc1, 0x1, 0x0, 0x0, 0x4, 0x2},
Expand All @@ -75,11 +75,11 @@ func Test_Branch_EncodeAndHash(t *testing.T) {
Value: []byte{2},
Dirty: true,
Encoding: []byte{3},
hashDigest: []byte{4},
HashDigest: []byte{4},
},
expectedBranch: &Branch{
Encoding: []byte{0xc1, 0x1, 0x0, 0x0, 0x4, 0x2},
hashDigest: []byte{0xc1, 0x1, 0x0, 0x0, 0x4, 0x2},
HashDigest: []byte{0xc1, 0x1, 0x0, 0x0, 0x4, 0x2},
},
encoding: []byte{0xc1, 0x1, 0x0, 0x0, 0x4, 0x2},
hash: []byte{0xc1, 0x1, 0x0, 0x0, 0x4, 0x2},
Expand All @@ -90,13 +90,13 @@ func Test_Branch_EncodeAndHash(t *testing.T) {
Value: []byte{2},
Dirty: false,
Encoding: []byte{3},
hashDigest: []byte{4},
HashDigest: []byte{4},
},
expectedBranch: &Branch{
Key: []byte{1},
Value: []byte{2},
Encoding: []byte{3},
hashDigest: []byte{4},
HashDigest: []byte{4},
},
encoding: []byte{3},
hash: []byte{4},
Expand All @@ -107,7 +107,7 @@ func Test_Branch_EncodeAndHash(t *testing.T) {
},
expectedBranch: &Branch{
Encoding: []byte{0xbf, 0x2, 0x7, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x0, 0x0}, //nolint:lll
hashDigest: []byte{0x6b, 0xd8, 0xcc, 0xac, 0x71, 0x77, 0x44, 0x17, 0xfe, 0xe0, 0xde, 0xda, 0xd5, 0x97, 0x6e, 0x69, 0xeb, 0xe9, 0xdd, 0x80, 0x1d, 0x4b, 0x51, 0xf1, 0x5b, 0xf3, 0x4a, 0x93, 0x27, 0x32, 0x2c, 0xb0}, //nolint:lll
HashDigest: []byte{0x6b, 0xd8, 0xcc, 0xac, 0x71, 0x77, 0x44, 0x17, 0xfe, 0xe0, 0xde, 0xda, 0xd5, 0x97, 0x6e, 0x69, 0xeb, 0xe9, 0xdd, 0x80, 0x1d, 0x4b, 0x51, 0xf1, 0x5b, 0xf3, 0x4a, 0x93, 0x27, 0x32, 0x2c, 0xb0}, //nolint:lll
},
encoding: []byte{0xbf, 0x2, 0x7, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x0, 0x0}, //nolint:lll
hash: []byte{0x6b, 0xd8, 0xcc, 0xac, 0x71, 0x77, 0x44, 0x17, 0xfe, 0xe0, 0xde, 0xda, 0xd5, 0x97, 0x6e, 0x69, 0xeb, 0xe9, 0xdd, 0x80, 0x1d, 0x4b, 0x51, 0xf1, 0x5b, 0xf3, 0x4a, 0x93, 0x27, 0x32, 0x2c, 0xb0}, //nolint:lll
Expand Down Expand Up @@ -136,13 +136,13 @@ func Test_Leaf_SetEncodingAndHash(t *testing.T) {

leaf := &Leaf{
Encoding: []byte{2},
hashDigest: []byte{3},
HashDigest: []byte{3},
}
leaf.SetEncodingAndHash([]byte{4}, []byte{5})

expectedLeaf := &Leaf{
Encoding: []byte{4},
hashDigest: []byte{5},
HashDigest: []byte{5},
}
assert.Equal(t, expectedLeaf, leaf)
}
Expand All @@ -151,7 +151,7 @@ func Test_Leaf_GetHash(t *testing.T) {
t.Parallel()

leaf := &Leaf{
hashDigest: []byte{3},
HashDigest: []byte{3},
}
hash := leaf.GetHash()

Expand All @@ -174,7 +174,7 @@ func Test_Leaf_EncodeAndHash(t *testing.T) {
leaf: &Leaf{},
expectedLeaf: &Leaf{
Encoding: []byte{0x40, 0x0},
hashDigest: []byte{0x40, 0x0},
HashDigest: []byte{0x40, 0x0},
},
encoding: []byte{0x40, 0x0},
hash: []byte{0x40, 0x0},
Expand All @@ -186,7 +186,7 @@ func Test_Leaf_EncodeAndHash(t *testing.T) {
},
expectedLeaf: &Leaf{
Encoding: []byte{0x41, 0x1, 0x4, 0x2},
hashDigest: []byte{0x41, 0x1, 0x4, 0x2},
HashDigest: []byte{0x41, 0x1, 0x4, 0x2},
},
encoding: []byte{0x41, 0x1, 0x4, 0x2},
hash: []byte{0x41, 0x1, 0x4, 0x2},
Expand All @@ -197,11 +197,11 @@ func Test_Leaf_EncodeAndHash(t *testing.T) {
Value: []byte{2},
Dirty: true,
Encoding: []byte{3},
hashDigest: []byte{4},
HashDigest: []byte{4},
},
expectedLeaf: &Leaf{
Encoding: []byte{0x41, 0x1, 0x4, 0x2},
hashDigest: []byte{0x41, 0x1, 0x4, 0x2},
HashDigest: []byte{0x41, 0x1, 0x4, 0x2},
},
encoding: []byte{0x41, 0x1, 0x4, 0x2},
hash: []byte{0x41, 0x1, 0x4, 0x2},
Expand All @@ -212,13 +212,13 @@ func Test_Leaf_EncodeAndHash(t *testing.T) {
Value: []byte{2},
Dirty: false,
Encoding: []byte{3},
hashDigest: []byte{4},
HashDigest: []byte{4},
},
expectedLeaf: &Leaf{
Key: []byte{1},
Value: []byte{2},
Encoding: []byte{3},
hashDigest: []byte{4},
HashDigest: []byte{4},
},
encoding: []byte{3},
hash: []byte{4},
Expand All @@ -229,7 +229,7 @@ func Test_Leaf_EncodeAndHash(t *testing.T) {
},
expectedLeaf: &Leaf{
Encoding: []byte{0x7f, 0x2, 0x7, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x0}, //nolint:lll
hashDigest: []byte{0xfb, 0xae, 0x31, 0x4b, 0xef, 0x31, 0x9, 0xc7, 0x62, 0x99, 0x9d, 0x40, 0x9b, 0xd4, 0xdc, 0x64, 0xe7, 0x39, 0x46, 0x8b, 0xd3, 0xaf, 0xe8, 0x63, 0x9d, 0xf9, 0x41, 0x40, 0x76, 0x40, 0x10, 0xa3}, //nolint:lll
HashDigest: []byte{0xfb, 0xae, 0x31, 0x4b, 0xef, 0x31, 0x9, 0xc7, 0x62, 0x99, 0x9d, 0x40, 0x9b, 0xd4, 0xdc, 0x64, 0xe7, 0x39, 0x46, 0x8b, 0xd3, 0xaf, 0xe8, 0x63, 0x9d, 0xf9, 0x41, 0x40, 0x76, 0x40, 0x10, 0xa3}, //nolint:lll
},
encoding: []byte{0x7f, 0x2, 0x7, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x0}, //nolint:lll
hash: []byte{0xfb, 0xae, 0x31, 0x4b, 0xef, 0x31, 0x9, 0xc7, 0x62, 0x99, 0x9d, 0x40, 0x9b, 0xd4, 0xdc, 0x64, 0xe7, 0x39, 0x46, 0x8b, 0xd3, 0xaf, 0xe8, 0x63, 0x9d, 0xf9, 0x41, 0x40, 0x76, 0x40, 0x10, 0xa3}, //nolint:lll
Expand Down
Loading

0 comments on commit 6d9205c

Please sign in to comment.