From 41ccd40322a06f7c493c62734d7b055a1eee98d7 Mon Sep 17 00:00:00 2001 From: Quentin McGaw Date: Thu, 10 Nov 2022 17:18:57 +0000 Subject: [PATCH] fix(trie): no in-memory encoding caching --- internal/trie/node/copy.go | 5 -- internal/trie/node/copy_test.go | 7 -- internal/trie/node/dirty.go | 4 +- internal/trie/node/dirty_test.go | 6 -- internal/trie/node/encode.go | 16 ---- internal/trie/node/encode_test.go | 62 -------------- internal/trie/node/hash.go | 33 ++------ internal/trie/node/hash_test.go | 129 ++++-------------------------- internal/trie/node/node.go | 3 - internal/trie/node/node_test.go | 12 --- lib/trie/database.go | 2 - lib/trie/print_test.go | 4 - lib/trie/trie_test.go | 63 ++++----------- 13 files changed, 40 insertions(+), 306 deletions(-) diff --git a/internal/trie/node/copy.go b/internal/trie/node/copy.go index 65b09d5b01..b112db7e47 100644 --- a/internal/trie/node/copy.go +++ b/internal/trie/node/copy.go @@ -99,11 +99,6 @@ func (n *Node) Copy(settings CopySettings) *Node { cpy.MerkleValue = make([]byte, len(n.MerkleValue)) copy(cpy.MerkleValue, n.MerkleValue) } - - if n.Encoding != nil { - cpy.Encoding = make([]byte, len(n.Encoding)) - copy(cpy.Encoding, n.Encoding) - } } return cpy diff --git a/internal/trie/node/copy_test.go b/internal/trie/node/copy_test.go index 36d2df7174..cb3c7059f5 100644 --- a/internal/trie/node/copy_test.go +++ b/internal/trie/node/copy_test.go @@ -48,7 +48,6 @@ func Test_Node_Copy(t *testing.T) { }), Dirty: true, MerkleValue: []byte{5}, - Encoding: []byte{6}, }, settings: DefaultCopySettings, expectedNode: &Node{ @@ -96,7 +95,6 @@ func Test_Node_Copy(t *testing.T) { }), Dirty: true, MerkleValue: []byte{5}, - Encoding: []byte{6}, }, settings: DeepCopySettings, expectedNode: &Node{ @@ -110,7 +108,6 @@ func Test_Node_Copy(t *testing.T) { }), Dirty: true, MerkleValue: []byte{5}, - Encoding: []byte{6}, }, }, "non empty leaf": { @@ -119,7 +116,6 @@ func Test_Node_Copy(t *testing.T) { SubValue: []byte{3, 4}, Dirty: true, MerkleValue: []byte{5}, - Encoding: []byte{6}, }, settings: DefaultCopySettings, expectedNode: &Node{ @@ -134,7 +130,6 @@ func Test_Node_Copy(t *testing.T) { SubValue: []byte{3, 4}, Dirty: true, MerkleValue: []byte{5}, - Encoding: []byte{6}, }, settings: DeepCopySettings, expectedNode: &Node{ @@ -142,7 +137,6 @@ func Test_Node_Copy(t *testing.T) { SubValue: []byte{3, 4}, Dirty: true, MerkleValue: []byte{5}, - Encoding: []byte{6}, }, }, } @@ -158,7 +152,6 @@ func Test_Node_Copy(t *testing.T) { testForSliceModif(t, testCase.node.Key, nodeCopy.Key) testForSliceModif(t, testCase.node.SubValue, nodeCopy.SubValue) testForSliceModif(t, testCase.node.MerkleValue, nodeCopy.MerkleValue) - testForSliceModif(t, testCase.node.Encoding, nodeCopy.Encoding) if testCase.node.Kind() == Branch { testCase.node.Children[15] = &Node{Key: []byte("modified")} diff --git a/internal/trie/node/dirty.go b/internal/trie/node/dirty.go index f4377d768b..26dc2248b7 100644 --- a/internal/trie/node/dirty.go +++ b/internal/trie/node/dirty.go @@ -7,9 +7,7 @@ package node func (n *Node) SetDirty() { n.Dirty = true // A node is marked dirty if its key or value is modified. - // This means its cached encoding and hash fields are no longer - // valid. To improve memory usage, we clear these fields. - n.Encoding = nil + // This means its Merkle value field is no longer valid. n.MerkleValue = nil } diff --git a/internal/trie/node/dirty_test.go b/internal/trie/node/dirty_test.go index 533f43e149..27867266a4 100644 --- a/internal/trie/node/dirty_test.go +++ b/internal/trie/node/dirty_test.go @@ -18,14 +18,12 @@ func Test_Node_SetDirty(t *testing.T) { }{ "not dirty to dirty": { node: Node{ - Encoding: []byte{1}, MerkleValue: []byte{1}, }, expected: Node{Dirty: true}, }, "dirty to dirty": { node: Node{ - Encoding: []byte{1}, MerkleValue: []byte{1}, Dirty: true, }, @@ -54,22 +52,18 @@ func Test_Node_SetClean(t *testing.T) { }{ "not dirty to not dirty": { node: Node{ - Encoding: []byte{1}, MerkleValue: []byte{1}, }, expected: Node{ - Encoding: []byte{1}, MerkleValue: []byte{1}, }, }, "dirty to not dirty": { node: Node{ - Encoding: []byte{1}, MerkleValue: []byte{1}, Dirty: true, }, expected: Node{ - Encoding: []byte{1}, MerkleValue: []byte{1}, }, }, diff --git a/internal/trie/node/encode.go b/internal/trie/node/encode.go index 54283edbfe..b8b9f11b5f 100644 --- a/internal/trie/node/encode.go +++ b/internal/trie/node/encode.go @@ -16,14 +16,6 @@ import ( // of this package, and specified in the Polkadot spec at // https://spec.polkadot.network/#sect-state-storage func (n *Node) Encode(buffer Buffer) (err error) { - if !n.Dirty && n.Encoding != nil { - _, err = buffer.Write(n.Encoding) - if err != nil { - return fmt.Errorf("cannot write stored encoding to buffer: %w", err) - } - return nil - } - err = encodeHeader(n, buffer) if err != nil { return fmt.Errorf("cannot encode header: %w", err) @@ -66,13 +58,5 @@ func (n *Node) Encode(buffer Buffer) (err error) { } } - if kind == 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. - n.Encoding = make([]byte, buffer.Len()) - copy(n.Encoding, buffer.Bytes()) - } - return nil } diff --git a/internal/trie/node/encode_test.go b/internal/trie/node/encode_test.go index 4cf70c5f3c..a81fb875de 100644 --- a/internal/trie/node/encode_test.go +++ b/internal/trie/node/encode_test.go @@ -26,37 +26,10 @@ func Test_Node_Encode(t *testing.T) { testCases := map[string]struct { node *Node writes []writeCall - bufferLenCall bool - bufferBytesCall bool expectedEncoding []byte wrappedErr error errMessage string }{ - "clean leaf with encoding": { - node: &Node{ - Encoding: []byte{1, 2, 3}, - }, - writes: []writeCall{ - { - written: []byte{1, 2, 3}, - }, - }, - expectedEncoding: []byte{1, 2, 3}, - }, - "write error for clean leaf with encoding": { - node: &Node{ - Encoding: []byte{1, 2, 3}, - }, - writes: []writeCall{ - { - written: []byte{1, 2, 3}, - err: errTest, - }, - }, - expectedEncoding: []byte{1, 2, 3}, - wrappedErr: errTest, - errMessage: "cannot write stored encoding to buffer: test error", - }, "leaf header encoding error": { node: &Node{ Key: make([]byte, 1), @@ -123,8 +96,6 @@ func Test_Node_Encode(t *testing.T) { written: []byte{12, 4, 5, 6}, }, }, - bufferLenCall: true, - bufferBytesCall: true, expectedEncoding: []byte{1, 2, 3}, }, "leaf with empty value success": { @@ -142,35 +113,8 @@ func Test_Node_Encode(t *testing.T) { written: []byte{0}, }, }, - bufferLenCall: true, - bufferBytesCall: true, expectedEncoding: []byte{1, 2, 3}, }, - "clean branch with encoding": { - node: &Node{ - Children: make([]*Node, ChildrenCapacity), - Encoding: []byte{1, 2, 3}, - }, - writes: []writeCall{ - { // stored encoding - written: []byte{1, 2, 3}, - }, - }, - }, - "write error for clean branch with encoding": { - node: &Node{ - Children: make([]*Node, ChildrenCapacity), - Encoding: []byte{1, 2, 3}, - }, - writes: []writeCall{ - { // stored encoding - written: []byte{1, 2, 3}, - err: errTest, - }, - }, - wrappedErr: errTest, - errMessage: "cannot write stored encoding to buffer: test error", - }, "branch header encoding error": { node: &Node{ Children: make([]*Node, ChildrenCapacity), @@ -362,12 +306,6 @@ func Test_Node_Encode(t *testing.T) { } previousCall = call } - if testCase.bufferLenCall { - buffer.EXPECT().Len().Return(len(testCase.expectedEncoding)) - } - if testCase.bufferBytesCall { - buffer.EXPECT().Bytes().Return(testCase.expectedEncoding) - } err := testCase.node.Encode(buffer) diff --git a/internal/trie/node/hash.go b/internal/trie/node/hash.go index 2ea5806ead..595e06fc0f 100644 --- a/internal/trie/node/hash.go +++ b/internal/trie/node/hash.go @@ -90,14 +90,12 @@ func (n *Node) CalculateRootMerkleValue() (merkleValue []byte, err error) { // and a merkle value writer, such that buffer sync pools can be used // by the caller. func (n *Node) EncodeAndHash() (encoding, merkleValue []byte, err error) { - if !n.Dirty && n.Encoding != nil && n.MerkleValue != nil { - return n.Encoding, n.MerkleValue, nil - } - - encoding, err = n.encodeIfNeeded() + encodingBuffer := bytes.NewBuffer(nil) + err = n.Encode(encodingBuffer) if err != nil { return nil, nil, fmt.Errorf("encoding node: %w", err) } + encoding = encodingBuffer.Bytes() const maxMerkleValueSize = 32 merkleValueBuffer := bytes.NewBuffer(make([]byte, 0, maxMerkleValueSize)) @@ -118,15 +116,12 @@ func (n *Node) EncodeAndHash() (encoding, merkleValue []byte, err error) { // and a merkle value writer, such that buffer sync pools can be used // by the caller. func (n *Node) EncodeAndHashRoot() (encoding, merkleValue []byte, err error) { - const rootMerkleValueLength = 32 - if !n.Dirty && n.Encoding != nil && len(n.MerkleValue) == rootMerkleValueLength { - return n.Encoding, n.MerkleValue, nil - } - - encoding, err = n.encodeIfNeeded() + encodingBuffer := bytes.NewBuffer(nil) + err = n.Encode(encodingBuffer) if err != nil { return nil, nil, fmt.Errorf("encoding node: %w", err) } + encoding = encodingBuffer.Bytes() const merkleValueSize = 32 merkleValueBuffer := bytes.NewBuffer(make([]byte, 0, merkleValueSize)) @@ -139,19 +134,3 @@ func (n *Node) EncodeAndHashRoot() (encoding, merkleValue []byte, err error) { return encoding, merkleValue, nil } - -func (n *Node) encodeIfNeeded() (encoding []byte, err error) { - if !n.Dirty && n.Encoding != nil { - return n.Encoding, nil // no need to copy - } - - buffer := bytes.NewBuffer(nil) - err = n.Encode(buffer) - if err != nil { - return nil, fmt.Errorf("encoding: %w", err) - } - - n.Encoding = buffer.Bytes() - - return n.Encoding, nil // no need to copy -} diff --git a/internal/trie/node/hash_test.go b/internal/trie/node/hash_test.go index 156e723fc1..87509f155b 100644 --- a/internal/trie/node/hash_test.go +++ b/internal/trie/node/hash_test.go @@ -185,9 +185,10 @@ func Test_Node_CalculateMerkleValue(t *testing.T) { }, "small encoding": { node: Node{ - Encoding: []byte{1}, + Key: []byte{1}, + SubValue: []byte{1}, }, - merkleValue: []byte{1}, + merkleValue: []byte{0x41, 0x1, 0x4, 0x1}, }, } @@ -230,24 +231,26 @@ func Test_Node_CalculateRootMerkleValue(t *testing.T) { }, "cached merkle value not 32 bytes": { node: Node{ - Encoding: []byte{1}, + Key: []byte{1}, + SubValue: []byte{2}, MerkleValue: []byte{1}, }, merkleValue: []byte{ - 0xee, 0x15, 0x5a, 0xce, 0x9c, 0x40, 0x29, 0x20, - 0x74, 0xcb, 0x6a, 0xff, 0x8c, 0x9c, 0xcd, 0xd2, - 0x73, 0xc8, 0x16, 0x48, 0xff, 0x11, 0x49, 0xef, - 0x36, 0xbc, 0xea, 0x6e, 0xbb, 0x8a, 0x3e, 0x25}, + 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}, }, "root small encoding": { node: Node{ - Encoding: []byte{1}, + Key: []byte{1}, + SubValue: []byte{2}, }, merkleValue: []byte{ - 0xee, 0x15, 0x5a, 0xce, 0x9c, 0x40, 0x29, 0x20, - 0x74, 0xcb, 0x6a, 0xff, 0x8c, 0x9c, 0xcd, 0xd2, - 0x73, 0xc8, 0x16, 0x48, 0xff, 0x11, 0x49, 0xef, - 0x36, 0xbc, 0xea, 0x6e, 0xbb, 0x8a, 0x3e, 0x25}, + 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}, }, } @@ -284,52 +287,18 @@ func Test_Node_EncodeAndHash(t *testing.T) { SubValue: []byte{2}, }, expectedNode: Node{ - Encoding: []byte{0x41, 0x1, 0x4, 0x2}, MerkleValue: []byte{0x41, 0x1, 0x4, 0x2}, }, encoding: []byte{0x41, 0x1, 0x4, 0x2}, hash: []byte{0x41, 0x1, 0x4, 0x2}, }, - "leaf dirty with precomputed encoding and hash": { - node: Node{ - Key: []byte{1}, - SubValue: []byte{2}, - Dirty: true, - Encoding: []byte{3}, - MerkleValue: []byte{4}, - }, - expectedNode: Node{ - Encoding: []byte{0x41, 0x1, 0x4, 0x2}, - MerkleValue: []byte{0x41, 0x1, 0x4, 0x2}, - }, - encoding: []byte{0x41, 0x1, 0x4, 0x2}, - hash: []byte{0x41, 0x1, 0x4, 0x2}, - }, - "leaf not dirty with precomputed encoding and hash": { - node: Node{ - Key: []byte{1}, - SubValue: []byte{2}, - Dirty: false, - Encoding: []byte{3}, - MerkleValue: []byte{4}, - }, - expectedNode: Node{ - Key: []byte{1}, - SubValue: []byte{2}, - Encoding: []byte{3}, - MerkleValue: []byte{4}, - }, - encoding: []byte{3}, - hash: []byte{4}, - }, "large leaf encoding": { node: Node{ Key: repeatBytes(65, 7), SubValue: []byte{0x01}, }, expectedNode: Node{ - Encoding: []byte{0x7f, 0x2, 0x7, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x4, 0x1}, //nolint:lll - MerkleValue: []byte{0xd2, 0x1d, 0x43, 0x7, 0x18, 0x17, 0x1b, 0xf1, 0x45, 0x9c, 0xe5, 0x8f, 0xd7, 0x79, 0x82, 0xb, 0xc8, 0x5c, 0x8, 0x47, 0xfe, 0x6c, 0x99, 0xc5, 0xe9, 0x57, 0x87, 0x7, 0x1d, 0x2e, 0x24, 0x5d}, //nolint:lll + MerkleValue: []byte{0xd2, 0x1d, 0x43, 0x7, 0x18, 0x17, 0x1b, 0xf1, 0x45, 0x9c, 0xe5, 0x8f, 0xd7, 0x79, 0x82, 0xb, 0xc8, 0x5c, 0x8, 0x47, 0xfe, 0x6c, 0x99, 0xc5, 0xe9, 0x57, 0x87, 0x7, 0x1d, 0x2e, 0x24, 0x5d}, //nolint:lll }, encoding: []byte{0x7f, 0x2, 0x7, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x4, 0x1}, //nolint:lll hash: []byte{0xd2, 0x1d, 0x43, 0x7, 0x18, 0x17, 0x1b, 0xf1, 0x45, 0x9c, 0xe5, 0x8f, 0xd7, 0x79, 0x82, 0xb, 0xc8, 0x5c, 0x8, 0x47, 0xfe, 0x6c, 0x99, 0xc5, 0xe9, 0x57, 0x87, 0x7, 0x1d, 0x2e, 0x24, 0x5d}, //nolint:lll @@ -340,7 +309,6 @@ func Test_Node_EncodeAndHash(t *testing.T) { }, expectedNode: Node{ Children: make([]*Node, ChildrenCapacity), - Encoding: []byte{0x80, 0x0, 0x0}, MerkleValue: []byte{0x80, 0x0, 0x0}, }, encoding: []byte{0x80, 0x0, 0x0}, @@ -354,48 +322,11 @@ func Test_Node_EncodeAndHash(t *testing.T) { }, expectedNode: Node{ Children: make([]*Node, ChildrenCapacity), - Encoding: []byte{0xc1, 0x1, 0x0, 0x0, 0x4, 0x2}, MerkleValue: []byte{0xc1, 0x1, 0x0, 0x0, 0x4, 0x2}, }, encoding: []byte{0xc1, 0x1, 0x0, 0x0, 0x4, 0x2}, hash: []byte{0xc1, 0x1, 0x0, 0x0, 0x4, 0x2}, }, - "branch dirty with precomputed encoding and hash": { - node: Node{ - Children: make([]*Node, ChildrenCapacity), - Key: []byte{1}, - SubValue: []byte{2}, - Dirty: true, - Encoding: []byte{3}, - MerkleValue: []byte{4}, - }, - expectedNode: Node{ - Children: make([]*Node, ChildrenCapacity), - Encoding: []byte{0xc1, 0x1, 0x0, 0x0, 0x4, 0x2}, - MerkleValue: []byte{0xc1, 0x1, 0x0, 0x0, 0x4, 0x2}, - }, - encoding: []byte{0xc1, 0x1, 0x0, 0x0, 0x4, 0x2}, - hash: []byte{0xc1, 0x1, 0x0, 0x0, 0x4, 0x2}, - }, - "branch not dirty with precomputed encoding and hash": { - node: Node{ - Children: make([]*Node, ChildrenCapacity), - Key: []byte{1}, - SubValue: []byte{2}, - Dirty: false, - Encoding: []byte{3}, - MerkleValue: []byte{4}, - }, - expectedNode: Node{ - Children: make([]*Node, ChildrenCapacity), - Key: []byte{1}, - SubValue: []byte{2}, - Encoding: []byte{3}, - MerkleValue: []byte{4}, - }, - encoding: []byte{3}, - hash: []byte{4}, - }, "large branch encoding": { node: Node{ Children: make([]*Node, ChildrenCapacity), @@ -403,8 +334,7 @@ func Test_Node_EncodeAndHash(t *testing.T) { }, expectedNode: Node{ 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 - MerkleValue: []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 + MerkleValue: []byte{0x6b, 0xd8, 0xcc, 0xac, 0x71, 0x77, 0x44, 0x17, 0xfe, 0xe0, 0xde, 0xda, 0xd5, 0x97, 0x6e, 0x69, 0xeb, 0xe9, 0xdd, 0x80, 0x1d, 0x4b, 0x51, 0xf1, 0x5b, 0xf3, 0x4a, 0x93, 0x27, 0x32, 0x2c, 0xb0}, //nolint:lll }, encoding: []byte{0xbf, 0x2, 0x7, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x0, 0x0}, //nolint:lll hash: []byte{0x6b, 0xd8, 0xcc, 0xac, 0x71, 0x77, 0x44, 0x17, 0xfe, 0xe0, 0xde, 0xda, 0xd5, 0x97, 0x6e, 0x69, 0xeb, 0xe9, 0xdd, 0x80, 0x1d, 0x4b, 0x51, 0xf1, 0x5b, 0xf3, 0x4a, 0x93, 0x27, 0x32, 0x2c, 0xb0}, //nolint:lll @@ -431,12 +361,6 @@ func Test_Node_EncodeAndHash(t *testing.T) { func Test_Node_EncodeAndHashRoot(t *testing.T) { t.Parallel() - some32BHashDigest := []byte{ - 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, - 0x74, 0xcb, 0x6a, 0xff, 0x8c, 0x9c, 0xcd, 0xd2, - 0x73, 0xc8, 0x16, 0x48, 0xff, 0x11, 0x49, 0xef, - 0x36, 0xbc, 0xea, 0x6e, 0xbb, 0x8a, 0x3e, 0x25} - testCases := map[string]struct { node Node expectedNode Node @@ -445,30 +369,12 @@ func Test_Node_EncodeAndHashRoot(t *testing.T) { errWrapped error errMessage string }{ - "leaf not dirty with precomputed encoding and hash": { - node: Node{ - Key: []byte{1}, - SubValue: []byte{2}, - Dirty: false, - Encoding: []byte{3}, - MerkleValue: some32BHashDigest, - }, - expectedNode: Node{ - Key: []byte{1}, - SubValue: []byte{2}, - Encoding: []byte{3}, - MerkleValue: some32BHashDigest, - }, - encoding: []byte{3}, - hash: some32BHashDigest, - }, "small leaf encoding": { node: Node{ Key: []byte{1}, SubValue: []byte{2}, }, expectedNode: Node{ - Encoding: []byte{0x41, 0x1, 0x4, 0x2}, MerkleValue: []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 }, encoding: []byte{0x41, 0x1, 0x4, 0x2}, @@ -482,7 +388,6 @@ func Test_Node_EncodeAndHashRoot(t *testing.T) { }, expectedNode: Node{ Children: make([]*Node, ChildrenCapacity), - Encoding: []byte{0xc1, 0x1, 0x0, 0x0, 0x4, 0x2}, MerkleValue: []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 }, encoding: []byte{0xc1, 0x1, 0x0, 0x0, 0x4, 0x2}, diff --git a/internal/trie/node/node.go b/internal/trie/node/node.go index 14bd3fa434..db7fb0cf94 100644 --- a/internal/trie/node/node.go +++ b/internal/trie/node/node.go @@ -32,8 +32,6 @@ type Node struct { Dirty bool // MerkleValue is the cached Merkle value of the node. MerkleValue []byte - // Encoding is the cached encoding of the node. - Encoding []byte // Descendants is the number of descendant nodes for // this particular node. @@ -64,7 +62,6 @@ func (n Node) StringNode() (stringNode *gotree.Node) { if n.Descendants > 0 { // must be a branch stringNode.Appendf("Descendants: %d", n.Descendants) } - stringNode.Appendf("Calculated encoding: " + bytesToString(n.Encoding)) stringNode.Appendf("Merkle value: " + bytesToString(n.MerkleValue)) for i, child := range n.Children { diff --git a/internal/trie/node/node_test.go b/internal/trie/node/node_test.go index d4e51256d9..c061065ef9 100644 --- a/internal/trie/node/node_test.go +++ b/internal/trie/node/node_test.go @@ -27,7 +27,6 @@ func Test_Node_String(t *testing.T) { ├── Dirty: true ├── Key: 0x0102 ├── Value: 0x0304 -├── Calculated encoding: nil └── Merkle value: nil`, }, "leaf with value higher than 1024": { @@ -41,7 +40,6 @@ func Test_Node_String(t *testing.T) { ├── Dirty: true ├── Key: 0x0102 ├── Value: 0x0000000000000000...0000000000000000 -├── Calculated encoding: nil └── Merkle value: nil`, }, "branch with value smaller than 1024": { @@ -69,7 +67,6 @@ func Test_Node_String(t *testing.T) { ├── Key: 0x0102 ├── Value: 0x0304 ├── Descendants: 3 -├── Calculated encoding: nil ├── Merkle value: nil ├── Child 3 | └── Leaf @@ -77,7 +74,6 @@ func Test_Node_String(t *testing.T) { | ├── Dirty: false | ├── Key: nil | ├── Value: nil -| ├── Calculated encoding: nil | └── Merkle value: nil ├── Child 7 | └── Branch @@ -86,7 +82,6 @@ func Test_Node_String(t *testing.T) { | ├── Key: nil | ├── Value: nil | ├── Descendants: 1 -| ├── Calculated encoding: nil | ├── Merkle value: nil | └── Child 0 | └── Leaf @@ -94,7 +89,6 @@ func Test_Node_String(t *testing.T) { | ├── Dirty: false | ├── Key: nil | ├── Value: nil -| ├── Calculated encoding: nil | └── Merkle value: nil └── Child 11 └── Leaf @@ -102,7 +96,6 @@ func Test_Node_String(t *testing.T) { ├── Dirty: false ├── Key: nil ├── Value: nil - ├── Calculated encoding: nil └── Merkle value: nil`, }, "branch with value higher than 1024": { @@ -130,7 +123,6 @@ func Test_Node_String(t *testing.T) { ├── Key: 0x0102 ├── Value: 0x0000000000000000...0000000000000000 ├── Descendants: 3 -├── Calculated encoding: nil ├── Merkle value: nil ├── Child 3 | └── Leaf @@ -138,7 +130,6 @@ func Test_Node_String(t *testing.T) { | ├── Dirty: false | ├── Key: nil | ├── Value: nil -| ├── Calculated encoding: nil | └── Merkle value: nil ├── Child 7 | └── Branch @@ -147,7 +138,6 @@ func Test_Node_String(t *testing.T) { | ├── Key: nil | ├── Value: nil | ├── Descendants: 1 -| ├── Calculated encoding: nil | ├── Merkle value: nil | └── Child 0 | └── Leaf @@ -155,7 +145,6 @@ func Test_Node_String(t *testing.T) { | ├── Dirty: false | ├── Key: nil | ├── Value: nil -| ├── Calculated encoding: nil | └── Merkle value: nil └── Child 11 └── Leaf @@ -163,7 +152,6 @@ func Test_Node_String(t *testing.T) { ├── Dirty: false ├── Key: nil ├── Value: nil - ├── Calculated encoding: nil └── Merkle value: nil`, }, } diff --git a/lib/trie/database.go b/lib/trie/database.go index 2a0682bba1..b644878a2c 100644 --- a/lib/trie/database.go +++ b/lib/trie/database.go @@ -102,7 +102,6 @@ func (t *Trie) Load(db Database, rootHash common.Hash) error { } t.root = root - t.root.Encoding = encodedNode t.root.MerkleValue = rootHashBytes return t.loadNode(db, t.root) @@ -142,7 +141,6 @@ func (t *Trie) loadNode(db Database, n *Node) error { return fmt.Errorf("decoding node with Merkle value 0x%x: %w", merkleValue, err) } - decodedNode.Encoding = encodedNode decodedNode.MerkleValue = merkleValue branch.Children[i] = decodedNode diff --git a/lib/trie/print_test.go b/lib/trie/print_test.go index d4ee6e8956..7db2ce40d7 100644 --- a/lib/trie/print_test.go +++ b/lib/trie/print_test.go @@ -32,7 +32,6 @@ func Test_Trie_String(t *testing.T) { ├── Dirty: false ├── Key: 0x010203 ├── Value: 0x030405 -├── Calculated encoding: nil └── Merkle value: nil`, }, "branch root": { @@ -62,7 +61,6 @@ func Test_Trie_String(t *testing.T) { ├── Key: nil ├── Value: 0x0102 ├── Descendants: 2 -├── Calculated encoding: nil ├── Merkle value: nil ├── Child 0 | └── Leaf @@ -70,7 +68,6 @@ func Test_Trie_String(t *testing.T) { | ├── Dirty: false | ├── Key: 0x010203 | ├── Value: 0x030405 -| ├── Calculated encoding: nil | └── Merkle value: nil └── Child 3 └── Leaf @@ -78,7 +75,6 @@ func Test_Trie_String(t *testing.T) { ├── Dirty: false ├── Key: 0x010203 ├── Value: 0x030405 - ├── Calculated encoding: nil └── Merkle value: nil`, }, } diff --git a/lib/trie/trie_test.go b/lib/trie/trie_test.go index 0e9ef8d8ad..d799fa9f66 100644 --- a/lib/trie/trie_test.go +++ b/lib/trie/trie_test.go @@ -295,35 +295,23 @@ func Test_Trie_RootNode(t *testing.T) { func Test_encodeRoot(t *testing.T) { t.Parallel() - type bufferCalls struct { - writeCalls []writeCall - lenCall bool - lenReturn int - bytesCall bool - bytesReturn []byte - } - testCases := map[string]struct { root *Node - bufferCalls bufferCalls + writeCalls []writeCall errWrapped error errMessage string expectedRoot *Node }{ "nil root and no error": { - bufferCalls: bufferCalls{ - writeCalls: []writeCall{ - {written: []byte{0}}, - }, + writeCalls: []writeCall{ + {written: []byte{0}}, }, }, "nil root and write error": { - bufferCalls: bufferCalls{ - writeCalls: []writeCall{ - { - written: []byte{0}, - err: errTest, - }, + writeCalls: []writeCall{ + { + written: []byte{0}, + err: errTest, }, }, errWrapped: errTest, @@ -334,12 +322,10 @@ func Test_encodeRoot(t *testing.T) { Key: []byte{1, 2}, SubValue: []byte{1}, }, - bufferCalls: bufferCalls{ - writeCalls: []writeCall{ - { - written: []byte{66}, - err: errTest, - }, + writeCalls: []writeCall{ + { + written: []byte{66}, + err: errTest, }, }, errWrapped: errTest, @@ -354,21 +340,14 @@ func Test_encodeRoot(t *testing.T) { Key: []byte{1, 2}, SubValue: []byte{1}, }, - bufferCalls: bufferCalls{ - writeCalls: []writeCall{ - {written: []byte{66}}, - {written: []byte{18}}, - {written: []byte{4, 1}}, - }, - lenCall: true, - lenReturn: 3, - bytesCall: true, - bytesReturn: []byte{66, 18, 4, 1}, + writeCalls: []writeCall{ + {written: []byte{66}}, + {written: []byte{18}}, + {written: []byte{4, 1}}, }, expectedRoot: &Node{ Key: []byte{1, 2}, SubValue: []byte{1}, - Encoding: []byte{66, 18, 4}, }, }, } @@ -382,7 +361,7 @@ func Test_encodeRoot(t *testing.T) { buffer := NewMockBuffer(ctrl) var previousCall *gomock.Call - for _, write := range testCase.bufferCalls.writeCalls { + for _, write := range testCase.writeCalls { call := buffer.EXPECT(). Write(write.written). Return(write.n, write.err) @@ -392,14 +371,6 @@ func Test_encodeRoot(t *testing.T) { } previousCall = call } - if testCase.bufferCalls.lenCall { - buffer.EXPECT().Len(). - Return(testCase.bufferCalls.lenReturn) - } - if testCase.bufferCalls.bytesCall { - buffer.EXPECT().Bytes(). - Return(testCase.bufferCalls.bytesReturn) - } err := encodeRoot(testCase.root, buffer) @@ -464,7 +435,6 @@ func Test_Trie_Hash(t *testing.T) { root: &Node{ Key: []byte{1, 2, 3}, SubValue: []byte{1}, - Encoding: []byte{0x43, 0x01, 0x23, 0x04, 0x01}, }, }, }, @@ -493,7 +463,6 @@ func Test_Trie_Hash(t *testing.T) { { Key: []byte{9}, SubValue: []byte{1}, - Encoding: []byte{0x41, 0x09, 0x04, 0x01}, MerkleValue: []byte{0x41, 0x09, 0x04, 0x01}, }, }),