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

chore(trie): update tests to always have leaves with values #2588

Merged
merged 3 commits into from
Jul 4, 2022
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
6 changes: 4 additions & 2 deletions dot/state/tries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,15 @@ func Test_Tries_get(t *testing.T) {
tries: &Tries{
rootToTrie: map[common.Hash]*trie.Trie{
{1, 2, 3}: trie.NewTrie(&node.Node{
Key: []byte{1, 2, 3},
Key: []byte{1, 2, 3},
Value: []byte{1},
}),
},
},
root: common.Hash{1, 2, 3},
trie: trie.NewTrie(&node.Node{
Key: []byte{1, 2, 3},
Key: []byte{1, 2, 3},
Value: []byte{1},
}),
},
"not found in map": {
Expand Down
67 changes: 25 additions & 42 deletions internal/trie/node/branch_encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,11 @@ func Test_encodeChildrenOpportunisticParallel(t *testing.T) {
"no children": {},
"first child not nil": {
children: []*Node{
{Key: []byte{1}},
{Key: []byte{1}, Value: []byte{2}},
},
writes: []writeCall{
{
written: []byte{12, 65, 1, 0},
written: []byte{16, 65, 1, 4, 2},
},
},
},
Expand All @@ -231,25 +231,25 @@ func Test_encodeChildrenOpportunisticParallel(t *testing.T) {
nil, nil, nil, nil, nil,
nil, nil, nil, nil, nil,
nil, nil, nil, nil, nil,
{Key: []byte{1}},
{Key: []byte{1}, Value: []byte{2}},
},
writes: []writeCall{
{
written: []byte{12, 65, 1, 0},
written: []byte{16, 65, 1, 4, 2},
},
},
},
"first two children not nil": {
children: []*Node{
{Key: []byte{1}},
{Key: []byte{2}},
{Key: []byte{1}, Value: []byte{2}},
{Key: []byte{3}, Value: []byte{4}},
},
writes: []writeCall{
{
written: []byte{12, 65, 1, 0},
written: []byte{16, 65, 1, 4, 2},
},
{
written: []byte{12, 65, 2, 0},
written: []byte{16, 65, 3, 4, 4},
},
},
},
Expand All @@ -258,12 +258,12 @@ func Test_encodeChildrenOpportunisticParallel(t *testing.T) {
nil, nil, nil, nil,
nil, nil, nil, nil,
nil, nil, nil,
{Key: []byte{1}},
{Key: []byte{1}, Value: []byte{2}},
nil, nil, nil, nil,
},
writes: []writeCall{
{
written: []byte{12, 65, 1, 0},
written: []byte{16, 65, 1, 4, 2},
err: errTest,
},
},
Expand All @@ -278,13 +278,13 @@ func Test_encodeChildrenOpportunisticParallel(t *testing.T) {
{
Key: []byte{1},
Children: []*Node{
{Key: []byte{1}},
{Key: []byte{1}, Value: []byte{2}},
},
},
},
writes: []writeCall{
{
written: []byte{32, 129, 1, 1, 0, 12, 65, 1, 0},
written: []byte{36, 129, 1, 1, 0, 16, 65, 1, 4, 2},
},
},
},
Expand Down Expand Up @@ -360,11 +360,11 @@ func Test_encodeChildrenSequentially(t *testing.T) {
"no children": {},
"first child not nil": {
children: []*Node{
{Key: []byte{1}},
{Key: []byte{1}, Value: []byte{2}},
},
writes: []writeCall{
{
written: []byte{12, 65, 1, 0},
written: []byte{16, 65, 1, 4, 2},
},
},
},
Expand All @@ -373,25 +373,25 @@ func Test_encodeChildrenSequentially(t *testing.T) {
nil, nil, nil, nil, nil,
nil, nil, nil, nil, nil,
nil, nil, nil, nil, nil,
{Key: []byte{1}},
{Key: []byte{1}, Value: []byte{2}},
},
writes: []writeCall{
{
written: []byte{12, 65, 1, 0},
written: []byte{16, 65, 1, 4, 2},
},
},
},
"first two children not nil": {
children: []*Node{
{Key: []byte{1}},
{Key: []byte{2}},
{Key: []byte{1}, Value: []byte{2}},
{Key: []byte{3}, Value: []byte{4}},
},
writes: []writeCall{
{
written: []byte{12, 65, 1, 0},
written: []byte{16, 65, 1, 4, 2},
},
{
written: []byte{12, 65, 2, 0},
written: []byte{16, 65, 3, 4, 4},
},
},
},
Expand All @@ -400,12 +400,12 @@ func Test_encodeChildrenSequentially(t *testing.T) {
nil, nil, nil, nil,
nil, nil, nil, nil,
nil, nil, nil,
{Key: []byte{1}},
{Key: []byte{1}, Value: []byte{2}},
nil, nil, nil, nil,
},
writes: []writeCall{
{
written: []byte{12, 65, 1, 0},
written: []byte{16, 65, 1, 4, 2},
err: errTest,
},
},
Expand Down Expand Up @@ -457,13 +457,6 @@ func Test_encodeChild(t *testing.T) {
errMessage string
}{
"nil node": {},
"empty leaf child": {
child: &Node{},
writeCall: true,
write: writeCall{
written: []byte{8, 64, 0},
},
},
"empty branch child": {
child: &Node{
Children: make([]*Node, ChildrenCapacity),
Expand Down Expand Up @@ -547,25 +540,15 @@ func Test_scaleEncodeHash(t *testing.T) {
wrappedErr error
errMessage string
}{
"empty leaf": {
node: &Node{},
encoding: []byte{0x8, 0x40, 0},
},
"empty branch": {
node: &Node{
Children: make([]*Node, ChildrenCapacity),
},
encoding: []byte{0xc, 0x80, 0x0, 0x0},
},
"non empty branch": {
"branch": {
node: &Node{
Key: []byte{1, 2},
Value: []byte{3, 4},
Children: []*Node{
nil, nil, {Key: []byte{9}},
nil, nil, {Key: []byte{9}, Value: []byte{1}},
},
},
encoding: []byte{0x2c, 0xc2, 0x12, 0x4, 0x0, 0x8, 0x3, 0x4, 0xc, 0x41, 0x9, 0x0},
encoding: []byte{0x30, 0xc2, 0x12, 0x4, 0x0, 0x8, 0x3, 0x4, 0x10, 0x41, 0x9, 0x4, 0x1},
},
}

Expand Down
23 changes: 12 additions & 11 deletions internal/trie/node/copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ func Test_Node_Copy(t *testing.T) {
Value: []byte{3, 4},
Children: padRightChildren([]*Node{
nil, nil, {
Key: []byte{9},
Key: []byte{9},
Value: []byte{1},
},
}),
Dirty: true,
Expand All @@ -55,7 +56,8 @@ func Test_Node_Copy(t *testing.T) {
Value: []byte{3, 4},
Children: padRightChildren([]*Node{
nil, nil, {
Key: []byte{9},
Key: []byte{9},
Value: []byte{1},
},
}),
Dirty: true,
Expand All @@ -65,7 +67,8 @@ func Test_Node_Copy(t *testing.T) {
node: &Node{
Children: padRightChildren([]*Node{
nil, nil, {
Key: []byte{9},
Key: []byte{9},
Value: []byte{1},
},
}),
},
Expand All @@ -75,7 +78,8 @@ func Test_Node_Copy(t *testing.T) {
expectedNode: &Node{
Children: padRightChildren([]*Node{
nil, nil, {
Key: []byte{9},
Key: []byte{9},
Value: []byte{1},
},
}),
},
Expand All @@ -86,7 +90,8 @@ func Test_Node_Copy(t *testing.T) {
Value: []byte{3, 4},
Children: padRightChildren([]*Node{
nil, nil, {
Key: []byte{9},
Key: []byte{9},
Value: []byte{1},
},
}),
Dirty: true,
Expand All @@ -99,19 +104,15 @@ func Test_Node_Copy(t *testing.T) {
Value: []byte{3, 4},
Children: padRightChildren([]*Node{
nil, nil, {
Key: []byte{9},
Key: []byte{9},
Value: []byte{1},
},
}),
Dirty: true,
HashDigest: []byte{5},
Encoding: []byte{6},
},
},
"empty leaf": {
node: &Node{},
settings: DefaultCopySettings,
expectedNode: &Node{},
},
"non empty leaf": {
node: &Node{
Key: []byte{1, 2},
Expand Down
3 changes: 1 addition & 2 deletions internal/trie/node/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ func (n *Node) Encode(buffer Buffer) (err error) {

// check value is not nil for branch nodes, even though
// leaf nodes always have a non-nil value.
if n.Type() == Leaf || n.Value != nil {
// TODO remove `n.Type() == Leaf` and update tests
if n.Value != nil {
encodedValue, err := scale.Marshal(n.Value) // TODO scale encoder to write to buffer
if err != nil {
return fmt.Errorf("cannot scale encode value: %w", err)
Expand Down
25 changes: 13 additions & 12 deletions internal/trie/node/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ func Test_Node_Encode(t *testing.T) {
},
"leaf buffer write error for encoded key": {
node: &Node{
Key: []byte{1, 2, 3},
Key: []byte{1, 2, 3},
Value: []byte{1},
},
writes: []writeCall{
{
Expand Down Expand Up @@ -188,8 +189,8 @@ func Test_Node_Encode(t *testing.T) {
Key: []byte{1, 2, 3},
Value: []byte{100},
Children: []*Node{
nil, nil, nil, {Key: []byte{9}},
nil, nil, nil, {Key: []byte{11}},
nil, nil, nil, {Key: []byte{9}, Value: []byte{1}},
nil, nil, nil, {Key: []byte{11}, Value: []byte{1}},
},
},
writes: []writeCall{
Expand All @@ -212,8 +213,8 @@ func Test_Node_Encode(t *testing.T) {
Key: []byte{1, 2, 3},
Value: []byte{100},
Children: []*Node{
nil, nil, nil, {Key: []byte{9}},
nil, nil, nil, {Key: []byte{11}},
nil, nil, nil, {Key: []byte{9}, Value: []byte{1}},
nil, nil, nil, {Key: []byte{11}, Value: []byte{1}},
},
},
writes: []writeCall{
Expand All @@ -239,8 +240,8 @@ func Test_Node_Encode(t *testing.T) {
Key: []byte{1, 2, 3},
Value: []byte{100},
Children: []*Node{
nil, nil, nil, {Key: []byte{9}},
nil, nil, nil, {Key: []byte{11}},
nil, nil, nil, {Key: []byte{9}, Value: []byte{1}},
nil, nil, nil, {Key: []byte{11}, Value: []byte{1}},
},
},
writes: []writeCall{
Expand All @@ -257,7 +258,7 @@ func Test_Node_Encode(t *testing.T) {
written: []byte{4, 100},
},
{ // children
written: []byte{12, 65, 9, 0},
written: []byte{16, 65, 9, 4, 1},
err: errTest,
},
},
Expand All @@ -271,8 +272,8 @@ func Test_Node_Encode(t *testing.T) {
Key: []byte{1, 2, 3},
Value: []byte{100},
Children: []*Node{
nil, nil, nil, {Key: []byte{9}},
nil, nil, nil, {Key: []byte{11}},
nil, nil, nil, {Key: []byte{9}, Value: []byte{1}},
nil, nil, nil, {Key: []byte{11}, Value: []byte{1}},
},
},
writes: []writeCall{
Expand All @@ -289,10 +290,10 @@ func Test_Node_Encode(t *testing.T) {
written: []byte{4, 100},
},
{ // first children
written: []byte{12, 65, 9, 0},
written: []byte{16, 65, 9, 4, 1},
},
{ // second children
written: []byte{12, 65, 11, 0},
written: []byte{16, 65, 11, 4, 1},
},
},
},
Expand Down
21 changes: 6 additions & 15 deletions internal/trie/node/hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,6 @@ func Test_Node_EncodeAndHash(t *testing.T) {
errWrapped error
errMessage string
}{
"empty leaf": {
node: Node{},
expectedNode: Node{
Encoding: []byte{0x40, 0x0},
HashDigest: []byte{0x40, 0x0},
},
encoding: []byte{0x40, 0x0},
hash: []byte{0x40, 0x0},
isRoot: false,
},
"small leaf encoding": {
node: Node{
Key: []byte{1},
Expand Down Expand Up @@ -93,14 +83,15 @@ func Test_Node_EncodeAndHash(t *testing.T) {
},
"large leaf encoding": {
node: Node{
Key: repeatBytes(65, 7),
Key: repeatBytes(65, 7),
Value: []byte{0x01},
},
expectedNode: Node{
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
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, 0x4, 0x1}, //nolint:lll
HashDigest: []byte{0xd2, 0x1d, 0x43, 0x7, 0x18, 0x17, 0x1b, 0xf1, 0x45, 0x9c, 0xe5, 0x8f, 0xd7, 0x79, 0x82, 0xb, 0xc8, 0x5c, 0x8, 0x47, 0xfe, 0x6c, 0x99, 0xc5, 0xe9, 0x57, 0x87, 0x7, 0x1d, 0x2e, 0x24, 0x5d}, //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
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, 0x4, 0x1}, //nolint:lll
hash: []byte{0xd2, 0x1d, 0x43, 0x7, 0x18, 0x17, 0x1b, 0xf1, 0x45, 0x9c, 0xe5, 0x8f, 0xd7, 0x79, 0x82, 0xb, 0xc8, 0x5c, 0x8, 0x47, 0xfe, 0x6c, 0x99, 0xc5, 0xe9, 0x57, 0x87, 0x7, 0x1d, 0x2e, 0x24, 0x5d}, //nolint:lll
isRoot: false,
},
"empty branch": {
Expand Down
Loading