From 9598892fb1aca7f8318c858e7c64745e4463c96c Mon Sep 17 00:00:00 2001 From: Quentin McGaw Date: Fri, 18 Nov 2022 17:23:03 +0000 Subject: [PATCH] Adjust minor comments and error wrappings --- internal/trie/node/README.md | 4 ++-- internal/trie/node/copy.go | 12 ++++++------ internal/trie/node/decode.go | 12 ++++++------ internal/trie/node/decode_test.go | 26 ++++++++++++------------- internal/trie/node/dirty.go | 2 +- internal/trie/node/encode.go | 6 +++--- internal/trie/node/encode_test.go | 20 +++++++++---------- internal/trie/node/node.go | 2 +- internal/trie/node/node_test.go | 32 +++++++++++++++---------------- lib/trie/print_test.go | 8 ++++---- 10 files changed, 62 insertions(+), 62 deletions(-) diff --git a/internal/trie/node/README.md b/internal/trie/node/README.md index cae00b0ff0..a42176636d 100644 --- a/internal/trie/node/README.md +++ b/internal/trie/node/README.md @@ -21,10 +21,10 @@ The header is then concatenated with the partial key of the node, encoded as Lit The remaining bytes appended depend on the node variant. -- For leaves, the SCALE-encoded leaf value is appended. +- For leaves, the SCALE-encoded leaf storage value is appended. - For branches, the following elements are concatenated in this order and appended to the previous header+partial key: - Children bitmap (2 bytes) - - SCALE-encoded node value + - SCALE-encoded node storage value - Hash(Encoding(Child[0])) - Hash(Encoding(Child[1])) - ... diff --git a/internal/trie/node/copy.go b/internal/trie/node/copy.go index ef1a9f9320..9fbd9088b7 100644 --- a/internal/trie/node/copy.go +++ b/internal/trie/node/copy.go @@ -9,7 +9,7 @@ var ( // - the HashDigest field is left empty on the copy // - the Encoding field is left empty on the copy // - the key field is deep copied - // - the value field is deep copied + // - the storage value field is deep copied DefaultCopySettings = CopySettings{ CopyKey: true, CopyStorageValue: true, @@ -20,7 +20,7 @@ var ( // - the HashDigest field is deep copied // - the Encoding field is deep copied // - the key field is deep copied - // - the value field is deep copied + // - the storage value field is deep copied DeepCopySettings = CopySettings{ CopyChildren: true, CopyCached: true, @@ -46,8 +46,8 @@ type CopySettings struct { // the node. This is useful when false if the key is about to // be assigned after the Copy operation, to save a memory operation. CopyKey bool - // CopyStorageValue can be set to true to deep copy the value field of - // the node. This is useful when false if the value is about to + // CopyStorageValue can be set to true to deep copy the storage value field of + // the node. This is useful when false if the storage value is about to // be assigned after the Copy operation, to save a memory operation. CopyStorageValue bool } @@ -87,8 +87,8 @@ func (n *Node) Copy(settings CopySettings) *Node { copy(cpy.PartialKey, n.PartialKey) } - // nil and []byte{} values for branches result in a different node encoding, - // so we ensure to keep the `nil` value. + // nil and []byte{} storage values for branches result in a different node encoding, + // so we ensure to keep the `nil` storage value. if settings.CopyStorageValue && n.StorageValue != nil { cpy.StorageValue = make([]byte, len(n.StorageValue)) copy(cpy.StorageValue, n.StorageValue) diff --git a/internal/trie/node/decode.go b/internal/trie/node/decode.go index c5d42c7948..bc12db565b 100644 --- a/internal/trie/node/decode.go +++ b/internal/trie/node/decode.go @@ -17,7 +17,7 @@ var ( // in the scale package. // TODO remove once the following issue is done: // https://github.com/ChainSafe/gossamer/issues/2631 . - ErrDecodeStorageValue = errors.New("cannot decode value") + ErrDecodeStorageValue = errors.New("cannot decode storage value") ErrReadChildrenBitmap = errors.New("cannot read children bitmap") // ErrDecodeChildHash is defined since no sentinel error is defined // in the scale package. @@ -62,7 +62,7 @@ func Decode(reader io.Reader) (n *Node, err error) { // Note that since the encoded branch stores the hash of the children nodes, we are not // reconstructing the child nodes from the encoding. This function instead stubs where the // children are known to be with an empty leaf. The children nodes hashes are then used to -// find other values using the persistent database. +// find other storage values using the persistent database. func decodeBranch(reader io.Reader, variant byte, partialKeyLength uint16) ( node *Node, err error) { node = &Node{ @@ -132,14 +132,14 @@ func decodeLeaf(reader io.Reader, partialKeyLength uint16) (node *Node, err erro } sd := scale.NewDecoder(reader) - var value []byte - err = sd.Decode(&value) + var storageValue []byte + err = sd.Decode(&storageValue) if err != nil && !errors.Is(err, io.EOF) { return nil, fmt.Errorf("%w: %s", ErrDecodeStorageValue, err) } - if len(value) > 0 { - node.StorageValue = value + if len(storageValue) > 0 { + node.StorageValue = storageValue } return node, nil diff --git a/internal/trie/node/decode_test.go b/internal/trie/node/decode_test.go index 8c2c37e105..8c4635914d 100644 --- a/internal/trie/node/decode_test.go +++ b/internal/trie/node/decode_test.go @@ -193,19 +193,19 @@ func Test_decodeBranch(t *testing.T) { concatByteSlices([][]byte{ {9}, // key data {0, 4}, // children bitmap - // missing encoded branch value + // missing encoded branch storage value }), ), variant: branchWithValueVariant.bits, partialKeyLength: 1, errWrapped: ErrDecodeStorageValue, - errMessage: "cannot decode value: reading byte: EOF", + errMessage: "cannot decode storage value: reading byte: EOF", }, "success for branch with value": { reader: bytes.NewBuffer(concatByteSlices([][]byte{ {9}, // key data {0, 4}, // children bitmap - scaleEncodeBytes(t, 7, 8, 9), // branch value + scaleEncodeBytes(t, 7, 8, 9), // branch storage value scaleEncodedChildHash, })), variant: branchWithValueVariant.bits, @@ -227,7 +227,7 @@ func Test_decodeBranch(t *testing.T) { reader: bytes.NewBuffer(concatByteSlices([][]byte{ {1}, // key data {0b0000_0001, 0b0000_0000}, // children bitmap - scaleEncodeBytes(t, 1), // branch value + scaleEncodeBytes(t, 1), // branch storage value {0}, // garbage inlined node })), variant: branchWithValueVariant.bits, @@ -244,19 +244,19 @@ func Test_decodeBranch(t *testing.T) { scaleEncodeByteSlice(t, concatByteSlices([][]byte{ {leafVariant.bits | 1}, // partial key length of 1 {2}, // key data - scaleEncodeBytes(t, 2), // value data + scaleEncodeBytes(t, 2), // storage value data })), // top level inlined branch less than 32 bytes scaleEncodeByteSlice(t, concatByteSlices([][]byte{ {branchWithValueVariant.bits | 1}, // partial key length of 1 {3}, // key data {0b0000_0001, 0b0000_0000}, // children bitmap - scaleEncodeBytes(t, 3), // branch value + scaleEncodeBytes(t, 3), // branch storage value // bottom level leaf scaleEncodeByteSlice(t, concatByteSlices([][]byte{ {leafVariant.bits | 1}, // partial key length of 1 {4}, // key data - scaleEncodeBytes(t, 4), // value data + scaleEncodeBytes(t, 4), // storage value data })), })), })), @@ -320,17 +320,17 @@ func Test_decodeLeaf(t *testing.T) { "value decoding error": { reader: bytes.NewBuffer([]byte{ 9, // key data - 255, 255, // bad value data + 255, 255, // bad storage value data }), variant: leafVariant.bits, partialKeyLength: 1, errWrapped: ErrDecodeStorageValue, - errMessage: "cannot decode value: unknown prefix for compact uint: 255", + errMessage: "cannot decode storage value: unknown prefix for compact uint: 255", }, - "missing value data": { + "missing storage value data": { reader: bytes.NewBuffer([]byte{ 9, // key data - // missing value data + // missing storage value data }), variant: leafVariant.bits, partialKeyLength: 1, @@ -338,7 +338,7 @@ func Test_decodeLeaf(t *testing.T) { PartialKey: []byte{9}, }, }, - "empty value data": { + "empty storage value data": { reader: bytes.NewBuffer(concatByteSlices([][]byte{ {9}, // key data scaleEncodeByteSlice(t, nil), @@ -353,7 +353,7 @@ func Test_decodeLeaf(t *testing.T) { reader: bytes.NewBuffer( concatByteSlices([][]byte{ {9}, // key data - scaleEncodeBytes(t, 1, 2, 3, 4, 5), // value data + scaleEncodeBytes(t, 1, 2, 3, 4, 5), // storage value data }), ), variant: leafVariant.bits, diff --git a/internal/trie/node/dirty.go b/internal/trie/node/dirty.go index 26dc2248b7..d41f779440 100644 --- a/internal/trie/node/dirty.go +++ b/internal/trie/node/dirty.go @@ -6,7 +6,7 @@ package node // SetDirty sets the dirty status to true for the node. func (n *Node) SetDirty() { n.Dirty = true - // A node is marked dirty if its key or value is modified. + // A node is marked dirty if its partial key or storage value is modified. // This means its Merkle value field is no longer valid. n.MerkleValue = nil } diff --git a/internal/trie/node/encode.go b/internal/trie/node/encode.go index 2c0143899b..c7a2e2c722 100644 --- a/internal/trie/node/encode.go +++ b/internal/trie/node/encode.go @@ -37,13 +37,13 @@ func (n *Node) Encode(buffer Buffer) (err error) { } } - // Only encode node value if the node is a leaf or - // the node is a branch with a non empty value. + // Only encode node storage value if the node is a leaf or + // the node is a branch with a non empty storage value. if !nodeIsBranch || (nodeIsBranch && n.StorageValue != nil) { encoder := scale.NewEncoder(buffer) err = encoder.Encode(n.StorageValue) if err != nil { - return fmt.Errorf("scale encoding value: %w", err) + return fmt.Errorf("scale encoding storage value: %w", err) } } diff --git a/internal/trie/node/encode_test.go b/internal/trie/node/encode_test.go index 69023a6b52..6ab5327714 100644 --- a/internal/trie/node/encode_test.go +++ b/internal/trie/node/encode_test.go @@ -60,7 +60,7 @@ func Test_Node_Encode(t *testing.T) { wrappedErr: errTest, errMessage: "cannot write LE key to buffer: test error", }, - "leaf buffer write error for encoded value": { + "leaf buffer write error for encoded storage value": { node: &Node{ PartialKey: []byte{1, 2, 3}, StorageValue: []byte{4, 5, 6}, @@ -78,7 +78,7 @@ func Test_Node_Encode(t *testing.T) { }, }, wrappedErr: errTest, - errMessage: "scale encoding value: test error", + errMessage: "scale encoding storage value: test error", }, "leaf success": { node: &Node{ @@ -95,15 +95,15 @@ func Test_Node_Encode(t *testing.T) { }, expectedEncoding: []byte{1, 2, 3}, }, - "leaf with empty value success": { + "leaf with empty storage value success": { node: &Node{ PartialKey: []byte{1, 2, 3}, }, writes: []writeCall{ {written: []byte{leafVariant.bits | 3}}, // partial key length 3 {written: []byte{0x01, 0x23}}, // partial key - {written: []byte{0}}, // node value encoded length - {written: nil}, // node value + {written: []byte{0}}, // node storage value encoded length + {written: nil}, // node storage value }, expectedEncoding: []byte{1, 2, 3}, }, @@ -163,7 +163,7 @@ func Test_Node_Encode(t *testing.T) { wrappedErr: errTest, errMessage: "cannot write children bitmap to buffer: test error", }, - "buffer write error for value": { + "buffer write error for storage value": { node: &Node{ PartialKey: []byte{1, 2, 3}, StorageValue: []byte{100}, @@ -182,13 +182,13 @@ func Test_Node_Encode(t *testing.T) { { // children bitmap written: []byte{136, 0}, }, - { // value + { // storage value written: []byte{4}, err: errTest, }, }, wrappedErr: errTest, - errMessage: "scale encoding value: test error", + errMessage: "scale encoding storage value: test error", }, "buffer write error for children encoding": { node: &Node{ @@ -209,7 +209,7 @@ func Test_Node_Encode(t *testing.T) { { // children bitmap written: []byte{136, 0}, }, - // value + // storage value {written: []byte{4}}, {written: []byte{100}}, { // children @@ -241,7 +241,7 @@ func Test_Node_Encode(t *testing.T) { { // children bitmap written: []byte{136, 0}, }, - // value + // storage value {written: []byte{4}}, {written: []byte{100}}, { // first children diff --git a/internal/trie/node/node.go b/internal/trie/node/node.go index 8c84d0d186..bcb8204457 100644 --- a/internal/trie/node/node.go +++ b/internal/trie/node/node.go @@ -58,7 +58,7 @@ func (n Node) StringNode() (stringNode *gotree.Node) { stringNode.Appendf("Generation: %d", n.Generation) stringNode.Appendf("Dirty: %t", n.Dirty) stringNode.Appendf("Key: " + bytesToString(n.PartialKey)) - stringNode.Appendf("Value: " + bytesToString(n.StorageValue)) + stringNode.Appendf("Storage value: " + bytesToString(n.StorageValue)) if n.Descendants > 0 { // must be a branch stringNode.Appendf("Descendants: %d", n.Descendants) } diff --git a/internal/trie/node/node_test.go b/internal/trie/node/node_test.go index 32b8983c98..b9c9ac47de 100644 --- a/internal/trie/node/node_test.go +++ b/internal/trie/node/node_test.go @@ -16,7 +16,7 @@ func Test_Node_String(t *testing.T) { node *Node s string }{ - "leaf with value smaller than 1024": { + "leaf with storage value smaller than 1024": { node: &Node{ PartialKey: []byte{1, 2}, StorageValue: []byte{3, 4}, @@ -26,10 +26,10 @@ func Test_Node_String(t *testing.T) { ├── Generation: 0 ├── Dirty: true ├── Key: 0x0102 -├── Value: 0x0304 +├── Storage value: 0x0304 └── Merkle value: nil`, }, - "leaf with value higher than 1024": { + "leaf with storage value higher than 1024": { node: &Node{ PartialKey: []byte{1, 2}, StorageValue: make([]byte, 1025), @@ -39,10 +39,10 @@ func Test_Node_String(t *testing.T) { ├── Generation: 0 ├── Dirty: true ├── Key: 0x0102 -├── Value: 0x0000000000000000...0000000000000000 +├── Storage value: 0x0000000000000000...0000000000000000 └── Merkle value: nil`, }, - "branch with value smaller than 1024": { + "branch with storage value smaller than 1024": { node: &Node{ PartialKey: []byte{1, 2}, StorageValue: []byte{3, 4}, @@ -65,7 +65,7 @@ func Test_Node_String(t *testing.T) { ├── Generation: 0 ├── Dirty: true ├── Key: 0x0102 -├── Value: 0x0304 +├── Storage value: 0x0304 ├── Descendants: 3 ├── Merkle value: nil ├── Child 3 @@ -73,14 +73,14 @@ func Test_Node_String(t *testing.T) { | ├── Generation: 0 | ├── Dirty: false | ├── Key: nil -| ├── Value: nil +| ├── Storage value: nil | └── Merkle value: nil ├── Child 7 | └── Branch | ├── Generation: 0 | ├── Dirty: false | ├── Key: nil -| ├── Value: nil +| ├── Storage value: nil | ├── Descendants: 1 | ├── Merkle value: nil | └── Child 0 @@ -88,17 +88,17 @@ func Test_Node_String(t *testing.T) { | ├── Generation: 0 | ├── Dirty: false | ├── Key: nil -| ├── Value: nil +| ├── Storage value: nil | └── Merkle value: nil └── Child 11 └── Leaf ├── Generation: 0 ├── Dirty: false ├── Key: nil - ├── Value: nil + ├── Storage value: nil └── Merkle value: nil`, }, - "branch with value higher than 1024": { + "branch with storage value higher than 1024": { node: &Node{ PartialKey: []byte{1, 2}, StorageValue: make([]byte, 1025), @@ -121,7 +121,7 @@ func Test_Node_String(t *testing.T) { ├── Generation: 0 ├── Dirty: true ├── Key: 0x0102 -├── Value: 0x0000000000000000...0000000000000000 +├── Storage value: 0x0000000000000000...0000000000000000 ├── Descendants: 3 ├── Merkle value: nil ├── Child 3 @@ -129,14 +129,14 @@ func Test_Node_String(t *testing.T) { | ├── Generation: 0 | ├── Dirty: false | ├── Key: nil -| ├── Value: nil +| ├── Storage value: nil | └── Merkle value: nil ├── Child 7 | └── Branch | ├── Generation: 0 | ├── Dirty: false | ├── Key: nil -| ├── Value: nil +| ├── Storage value: nil | ├── Descendants: 1 | ├── Merkle value: nil | └── Child 0 @@ -144,14 +144,14 @@ func Test_Node_String(t *testing.T) { | ├── Generation: 0 | ├── Dirty: false | ├── Key: nil -| ├── Value: nil +| ├── Storage value: nil | └── Merkle value: nil └── Child 11 └── Leaf ├── Generation: 0 ├── Dirty: false ├── Key: nil - ├── Value: nil + ├── Storage value: nil └── Merkle value: nil`, }, } diff --git a/lib/trie/print_test.go b/lib/trie/print_test.go index 770f530ef5..b0b4ad0975 100644 --- a/lib/trie/print_test.go +++ b/lib/trie/print_test.go @@ -31,7 +31,7 @@ func Test_Trie_String(t *testing.T) { ├── Generation: 1 ├── Dirty: false ├── Key: 0x010203 -├── Value: 0x030405 +├── Storage value: 0x030405 └── Merkle value: nil`, }, "branch root": { @@ -59,7 +59,7 @@ func Test_Trie_String(t *testing.T) { ├── Generation: 0 ├── Dirty: false ├── Key: nil -├── Value: 0x0102 +├── Storage value: 0x0102 ├── Descendants: 2 ├── Merkle value: nil ├── Child 0 @@ -67,14 +67,14 @@ func Test_Trie_String(t *testing.T) { | ├── Generation: 2 | ├── Dirty: false | ├── Key: 0x010203 -| ├── Value: 0x030405 +| ├── Storage value: 0x030405 | └── Merkle value: nil └── Child 3 └── Leaf ├── Generation: 3 ├── Dirty: false ├── Key: 0x010203 - ├── Value: 0x030405 + ├── Storage value: 0x030405 └── Merkle value: nil`, }, }