From 91ffe79dabdf0ea154365bc9a802b7892b010b83 Mon Sep 17 00:00:00 2001 From: Quentin McGaw Date: Mon, 2 May 2022 12:09:25 +0000 Subject: [PATCH] Determine node type according to children field --- dot/state/storage_test.go | 1 - dot/state/tries_test.go | 6 +- internal/trie/node/branch_encode.go | 14 +- internal/trie/node/branch_encode_test.go | 77 +-- internal/trie/node/branch_test.go | 4 - internal/trie/node/copy.go | 3 +- internal/trie/node/copy_test.go | 38 +- internal/trie/node/decode.go | 3 - internal/trie/node/decode_test.go | 13 +- internal/trie/node/encode.go | 10 +- internal/trie/node/encode_decode_test.go | 22 +- internal/trie/node/encode_test.go | 44 +- internal/trie/node/hash_test.go | 52 +- internal/trie/node/header.go | 2 +- internal/trie/node/header_test.go | 56 +- internal/trie/node/node.go | 13 +- internal/trie/node/node_test.go | 20 +- internal/trie/node/types.go | 2 +- lib/trie/database.go | 18 +- lib/trie/lookup.go | 2 +- lib/trie/print_test.go | 5 - lib/trie/trie.go | 30 +- lib/trie/trie_endtoend_test.go | 2 - lib/trie/trie_test.go | 758 +++++++---------------- 24 files changed, 400 insertions(+), 795 deletions(-) delete mode 100644 internal/trie/node/branch_test.go diff --git a/dot/state/storage_test.go b/dot/state/storage_test.go index 18c9ead36bb..ea6cee81a9d 100644 --- a/dot/state/storage_test.go +++ b/dot/state/storage_test.go @@ -181,7 +181,6 @@ func TestGetStorageChildAndGetStorageFromChild(t *testing.T) { )) trieRoot := &node.Node{ - Type: node.Leaf, Key: []byte{1, 2}, Value: []byte{3, 4}, Dirty: true, diff --git a/dot/state/tries_test.go b/dot/state/tries_test.go index e748cf4de76..388a6891066 100644 --- a/dot/state/tries_test.go +++ b/dot/state/tries_test.go @@ -169,15 +169,13 @@ func Test_Tries_get(t *testing.T) { tries: &Tries{ rootToTrie: map[common.Hash]*trie.Trie{ {1, 2, 3}: trie.NewTrie(&node.Node{ - Type: node.Leaf, - Key: []byte{1, 2, 3}, + Key: []byte{1, 2, 3}, }), }, }, root: common.Hash{1, 2, 3}, trie: trie.NewTrie(&node.Node{ - Type: node.Leaf, - Key: []byte{1, 2, 3}, + Key: []byte{1, 2, 3}, }), }, "not found in map": { diff --git a/internal/trie/node/branch_encode.go b/internal/trie/node/branch_encode.go index f31de5c407f..58f8acd16d5 100644 --- a/internal/trie/node/branch_encode.go +++ b/internal/trie/node/branch_encode.go @@ -56,7 +56,7 @@ func encodeChildrenOpportunisticParallel(children []*Node, buffer io.Writer) (er resultsCh := make(chan encodingAsyncResult, ChildrenCapacity) for i, child := range children { - if child == nil || child.Type == Leaf { + if child == nil || child.Type() == Leaf { runEncodeChild(child, i, resultsCh, nil) continue } @@ -153,12 +153,12 @@ func scaleEncodeHash(node *Node) (encoding []byte, err error) { err = hashNode(node, buffer) if err != nil { - return nil, fmt.Errorf("cannot hash %s: %w", node.Type, err) + return nil, fmt.Errorf("cannot hash %s: %w", node.Type(), err) } encoding, err = scale.Marshal(buffer.Bytes()) if err != nil { - return nil, fmt.Errorf("cannot scale encode hashed %s: %w", node.Type, err) + return nil, fmt.Errorf("cannot scale encode hashed %s: %w", node.Type(), err) } return encoding, nil @@ -171,14 +171,14 @@ func hashNode(node *Node, digestWriter io.Writer) (err error) { err = node.Encode(encodingBuffer) if err != nil { - return fmt.Errorf("cannot encode %s: %w", node.Type, err) + return fmt.Errorf("cannot encode %s: %w", node.Type(), err) } // if length of encoded leaf is less than 32 bytes, do not hash if encodingBuffer.Len() < 32 { _, err = digestWriter.Write(encodingBuffer.Bytes()) if err != nil { - return fmt.Errorf("cannot write encoded %s to buffer: %w", node.Type, err) + return fmt.Errorf("cannot write encoded %s to buffer: %w", node.Type(), err) } return nil } @@ -191,12 +191,12 @@ func hashNode(node *Node, digestWriter io.Writer) (err error) { // Note: using the sync.Pool's buffer is useful here. _, err = hasher.Write(encodingBuffer.Bytes()) if err != nil { - return fmt.Errorf("cannot hash encoding of %s: %w", node.Type, err) + return fmt.Errorf("cannot hash encoding of %s: %w", node.Type(), err) } _, err = digestWriter.Write(hasher.Sum(nil)) if err != nil { - return fmt.Errorf("cannot write hash sum of %s to buffer: %w", node.Type, err) + return fmt.Errorf("cannot write hash sum of %s to buffer: %w", node.Type(), err) } return nil } diff --git a/internal/trie/node/branch_encode_test.go b/internal/trie/node/branch_encode_test.go index 8f65fb7f960..88efb2e7529 100644 --- a/internal/trie/node/branch_encode_test.go +++ b/internal/trie/node/branch_encode_test.go @@ -24,7 +24,6 @@ func Test_hashNode(t *testing.T) { }{ "small leaf buffer write error": { node: &Node{ - Type: Leaf, Encoding: []byte{1, 2, 3}, }, write: writeCall{ @@ -37,7 +36,6 @@ func Test_hashNode(t *testing.T) { }, "small leaf success": { node: &Node{ - Type: Leaf, Encoding: []byte{1, 2, 3}, }, write: writeCall{ @@ -46,7 +44,6 @@ func Test_hashNode(t *testing.T) { }, "leaf hash sum buffer write error": { node: &Node{ - Type: Leaf, Encoding: []byte{ 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, @@ -70,7 +67,6 @@ func Test_hashNode(t *testing.T) { }, "leaf hash sum success": { node: &Node{ - Type: Leaf, Encoding: []byte{ 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, @@ -90,7 +86,7 @@ func Test_hashNode(t *testing.T) { }, "empty branch": { node: &Node{ - Type: Branch, + Children: make([]*Node, ChildrenCapacity), }, write: writeCall{ written: []byte{128, 0, 0}, @@ -98,8 +94,8 @@ func Test_hashNode(t *testing.T) { }, "less than 32 bytes encoding": { node: &Node{ - Type: Branch, - Key: []byte{1, 2}, + Children: make([]*Node, ChildrenCapacity), + Key: []byte{1, 2}, }, write: writeCall{ written: []byte{130, 18, 0, 0}, @@ -107,8 +103,8 @@ func Test_hashNode(t *testing.T) { }, "less than 32 bytes encoding write error": { node: &Node{ - Type: Branch, - Key: []byte{1, 2}, + Children: make([]*Node, ChildrenCapacity), + Key: []byte{1, 2}, }, write: writeCall{ written: []byte{130, 18, 0, 0}, @@ -119,8 +115,8 @@ func Test_hashNode(t *testing.T) { }, "more than 32 bytes encoding": { node: &Node{ - Type: Branch, - Key: repeatBytes(100, 1), + Children: make([]*Node, ChildrenCapacity), + Key: repeatBytes(100, 1), }, write: writeCall{ written: []byte{ @@ -132,8 +128,8 @@ func Test_hashNode(t *testing.T) { }, "more than 32 bytes encoding write error": { node: &Node{ - Type: Branch, - Key: repeatBytes(100, 1), + Children: make([]*Node, ChildrenCapacity), + Key: repeatBytes(100, 1), }, write: writeCall{ written: []byte{ @@ -192,7 +188,6 @@ func populateChildren(valueSize, depth int) (children []*Node) { if depth == 0 { for i := range children { children[i] = &Node{ - Type: Leaf, Key: someValue, Value: someValue, } @@ -202,7 +197,6 @@ func populateChildren(valueSize, depth int) (children []*Node) { for i := range children { children[i] = &Node{ - Type: Branch, Key: someValue, Value: someValue, Children: populateChildren(valueSize, depth-1), @@ -224,7 +218,7 @@ func Test_encodeChildrenOpportunisticParallel(t *testing.T) { "no children": {}, "first child not nil": { children: []*Node{ - {Type: Leaf, Key: []byte{1}}, + {Key: []byte{1}}, }, writes: []writeCall{ { @@ -237,7 +231,7 @@ func Test_encodeChildrenOpportunisticParallel(t *testing.T) { nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, - {Type: Leaf, Key: []byte{1}}, + {Key: []byte{1}}, }, writes: []writeCall{ { @@ -247,8 +241,8 @@ func Test_encodeChildrenOpportunisticParallel(t *testing.T) { }, "first two children not nil": { children: []*Node{ - {Type: Leaf, Key: []byte{1}}, - {Type: Leaf, Key: []byte{2}}, + {Key: []byte{1}}, + {Key: []byte{2}}, }, writes: []writeCall{ { @@ -264,9 +258,7 @@ func Test_encodeChildrenOpportunisticParallel(t *testing.T) { nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, - {Type: Leaf, - Key: []byte{1}, - }, + {Key: []byte{1}}, nil, nil, nil, nil, }, writes: []writeCall{ @@ -284,10 +276,9 @@ func Test_encodeChildrenOpportunisticParallel(t *testing.T) { // running in parallel. children: []*Node{ { - Type: Branch, - Key: []byte{1}, + Key: []byte{1}, Children: []*Node{ - {Type: Leaf, Key: []byte{1}}, + {Key: []byte{1}}, }, }, }, @@ -335,7 +326,7 @@ func Test_encodeChildrenOpportunisticParallel(t *testing.T) { children := make([]*Node, ChildrenCapacity) for i := range children { children[i] = &Node{ - Type: Branch, + Children: make([]*Node, ChildrenCapacity), } } @@ -369,7 +360,7 @@ func Test_encodeChildrenSequentially(t *testing.T) { "no children": {}, "first child not nil": { children: []*Node{ - {Type: Leaf, Key: []byte{1}}, + {Key: []byte{1}}, }, writes: []writeCall{ { @@ -382,7 +373,7 @@ func Test_encodeChildrenSequentially(t *testing.T) { nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, - {Type: Leaf, Key: []byte{1}}, + {Key: []byte{1}}, }, writes: []writeCall{ { @@ -392,8 +383,8 @@ func Test_encodeChildrenSequentially(t *testing.T) { }, "first two children not nil": { children: []*Node{ - {Type: Leaf, Key: []byte{1}}, - {Type: Leaf, Key: []byte{2}}, + {Key: []byte{1}}, + {Key: []byte{2}}, }, writes: []writeCall{ { @@ -409,9 +400,7 @@ func Test_encodeChildrenSequentially(t *testing.T) { nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, - {Type: Leaf, - Key: []byte{1}, - }, + {Key: []byte{1}}, nil, nil, nil, nil, }, writes: []writeCall{ @@ -469,7 +458,7 @@ func Test_encodeChild(t *testing.T) { }{ "nil node": {}, "empty leaf child": { - child: &Node{Type: Leaf}, + child: &Node{}, writeCall: true, write: writeCall{ written: []byte{8, 64, 0}, @@ -477,7 +466,8 @@ func Test_encodeChild(t *testing.T) { }, "empty branch child": { child: &Node{ - Type: Branch}, + Children: make([]*Node, ChildrenCapacity), + }, writeCall: true, write: writeCall{ written: []byte{12, 128, 0, 0}, @@ -485,7 +475,8 @@ func Test_encodeChild(t *testing.T) { }, "buffer write error": { child: &Node{ - Type: Branch}, + Children: make([]*Node, ChildrenCapacity), + }, writeCall: true, write: writeCall{ written: []byte{12, 128, 0, 0}, @@ -496,7 +487,6 @@ func Test_encodeChild(t *testing.T) { }, "leaf child": { child: &Node{ - Type: Leaf, Key: []byte{1}, Value: []byte{2}, }, @@ -507,12 +497,10 @@ func Test_encodeChild(t *testing.T) { }, "branch child": { child: &Node{ - Type: Branch, Key: []byte{1}, Value: []byte{2}, Children: []*Node{ - nil, nil, {Type: Leaf, - Key: []byte{5}, + nil, nil, {Key: []byte{5}, Value: []byte{6}, }, }, @@ -560,24 +548,21 @@ func Test_scaleEncodeHash(t *testing.T) { errMessage string }{ "empty leaf": { - node: &Node{ - Type: Leaf, - }, + node: &Node{}, encoding: []byte{0x8, 0x40, 0}, }, "empty branch": { node: &Node{ - Type: Branch, + Children: make([]*Node, ChildrenCapacity), }, encoding: []byte{0xc, 0x80, 0x0, 0x0}, }, "non empty branch": { node: &Node{ - Type: Branch, Key: []byte{1, 2}, Value: []byte{3, 4}, Children: []*Node{ - nil, nil, {Type: Leaf, Key: []byte{9}}, + nil, nil, {Key: []byte{9}}, }, }, encoding: []byte{0x2c, 0xc2, 0x12, 0x4, 0x0, 0x8, 0x3, 0x4, 0xc, 0x41, 0x9, 0x0}, diff --git a/internal/trie/node/branch_test.go b/internal/trie/node/branch_test.go deleted file mode 100644 index 0a67150fcec..00000000000 --- a/internal/trie/node/branch_test.go +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright 2021 ChainSafe Systems (ON) -// SPDX-License-Identifier: LGPL-3.0-only - -package node diff --git a/internal/trie/node/copy.go b/internal/trie/node/copy.go index 5023022756a..e59af589889 100644 --- a/internal/trie/node/copy.go +++ b/internal/trie/node/copy.go @@ -57,12 +57,11 @@ type CopySettings struct { // children as well. func (n *Node) Copy(settings CopySettings) *Node { cpy := &Node{ - Type: n.Type, Dirty: n.Dirty, Generation: n.Generation, } - if n.Type == Branch { + if n.Type() == Branch { if settings.CopyChildren { // Copy all fields of children if we deep copy children childSettings := settings diff --git a/internal/trie/node/copy_test.go b/internal/trie/node/copy_test.go index a6e6fefc81c..0545061a2d9 100644 --- a/internal/trie/node/copy_test.go +++ b/internal/trie/node/copy_test.go @@ -29,13 +29,12 @@ func Test_Node_Copy(t *testing.T) { expectedNode *Node }{ "empty leaf": { - node: &Node{Type: Leaf}, + node: &Node{}, settings: DefaultCopySettings, - expectedNode: &Node{Type: Leaf}, + expectedNode: &Node{}, }, "non empty leaf": { node: &Node{ - Type: Leaf, Key: []byte{1, 2}, Value: []byte{3, 4}, Dirty: true, @@ -44,7 +43,6 @@ func Test_Node_Copy(t *testing.T) { }, settings: DefaultCopySettings, expectedNode: &Node{ - Type: Leaf, Key: []byte{1, 2}, Value: []byte{3, 4}, Dirty: true, @@ -52,7 +50,6 @@ func Test_Node_Copy(t *testing.T) { }, "deep copy leaf": { node: &Node{ - Type: Leaf, Key: []byte{1, 2}, Value: []byte{3, 4}, Dirty: true, @@ -61,7 +58,6 @@ func Test_Node_Copy(t *testing.T) { }, settings: DeepCopySettings, expectedNode: &Node{ - Type: Leaf, Key: []byte{1, 2}, Value: []byte{3, 4}, Dirty: true, @@ -71,23 +67,19 @@ func Test_Node_Copy(t *testing.T) { }, "empty branch": { node: &Node{ - Type: Branch, Children: make([]*Node, ChildrenCapacity), }, expectedNode: &Node{ - Type: Branch, Children: make([]*Node, ChildrenCapacity), }, }, "non empty branch": { node: &Node{ - Type: Branch, Key: []byte{1, 2}, Value: []byte{3, 4}, Children: padRightChildren([]*Node{ nil, nil, { - Type: Leaf, - Key: []byte{9}, + Key: []byte{9}, }, }), Dirty: true, @@ -96,13 +88,11 @@ func Test_Node_Copy(t *testing.T) { }, settings: DefaultCopySettings, expectedNode: &Node{ - Type: Branch, Key: []byte{1, 2}, Value: []byte{3, 4}, Children: padRightChildren([]*Node{ nil, nil, { - Type: Leaf, - Key: []byte{9}, + Key: []byte{9}, }, }), Dirty: true, @@ -110,11 +100,9 @@ func Test_Node_Copy(t *testing.T) { }, "branch with children copied": { node: &Node{ - Type: Branch, Children: padRightChildren([]*Node{ nil, nil, { - Type: Leaf, - Key: []byte{9}, + Key: []byte{9}, }, }), }, @@ -122,24 +110,20 @@ func Test_Node_Copy(t *testing.T) { CopyChildren: true, }, expectedNode: &Node{ - Type: Branch, Children: padRightChildren([]*Node{ nil, nil, { - Type: Leaf, - Key: []byte{9}, + Key: []byte{9}, }, }), }, }, "deep copy branch": { node: &Node{ - Type: Branch, Key: []byte{1, 2}, Value: []byte{3, 4}, Children: padRightChildren([]*Node{ nil, nil, { - Type: Leaf, - Key: []byte{9}, + Key: []byte{9}, }, }), Dirty: true, @@ -148,13 +132,11 @@ func Test_Node_Copy(t *testing.T) { }, settings: DeepCopySettings, expectedNode: &Node{ - Type: Branch, Key: []byte{1, 2}, Value: []byte{3, 4}, Children: padRightChildren([]*Node{ nil, nil, { - Type: Leaf, - Key: []byte{9}, + Key: []byte{9}, }, }), Dirty: true, @@ -177,8 +159,8 @@ func Test_Node_Copy(t *testing.T) { testForSliceModif(t, testCase.node.HashDigest, nodeCopy.HashDigest) testForSliceModif(t, testCase.node.Encoding, nodeCopy.Encoding) - if testCase.node.Children != nil { // branch - testCase.node.Children[15] = &Node{Type: Leaf, Key: []byte("modified")} + if testCase.node.Type() == Branch { + testCase.node.Children[15] = &Node{Key: []byte("modified")} assert.NotEqual(t, nodeCopy.Children, testCase.node.Children) } }) diff --git a/internal/trie/node/decode.go b/internal/trie/node/decode.go index a81a3496381..032cf1cb8b2 100644 --- a/internal/trie/node/decode.go +++ b/internal/trie/node/decode.go @@ -60,7 +60,6 @@ func Decode(reader io.Reader) (n *Node, err error) { // find other values using the persistent database. func decodeBranch(reader io.Reader, header byte) (node *Node, err error) { node = &Node{ - Type: Branch, Children: make([]*Node, ChildrenCapacity), } @@ -114,7 +113,6 @@ func decodeBranch(reader io.Reader, header byte) (node *Node, err error) { } node.Children[i] = &Node{ - Type: Leaf, HashDigest: hash, } } @@ -127,7 +125,6 @@ func decodeBranch(reader io.Reader, header byte) (node *Node, err error) { // decodeLeaf reads and decodes from a reader with the encoding specified in lib/trie/node/encode_doc.go. func decodeLeaf(reader io.Reader, header byte) (node *Node, err error) { node = &Node{ - Type: Leaf, Dirty: true, } diff --git a/internal/trie/node/decode_test.go b/internal/trie/node/decode_test.go index 8af56ca5e38..444425ccb13 100644 --- a/internal/trie/node/decode_test.go +++ b/internal/trie/node/decode_test.go @@ -69,7 +69,6 @@ func Test_Decode(t *testing.T) { ), ), n: &Node{ - Type: Leaf, Key: []byte{9}, Value: []byte{1, 2, 3}, Dirty: true, @@ -92,7 +91,6 @@ func Test_Decode(t *testing.T) { }, ), n: &Node{ - Type: Branch, Key: []byte{9}, Children: make([]*Node, ChildrenCapacity), Dirty: true, @@ -115,7 +113,6 @@ func Test_Decode(t *testing.T) { }, ), n: &Node{ - Type: Branch, Key: []byte{ 12, 3, 6, 5, 12, 3, 12, 15, 5, 9, 13, 6, @@ -126,7 +123,6 @@ func Test_Decode(t *testing.T) { Children: []*Node{ nil, nil, nil, nil, { - Type: Leaf, Key: []byte{ 14, 7, 11, 9, 0, 1, 2, 0, 9, 6, 11, 4, @@ -140,7 +136,6 @@ func Test_Decode(t *testing.T) { }, nil, nil, nil, nil, { - Type: Leaf, Key: []byte{ 15, 1, 15, 0, 5, 1, 5, 15, 4, 6, 2, 12, @@ -227,13 +222,11 @@ func Test_decodeBranch(t *testing.T) { ), header: 129, // node type 2 (branch without value) and key length 1 branch: &Node{ - Type: Branch, - Key: []byte{9}, + Key: []byte{9}, Children: padRightChildren([]*Node{ nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, { - Type: Leaf, HashDigest: []byte{1, 2, 3, 4, 5}, }, }), @@ -263,14 +256,12 @@ func Test_decodeBranch(t *testing.T) { ), header: 193, // node type 3 (branch with value) and key length 1 branch: &Node{ - Type: Branch, Key: []byte{9}, Value: []byte{7, 8, 9}, Children: padRightChildren([]*Node{ nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, { - Type: Leaf, HashDigest: []byte{1, 2, 3, 4, 5}, }, }), @@ -329,7 +320,6 @@ func Test_decodeLeaf(t *testing.T) { }), header: 65, // node type 1 (leaf) and key length 1 leaf: &Node{ - Type: Leaf, Key: []byte{9}, Dirty: true, }, @@ -343,7 +333,6 @@ func Test_decodeLeaf(t *testing.T) { ), header: 65, // node type 1 (leaf) and key length 1 leaf: &Node{ - Type: Leaf, Key: []byte{9}, Value: []byte{1, 2, 3, 4, 5}, Dirty: true, diff --git a/internal/trie/node/encode.go b/internal/trie/node/encode.go index 36138252711..c7890e16a82 100644 --- a/internal/trie/node/encode.go +++ b/internal/trie/node/encode.go @@ -33,7 +33,7 @@ func (n *Node) Encode(buffer Buffer) (err error) { return fmt.Errorf("cannot write LE key to buffer: %w", err) } - if n.Type == Branch { + if n.Type() == Branch { childrenBitmap := common.Uint16ToBytes(n.ChildrenBitmap()) _, err = buffer.Write(childrenBitmap) if err != nil { @@ -43,8 +43,8 @@ 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.Type() == Leaf || n.Value != nil { + // TODO remove `n.Type() == Leaf` and update tests 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) @@ -56,14 +56,14 @@ func (n *Node) Encode(buffer Buffer) (err error) { } } - if n.Type == Branch { + if n.Type() == Branch { err = encodeChildrenOpportunisticParallel(n.Children, buffer) if err != nil { return fmt.Errorf("cannot encode children of branch: %w", err) } } - if n.Type == Leaf { + if n.Type() == Leaf { // TODO cache this for branches too and update test cases. // TODO remove this copying since it defeats the purpose of `buffer` // and the sync.Pool. diff --git a/internal/trie/node/encode_decode_test.go b/internal/trie/node/encode_decode_test.go index 961c8d14c30..6962ae1b697 100644 --- a/internal/trie/node/encode_decode_test.go +++ b/internal/trie/node/encode_decode_test.go @@ -20,11 +20,9 @@ func Test_Branch_Encode_Decode(t *testing.T) { }{ "empty branch": { branchToEncode: &Node{ - Type: Branch, Children: make([]*Node, ChildrenCapacity), }, branchDecoded: &Node{ - Type: Branch, Key: []byte{}, Children: make([]*Node, ChildrenCapacity), Dirty: true, @@ -32,12 +30,10 @@ func Test_Branch_Encode_Decode(t *testing.T) { }, "branch with key 5": { branchToEncode: &Node{ - Type: Branch, Children: make([]*Node, ChildrenCapacity), Key: []byte{5}, }, branchDecoded: &Node{ - Type: Branch, Key: []byte{5}, Children: make([]*Node, ChildrenCapacity), Dirty: true, @@ -45,12 +41,10 @@ func Test_Branch_Encode_Decode(t *testing.T) { }, "branch with two bytes key": { branchToEncode: &Node{ - Type: Branch, Key: []byte{0xf, 0xa}, // note: each byte cannot be larger than 0xf Children: make([]*Node, ChildrenCapacity), }, branchDecoded: &Node{ - Type: Branch, Key: []byte{0xf, 0xa}, Children: make([]*Node, ChildrenCapacity), Dirty: true, @@ -58,22 +52,18 @@ func Test_Branch_Encode_Decode(t *testing.T) { }, "branch with child leaf inline": { branchToEncode: &Node{ - Type: Branch, - Key: []byte{5}, + Key: []byte{5}, Children: padRightChildren([]*Node{ { - Type: Leaf, Key: []byte{9}, Value: []byte{10}, }, }), }, branchDecoded: &Node{ - Type: Branch, - Key: []byte{5}, + Key: []byte{5}, Children: padRightChildren([]*Node{ { - Type: Leaf, Key: []byte{9}, Value: []byte{10}, Dirty: true, @@ -84,11 +74,9 @@ func Test_Branch_Encode_Decode(t *testing.T) { }, "branch with child leaf hash": { branchToEncode: &Node{ - Type: Branch, - Key: []byte{5}, + Key: []byte{5}, Children: padRightChildren([]*Node{ { - Type: Leaf, Key: []byte{ 10, 11, 12, 13, 14, 15, 16, 17, @@ -108,11 +96,9 @@ func Test_Branch_Encode_Decode(t *testing.T) { }), }, branchDecoded: &Node{ - Type: Branch, - Key: []byte{5}, + Key: []byte{5}, Children: padRightChildren([]*Node{ { - Type: Leaf, HashDigest: []byte{ 2, 18, 48, 30, 98, 133, 244, 78, 70, diff --git a/internal/trie/node/encode_test.go b/internal/trie/node/encode_test.go index a1740a9d77e..e57c13902ba 100644 --- a/internal/trie/node/encode_test.go +++ b/internal/trie/node/encode_test.go @@ -34,7 +34,6 @@ func Test_Node_Encode(t *testing.T) { }{ "clean leaf with encoding": { node: &Node{ - Type: Leaf, Encoding: []byte{1, 2, 3}, }, writes: []writeCall{ @@ -46,7 +45,6 @@ func Test_Node_Encode(t *testing.T) { }, "write error for clean leaf with encoding": { node: &Node{ - Type: Leaf, Encoding: []byte{1, 2, 3}, }, writes: []writeCall{ @@ -61,8 +59,7 @@ func Test_Node_Encode(t *testing.T) { }, "leaf header encoding error": { node: &Node{ - Type: Leaf, - Key: make([]byte, 63+(1<<16)), + Key: make([]byte, 63+(1<<16)), }, writes: []writeCall{ { @@ -74,8 +71,7 @@ func Test_Node_Encode(t *testing.T) { }, "leaf buffer write error for encoded key": { node: &Node{ - Type: Leaf, - Key: []byte{1, 2, 3}, + Key: []byte{1, 2, 3}, }, writes: []writeCall{ { @@ -91,7 +87,6 @@ func Test_Node_Encode(t *testing.T) { }, "leaf buffer write error for encoded value": { node: &Node{ - Type: Leaf, Key: []byte{1, 2, 3}, Value: []byte{4, 5, 6}, }, @@ -112,7 +107,6 @@ func Test_Node_Encode(t *testing.T) { }, "leaf success": { node: &Node{ - Type: Leaf, Key: []byte{1, 2, 3}, Value: []byte{4, 5, 6}, }, @@ -133,7 +127,7 @@ func Test_Node_Encode(t *testing.T) { }, "clean branch with encoding": { node: &Node{ - Type: Branch, + Children: make([]*Node, ChildrenCapacity), Encoding: []byte{1, 2, 3}, }, writes: []writeCall{ @@ -144,7 +138,7 @@ func Test_Node_Encode(t *testing.T) { }, "write error for clean branch with encoding": { node: &Node{ - Type: Branch, + Children: make([]*Node, ChildrenCapacity), Encoding: []byte{1, 2, 3}, }, writes: []writeCall{ @@ -158,8 +152,8 @@ func Test_Node_Encode(t *testing.T) { }, "branch header encoding error": { node: &Node{ - Type: Branch, - Key: make([]byte, 63+(1<<16)), + Children: make([]*Node, ChildrenCapacity), + Key: make([]byte, 63+(1<<16)), }, writes: []writeCall{ { // header @@ -171,9 +165,9 @@ func Test_Node_Encode(t *testing.T) { }, "buffer write error for encoded key": { node: &Node{ - Type: Branch, - Key: []byte{1, 2, 3}, - Value: []byte{100}, + Children: make([]*Node, ChildrenCapacity), + Key: []byte{1, 2, 3}, + Value: []byte{100}, }, writes: []writeCall{ { // header @@ -189,12 +183,11 @@ func Test_Node_Encode(t *testing.T) { }, "buffer write error for children bitmap": { node: &Node{ - Type: Branch, Key: []byte{1, 2, 3}, Value: []byte{100}, Children: []*Node{ - nil, nil, nil, {Type: Leaf, Key: []byte{9}}, - nil, nil, nil, {Type: Leaf, Key: []byte{11}}, + nil, nil, nil, {Key: []byte{9}}, + nil, nil, nil, {Key: []byte{11}}, }, }, writes: []writeCall{ @@ -214,12 +207,11 @@ func Test_Node_Encode(t *testing.T) { }, "buffer write error for value": { node: &Node{ - Type: Branch, Key: []byte{1, 2, 3}, Value: []byte{100}, Children: []*Node{ - nil, nil, nil, {Type: Leaf, Key: []byte{9}}, - nil, nil, nil, {Type: Leaf, Key: []byte{11}}, + nil, nil, nil, {Key: []byte{9}}, + nil, nil, nil, {Key: []byte{11}}, }, }, writes: []writeCall{ @@ -242,12 +234,11 @@ func Test_Node_Encode(t *testing.T) { }, "buffer write error for children encoding": { node: &Node{ - Type: Branch, Key: []byte{1, 2, 3}, Value: []byte{100}, Children: []*Node{ - nil, nil, nil, {Type: Leaf, Key: []byte{9}}, - nil, nil, nil, {Type: Leaf, Key: []byte{11}}, + nil, nil, nil, {Key: []byte{9}}, + nil, nil, nil, {Key: []byte{11}}, }, }, writes: []writeCall{ @@ -275,12 +266,11 @@ func Test_Node_Encode(t *testing.T) { }, "success with children encoding": { node: &Node{ - Type: Branch, Key: []byte{1, 2, 3}, Value: []byte{100}, Children: []*Node{ - nil, nil, nil, {Type: Leaf, Key: []byte{9}}, - nil, nil, nil, {Type: Leaf, Key: []byte{11}}, + nil, nil, nil, {Key: []byte{9}}, + nil, nil, nil, {Key: []byte{11}}, }, }, writes: []writeCall{ diff --git a/internal/trie/node/hash_test.go b/internal/trie/node/hash_test.go index 6d3417977ef..b2d785342ba 100644 --- a/internal/trie/node/hash_test.go +++ b/internal/trie/node/hash_test.go @@ -22,10 +22,8 @@ func Test_Node_EncodeAndHash(t *testing.T) { errMessage string }{ "empty leaf": { - node: Node{ - Type: Leaf}, + node: Node{}, expectedNode: Node{ - Type: Leaf, Encoding: []byte{0x40, 0x0}, HashDigest: []byte{0x40, 0x0}, }, @@ -35,12 +33,10 @@ func Test_Node_EncodeAndHash(t *testing.T) { }, "small leaf encoding": { node: Node{ - Type: Leaf, Key: []byte{1}, Value: []byte{2}, }, expectedNode: Node{ - Type: Leaf, Encoding: []byte{0x41, 0x1, 0x4, 0x2}, HashDigest: []byte{0x41, 0x1, 0x4, 0x2}, }, @@ -50,12 +46,10 @@ func Test_Node_EncodeAndHash(t *testing.T) { }, "small leaf encoding for root node": { node: Node{ - Type: Leaf, Key: []byte{1}, Value: []byte{2}, }, expectedNode: Node{ - Type: Leaf, Encoding: []byte{0x41, 0x1, 0x4, 0x2}, HashDigest: []byte{0x60, 0x51, 0x6d, 0xb, 0xb6, 0xe1, 0xbb, 0xfb, 0x12, 0x93, 0xf1, 0xb2, 0x76, 0xea, 0x95, 0x5, 0xe9, 0xf4, 0xa4, 0xe7, 0xd9, 0x8f, 0x62, 0xd, 0x5, 0x11, 0x5e, 0xb, 0x85, 0x27, 0x4a, 0xe1}, //nolint: lll }, @@ -65,7 +59,6 @@ func Test_Node_EncodeAndHash(t *testing.T) { }, "leaf dirty with precomputed encoding and hash": { node: Node{ - Type: Leaf, Key: []byte{1}, Value: []byte{2}, Dirty: true, @@ -73,7 +66,6 @@ func Test_Node_EncodeAndHash(t *testing.T) { HashDigest: []byte{4}, }, expectedNode: Node{ - Type: Leaf, Encoding: []byte{0x41, 0x1, 0x4, 0x2}, HashDigest: []byte{0x41, 0x1, 0x4, 0x2}, }, @@ -83,7 +75,6 @@ func Test_Node_EncodeAndHash(t *testing.T) { }, "leaf not dirty with precomputed encoding and hash": { node: Node{ - Type: Leaf, Key: []byte{1}, Value: []byte{2}, Dirty: false, @@ -91,7 +82,6 @@ func Test_Node_EncodeAndHash(t *testing.T) { HashDigest: []byte{4}, }, expectedNode: Node{ - Type: Leaf, Key: []byte{1}, Value: []byte{2}, Encoding: []byte{3}, @@ -103,11 +93,9 @@ func Test_Node_EncodeAndHash(t *testing.T) { }, "large leaf encoding": { node: Node{ - Type: Leaf, - Key: repeatBytes(65, 7), + Key: repeatBytes(65, 7), }, expectedNode: Node{ - Type: 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 }, @@ -116,9 +104,11 @@ func Test_Node_EncodeAndHash(t *testing.T) { isRoot: false, }, "empty branch": { - node: Node{Type: Branch}, + node: Node{ + Children: make([]*Node, ChildrenCapacity), + }, expectedNode: Node{ - Type: Branch, + Children: make([]*Node, ChildrenCapacity), Encoding: []byte{0x80, 0x0, 0x0}, HashDigest: []byte{0x80, 0x0, 0x0}, }, @@ -128,12 +118,12 @@ func Test_Node_EncodeAndHash(t *testing.T) { }, "small branch encoding": { node: Node{ - Type: Branch, - Key: []byte{1}, - Value: []byte{2}, + Children: make([]*Node, ChildrenCapacity), + Key: []byte{1}, + Value: []byte{2}, }, expectedNode: Node{ - Type: Branch, + Children: make([]*Node, ChildrenCapacity), Encoding: []byte{0xc1, 0x1, 0x0, 0x0, 0x4, 0x2}, HashDigest: []byte{0xc1, 0x1, 0x0, 0x0, 0x4, 0x2}, }, @@ -143,12 +133,12 @@ func Test_Node_EncodeAndHash(t *testing.T) { }, "small branch encoding for root node": { node: Node{ - Type: Branch, - Key: []byte{1}, - Value: []byte{2}, + Children: make([]*Node, ChildrenCapacity), + Key: []byte{1}, + Value: []byte{2}, }, expectedNode: Node{ - Type: Branch, + Children: make([]*Node, ChildrenCapacity), Encoding: []byte{0xc1, 0x1, 0x0, 0x0, 0x4, 0x2}, HashDigest: []byte{0x48, 0x3c, 0xf6, 0x87, 0xcc, 0x5a, 0x60, 0x42, 0xd3, 0xcf, 0xa6, 0x91, 0xe6, 0x88, 0xfb, 0xdc, 0x1b, 0x38, 0x39, 0x5d, 0x6, 0x0, 0xbf, 0xc3, 0xb, 0x4b, 0x5d, 0x6a, 0x37, 0xd9, 0xc5, 0x1c}, // nolint: lll }, @@ -158,7 +148,7 @@ func Test_Node_EncodeAndHash(t *testing.T) { }, "branch dirty with precomputed encoding and hash": { node: Node{ - Type: Branch, + Children: make([]*Node, ChildrenCapacity), Key: []byte{1}, Value: []byte{2}, Dirty: true, @@ -166,7 +156,7 @@ func Test_Node_EncodeAndHash(t *testing.T) { HashDigest: []byte{4}, }, expectedNode: Node{ - Type: Branch, + Children: make([]*Node, ChildrenCapacity), Encoding: []byte{0xc1, 0x1, 0x0, 0x0, 0x4, 0x2}, HashDigest: []byte{0xc1, 0x1, 0x0, 0x0, 0x4, 0x2}, }, @@ -176,7 +166,7 @@ func Test_Node_EncodeAndHash(t *testing.T) { }, "branch not dirty with precomputed encoding and hash": { node: Node{ - Type: Branch, + Children: make([]*Node, ChildrenCapacity), Key: []byte{1}, Value: []byte{2}, Dirty: false, @@ -184,7 +174,7 @@ func Test_Node_EncodeAndHash(t *testing.T) { HashDigest: []byte{4}, }, expectedNode: Node{ - Type: Branch, + Children: make([]*Node, ChildrenCapacity), Key: []byte{1}, Value: []byte{2}, Encoding: []byte{3}, @@ -196,11 +186,11 @@ func Test_Node_EncodeAndHash(t *testing.T) { }, "large branch encoding": { node: Node{ - Type: Branch, - Key: repeatBytes(65, 7), + Children: make([]*Node, ChildrenCapacity), + Key: repeatBytes(65, 7), }, expectedNode: Node{ - Type: Branch, + Children: make([]*Node, ChildrenCapacity), 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 }, diff --git a/internal/trie/node/header.go b/internal/trie/node/header.go index 25efced6797..adf51e2c080 100644 --- a/internal/trie/node/header.go +++ b/internal/trie/node/header.go @@ -16,7 +16,7 @@ const ( ) func encodeHeader(node *Node, writer io.Writer) (err error) { - switch node.Type { + switch node.Type() { case Leaf: return encodeLeafHeader(node, writer) case Branch: diff --git a/internal/trie/node/header_test.go b/internal/trie/node/header_test.go index 3d4d79e965e..16147d75de8 100644 --- a/internal/trie/node/header_test.go +++ b/internal/trie/node/header_test.go @@ -19,7 +19,7 @@ func Test_encodeBranchHeader(t *testing.T) { }{ "no key": { branch: &Node{ - Type: Branch, + Children: make([]*Node, ChildrenCapacity), }, writes: []writeCall{ {written: []byte{0x80}}, @@ -27,8 +27,8 @@ func Test_encodeBranchHeader(t *testing.T) { }, "with value": { branch: &Node{ - Type: Branch, - Value: []byte{}, + Value: []byte{}, + Children: make([]*Node, ChildrenCapacity), }, writes: []writeCall{ {written: []byte{0xc0}}, @@ -36,8 +36,8 @@ func Test_encodeBranchHeader(t *testing.T) { }, "key of length 30": { branch: &Node{ - Type: Branch, - Key: make([]byte, 30), + Key: make([]byte, 30), + Children: make([]*Node, ChildrenCapacity), }, writes: []writeCall{ {written: []byte{0x9e}}, @@ -45,8 +45,8 @@ func Test_encodeBranchHeader(t *testing.T) { }, "key of length 62": { branch: &Node{ - Type: Branch, - Key: make([]byte, 62), + Key: make([]byte, 62), + Children: make([]*Node, ChildrenCapacity), }, writes: []writeCall{ {written: []byte{0xbe}}, @@ -54,8 +54,8 @@ func Test_encodeBranchHeader(t *testing.T) { }, "key of length 63": { branch: &Node{ - Type: Branch, - Key: make([]byte, 63), + Key: make([]byte, 63), + Children: make([]*Node, ChildrenCapacity), }, writes: []writeCall{ {written: []byte{0xbf}}, @@ -64,8 +64,8 @@ func Test_encodeBranchHeader(t *testing.T) { }, "key of length 64": { branch: &Node{ - Type: Branch, - Key: make([]byte, 64), + Key: make([]byte, 64), + Children: make([]*Node, ChildrenCapacity), }, writes: []writeCall{ {written: []byte{0xbf}}, @@ -74,8 +74,8 @@ func Test_encodeBranchHeader(t *testing.T) { }, "key too big": { branch: &Node{ - Type: Branch, - Key: make([]byte, 65535+63), + Key: make([]byte, 65535+63), + Children: make([]*Node, ChildrenCapacity), }, writes: []writeCall{ {written: []byte{0xbf}}, @@ -85,7 +85,8 @@ func Test_encodeBranchHeader(t *testing.T) { }, "small key length write error": { branch: &Node{ - Type: Branch}, + Children: make([]*Node, ChildrenCapacity), + }, writes: []writeCall{ { written: []byte{0x80}, @@ -97,8 +98,8 @@ func Test_encodeBranchHeader(t *testing.T) { }, "long key length write error": { branch: &Node{ - Type: Branch, - Key: make([]byte, 64), + Key: make([]byte, 64), + Children: make([]*Node, ChildrenCapacity), }, writes: []writeCall{ { @@ -148,15 +149,14 @@ func Test_encodeLeafHeader(t *testing.T) { errMessage string }{ "no key": { - leaf: &Node{Type: Leaf}, + leaf: &Node{}, writes: []writeCall{ {written: []byte{0x40}}, }, }, "key of length 30": { leaf: &Node{ - Type: Leaf, - Key: make([]byte, 30), + Key: make([]byte, 30), }, writes: []writeCall{ {written: []byte{0x5e}}, @@ -164,8 +164,7 @@ func Test_encodeLeafHeader(t *testing.T) { }, "short key write error": { leaf: &Node{ - Type: Leaf, - Key: make([]byte, 30), + Key: make([]byte, 30), }, writes: []writeCall{ { @@ -178,8 +177,7 @@ func Test_encodeLeafHeader(t *testing.T) { }, "key of length 62": { leaf: &Node{ - Type: Leaf, - Key: make([]byte, 62), + Key: make([]byte, 62), }, writes: []writeCall{ {written: []byte{0x7e}}, @@ -187,8 +185,7 @@ func Test_encodeLeafHeader(t *testing.T) { }, "key of length 63": { leaf: &Node{ - Type: Leaf, - Key: make([]byte, 63), + Key: make([]byte, 63), }, writes: []writeCall{ {written: []byte{0x7f}}, @@ -197,8 +194,7 @@ func Test_encodeLeafHeader(t *testing.T) { }, "key of length 64": { leaf: &Node{ - Type: Leaf, - Key: make([]byte, 64), + Key: make([]byte, 64), }, writes: []writeCall{ {written: []byte{0x7f}}, @@ -207,8 +203,7 @@ func Test_encodeLeafHeader(t *testing.T) { }, "long key first byte write error": { leaf: &Node{ - Type: Leaf, - Key: make([]byte, 63), + Key: make([]byte, 63), }, writes: []writeCall{ { @@ -221,8 +216,7 @@ func Test_encodeLeafHeader(t *testing.T) { }, "key too big": { leaf: &Node{ - Type: Leaf, - Key: make([]byte, 65535+63), + Key: make([]byte, 65535+63), }, writes: []writeCall{ {written: []byte{0x7f}}, diff --git a/internal/trie/node/node.go b/internal/trie/node/node.go index 220ea859f1a..1bae81f0829 100644 --- a/internal/trie/node/node.go +++ b/internal/trie/node/node.go @@ -13,8 +13,6 @@ import ( // Node is a node in the trie and can be a leaf or a branch. type Node struct { - // Type is the node type. - Type Type // Key is the partial key bytes in nibbles (0 to f in hexadecimal) Key []byte Value []byte @@ -37,6 +35,15 @@ type Node struct { Encoding []byte } +// Type returns Leaf or Branch depending on what type +// the node is. +func (n *Node) Type() Type { + if n.Children != nil { + return Branch + } + return Leaf +} + func (n *Node) String() string { return n.StringNode().String() } @@ -44,7 +51,7 @@ func (n *Node) String() string { // StringNode returns a gotree compatible node for String methods. func (n Node) StringNode() (stringNode *gotree.Node) { caser := cases.Title(language.BritishEnglish) - stringNode = gotree.New(caser.String(n.Type.String())) + stringNode = gotree.New(caser.String(n.Type().String())) stringNode.Appendf("Generation: %d", n.Generation) stringNode.Appendf("Dirty: %t", n.Dirty) stringNode.Appendf("Key: " + bytesToString(n.Key)) diff --git a/internal/trie/node/node_test.go b/internal/trie/node/node_test.go index 3a07be48c04..dfba1debf07 100644 --- a/internal/trie/node/node_test.go +++ b/internal/trie/node/node_test.go @@ -17,7 +17,7 @@ func Test_Node_String(t *testing.T) { s string }{ "empty leaf": { - node: &Node{Type: Leaf}, + node: &Node{}, s: `Leaf ├── Generation: 0 ├── Dirty: false @@ -28,7 +28,6 @@ func Test_Node_String(t *testing.T) { }, "leaf with value smaller than 1024": { node: &Node{ - Type: Leaf, Key: []byte{1, 2}, Value: []byte{3, 4}, Dirty: true, @@ -43,7 +42,6 @@ func Test_Node_String(t *testing.T) { }, "leaf with value higher than 1024": { node: &Node{ - Type: Leaf, Key: []byte{1, 2}, Value: make([]byte, 1025), Dirty: true, @@ -58,7 +56,7 @@ func Test_Node_String(t *testing.T) { }, "empty branch": { node: &Node{ - Type: Branch, + Children: make([]*Node, ChildrenCapacity), }, s: `Branch ├── Generation: 0 @@ -70,17 +68,16 @@ func Test_Node_String(t *testing.T) { }, "branch with value smaller than 1024": { node: &Node{ - Type: Branch, Key: []byte{1, 2}, Value: []byte{3, 4}, Dirty: true, Children: []*Node{ nil, nil, nil, - {Type: Leaf}, + {}, nil, nil, nil, - {Type: Branch}, + {Children: make([]*Node, ChildrenCapacity)}, nil, nil, nil, - {Type: Leaf}, + {}, nil, nil, nil, nil, }, }, @@ -118,17 +115,16 @@ func Test_Node_String(t *testing.T) { }, "branch with value higher than 1024": { node: &Node{ - Type: Branch, Key: []byte{1, 2}, Value: make([]byte, 1025), Dirty: true, Children: []*Node{ nil, nil, nil, - {Type: Leaf}, + {}, nil, nil, nil, - {Type: Branch}, + {Children: make([]*Node, ChildrenCapacity)}, nil, nil, nil, - {Type: Leaf}, + {}, nil, nil, nil, nil, }, }, diff --git a/internal/trie/node/types.go b/internal/trie/node/types.go index 0f49bf01098..ea4a9be136d 100644 --- a/internal/trie/node/types.go +++ b/internal/trie/node/types.go @@ -10,7 +10,7 @@ type Type byte const ( // Leaf type for leaf nodes. - Leaf = iota + Leaf Type = iota // Branch type for branches (with or without value). Branch ) diff --git a/lib/trie/database.go b/lib/trie/database.go index 3b81bc87067..504a1085519 100644 --- a/lib/trie/database.go +++ b/lib/trie/database.go @@ -56,7 +56,7 @@ func (t *Trie) store(db chaindb.Batch, n *Node) error { return err } - if n.Type == node.Branch { + if n.Type() == node.Branch { for _, child := range n.Children { if child == nil { continue @@ -120,7 +120,7 @@ func (t *Trie) LoadFromProof(proofEncodedNodes [][]byte, rootHash []byte) error // loadProof is a recursive function that will create all the trie paths based // on the mapped proofs slice starting at the root func (t *Trie) loadProof(proofHashToNode map[string]*Node, n *Node) { - if n.Type != node.Branch { + if n.Type() != node.Branch { return } @@ -170,7 +170,7 @@ func (t *Trie) Load(db chaindb.Database, rootHash common.Hash) error { } func (t *Trie) load(db chaindb.Database, n *Node) error { - if n.Type != node.Branch { + if n.Type() != node.Branch { return nil } @@ -182,7 +182,7 @@ func (t *Trie) load(db chaindb.Database, n *Node) error { hash := child.HashDigest - if len(hash) == 0 && child.Type == node.Leaf { + if len(hash) == 0 && child.Type() == node.Leaf { // node has already been loaded inline // just set encoding + hash digest _, _, err := child.EncodeAndHash(false) @@ -237,7 +237,7 @@ func (t *Trie) load(db chaindb.Database, n *Node) error { // PopulateNodeHashes writes hashes of each children of the node given // as keys to the map hashesSet. func (t *Trie) PopulateNodeHashes(n *Node, hashesSet map[common.Hash]struct{}) { - if n.Type != node.Branch { + if n.Type() != node.Branch { return } @@ -311,7 +311,7 @@ func GetFromDB(db chaindb.Database, rootHash common.Hash, key []byte) ( // slice will modify the value of the node in the trie. func getFromDB(db chaindb.Database, n *Node, key []byte) ( value []byte, err error) { - if n.Type == node.Leaf { + if n.Type() == node.Leaf { if bytes.Equal(n.Key, key) { return n.Value, nil } @@ -341,7 +341,7 @@ func getFromDB(db chaindb.Database, n *Node, key []byte) ( // Child can be either inlined or a hash pointer. childHash := child.HashDigest - if len(childHash) == 0 && child.Type == node.Leaf { + if len(childHash) == 0 && child.Type() == node.Leaf { return getFromDB(db, child, key[commonPrefixLength+1:]) } @@ -395,7 +395,7 @@ func (t *Trie) writeDirty(db chaindb.Batch, n *Node) error { hash, err) } - if n.Type != node.Branch { + if n.Type() != node.Branch { n.SetDirty(false) return nil } @@ -452,7 +452,7 @@ func (t *Trie) getInsertedNodeHashes(n *Node, hashes map[common.Hash]struct{}) ( hashes[common.BytesToHash(hash)] = struct{}{} - if n.Type != node.Branch { + if n.Type() != node.Branch { return nil } diff --git a/lib/trie/lookup.go b/lib/trie/lookup.go index 6a7fe28bfe8..4c0a1699364 100644 --- a/lib/trie/lookup.go +++ b/lib/trie/lookup.go @@ -29,7 +29,7 @@ func find(parent *Node, key []byte, recorder recorder, isCurrentRoot bool) error recorder.Record(hash, enc) - if parent.Type != node.Branch { + if parent.Type() != node.Branch { return nil } diff --git a/lib/trie/print_test.go b/lib/trie/print_test.go index c356fa7c7bc..2129824fb7d 100644 --- a/lib/trie/print_test.go +++ b/lib/trie/print_test.go @@ -6,7 +6,6 @@ package trie import ( "testing" - "github.com/ChainSafe/gossamer/internal/trie/node" "github.com/stretchr/testify/assert" ) @@ -23,7 +22,6 @@ func Test_Trie_String(t *testing.T) { "leaf root": { trie: Trie{ root: &Node{ - Type: node.Leaf, Key: []byte{1, 2, 3}, Value: []byte{3, 4, 5}, Generation: 1, @@ -40,19 +38,16 @@ func Test_Trie_String(t *testing.T) { "branch root": { trie: Trie{ root: &Node{ - Type: node.Branch, Key: nil, Value: []byte{1, 2}, Children: []*Node{ { - Type: node.Leaf, Key: []byte{1, 2, 3}, Value: []byte{3, 4, 5}, Generation: 2, }, nil, nil, { - Type: node.Leaf, Key: []byte{1, 2, 3}, Value: []byte{3, 4, 5}, Generation: 3, diff --git a/lib/trie/trie.go b/lib/trie/trie.go index a69b0d11230..10f951a92c0 100644 --- a/lib/trie/trie.go +++ b/lib/trie/trie.go @@ -201,7 +201,7 @@ func entries(parent *Node, prefix []byte, kv map[string][]byte) map[string][]byt return kv } - if parent.Type == node.Leaf { + if parent.Type() == node.Leaf { parentKey := parent.Key fullKeyNibbles := concatenateSlices(prefix, parentKey) keyLE := string(codec.NibblesToKeyLE(fullKeyNibbles)) @@ -244,7 +244,7 @@ func findNextKey(parent *Node, prefix, searchKey []byte) (nextKey []byte) { return nil } - if parent.Type == node.Leaf { + if parent.Type() == node.Leaf { return findNextKeyLeaf(parent, prefix, searchKey) } return findNextKeyBranch(parent, prefix, searchKey) @@ -331,7 +331,6 @@ func (t *Trie) put(key, value []byte) { func (t *Trie) insert(parent *Node, key, value []byte) (newParent *Node) { if parent == nil { return &Node{ - Type: node.Leaf, Key: key, Value: value, Generation: t.generation, @@ -341,7 +340,7 @@ func (t *Trie) insert(parent *Node, key, value []byte) (newParent *Node) { // TODO ensure all values have dirty set to true - if parent.Type == node.Branch { + if parent.Type() == node.Branch { return t.insertInBranch(parent, key, value) } return t.insertInLeaf(parent, key, value) @@ -365,7 +364,6 @@ func (t *Trie) insertInLeaf(parentLeaf *Node, key, // Convert the current leaf parent into a branch parent newBranchParent := &Node{ - Type: node.Branch, Key: key[:commonPrefixLength], Generation: t.generation, Children: make([]*node.Node, node.ChildrenCapacity), @@ -402,7 +400,6 @@ func (t *Trie) insertInLeaf(parentLeaf *Node, key, } childIndex := key[commonPrefixLength] newBranchParent.Children[childIndex] = &Node{ - Type: node.Leaf, Key: key[commonPrefixLength+1:], Value: value, Generation: t.generation, @@ -430,7 +427,6 @@ func (t *Trie) insertInBranch(parentBranch *Node, key, value []byte) (newParent if child == nil { child = &Node{ - Type: node.Leaf, Key: remainingKey, Value: value, Generation: t.generation, @@ -448,7 +444,6 @@ func (t *Trie) insertInBranch(parentBranch *Node, key, value []byte) (newParent // update partial keys, new branch has key up to matching length commonPrefixLength := lenCommonPrefix(key, parentBranch.Key) newParentBranch := &Node{ - Type: node.Branch, Key: key[:commonPrefixLength], Generation: t.generation, Children: make([]*node.Node, node.ChildrenCapacity), @@ -518,7 +513,7 @@ func getKeysWithPrefix(parent *Node, prefix, key []byte, return keysLE } - if parent.Type == node.Leaf { + if parent.Type() == node.Leaf { return getKeysWithPrefixFromLeaf(parent, prefix, key, keysLE) } @@ -564,7 +559,7 @@ func addAllKeys(parent *Node, prefix []byte, keysLE [][]byte) (newKeysLE [][]byt return keysLE } - if parent.Type == node.Leaf { + if parent.Type() == node.Leaf { keyLE := makeFullKeyLE(prefix, parent.Key) keysLE = append(keysLE, keyLE) return keysLE @@ -608,7 +603,7 @@ func retrieve(parent *Node, key []byte) (value []byte) { return nil } - if parent.Type == node.Leaf { + if parent.Type() == node.Leaf { return retrieveFromLeaf(parent, key) } return retrieveFromBranch(parent, key) @@ -662,7 +657,7 @@ func (t *Trie) clearPrefixLimit(parent *Node, prefix []byte, limit uint32) ( return nil, 0, true } - if parent.Type == node.Leaf { + if parent.Type() == node.Leaf { // if prefix is not found, it's also all deleted. // TODO check this is the same behaviour as in substrate const allDeleted = true @@ -756,7 +751,7 @@ func (t *Trie) deleteNodesLimit(parent *Node, prefix []byte, limit uint32) ( return nil, 0 } - if parent.Type == node.Leaf { + if parent.Type() == node.Leaf { valuesDeleted = 1 return nil, valuesDeleted } @@ -824,7 +819,7 @@ func (t *Trie) clearPrefix(parent *Node, prefix []byte) ( return nil, true } - if parent.Type == node.Leaf { + if parent.Type() == node.Leaf { return parent, false } @@ -881,7 +876,7 @@ func (t *Trie) delete(parent *Node, key []byte) (newParent *Node, deleted bool) return nil, false } - if parent.Type == node.Leaf { + if parent.Type() == node.Leaf { if deleteLeaf(parent, key) == nil { return nil, true } @@ -949,7 +944,6 @@ func handleDeletion(branch *Node, key []byte) (newNode *Node) { case childrenCount == 0 && branch.Value != nil: commonPrefixLength := lenCommonPrefix(branch.Key, key) return &Node{ - Type: node.Leaf, Key: key[:commonPrefixLength], Value: branch.Value, Dirty: true, @@ -959,10 +953,9 @@ func handleDeletion(branch *Node, key []byte) (newNode *Node) { childIndex := firstChildIndex child := branch.Children[firstChildIndex] - if child.Type == node.Leaf { + if child.Type() == node.Leaf { newLeafKey := concatenateSlices(branch.Key, intToByteSlice(childIndex), child.Key) return &Node{ - Type: node.Leaf, Key: newLeafKey, Value: child.Value, Dirty: true, @@ -973,7 +966,6 @@ func handleDeletion(branch *Node, key []byte) (newNode *Node) { childBranch := child newBranchKey := concatenateSlices(branch.Key, intToByteSlice(childIndex), childBranch.Key) newBranch := &Node{ - Type: node.Branch, Key: newBranchKey, Value: childBranch.Value, Generation: branch.Generation, diff --git a/lib/trie/trie_endtoend_test.go b/lib/trie/trie_endtoend_test.go index d91c44a3f66..7f6a0c5b0ea 100644 --- a/lib/trie/trie_endtoend_test.go +++ b/lib/trie/trie_endtoend_test.go @@ -16,7 +16,6 @@ import ( "github.com/stretchr/testify/require" "github.com/ChainSafe/gossamer/internal/trie/codec" - "github.com/ChainSafe/gossamer/internal/trie/node" "github.com/ChainSafe/gossamer/lib/common" ) @@ -494,7 +493,6 @@ func TestClearPrefix_Small(t *testing.T) { ssTrie.ClearPrefix([]byte("noo")) expectedRoot := &Node{ - Type: node.Leaf, Key: codec.KeyLEToNibbles([]byte("other")), Value: []byte("other"), Generation: 1, diff --git a/lib/trie/trie_test.go b/lib/trie/trie_test.go index b3834308ea0..de247f83c56 100644 --- a/lib/trie/trie_test.go +++ b/lib/trie/trie_test.go @@ -27,13 +27,11 @@ func Test_NewEmptyTrie(t *testing.T) { func Test_NewTrie(t *testing.T) { root := &Node{ - Type: node.Leaf, Key: []byte{0}, Value: []byte{17}, } expectedTrie := &Trie{ root: &Node{ - Type: node.Leaf, Key: []byte{0}, Value: []byte{17}, }, @@ -49,21 +47,18 @@ func Test_Trie_Snapshot(t *testing.T) { trie := &Trie{ generation: 8, - root: &Node{ - Type: node.Leaf, Key: []byte{8}}, + root: &Node{Key: []byte{8}}, childTries: map[common.Hash]*Trie{ {1}: { generation: 1, - root: &Node{ - Type: node.Leaf, Key: []byte{1}}, + root: &Node{Key: []byte{1}}, deletedKeys: map[common.Hash]struct{}{ {1}: {}, }, }, {2}: { generation: 2, - root: &Node{ - Type: node.Leaf, Key: []byte{2}}, + root: &Node{Key: []byte{2}}, deletedKeys: map[common.Hash]struct{}{ {2}: {}, }, @@ -77,19 +72,16 @@ func Test_Trie_Snapshot(t *testing.T) { expectedTrie := &Trie{ generation: 9, - root: &Node{ - Type: node.Leaf, Key: []byte{8}}, + root: &Node{Key: []byte{8}}, childTries: map[common.Hash]*Trie{ {1}: { - generation: 2, - root: &Node{ - Type: node.Leaf, Key: []byte{1}}, + generation: 2, + root: &Node{Key: []byte{1}}, deletedKeys: map[common.Hash]struct{}{}, }, {2}: { - generation: 3, - root: &Node{ - Type: node.Leaf, Key: []byte{2}}, + generation: 3, + root: &Node{Key: []byte{2}}, deletedKeys: map[common.Hash]struct{}{}, }, }, @@ -115,13 +107,11 @@ func Test_Trie_updateGeneration(t *testing.T) { "trie generation higher and empty hash": { trieGeneration: 2, node: &Node{ - Type: node.Leaf, Generation: 1, Key: []byte{1}, }, copySettings: node.DefaultCopySettings, newNode: &Node{ - Type: node.Leaf, Generation: 2, Key: []byte{1}, }, @@ -131,14 +121,12 @@ func Test_Trie_updateGeneration(t *testing.T) { "trie generation higher and hash": { trieGeneration: 2, node: &Node{ - Type: node.Leaf, Generation: 1, Key: []byte{1}, HashDigest: []byte{1, 2, 3}, }, copySettings: node.DefaultCopySettings, newNode: &Node{ - Type: node.Leaf, Generation: 2, Key: []byte{1}, }, @@ -233,13 +221,11 @@ func Test_Trie_DeepCopy(t *testing.T) { "filled trie": { trieOriginal: &Trie{ generation: 1, - root: &Node{ - Type: node.Leaf, Key: []byte{1, 2}}, + root: &Node{Key: []byte{1, 2}}, childTries: map[common.Hash]*Trie{ {1, 2, 3}: { generation: 2, - root: &Node{ - Type: node.Leaf, Key: []byte{1}}, + root: &Node{Key: []byte{1}}, deletedKeys: map[common.Hash]struct{}{ {1, 2, 3}: {}, {3, 4, 5}: {}, @@ -253,13 +239,11 @@ func Test_Trie_DeepCopy(t *testing.T) { }, trieCopy: &Trie{ generation: 1, - root: &Node{ - Type: node.Leaf, Key: []byte{1, 2}}, + root: &Node{Key: []byte{1, 2}}, childTries: map[common.Hash]*Trie{ {1, 2, 3}: { generation: 2, - root: &Node{ - Type: node.Leaf, Key: []byte{1}}, + root: &Node{Key: []byte{1}}, deletedKeys: map[common.Hash]struct{}{ {1, 2, 3}: {}, {3, 4, 5}: {}, @@ -293,13 +277,11 @@ func Test_Trie_RootNode(t *testing.T) { trie := Trie{ root: &Node{ - Type: node.Leaf, - Key: []byte{1, 2, 3}, + Key: []byte{1, 2, 3}, }, } expectedRoot := &Node{ - Type: node.Leaf, - Key: []byte{1, 2, 3}, + Key: []byte{1, 2, 3}, } root := trie.RootNode() @@ -348,8 +330,7 @@ func Test_encodeRoot(t *testing.T) { }, "root encoding error": { root: &Node{ - Type: node.Leaf, - Key: []byte{1, 2}, + Key: []byte{1, 2}, }, bufferCalls: bufferCalls{ writeCalls: []writeCall{ @@ -362,14 +343,12 @@ func Test_encodeRoot(t *testing.T) { errWrapped: errTest, errMessage: "cannot encode header: test error", expectedRoot: &Node{ - Type: node.Leaf, - Key: []byte{1, 2}, + Key: []byte{1, 2}, }, }, "root encoding success": { root: &Node{ - Type: node.Leaf, - Key: []byte{1, 2}, + Key: []byte{1, 2}, }, bufferCalls: bufferCalls{ writeCalls: []writeCall{ @@ -383,7 +362,6 @@ func Test_encodeRoot(t *testing.T) { bytesReturn: []byte{66, 18, 0}, }, expectedRoot: &Node{ - Type: node.Leaf, Key: []byte{1, 2}, Encoding: []byte{66, 18, 0}, }, @@ -468,8 +446,7 @@ func Test_Trie_Hash(t *testing.T) { "leaf root": { trie: Trie{ root: &Node{ - Type: node.Leaf, - Key: []byte{1, 2, 3}, + Key: []byte{1, 2, 3}, }, }, hash: common.Hash{ @@ -479,7 +456,6 @@ func Test_Trie_Hash(t *testing.T) { 0x9f, 0x57, 0x82, 0x94, 0xc9, 0x76, 0xf4, 0x6f}, expectedTrie: Trie{ root: &Node{ - Type: node.Leaf, Key: []byte{1, 2, 3}, Encoding: []byte{67, 1, 35, 0}, }, @@ -488,12 +464,10 @@ func Test_Trie_Hash(t *testing.T) { "branch root": { trie: Trie{ root: &Node{ - Type: node.Branch, Key: []byte{1, 2, 3}, Value: []byte("branch"), Children: padRightChildren([]*Node{ - { - Type: node.Leaf, Key: []byte{9}}, + {Key: []byte{9}}, }), }, }, @@ -504,12 +478,10 @@ func Test_Trie_Hash(t *testing.T) { 0xbe, 0x27, 0xab, 0x13, 0xcb, 0xf0, 0xfd, 0xd7}, expectedTrie: Trie{ root: &Node{ - Type: node.Branch, Key: []byte{1, 2, 3}, Value: []byte("branch"), Children: padRightChildren([]*Node{ { - Type: node.Leaf, Key: []byte{9}, Encoding: []byte{0x41, 0x09, 0x00}, }, @@ -570,18 +542,15 @@ func Test_Trie_Entries(t *testing.T) { t.Parallel() root := &Node{ - Type: node.Branch, Key: []byte{0xa}, Value: []byte("root"), Children: padRightChildren([]*Node{ - { - Type: node.Leaf, // index 0 + { // index 0 Key: []byte{2, 0xb}, Value: []byte("leaf"), }, nil, - { - Type: node.Leaf, // index 2 + { // index 2 Key: []byte{0xb}, Value: []byte("leaf"), }, @@ -605,38 +574,32 @@ func Test_Trie_Entries(t *testing.T) { t.Parallel() root := &Node{ - Type: node.Branch, Key: []byte{0xa, 0xb}, Value: []byte("root"), Children: padRightChildren([]*Node{ nil, nil, nil, - { - Type: node.Branch, // branch with value at child index 3 + { // branch with value at child index 3 Key: []byte{0xb}, Value: []byte("branch 1"), Children: padRightChildren([]*Node{ nil, nil, nil, - { - Type: node.Leaf, // leaf at child index 3 + { // leaf at child index 3 Key: []byte{0xc}, Value: []byte("bottom leaf"), }, }), }, nil, nil, nil, - { - Type: node.Leaf, // leaf at child index 7 + { // leaf at child index 7 Key: []byte{0xd}, Value: []byte("top leaf"), }, nil, - { - Type: node.Branch, // branch without value at child index 9 + { // branch without value at child index 9 Key: []byte{0xe}, Value: []byte("branch 2"), Children: padRightChildren([]*Node{ - { - Type: node.Leaf, // leaf at child index 0 + { // leaf at child index 0 Key: []byte{0xf}, Value: []byte("bottom leaf 2"), }, nil, nil, @@ -702,8 +665,7 @@ func Test_Trie_NextKey(t *testing.T) { "nil key returns root leaf": { trie: Trie{ root: &Node{ - Type: node.Leaf, - Key: []byte{2}, + Key: []byte{2}, }, }, nextKey: []byte{2}, @@ -711,8 +673,7 @@ func Test_Trie_NextKey(t *testing.T) { "key smaller than root leaf full key": { trie: Trie{ root: &Node{ - Type: node.Leaf, - Key: []byte{2}, + Key: []byte{2}, }, }, key: []byte{0x10}, // 10 => [1, 0] in nibbles @@ -750,8 +711,7 @@ func Test_nextKey(t *testing.T) { "nil key returns root leaf": { trie: Trie{ root: &Node{ - Type: node.Leaf, - Key: []byte{2}, + Key: []byte{2}, }, }, nextKey: []byte{2}, @@ -759,8 +719,7 @@ func Test_nextKey(t *testing.T) { "key smaller than root leaf full key": { trie: Trie{ root: &Node{ - Type: node.Leaf, - Key: []byte{2}, + Key: []byte{2}, }, }, key: []byte{1}, @@ -769,8 +728,7 @@ func Test_nextKey(t *testing.T) { "key equal to root leaf full key": { trie: Trie{ root: &Node{ - Type: node.Leaf, - Key: []byte{2}, + Key: []byte{2}, }, }, key: []byte{2}, @@ -778,8 +736,7 @@ func Test_nextKey(t *testing.T) { "key greater than root leaf full key": { trie: Trie{ root: &Node{ - Type: node.Leaf, - Key: []byte{2}, + Key: []byte{2}, }, }, key: []byte{3}, @@ -787,13 +744,11 @@ func Test_nextKey(t *testing.T) { "key smaller than root branch full key": { trie: Trie{ root: &Node{ - Type: node.Branch, Key: []byte{2}, Value: []byte("branch"), Children: padRightChildren([]*Node{ { - Type: node.Leaf, - Key: []byte{1}, + Key: []byte{1}, }, }), }, @@ -804,13 +759,11 @@ func Test_nextKey(t *testing.T) { "key equal to root branch full key": { trie: Trie{ root: &Node{ - Type: node.Branch, Key: []byte{2}, Value: []byte("branch"), Children: padRightChildren([]*Node{ { - Type: node.Leaf, - Key: []byte{1}, + Key: []byte{1}, }, }), }, @@ -820,13 +773,11 @@ func Test_nextKey(t *testing.T) { "key smaller than leaf full key": { trie: Trie{ root: &Node{ - Type: node.Branch, Key: []byte{1}, Value: []byte("branch"), Children: padRightChildren([]*Node{ nil, nil, { - Type: node.Leaf, // full key [1, 2, 3] Key: []byte{3}, }, @@ -839,13 +790,11 @@ func Test_nextKey(t *testing.T) { "key equal to leaf full key": { trie: Trie{ root: &Node{ - Type: node.Branch, Key: []byte{1}, Value: []byte("branch"), Children: padRightChildren([]*Node{ nil, nil, { - Type: node.Leaf, // full key [1, 2, 3] Key: []byte{3}, }, @@ -857,13 +806,11 @@ func Test_nextKey(t *testing.T) { "key greater than leaf full key": { trie: Trie{ root: &Node{ - Type: node.Branch, Key: []byte{1}, Value: []byte("branch"), Children: padRightChildren([]*Node{ nil, nil, { - Type: node.Leaf, // full key [1, 2, 3] Key: []byte{3}, }, @@ -875,20 +822,17 @@ func Test_nextKey(t *testing.T) { "next key branch with value": { trie: Trie{ root: &Node{ - Type: node.Branch, Key: []byte{1}, Value: []byte("top branch"), Children: padRightChildren([]*Node{ nil, nil, { - Type: node.Branch, // full key [1, 2, 3] Key: []byte{3}, Value: []byte("branch 1"), Children: padRightChildren([]*Node{ nil, nil, nil, nil, { - Type: node.Leaf, // full key [1, 2, 3, 4, 5] Key: []byte{0x5}, Value: []byte("bottom leaf"), @@ -904,18 +848,15 @@ func Test_nextKey(t *testing.T) { "next key go through branch without value": { trie: Trie{ root: &Node{ - Type: node.Branch, - Key: []byte{1}, + Key: []byte{1}, Children: padRightChildren([]*Node{ nil, nil, { - Type: node.Branch, // full key [1, 2, 3] Key: []byte{3}, Children: padRightChildren([]*Node{ nil, nil, nil, nil, { - Type: node.Leaf, // full key [1, 2, 3, 4, 5] Key: []byte{0x5}, Value: []byte("bottom leaf"), @@ -931,19 +872,16 @@ func Test_nextKey(t *testing.T) { "next key leaf from bottom branch": { trie: Trie{ root: &Node{ - Type: node.Branch, - Key: []byte{1}, + Key: []byte{1}, Children: padRightChildren([]*Node{ nil, nil, { - Type: node.Branch, // full key [1, 2, 3] Key: []byte{3}, Value: []byte("bottom branch"), Children: padRightChildren([]*Node{ nil, nil, nil, nil, { - Type: node.Leaf, // full key [1, 2, 3, 4, 5] Key: []byte{0x5}, Value: []byte("bottom leaf"), @@ -959,19 +897,16 @@ func Test_nextKey(t *testing.T) { "next key greater than branch": { trie: Trie{ root: &Node{ - Type: node.Branch, - Key: []byte{1}, + Key: []byte{1}, Children: padRightChildren([]*Node{ nil, nil, { - Type: node.Branch, // full key [1, 2, 3] Key: []byte{3}, Value: []byte("bottom branch"), Children: padRightChildren([]*Node{ nil, nil, nil, nil, { - Type: node.Leaf, // full key [1, 2, 3, 4, 5] Key: []byte{0x5}, Value: []byte("bottom leaf"), @@ -987,11 +922,10 @@ func Test_nextKey(t *testing.T) { "key smaller length and greater than root branch full key": { trie: Trie{ root: &Node{ - Type: node.Branch, Key: []byte{2, 0}, Value: []byte("branch"), Children: padRightChildren([]*Node{ - {Type: node.Leaf, Key: []byte{1}}, + {Key: []byte{1}}, }), }, }, @@ -1000,7 +934,6 @@ func Test_nextKey(t *testing.T) { "key smaller length and greater than root leaf full key": { trie: Trie{ root: &Node{ - Type: node.Leaf, Key: []byte{2, 0}, Value: []byte("leaf"), }, @@ -1037,7 +970,6 @@ func Test_Trie_Put(t *testing.T) { trie: Trie{ generation: 1, root: &Node{ - Type: node.Leaf, Key: []byte{1, 2, 0, 5}, Value: []byte{1}, }, @@ -1047,20 +979,17 @@ func Test_Trie_Put(t *testing.T) { expectedTrie: Trie{ generation: 1, root: &Node{ - Type: node.Branch, Key: []byte{1, 2}, Generation: 1, Dirty: true, Children: padRightChildren([]*Node{ { - Type: node.Leaf, Key: []byte{5}, Value: []byte{1}, Generation: 1, Dirty: true, }, { - Type: node.Leaf, Key: []byte{6}, Value: []byte{2}, Generation: 1, @@ -1101,7 +1030,6 @@ func Test_Trie_put(t *testing.T) { expectedTrie: Trie{ generation: 1, root: &Node{ - Type: node.Leaf, Generation: 1, Dirty: true, }, @@ -1115,7 +1043,6 @@ func Test_Trie_put(t *testing.T) { expectedTrie: Trie{ generation: 1, root: &Node{ - Type: node.Leaf, Value: []byte{3, 4}, Generation: 1, Dirty: true, @@ -1131,7 +1058,6 @@ func Test_Trie_put(t *testing.T) { expectedTrie: Trie{ generation: 1, root: &Node{ - Type: node.Leaf, Key: []byte{1, 2}, Value: []byte{3, 4}, Generation: 1, @@ -1143,7 +1069,6 @@ func Test_Trie_put(t *testing.T) { trie: Trie{ generation: 1, root: &Node{ - Type: node.Leaf, Key: []byte{1, 0, 5}, Value: []byte{1}, }, @@ -1153,20 +1078,17 @@ func Test_Trie_put(t *testing.T) { expectedTrie: Trie{ generation: 1, root: &Node{ - Type: node.Branch, Key: []byte{1}, Generation: 1, Dirty: true, Children: padRightChildren([]*Node{ { - Type: node.Leaf, Key: []byte{5}, Value: []byte{1}, Generation: 1, Dirty: true, }, { - Type: node.Leaf, Key: []byte{6}, Value: []byte{2}, Generation: 1, @@ -1208,7 +1130,6 @@ func Test_Trie_insert(t *testing.T) { key: []byte{1}, value: []byte("leaf"), newNode: &Node{ - Type: node.Leaf, Key: []byte{1}, Value: []byte("leaf"), Generation: 1, @@ -1220,32 +1141,28 @@ func Test_Trie_insert(t *testing.T) { generation: 1, }, parent: &Node{ - Type: node.Branch, Key: []byte{1}, Value: []byte("branch"), Children: padRightChildren([]*Node{ nil, - {Type: node.Leaf, Key: []byte{2}}, + {Key: []byte{2}}, }), }, key: []byte{1, 0}, value: []byte("leaf"), newNode: &Node{ - Type: node.Branch, Key: []byte{1}, Value: []byte("branch"), Generation: 1, Dirty: true, Children: padRightChildren([]*Node{ { - Type: node.Leaf, Key: []byte{}, Value: []byte("leaf"), Generation: 1, Dirty: true, }, - { - Type: node.Leaf, Key: []byte{2}}, + {Key: []byte{2}}, }), }, }, @@ -1254,14 +1171,12 @@ func Test_Trie_insert(t *testing.T) { generation: 1, }, parent: &Node{ - Type: node.Leaf, Key: []byte{1}, Value: []byte("original leaf"), }, key: []byte{1}, value: []byte("new leaf"), newNode: &Node{ - Type: node.Leaf, Key: []byte{1}, Value: []byte("new leaf"), Generation: 1, @@ -1273,14 +1188,12 @@ func Test_Trie_insert(t *testing.T) { generation: 1, }, parent: &Node{ - Type: node.Leaf, Key: []byte{1}, Value: []byte("same"), }, key: []byte{1}, value: []byte("same"), newNode: &Node{ - Type: node.Leaf, Key: []byte{1}, Value: []byte("same"), }, @@ -1290,21 +1203,18 @@ func Test_Trie_insert(t *testing.T) { generation: 1, }, parent: &Node{ - Type: node.Leaf, Key: []byte{1}, Value: []byte("original leaf"), }, key: []byte{1, 0}, value: []byte("leaf"), newNode: &Node{ - Type: node.Branch, Key: []byte{1}, Value: []byte("original leaf"), Dirty: true, Generation: 1, Children: padRightChildren([]*Node{ { - Type: node.Leaf, Key: []byte{}, Value: []byte("leaf"), Generation: 1, @@ -1318,28 +1228,24 @@ func Test_Trie_insert(t *testing.T) { generation: 1, }, parent: &Node{ - Type: node.Leaf, Key: []byte{1, 2}, Value: []byte("original leaf"), }, key: []byte{2, 3}, value: []byte("leaf"), newNode: &Node{ - Type: node.Branch, Key: []byte{}, Dirty: true, Generation: 1, Children: padRightChildren([]*Node{ nil, { - Type: node.Leaf, Key: []byte{2}, Value: []byte("original leaf"), Dirty: true, Generation: 1, }, { - Type: node.Leaf, Key: []byte{3}, Value: []byte("leaf"), Generation: 1, @@ -1353,13 +1259,11 @@ func Test_Trie_insert(t *testing.T) { generation: 1, }, parent: &Node{ - Type: node.Leaf, - Key: []byte{1}, + Key: []byte{1}, }, key: []byte{1}, value: []byte("leaf"), newNode: &Node{ - Type: node.Leaf, Key: []byte{1}, Value: []byte("leaf"), Dirty: true, @@ -1371,13 +1275,11 @@ func Test_Trie_insert(t *testing.T) { generation: 1, }, parent: &Node{ - Type: node.Leaf, - Key: []byte{1, 2}, + Key: []byte{1, 2}, }, key: []byte{1}, value: []byte("leaf"), newNode: &Node{ - Type: node.Branch, Key: []byte{1}, Value: []byte("leaf"), Dirty: true, @@ -1385,7 +1287,6 @@ func Test_Trie_insert(t *testing.T) { Children: padRightChildren([]*Node{ nil, nil, { - Type: node.Leaf, Key: []byte{}, Dirty: true, Generation: 1, @@ -1422,67 +1323,60 @@ func Test_Trie_insertInBranch(t *testing.T) { }{ "update with branch": { parent: &Node{ - Type: node.Branch, Key: []byte{2}, Value: []byte("old"), Children: padRightChildren([]*Node{ - {Type: node.Leaf, Key: []byte{1}}, + {Key: []byte{1}}, }), }, key: []byte{2}, value: []byte("new"), newNode: &Node{ - Type: node.Branch, Key: []byte{2}, Value: []byte("new"), Dirty: true, Children: padRightChildren([]*Node{ - {Type: node.Leaf, Key: []byte{1}}, + {Key: []byte{1}}, }), }, }, "update with leaf": { parent: &Node{ - Type: node.Branch, Key: []byte{2}, Value: []byte("old"), Children: padRightChildren([]*Node{ - {Type: node.Leaf, Key: []byte{1}}, + {Key: []byte{1}}, }), }, key: []byte{2}, value: []byte("new"), newNode: &Node{ - Type: node.Branch, Key: []byte{2}, Value: []byte("new"), Dirty: true, Children: padRightChildren([]*Node{ - {Type: node.Leaf, Key: []byte{1}}, + {Key: []byte{1}}, }), }, }, "add leaf as direct child": { parent: &Node{ - Type: node.Branch, Key: []byte{2}, Value: []byte{5}, Children: padRightChildren([]*Node{ - {Type: node.Leaf, Key: []byte{1}}, + {Key: []byte{1}}, }), }, key: []byte{2, 3, 4, 5}, value: []byte{6}, newNode: &Node{ - Type: node.Branch, Key: []byte{2}, Value: []byte{5}, Dirty: true, Children: padRightChildren([]*Node{ - {Type: node.Leaf, Key: []byte{1}}, + {Key: []byte{1}}, nil, nil, { - Type: node.Leaf, Key: []byte{4, 5}, Value: []byte{6}, Dirty: true, @@ -1492,16 +1386,14 @@ func Test_Trie_insertInBranch(t *testing.T) { }, "add leaf as nested child": { parent: &Node{ - Type: node.Branch, Key: []byte{2}, Value: []byte{5}, Children: padRightChildren([]*Node{ nil, nil, nil, { - Type: node.Branch, - Key: []byte{4}, + Key: []byte{4}, Children: padRightChildren([]*Node{ - {Type: node.Leaf, Key: []byte{1}}, + {Key: []byte{1}}, }), }, }), @@ -1509,21 +1401,18 @@ func Test_Trie_insertInBranch(t *testing.T) { key: []byte{2, 3, 4, 5, 6}, value: []byte{6}, newNode: &Node{ - Type: node.Branch, Key: []byte{2}, Value: []byte{5}, Dirty: true, Children: padRightChildren([]*Node{ nil, nil, nil, { - Type: node.Branch, Key: []byte{4}, Dirty: true, Children: padRightChildren([]*Node{ - {Type: node.Leaf, Key: []byte{1}}, + {Key: []byte{1}}, nil, nil, nil, nil, { - Type: node.Leaf, Key: []byte{6}, Value: []byte{6}, Dirty: true, @@ -1535,32 +1424,28 @@ func Test_Trie_insertInBranch(t *testing.T) { }, "split branch for longer key": { parent: &Node{ - Type: node.Branch, Key: []byte{2, 3}, Value: []byte{5}, Children: padRightChildren([]*Node{ - {Type: node.Leaf, Key: []byte{1}}, + {Key: []byte{1}}, }), }, key: []byte{2, 4, 5, 6}, value: []byte{6}, newNode: &Node{ - Type: node.Branch, Key: []byte{2}, Dirty: true, Children: padRightChildren([]*Node{ nil, nil, nil, { - Type: node.Branch, Key: []byte{}, Value: []byte{5}, Dirty: true, Children: padRightChildren([]*Node{ - {Type: node.Leaf, Key: []byte{1}}, + {Key: []byte{1}}, }), }, { - Type: node.Leaf, Key: []byte{5, 6}, Value: []byte{6}, Dirty: true, @@ -1570,32 +1455,28 @@ func Test_Trie_insertInBranch(t *testing.T) { }, "split root branch": { parent: &Node{ - Type: node.Branch, Key: []byte{2, 3}, Value: []byte{5}, Children: padRightChildren([]*Node{ - {Type: node.Leaf, Key: []byte{1}}, + {Key: []byte{1}}, }), }, key: []byte{3}, value: []byte{6}, newNode: &Node{ - Type: node.Branch, Key: []byte{}, Dirty: true, Children: padRightChildren([]*Node{ nil, nil, { - Type: node.Branch, Key: []byte{3}, Value: []byte{5}, Dirty: true, Children: padRightChildren([]*Node{ - {Type: node.Leaf, Key: []byte{1}}, + {Key: []byte{1}}, }), }, { - Type: node.Leaf, Key: []byte{}, Value: []byte{6}, Dirty: true, @@ -1605,29 +1486,26 @@ func Test_Trie_insertInBranch(t *testing.T) { }, "update with leaf at empty key": { parent: &Node{ - Type: node.Branch, Key: []byte{2}, Value: []byte{5}, Children: padRightChildren([]*Node{ - {Type: node.Leaf, Key: []byte{1}}, + {Key: []byte{1}}, }), }, key: []byte{}, value: []byte{6}, newNode: &Node{ - Type: node.Branch, Key: []byte{}, Value: []byte{6}, Dirty: true, Children: padRightChildren([]*Node{ nil, nil, { - Type: node.Branch, Key: []byte{}, Value: []byte{5}, Dirty: true, Children: padRightChildren([]*Node{ - {Type: node.Leaf, Key: []byte{1}}, + {Key: []byte{1}}, }), }, }), @@ -1686,20 +1564,17 @@ func Test_Trie_LoadFromMap(t *testing.T) { }, expectedTrie: Trie{ root: &Node{ - Type: node.Branch, Key: []byte{00, 01}, Value: []byte{6}, Dirty: true, Children: padRightChildren([]*Node{ nil, nil, { - Type: node.Leaf, Key: []byte{0}, Value: []byte{7}, Dirty: true, }, { - Type: node.Leaf, Key: []byte{0}, Value: []byte{8}, Dirty: true, @@ -1711,18 +1586,15 @@ func Test_Trie_LoadFromMap(t *testing.T) { "override trie": { trie: Trie{ root: &Node{ - Type: node.Branch, Key: []byte{00, 01}, Value: []byte{106}, Dirty: true, Children: padRightChildren([]*Node{ { - Type: node.Leaf, Value: []byte{9}, }, nil, { - Type: node.Leaf, Key: []byte{0}, Value: []byte{107}, Dirty: true, @@ -1737,24 +1609,20 @@ func Test_Trie_LoadFromMap(t *testing.T) { }, expectedTrie: Trie{ root: &Node{ - Type: node.Branch, Key: []byte{00, 01}, Value: []byte{6}, Dirty: true, Children: padRightChildren([]*Node{ { - Type: node.Leaf, Value: []byte{9}, }, nil, { - Type: node.Leaf, Key: []byte{0}, Value: []byte{7}, Dirty: true, }, { - Type: node.Leaf, Key: []byte{0}, Value: []byte{8}, Dirty: true, @@ -1793,26 +1661,21 @@ func Test_Trie_GetKeysWithPrefix(t *testing.T) { "some trie": { trie: Trie{ root: &Node{ - Type: node.Branch, - Key: []byte{0, 1}, + Key: []byte{0, 1}, Children: padRightChildren([]*Node{ - { - Type: node.Branch, // full key 0, 1, 0, 3 - Key: []byte{3}, + { // full key 0, 1, 0, 3 + Key: []byte{3}, Children: padRightChildren([]*Node{ - { - Type: node.Leaf, // full key 0, 1, 0, 0, 4 - Key: []byte{4}, + { // full key 0, 1, 0, 0, 4 + Key: []byte{4}, }, - { - Type: node.Leaf, // full key 0, 1, 0, 1, 5 - Key: []byte{5}, + { // full key 0, 1, 0, 1, 5 + Key: []byte{5}, }, }), }, - { - Type: node.Leaf, // full key 0, 1, 1, 9 - Key: []byte{9}, + { // full key 0, 1, 1, 9 + Key: []byte{9}, }, }), }, @@ -1854,11 +1717,10 @@ func Test_getKeysWithPrefix(t *testing.T) { }, "common prefix for parent branch and search key": { parent: &Node{ - Type: node.Branch, - Key: []byte{1, 2, 3}, + Key: []byte{1, 2, 3}, Children: padRightChildren([]*Node{ - {Type: node.Leaf, Key: []byte{4}}, - {Type: node.Leaf, Key: []byte{5}}, + {Key: []byte{4}}, + {Key: []byte{5}}, }), }, prefix: []byte{9, 8, 7}, @@ -1870,11 +1732,10 @@ func Test_getKeysWithPrefix(t *testing.T) { }, "parent branch and empty key": { parent: &Node{ - Type: node.Branch, - Key: []byte{1, 2, 3}, + Key: []byte{1, 2, 3}, Children: padRightChildren([]*Node{ - {Type: node.Leaf, Key: []byte{4}}, - {Type: node.Leaf, Key: []byte{5}}, + {Key: []byte{4}}, + {Key: []byte{5}}, }), }, prefix: []byte{9, 8, 7}, @@ -1886,11 +1747,10 @@ func Test_getKeysWithPrefix(t *testing.T) { }, "search key smaller than branch key with no full common prefix": { parent: &Node{ - Type: node.Branch, - Key: []byte{1, 2, 3}, + Key: []byte{1, 2, 3}, Children: padRightChildren([]*Node{ - {Type: node.Leaf, Key: []byte{4}}, - {Type: node.Leaf, Key: []byte{5}}, + {Key: []byte{4}}, + {Key: []byte{5}}, }), }, key: []byte{1, 3}, @@ -1899,11 +1759,10 @@ func Test_getKeysWithPrefix(t *testing.T) { }, "common prefix smaller tan search key": { parent: &Node{ - Type: node.Branch, - Key: []byte{1, 2}, + Key: []byte{1, 2}, Children: padRightChildren([]*Node{ - {Type: node.Leaf, Key: []byte{4}}, - {Type: node.Leaf, Key: []byte{5}}, + {Key: []byte{4}}, + {Key: []byte{5}}, }), }, key: []byte{1, 2, 3}, @@ -1912,11 +1771,10 @@ func Test_getKeysWithPrefix(t *testing.T) { }, "recursive call": { parent: &Node{ - Type: node.Branch, - Key: []byte{1, 2, 3}, + Key: []byte{1, 2, 3}, Children: padRightChildren([]*Node{ - {Type: node.Leaf, Key: []byte{4}}, - {Type: node.Leaf, Key: []byte{5}}, + {Key: []byte{4}}, + {Key: []byte{5}}, }), }, prefix: []byte{9, 8, 7}, @@ -1927,8 +1785,7 @@ func Test_getKeysWithPrefix(t *testing.T) { }, "parent leaf with search key equal to common prefix": { parent: &Node{ - Type: node.Leaf, - Key: []byte{1, 2, 3}, + Key: []byte{1, 2, 3}, }, prefix: []byte{9, 8, 7}, key: []byte{1, 2, 3}, @@ -1938,8 +1795,7 @@ func Test_getKeysWithPrefix(t *testing.T) { }, "parent leaf with empty search key": { parent: &Node{ - Type: node.Leaf, - Key: []byte{1, 2, 3}, + Key: []byte{1, 2, 3}, }, prefix: []byte{9, 8, 7}, key: []byte{}, @@ -1949,8 +1805,7 @@ func Test_getKeysWithPrefix(t *testing.T) { }, "parent leaf with too deep search key": { parent: &Node{ - Type: node.Leaf, - Key: []byte{1, 2, 3}, + Key: []byte{1, 2, 3}, }, prefix: []byte{9, 8, 7}, key: []byte{1, 2, 3, 4}, @@ -1959,8 +1814,7 @@ func Test_getKeysWithPrefix(t *testing.T) { }, "parent leaf with shorter matching search key": { parent: &Node{ - Type: node.Leaf, - Key: []byte{1, 2, 3}, + Key: []byte{1, 2, 3}, }, prefix: []byte{9, 8, 7}, key: []byte{1, 2}, @@ -1970,8 +1824,7 @@ func Test_getKeysWithPrefix(t *testing.T) { }, "parent leaf with not matching search key": { parent: &Node{ - Type: node.Leaf, - Key: []byte{1, 2, 3}, + Key: []byte{1, 2, 3}, }, prefix: []byte{9, 8, 7}, key: []byte{1, 3, 3}, @@ -2008,8 +1861,7 @@ func Test_addAllKeys(t *testing.T) { }, "leaf parent": { parent: &Node{ - Type: node.Leaf, - Key: []byte{1, 2, 3}, + Key: []byte{1, 2, 3}, }, prefix: []byte{9, 8, 7}, keys: [][]byte{{1}, {2}}, @@ -2018,11 +1870,10 @@ func Test_addAllKeys(t *testing.T) { }, "parent branch without value": { parent: &Node{ - Type: node.Branch, - Key: []byte{1, 2, 3}, + Key: []byte{1, 2, 3}, Children: padRightChildren([]*Node{ - {Type: node.Leaf, Key: []byte{4}}, - {Type: node.Leaf, Key: []byte{5}}, + {Key: []byte{4}}, + {Key: []byte{5}}, }), }, prefix: []byte{9, 8, 7}, @@ -2033,12 +1884,11 @@ func Test_addAllKeys(t *testing.T) { }, "parent branch with empty value": { parent: &Node{ - Type: node.Branch, Key: []byte{1, 2, 3}, Value: []byte{}, Children: padRightChildren([]*Node{ - {Type: node.Leaf, Key: []byte{4}}, - {Type: node.Leaf, Key: []byte{5}}, + {Key: []byte{4}}, + {Key: []byte{5}}, }), }, prefix: []byte{9, 8, 7}, @@ -2074,20 +1924,17 @@ func Test_Trie_Get(t *testing.T) { "some trie": { trie: Trie{ root: &Node{ - Type: node.Branch, Key: []byte{0, 1}, Value: []byte{1, 3}, Children: padRightChildren([]*Node{ - { - Type: node.Branch, // full key 0, 1, 0, 3 + { // full key 0, 1, 0, 3 Key: []byte{3}, Value: []byte{1, 2}, Children: padRightChildren([]*Node{ - {Type: node.Leaf, Key: []byte{1}}, + {Key: []byte{1}}, }), }, - { - Type: node.Leaf, // full key 0, 1, 1, 9 + { // full key 0, 1, 1, 9 Key: []byte{9}, Value: []byte{1, 2, 3, 4, 5}, }, @@ -2124,7 +1971,6 @@ func Test_retrieve(t *testing.T) { }, "leaf key match": { parent: &Node{ - Type: node.Leaf, Key: []byte{1}, Value: []byte{2}, }, @@ -2133,7 +1979,6 @@ func Test_retrieve(t *testing.T) { }, "leaf key mismatch": { parent: &Node{ - Type: node.Leaf, Key: []byte{1, 2}, Value: []byte{2}, }, @@ -2141,11 +1986,10 @@ func Test_retrieve(t *testing.T) { }, "branch key match": { parent: &Node{ - Type: node.Branch, Key: []byte{1}, Value: []byte{2}, Children: padRightChildren([]*Node{ - {Type: node.Leaf, Key: []byte{1}}, + {Key: []byte{1}}, }), }, key: []byte{1}, @@ -2153,41 +1997,36 @@ func Test_retrieve(t *testing.T) { }, "branch key with empty search key": { parent: &Node{ - Type: node.Branch, Key: []byte{1}, Value: []byte{2}, Children: padRightChildren([]*Node{ - {Type: node.Leaf, Key: []byte{1}}, + {Key: []byte{1}}, }), }, value: []byte{2}, }, "branch key mismatch with shorter search key": { parent: &Node{ - Type: node.Branch, Key: []byte{1, 2}, Value: []byte{2}, Children: padRightChildren([]*Node{ - {Type: node.Leaf, Key: []byte{1}}, + {Key: []byte{1}}, }), }, key: []byte{1}, }, "bottom leaf in branch": { parent: &Node{ - Type: node.Branch, Key: []byte{1}, Value: []byte{1}, Children: padRightChildren([]*Node{ nil, nil, - { - Type: node.Branch, // full key 1, 2, 3 + { // full key 1, 2, 3 Key: []byte{3}, Value: []byte{2}, Children: padRightChildren([]*Node{ nil, nil, nil, nil, - { - Type: node.Leaf, // full key 1, 2, 3, 4, 5 + { // full key 1, 2, 3, 4, 5 Key: []byte{5}, Value: []byte{3}, }, @@ -2235,14 +2074,12 @@ func Test_Trie_ClearPrefixLimit(t *testing.T) { "clear prefix limit": { trie: Trie{ root: &Node{ - Type: node.Branch, Key: []byte{1, 2}, Value: []byte{1}, Children: padRightChildren([]*Node{ nil, nil, nil, { - Type: node.Leaf, - Key: []byte{4}, + Key: []byte{4}, }, }), }, @@ -2291,8 +2128,7 @@ func Test_Trie_clearPrefixLimit(t *testing.T) { }, "leaf parent with common prefix": { parent: &Node{ - Type: node.Leaf, - Key: []byte{1, 2}, + Key: []byte{1, 2}, }, prefix: []byte{1}, limit: 1, @@ -2301,8 +2137,7 @@ func Test_Trie_clearPrefixLimit(t *testing.T) { }, "leaf parent with key equal prefix": { parent: &Node{ - Type: node.Leaf, - Key: []byte{1}, + Key: []byte{1}, }, prefix: []byte{1}, limit: 1, @@ -2314,14 +2149,12 @@ func Test_Trie_clearPrefixLimit(t *testing.T) { generation: 1, }, parent: &Node{ - Type: node.Leaf, - Key: []byte{1, 2}, + Key: []byte{1, 2}, }, prefix: []byte{1, 3}, limit: 1, newParent: &Node{ - Type: node.Leaf, - Key: []byte{1, 2}, + Key: []byte{1, 2}, }, allDeleted: true, }, @@ -2330,24 +2163,21 @@ func Test_Trie_clearPrefixLimit(t *testing.T) { generation: 1, }, parent: &Node{ - Type: node.Leaf, - Key: []byte{1}, + Key: []byte{1}, }, prefix: []byte{1, 2}, limit: 1, newParent: &Node{ - Type: node.Leaf, - Key: []byte{1}, + Key: []byte{1}, }, allDeleted: true, }, "branch without value parent with common prefix": { parent: &Node{ - Type: node.Branch, - Key: []byte{1, 2}, + Key: []byte{1, 2}, Children: padRightChildren([]*Node{ - {Type: node.Leaf, Key: []byte{1}}, - {Type: node.Leaf, Key: []byte{2}}, + {Key: []byte{1}}, + {Key: []byte{2}}, }), }, prefix: []byte{1}, @@ -2357,11 +2187,10 @@ func Test_Trie_clearPrefixLimit(t *testing.T) { }, "branch without value with key equal prefix": { parent: &Node{ - Type: node.Branch, - Key: []byte{1, 2}, + Key: []byte{1, 2}, Children: padRightChildren([]*Node{ - {Type: node.Leaf, Key: []byte{1}}, - {Type: node.Leaf, Key: []byte{2}}, + {Key: []byte{1}}, + {Key: []byte{2}}, }), }, prefix: []byte{1, 2}, @@ -2374,21 +2203,19 @@ func Test_Trie_clearPrefixLimit(t *testing.T) { generation: 1, }, parent: &Node{ - Type: node.Branch, - Key: []byte{1, 2}, + Key: []byte{1, 2}, Children: padRightChildren([]*Node{ - {Type: node.Leaf, Key: []byte{1}}, - {Type: node.Leaf, Key: []byte{2}}, + {Key: []byte{1}}, + {Key: []byte{2}}, }), }, prefix: []byte{1, 3}, limit: 1, newParent: &Node{ - Type: node.Branch, - Key: []byte{1, 2}, + Key: []byte{1, 2}, Children: padRightChildren([]*Node{ - {Type: node.Leaf, Key: []byte{1}}, - {Type: node.Leaf, Key: []byte{2}}, + {Key: []byte{1}}, + {Key: []byte{2}}, }), }, allDeleted: true, @@ -2398,21 +2225,19 @@ func Test_Trie_clearPrefixLimit(t *testing.T) { generation: 1, }, parent: &Node{ - Type: node.Branch, - Key: []byte{1}, + Key: []byte{1}, Children: padRightChildren([]*Node{ - {Type: node.Leaf, Key: []byte{1}}, - {Type: node.Leaf, Key: []byte{2}}, + {Key: []byte{1}}, + {Key: []byte{2}}, }), }, prefix: []byte{1, 2, 3}, limit: 1, newParent: &Node{ - Type: node.Branch, - Key: []byte{1}, + Key: []byte{1}, Children: padRightChildren([]*Node{ - {Type: node.Leaf, Key: []byte{1}}, - {Type: node.Leaf, Key: []byte{2}}, + {Key: []byte{1}}, + {Key: []byte{2}}, }), }, allDeleted: true, @@ -2422,32 +2247,29 @@ func Test_Trie_clearPrefixLimit(t *testing.T) { generation: 1, }, parent: &Node{ - Type: node.Branch, - Key: []byte{1}, + Key: []byte{1}, Children: padRightChildren([]*Node{ - {Type: node.Leaf, Key: []byte{1}}, - {Type: node.Leaf, Key: []byte{2}}, + {Key: []byte{1}}, + {Key: []byte{2}}, }), }, prefix: []byte{1, 2}, limit: 1, newParent: &Node{ - Type: node.Branch, - Key: []byte{1}, + Key: []byte{1}, Children: padRightChildren([]*Node{ - {Type: node.Leaf, Key: []byte{1}}, - {Type: node.Leaf, Key: []byte{2}}, + {Key: []byte{1}}, + {Key: []byte{2}}, }), }, allDeleted: true, }, "branch with value with common prefix": { parent: &Node{ - Type: node.Branch, Key: []byte{1, 2}, Value: []byte{1}, Children: padRightChildren([]*Node{ - {Type: node.Leaf, Key: []byte{1}}, + {Key: []byte{1}}, }), }, prefix: []byte{1}, @@ -2457,11 +2279,10 @@ func Test_Trie_clearPrefixLimit(t *testing.T) { }, "branch with value with key equal prefix": { parent: &Node{ - Type: node.Branch, Key: []byte{1, 2}, Value: []byte{1}, Children: padRightChildren([]*Node{ - {Type: node.Leaf, Key: []byte{1}}, + {Key: []byte{1}}, }), }, prefix: []byte{1, 2}, @@ -2474,21 +2295,19 @@ func Test_Trie_clearPrefixLimit(t *testing.T) { generation: 1, }, parent: &Node{ - Type: node.Branch, Key: []byte{1, 2}, Value: []byte{1}, Children: padRightChildren([]*Node{ - {Type: node.Leaf, Key: []byte{1}}, + {Key: []byte{1}}, }), }, prefix: []byte{1, 3}, limit: 1, newParent: &Node{ - Type: node.Branch, Key: []byte{1, 2}, Value: []byte{1}, Children: padRightChildren([]*Node{ - {Type: node.Leaf, Key: []byte{1}}, + {Key: []byte{1}}, }), }, allDeleted: true, @@ -2498,21 +2317,19 @@ func Test_Trie_clearPrefixLimit(t *testing.T) { generation: 1, }, parent: &Node{ - Type: node.Branch, Key: []byte{1}, Value: []byte{1}, Children: padRightChildren([]*Node{ - {Type: node.Leaf, Key: []byte{1}}, + {Key: []byte{1}}, }), }, prefix: []byte{1, 2, 3}, limit: 1, newParent: &Node{ - Type: node.Branch, Key: []byte{1}, Value: []byte{1}, Children: padRightChildren([]*Node{ - {Type: node.Leaf, Key: []byte{1}}, + {Key: []byte{1}}, }), }, allDeleted: true, @@ -2522,21 +2339,19 @@ func Test_Trie_clearPrefixLimit(t *testing.T) { generation: 1, }, parent: &Node{ - Type: node.Branch, Key: []byte{1}, Value: []byte{1}, Children: padRightChildren([]*Node{ - {Type: node.Leaf, Key: []byte{1}}, + {Key: []byte{1}}, }), }, prefix: []byte{1, 2}, limit: 1, newParent: &Node{ - Type: node.Branch, Key: []byte{1}, Value: []byte{1}, Children: padRightChildren([]*Node{ - {Type: node.Leaf, Key: []byte{1}}, + {Key: []byte{1}}, }), }, allDeleted: true, @@ -2546,42 +2361,38 @@ func Test_Trie_clearPrefixLimit(t *testing.T) { generation: 1, }, parent: &Node{ - Type: node.Branch, Key: []byte{1}, Value: []byte{1}, Children: padRightChildren([]*Node{ - {Type: node.Leaf, Key: []byte{3}}, - {Type: node.Leaf, Key: []byte{4}}, + {Key: []byte{3}}, + {Key: []byte{4}}, }), }, prefix: []byte{1}, limit: 1, newParent: &Node{ - Type: node.Branch, Key: []byte{1}, Value: []byte{1}, Dirty: true, Generation: 1, Children: padRightChildren([]*Node{ nil, - {Type: node.Leaf, Key: []byte{4}}, + {Key: []byte{4}}, }), }, valuesDeleted: 1, }, "delete only child of branch": { parent: &Node{ - Type: node.Branch, Key: []byte{1}, Value: []byte{1}, Children: padRightChildren([]*Node{ - {Type: node.Leaf, Key: []byte{3}}, + {Key: []byte{3}}, }), }, prefix: []byte{1, 0}, limit: 1, newParent: &Node{ - Type: node.Leaf, Key: []byte{1}, Value: []byte{1}, Dirty: true, @@ -2594,18 +2405,16 @@ func Test_Trie_clearPrefixLimit(t *testing.T) { generation: 1, }, parent: &Node{ - Type: node.Branch, Key: []byte{1}, Value: []byte{1}, Children: padRightChildren([]*Node{ - {Type: node.Leaf, Key: []byte{3}}, - {Type: node.Leaf, Key: []byte{4}}, + {Key: []byte{3}}, + {Key: []byte{4}}, }), }, prefix: []byte{1}, limit: 2, newParent: &Node{ - Type: node.Leaf, Key: []byte{1}, Value: []byte{1}, Dirty: true, @@ -2615,11 +2424,10 @@ func Test_Trie_clearPrefixLimit(t *testing.T) { }, "fully delete children of branch without value": { parent: &Node{ - Type: node.Branch, - Key: []byte{1}, + Key: []byte{1}, Children: padRightChildren([]*Node{ - {Type: node.Leaf, Key: []byte{3}}, - {Type: node.Leaf, Key: []byte{4}}, + {Key: []byte{3}}, + {Key: []byte{4}}, }), }, prefix: []byte{1}, @@ -2632,46 +2440,39 @@ func Test_Trie_clearPrefixLimit(t *testing.T) { generation: 1, }, parent: &Node{ - Type: node.Branch, Key: []byte{1}, Value: []byte{1}, Children: padRightChildren([]*Node{ - { - Type: node.Branch, // full key 1, 0, 3 + { // full key 1, 0, 3 Key: []byte{3}, Value: []byte{1}, Children: padRightChildren([]*Node{ - { - Type: node.Leaf, // full key 1, 0, 3, 0, 5 - Key: []byte{5}, + { // full key 1, 0, 3, 0, 5 + Key: []byte{5}, }, }), }, { - Type: node.Leaf, - Key: []byte{6}, + Key: []byte{6}, }, }), }, prefix: []byte{1, 0}, limit: 1, newParent: &Node{ - Type: node.Branch, Key: []byte{1}, Value: []byte{1}, Dirty: true, Generation: 1, Children: padRightChildren([]*Node{ - { - Type: node.Leaf, // full key 1, 0, 3 + { // full key 1, 0, 3 Key: []byte{3}, Value: []byte{1}, Dirty: true, Generation: 1, }, { - Type: node.Leaf, - Key: []byte{6}, + Key: []byte{6}, // Not modified so same generation as before }, }), @@ -2683,16 +2484,14 @@ func Test_Trie_clearPrefixLimit(t *testing.T) { generation: 1, }, parent: &Node{ - Type: node.Branch, Key: []byte{1}, Value: []byte{1}, Children: padRightChildren([]*Node{ - { - Type: node.Branch, // full key 1, 0, 2 + { // full key 1, 0, 2 Key: []byte{2}, Value: []byte{1}, Children: padRightChildren([]*Node{ - {Type: node.Leaf, Key: []byte{1}}, + {Key: []byte{1}}, }), }, }), @@ -2700,7 +2499,6 @@ func Test_Trie_clearPrefixLimit(t *testing.T) { prefix: []byte{1, 0, 2}, limit: 2, newParent: &Node{ - Type: node.Leaf, Key: []byte{1}, Value: []byte{1}, Dirty: true, @@ -2714,20 +2512,18 @@ func Test_Trie_clearPrefixLimit(t *testing.T) { generation: 1, }, parent: &Node{ - Type: node.Branch, Key: []byte{1}, Value: []byte{1}, Children: padRightChildren([]*Node{ - {Type: node.Leaf, Key: []byte{3}}, + {Key: []byte{3}}, }), }, prefix: []byte{1, 0}, newParent: &Node{ - Type: node.Branch, Key: []byte{1}, Value: []byte{1}, Children: padRightChildren([]*Node{ - {Type: node.Leaf, Key: []byte{3}}, + {Key: []byte{3}}, }), }, }, @@ -2768,29 +2564,25 @@ func Test_Trie_deleteNodesLimit(t *testing.T) { generation: 1, }, parent: &Node{ - Type: node.Leaf, - Key: []byte{1}, + Key: []byte{1}, }, newNode: &Node{ - Type: node.Leaf, - Key: []byte{1}, + Key: []byte{1}, }, }, "nil parent": { limit: 1, }, "delete leaf": { - parent: &Node{ - Type: node.Leaf}, + parent: &Node{}, limit: 2, valuesDeleted: 1, }, "delete branch without value": { parent: &Node{ - Type: node.Branch, Children: padRightChildren([]*Node{ - {Type: node.Leaf}, - {Type: node.Leaf}, + {}, + {}, }), }, limit: 3, @@ -2798,11 +2590,10 @@ func Test_Trie_deleteNodesLimit(t *testing.T) { }, "delete branch with value": { parent: &Node{ - Type: node.Branch, Key: []byte{3}, Value: []byte{1}, Children: padRightChildren([]*Node{ - {Type: node.Leaf}, + {}, }), }, limit: 3, @@ -2810,11 +2601,10 @@ func Test_Trie_deleteNodesLimit(t *testing.T) { }, "delete branch and all children": { parent: &Node{ - Type: node.Branch, - Key: []byte{3}, + Key: []byte{3}, Children: padRightChildren([]*Node{ - {Type: node.Leaf, Key: []byte{1}}, - {Type: node.Leaf, Key: []byte{2}}, + {Key: []byte{1}}, + {Key: []byte{2}}, }), }, limit: 10, @@ -2825,24 +2615,22 @@ func Test_Trie_deleteNodesLimit(t *testing.T) { generation: 1, }, parent: &Node{ - Type: node.Branch, Key: []byte{3}, Value: []byte{1, 2, 3}, Children: padRightChildren([]*Node{ - {Type: node.Leaf, Key: []byte{1}}, - {Type: node.Leaf, Key: []byte{2}}, + {Key: []byte{1}}, + {Key: []byte{2}}, }), }, limit: 1, newNode: &Node{ - Type: node.Branch, Key: []byte{3}, Value: []byte{1, 2, 3}, Dirty: true, Generation: 1, Children: padRightChildren([]*Node{ nil, - {Type: node.Leaf, Key: []byte{2}}, + {Key: []byte{2}}, }), }, valuesDeleted: 1, @@ -2852,17 +2640,15 @@ func Test_Trie_deleteNodesLimit(t *testing.T) { generation: 1, }, parent: &Node{ - Type: node.Branch, Key: []byte{3}, Value: []byte{1, 2, 3}, Children: padRightChildren([]*Node{ - {Type: node.Leaf, Key: []byte{1}}, - {Type: node.Leaf, Key: []byte{2}}, + {Key: []byte{1}}, + {Key: []byte{2}}, }), }, limit: 2, newNode: &Node{ - Type: node.Leaf, Key: []byte{3}, Value: []byte{1, 2, 3}, Dirty: true, @@ -2875,21 +2661,19 @@ func Test_Trie_deleteNodesLimit(t *testing.T) { generation: 1, }, parent: &Node{ - Type: node.Branch, - Key: []byte{3}, + Key: []byte{3}, Children: padRightChildren([]*Node{ nil, - {Type: node.Leaf, Key: []byte{1}}, + {Key: []byte{1}}, nil, - {Type: node.Leaf, Key: []byte{2}}, + {Key: []byte{2}}, nil, - {Type: node.Leaf, Key: []byte{3}}, + {Key: []byte{3}}, }), }, prefix: []byte{1, 2}, limit: 2, newNode: &Node{ - Type: node.Leaf, Key: []byte{3, 5, 3}, Generation: 1, Dirty: true, @@ -2925,14 +2709,12 @@ func Test_Trie_ClearPrefix(t *testing.T) { }{ "nil prefix": { trie: Trie{ - root: &Node{ - Type: node.Leaf}, + root: &Node{}, }, }, "empty prefix": { trie: Trie{ - root: &Node{ - Type: node.Leaf}, + root: &Node{}, }, prefix: []byte{}, }, @@ -2942,21 +2724,17 @@ func Test_Trie_ClearPrefix(t *testing.T) { "clear prefix": { trie: Trie{ root: &Node{ - Type: node.Branch, - Key: []byte{1, 2}, + Key: []byte{1, 2}, Children: padRightChildren([]*Node{ - { - Type: node.Leaf, // full key in nibbles 1, 2, 0, 5 - Key: []byte{5}, + { // full key in nibbles 1, 2, 0, 5 + Key: []byte{5}, }, - { - Type: node.Branch, // full key in nibbles 1, 2, 1, 6 + { // full key in nibbles 1, 2, 1, 6 Key: []byte{6}, Value: []byte("bottom branch"), Children: padRightChildren([]*Node{ - { - Type: node.Leaf, // full key in nibbles 1, 2, 1, 6, 0, 7 - Key: []byte{7}, + { // full key in nibbles 1, 2, 1, 6, 0, 7 + Key: []byte{7}, }, }), }, @@ -2966,7 +2744,6 @@ func Test_Trie_ClearPrefix(t *testing.T) { prefix: []byte{0x12, 0x16}, expectedTrie: Trie{ root: &Node{ - Type: node.Leaf, Key: []byte{1, 2, 0, 5}, Dirty: true, }, @@ -3007,16 +2784,14 @@ func Test_Trie_clearPrefix(t *testing.T) { "nil parent": {}, "leaf parent with common prefix": { parent: &Node{ - Type: node.Leaf, - Key: []byte{1, 2}, + Key: []byte{1, 2}, }, prefix: []byte{1}, updated: true, }, "leaf parent with key equal prefix": { parent: &Node{ - Type: node.Leaf, - Key: []byte{1}, + Key: []byte{1}, }, prefix: []byte{1}, updated: true, @@ -3026,13 +2801,11 @@ func Test_Trie_clearPrefix(t *testing.T) { generation: 1, }, parent: &Node{ - Type: node.Leaf, - Key: []byte{1, 2}, + Key: []byte{1, 2}, }, prefix: []byte{1, 3}, newParent: &Node{ - Type: node.Leaf, - Key: []byte{1, 2}, + Key: []byte{1, 2}, }, }, "leaf parent with key smaller than prefix": { @@ -3040,22 +2813,19 @@ func Test_Trie_clearPrefix(t *testing.T) { generation: 1, }, parent: &Node{ - Type: node.Leaf, - Key: []byte{1}, + Key: []byte{1}, }, prefix: []byte{1, 2}, newParent: &Node{ - Type: node.Leaf, - Key: []byte{1}, + Key: []byte{1}, }, }, "branch parent with common prefix": { parent: &Node{ - Type: node.Branch, Key: []byte{1, 2}, Value: []byte{1}, Children: padRightChildren([]*Node{ - {Type: node.Leaf}, + {}, }), }, prefix: []byte{1}, @@ -3063,11 +2833,10 @@ func Test_Trie_clearPrefix(t *testing.T) { }, "branch with key equal prefix": { parent: &Node{ - Type: node.Branch, Key: []byte{1, 2}, Value: []byte{1}, Children: padRightChildren([]*Node{ - {Type: node.Leaf}, + {}, }), }, prefix: []byte{1, 2}, @@ -3078,20 +2847,18 @@ func Test_Trie_clearPrefix(t *testing.T) { generation: 1, }, parent: &Node{ - Type: node.Branch, Key: []byte{1, 2}, Value: []byte{1}, Children: padRightChildren([]*Node{ - {Type: node.Leaf}, + {}, }), }, prefix: []byte{1, 3}, newParent: &Node{ - Type: node.Branch, Key: []byte{1, 2}, Value: []byte{1}, Children: padRightChildren([]*Node{ - {Type: node.Leaf}, + {}, }), }, }, @@ -3100,20 +2867,18 @@ func Test_Trie_clearPrefix(t *testing.T) { generation: 1, }, parent: &Node{ - Type: node.Branch, Key: []byte{1}, Value: []byte{1}, Children: padRightChildren([]*Node{ - {Type: node.Leaf}, + {}, }), }, prefix: []byte{1, 2, 3}, newParent: &Node{ - Type: node.Branch, Key: []byte{1}, Value: []byte{1}, Children: padRightChildren([]*Node{ - {Type: node.Leaf}, + {}, }), }, }, @@ -3122,20 +2887,18 @@ func Test_Trie_clearPrefix(t *testing.T) { generation: 1, }, parent: &Node{ - Type: node.Branch, Key: []byte{1}, Value: []byte{1}, Children: padRightChildren([]*Node{ - {Type: node.Leaf}, + {}, }), }, prefix: []byte{1, 2}, newParent: &Node{ - Type: node.Branch, Key: []byte{1}, Value: []byte{1}, Children: padRightChildren([]*Node{ - {Type: node.Leaf}, + {}, }), }, }, @@ -3144,24 +2907,22 @@ func Test_Trie_clearPrefix(t *testing.T) { generation: 1, }, parent: &Node{ - Type: node.Branch, Key: []byte{1}, Value: []byte{1}, Children: padRightChildren([]*Node{ - {Type: node.Leaf, Key: []byte{3}}, - {Type: node.Leaf, Key: []byte{4}}, + {Key: []byte{3}}, + {Key: []byte{4}}, }), }, prefix: []byte{1, 0, 3}, newParent: &Node{ - Type: node.Branch, Key: []byte{1}, Value: []byte{1}, Dirty: true, Generation: 1, Children: padRightChildren([]*Node{ nil, - {Type: node.Leaf, Key: []byte{4}}, + {Key: []byte{4}}, }), }, updated: true, @@ -3171,16 +2932,14 @@ func Test_Trie_clearPrefix(t *testing.T) { generation: 1, }, parent: &Node{ - Type: node.Branch, Key: []byte{1}, Value: []byte{1}, Children: padRightChildren([]*Node{ - {Type: node.Leaf, Key: []byte{3}}, + {Key: []byte{3}}, }), }, prefix: []byte{1, 0}, newParent: &Node{ - Type: node.Leaf, Key: []byte{1}, Value: []byte{1}, Dirty: true, @@ -3193,18 +2952,15 @@ func Test_Trie_clearPrefix(t *testing.T) { generation: 1, }, parent: &Node{ - Type: node.Branch, Key: []byte{1}, Value: []byte{1}, Children: padRightChildren([]*Node{ - { - Type: node.Branch, // full key 1, 0, 3 + { // full key 1, 0, 3 Key: []byte{3}, Value: []byte{1}, Children: padRightChildren([]*Node{ - { - Type: node.Leaf, // full key 1, 0, 3, 0, 5 - Key: []byte{5}, + { // full key 1, 0, 3, 0, 5 + Key: []byte{5}, }, }), }, @@ -3212,14 +2968,12 @@ func Test_Trie_clearPrefix(t *testing.T) { }, prefix: []byte{1, 0, 3, 0}, newParent: &Node{ - Type: node.Branch, Key: []byte{1}, Value: []byte{1}, Dirty: true, Generation: 1, Children: padRightChildren([]*Node{ - { - Type: node.Leaf, // full key 1, 0, 3 + { // full key 1, 0, 3 Key: []byte{3}, Value: []byte{1}, Dirty: true, @@ -3259,14 +3013,12 @@ func Test_Trie_Delete(t *testing.T) { }{ "nil key": { trie: Trie{ - root: &Node{ - Type: node.Leaf}, + root: &Node{}, }, }, "empty key": { trie: Trie{ - root: &Node{ - Type: node.Leaf}, + root: &Node{}, }, }, "empty trie": { @@ -3276,21 +3028,17 @@ func Test_Trie_Delete(t *testing.T) { trie: Trie{ generation: 1, root: &Node{ - Type: node.Branch, - Key: []byte{1, 2}, + Key: []byte{1, 2}, Children: padRightChildren([]*Node{ { - Type: node.Leaf, Key: []byte{5}, Value: []byte{97}, }, - { - Type: node.Branch, // full key in nibbles 1, 2, 1, 6 + { // full key in nibbles 1, 2, 1, 6 Key: []byte{6}, Value: []byte{98}, Children: padRightChildren([]*Node{ - { - Type: node.Leaf, // full key in nibbles 1, 2, 1, 6, 0, 7 + { // full key in nibbles 1, 2, 1, 6, 0, 7 Key: []byte{7}, Value: []byte{99}, }, @@ -3303,18 +3051,15 @@ func Test_Trie_Delete(t *testing.T) { expectedTrie: Trie{ generation: 1, root: &Node{ - Type: node.Branch, Key: []byte{1, 2}, Dirty: true, Generation: 1, Children: padRightChildren([]*Node{ { - Type: node.Leaf, Key: []byte{5}, Value: []byte{97}, }, - { - Type: node.Leaf, // full key in nibbles 1, 2, 1, 6 + { // full key in nibbles 1, 2, 1, 6 Key: []byte{6, 0, 7}, Value: []byte{99}, Dirty: true, @@ -3361,23 +3106,20 @@ func Test_Trie_delete(t *testing.T) { }, "leaf parent and nil key": { parent: &Node{ - Type: node.Leaf, - Key: []byte{1}, + Key: []byte{1}, }, updated: true, }, "leaf parent and empty key": { parent: &Node{ - Type: node.Leaf, - Key: []byte{1}, + Key: []byte{1}, }, key: []byte{}, updated: true, }, "leaf parent matches key": { parent: &Node{ - Type: node.Leaf, - Key: []byte{1}, + Key: []byte{1}, }, key: []byte{1}, updated: true, @@ -3387,13 +3129,11 @@ func Test_Trie_delete(t *testing.T) { generation: 1, }, parent: &Node{ - Type: node.Leaf, - Key: []byte{1}, + Key: []byte{1}, }, key: []byte{2}, newParent: &Node{ - Type: node.Leaf, - Key: []byte{1}, + Key: []byte{1}, }, }, "branch parent and nil key": { @@ -3401,18 +3141,15 @@ func Test_Trie_delete(t *testing.T) { generation: 1, }, parent: &Node{ - Type: node.Branch, Key: []byte{1}, Value: []byte{1}, Children: padRightChildren([]*Node{ { - Type: node.Leaf, - Key: []byte{2}, + Key: []byte{2}, }, }), }, newParent: &Node{ - Type: node.Leaf, Key: []byte{1, 0, 2}, Dirty: true, Generation: 1, @@ -3424,19 +3161,16 @@ func Test_Trie_delete(t *testing.T) { generation: 1, }, parent: &Node{ - Type: node.Branch, Key: []byte{1}, Value: []byte{1}, Children: padRightChildren([]*Node{ { - Type: node.Leaf, - Key: []byte{2}, + Key: []byte{2}, }, }), }, key: []byte{}, newParent: &Node{ - Type: node.Leaf, Key: []byte{1, 0, 2}, Dirty: true, Generation: 1, @@ -3448,19 +3182,16 @@ func Test_Trie_delete(t *testing.T) { generation: 1, }, parent: &Node{ - Type: node.Branch, Key: []byte{1}, Value: []byte{1}, Children: padRightChildren([]*Node{ { - Type: node.Leaf, - Key: []byte{2}, + Key: []byte{2}, }, }), }, key: []byte{1}, newParent: &Node{ - Type: node.Leaf, Key: []byte{1, 0, 2}, Dirty: true, Generation: 1, @@ -3472,19 +3203,16 @@ func Test_Trie_delete(t *testing.T) { generation: 1, }, parent: &Node{ - Type: node.Branch, Key: []byte{1}, Value: []byte{1}, Children: padRightChildren([]*Node{ - { - Type: node.Leaf, // full key 1, 0, 2 - Key: []byte{2}, + { // full key 1, 0, 2 + Key: []byte{2}, }, }), }, key: []byte{1, 0, 2}, newParent: &Node{ - Type: node.Leaf, Key: []byte{1}, Value: []byte{1}, Dirty: true, @@ -3497,20 +3225,18 @@ func Test_Trie_delete(t *testing.T) { generation: 1, }, parent: &Node{ - Type: node.Branch, Key: []byte{1}, Value: []byte{1}, Children: padRightChildren([]*Node{ - {Type: node.Leaf}, + {}, }), }, key: []byte{2}, newParent: &Node{ - Type: node.Branch, Key: []byte{1}, Value: []byte{1}, Children: padRightChildren([]*Node{ - {Type: node.Leaf}, + {}, }), }, }, @@ -3519,25 +3245,21 @@ func Test_Trie_delete(t *testing.T) { generation: 1, }, parent: &Node{ - Type: node.Branch, Key: []byte{1}, Value: []byte{1}, Children: padRightChildren([]*Node{ - { - Type: node.Leaf, // full key 1, 0, 2 - Key: []byte{2}, + { // full key 1, 0, 2 + Key: []byte{2}, }, }), }, key: []byte{1, 0, 3}, newParent: &Node{ - Type: node.Branch, Key: []byte{1}, Value: []byte{1}, Children: padRightChildren([]*Node{ - { - Type: node.Leaf, // full key 1, 0, 2 - Key: []byte{2}, + { // full key 1, 0, 2 + Key: []byte{2}, }, }), }, @@ -3577,14 +3299,12 @@ func Test_handleDeletion(t *testing.T) { }{ "branch with value and without children": { branch: &Node{ - Type: node.Branch, Key: []byte{1, 2, 3}, Value: []byte{5, 6, 7}, Generation: 1, }, deletedKey: []byte{1, 2, 3, 4}, newNode: &Node{ - Type: node.Leaf, Key: []byte{1, 2, 3}, Value: []byte{5, 6, 7}, Generation: 1, @@ -3596,35 +3316,31 @@ func Test_handleDeletion(t *testing.T) { // remaining. "branch with value and a single child": { branch: &Node{ - Type: node.Branch, Key: []byte{1, 2, 3}, Value: []byte{5, 6, 7}, Generation: 1, Children: padRightChildren([]*Node{ nil, - {Type: node.Leaf, Key: []byte{9}}, + {Key: []byte{9}}, }), }, newNode: &Node{ - Type: node.Branch, Key: []byte{1, 2, 3}, Value: []byte{5, 6, 7}, Generation: 1, Children: padRightChildren([]*Node{ nil, - {Type: node.Leaf, Key: []byte{9}}, + {Key: []byte{9}}, }), }, }, "branch without value and a single leaf child": { branch: &Node{ - Type: node.Branch, Key: []byte{1, 2, 3}, Generation: 1, Children: padRightChildren([]*Node{ nil, - { - Type: node.Leaf, // full key 1,2,3,1,9 + { // full key 1,2,3,1,9 Key: []byte{9}, Value: []byte{10}, }, @@ -3632,7 +3348,6 @@ func Test_handleDeletion(t *testing.T) { }, deletedKey: []byte{1, 2, 3, 4}, newNode: &Node{ - Type: node.Leaf, Key: []byte{1, 2, 3, 1, 9}, Value: []byte{10}, Generation: 1, @@ -3641,33 +3356,30 @@ func Test_handleDeletion(t *testing.T) { }, "branch without value and a single branch child": { branch: &Node{ - Type: node.Branch, Key: []byte{1, 2, 3}, Generation: 1, Children: padRightChildren([]*Node{ nil, { - Type: node.Branch, Key: []byte{9}, Value: []byte{10}, Children: padRightChildren([]*Node{ - {Type: node.Leaf, Key: []byte{7}}, + {Key: []byte{7}}, nil, - {Type: node.Leaf, Key: []byte{8}}, + {Key: []byte{8}}, }), }, }), }, newNode: &Node{ - Type: node.Branch, Key: []byte{1, 2, 3, 1, 9}, Value: []byte{10}, Generation: 1, Dirty: true, Children: padRightChildren([]*Node{ - {Type: node.Leaf, Key: []byte{7}}, + {Key: []byte{7}}, nil, - {Type: node.Leaf, Key: []byte{8}}, + {Key: []byte{8}}, }), }, },