diff --git a/internal/trie/node/copy.go b/internal/trie/node/copy.go index 6d7faa3feef..d531337b078 100644 --- a/internal/trie/node/copy.go +++ b/internal/trie/node/copy.go @@ -98,11 +98,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 36d2df71745..cb3c7059f5b 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 f4377d768b8..82786668b78 100644 --- a/internal/trie/node/dirty.go +++ b/internal/trie/node/dirty.go @@ -7,9 +7,8 @@ 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 533f43e1492..27867266a4c 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 59ce5da172b..37c4c596950 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) @@ -64,13 +56,5 @@ func (n *Node) Encode(buffer Buffer) (err error) { } } - if n.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 1d0506da4ef..1770631c385 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,35 +96,8 @@ func Test_Node_Encode(t *testing.T) { written: []byte{12, 4, 5, 6}, }, }, - 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), @@ -317,12 +263,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 8a97fa3f9f8..a986312b1d4 100644 --- a/internal/trie/node/hash.go +++ b/internal/trie/node/hash.go @@ -56,9 +56,9 @@ func hashEncoding(encoding []byte, writer io.Writer) (err error) { // CalculateMerkleValue returns the Merkle value of the non-root node. func (n *Node) CalculateMerkleValue() (merkleValue []byte, err error) { - if !n.Dirty && n.MerkleValue != nil { - return n.MerkleValue, nil - } + // if !n.Dirty && n.MerkleValue != nil { + // return n.MerkleValue, nil + // } _, merkleValue, err = n.EncodeAndHash() if err != nil { @@ -70,10 +70,10 @@ func (n *Node) CalculateMerkleValue() (merkleValue []byte, err error) { // CalculateRootMerkleValue returns the Merkle value of the root node. func (n *Node) CalculateRootMerkleValue() (merkleValue []byte, err error) { - const rootMerkleValueLength = 32 - if !n.Dirty && len(n.MerkleValue) == rootMerkleValueLength { - return n.MerkleValue, nil - } + // const rootMerkleValueLength = 32 + // if !n.Dirty && len(n.MerkleValue) == rootMerkleValueLength { + // return n.MerkleValue, nil + // } _, merkleValue, err = n.EncodeAndHashRoot() if err != nil { @@ -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,27 +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 := pools.EncodingBuffers.Get().(*bytes.Buffer) - buffer.Reset() - defer pools.EncodingBuffers.Put(buffer) - - err = n.Encode(buffer) - if err != nil { - return nil, fmt.Errorf("encoding: %w", err) - } - - bufferBytes := buffer.Bytes() - - // TODO remove this copying since it defeats the purpose of `buffer` - // and the sync.Pool. - n.Encoding = make([]byte, len(bufferBytes)) - copy(n.Encoding, bufferBytes) - - 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 156e723fc1b..87509f155b5 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 14bd3fa434b..db7fb0cf945 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 d4e51256d9d..c061065ef9c 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 4a5a17a9910..dc876d20267 100644 --- a/lib/trie/database.go +++ b/lib/trie/database.go @@ -46,6 +46,11 @@ func (t *Trie) storeNode(db chaindb.Batch, n *Node) (err error) { return nil } + // if !n.Dirty { + // // node and its eventual descendants are all already stored + // return nil + // } + var encoding, hash []byte if n == t.root { encoding, hash, err = n.EncodeAndHashRoot() @@ -103,7 +108,6 @@ func (t *Trie) Load(db Database, rootHash common.Hash) error { t.root = root t.root.SetClean() - t.root.Encoding = encodedNode t.root.MerkleValue = rootHashBytes return t.loadNode(db, t.root) @@ -146,7 +150,6 @@ func (t *Trie) loadNode(db Database, n *Node) error { } decodedNode.SetClean() - decodedNode.Encoding = encodedNode decodedNode.MerkleValue = nodeHash branch.Children[i] = decodedNode diff --git a/lib/trie/print_test.go b/lib/trie/print_test.go index d4ee6e89568..7db2ce40d76 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 fff06d54d44..3a1d78d1db5 100644 --- a/lib/trie/trie_test.go +++ b/lib/trie/trie_test.go @@ -286,7 +286,6 @@ func Test_Trie_registerDeletedNodeHash(t *testing.T) { 0x12, 0x93, 0xf1, 0xb2, 0x76, 0xea, 0x95, 0x05, 0xe9, 0xf4, 0xa4, 0xe7, 0xd9, 0x8f, 0x62, 0x0d, 0x05, 0x11, 0x5e, 0x0b, 0x85, 0x27, 0x4a, 0xe1}, - Encoding: []byte{0x41, 0x01, 0x04, 0x02}, }, }, }, @@ -447,35 +446,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, @@ -486,12 +473,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, @@ -506,21 +491,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}, }, }, } @@ -534,7 +512,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) @@ -544,14 +522,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) @@ -616,7 +586,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}, }, }, }, @@ -645,7 +614,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}, }, }), @@ -1268,7 +1236,6 @@ func Test_Trie_insert(t *testing.T) { { Key: []byte{2}, SubValue: []byte{1}, - Encoding: []byte{0x41, 0x02, 0x04, 0x01}, MerkleValue: []byte{0x41, 0x02, 0x04, 0x01}, }, }), @@ -2646,7 +2613,6 @@ func Test_Trie_clearPrefixLimitAtNode(t *testing.T) { { Key: []byte{4}, SubValue: []byte{1}, - Encoding: []byte{0x41, 0x04, 0x04, 0x01}, MerkleValue: []byte{0x41, 0x04, 0x04, 0x01}, }, }), @@ -2759,7 +2725,6 @@ func Test_Trie_clearPrefixLimitAtNode(t *testing.T) { Key: []byte{6}, SubValue: []byte{1}, // Not modified so same generation as before - Encoding: []byte{0x41, 0x06, 0x04, 0x01}, MerkleValue: []byte{0x41, 0x06, 0x04, 0x01}, }, }), @@ -2998,7 +2963,6 @@ func Test_Trie_deleteNodesLimit(t *testing.T) { { Key: []byte{2}, SubValue: []byte{1}, - Encoding: []byte{0x41, 0x02, 0x04, 0x01}, MerkleValue: []byte{0x41, 0x02, 0x04, 0x01}, }, }), @@ -3387,7 +3351,6 @@ func Test_Trie_clearPrefixAtNode(t *testing.T) { { Key: []byte{4}, SubValue: []byte{1}, - Encoding: []byte{0x41, 0x04, 0x04, 0x01}, MerkleValue: []byte{0x41, 0x04, 0x04, 0x01}, }, }), @@ -3580,7 +3543,6 @@ func Test_Trie_Delete(t *testing.T) { { Key: []byte{5}, SubValue: []byte{97}, - Encoding: []byte{0x41, 0x05, 0x04, 0x61}, MerkleValue: []byte{0x41, 0x05, 0x04, 0x61}, }, { // full key in nibbles 1, 2, 1, 6 @@ -3893,13 +3855,11 @@ func Test_Trie_deleteAtNode(t *testing.T) { { Key: []byte{2}, SubValue: []byte{1}, - Encoding: []byte{0x41, 0x02, 0x04, 0x01}, MerkleValue: []byte{0x41, 0x02, 0x04, 0x01}, }, { Key: []byte{2}, SubValue: []byte{1}, - Encoding: []byte{0x41, 0x02, 0x04, 0x01}, MerkleValue: []byte{0x41, 0x02, 0x04, 0x01}, }, }), @@ -4075,14 +4035,12 @@ func Test_Trie_handleDeletion(t *testing.T) { { Key: []byte{7}, SubValue: []byte{1}, - Encoding: []byte{0x41, 0x07, 0x04, 0x01}, MerkleValue: []byte{0x41, 0x07, 0x04, 0x01}, }, nil, { Key: []byte{8}, SubValue: []byte{1}, - Encoding: []byte{0x41, 0x08, 0x04, 0x01}, MerkleValue: []byte{0x41, 0x08, 0x04, 0x01}, }, }), @@ -4143,7 +4101,6 @@ func Test_ensureMerkleValueIsCalculated(t *testing.T) { expectedNode: &Node{ Key: []byte{1}, SubValue: []byte{2}, - Encoding: []byte{0x41, 0x01, 0x04, 0x02}, MerkleValue: []byte{ 0x60, 0x51, 0x6d, 0xb, 0xb6, 0xe1, 0xbb, 0xfb, 0x12, 0x93, 0xf1, 0xb2, 0x76, 0xea, 0x95, 0x5, @@ -4161,7 +4118,6 @@ func Test_ensureMerkleValueIsCalculated(t *testing.T) { expectedNode: &Node{ Key: []byte{1}, SubValue: []byte{2}, - Encoding: []byte{0x41, 0x01, 0x04, 0x02}, MerkleValue: []byte{ 0x60, 0x51, 0x6d, 0xb, 0xb6, 0xe1, 0xbb, 0xfb, 0x12, 0x93, 0xf1, 0xb2, 0x76, 0xea, 0x95, 0x5, @@ -4198,7 +4154,6 @@ func Test_ensureMerkleValueIsCalculated(t *testing.T) { expectedNode: &Node{ Key: []byte{1}, SubValue: []byte{2}, - Encoding: []byte{0x41, 0x01, 0x04, 0x02}, MerkleValue: []byte{0x41, 0x1, 0x4, 0x2}, }, },