From 1f370f27a4c26476790ec15530f75046b8cf0a38 Mon Sep 17 00:00:00 2001 From: Diego Date: Tue, 20 Jun 2023 22:58:15 -0300 Subject: [PATCH] fix(trie): Improve error messages --- internal/trie/node/decode.go | 6 +++--- internal/trie/node/decode_test.go | 4 ++-- internal/trie/node/encode.go | 2 +- internal/trie/node/encode_test.go | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/internal/trie/node/decode.go b/internal/trie/node/decode.go index d3afae2cd40..ec57dd4c6ba 100644 --- a/internal/trie/node/decode.go +++ b/internal/trie/node/decode.go @@ -146,10 +146,10 @@ func decodeLeaf(reader io.Reader, variant variant, partialKeyLength uint16) (nod } node.StorageValue = hashedValue node.HashedValue = true - } else { - err = sd.Decode(&node.StorageValue) + return node, nil } + err = sd.Decode(&node.StorageValue) if err != nil { return nil, fmt.Errorf("%w: %s", ErrDecodeStorageValue, err) } @@ -164,7 +164,7 @@ func decodeHashedValue(reader io.Reader) ([]byte, error) { return nil, fmt.Errorf("%w: %s", ErrDecodeStorageValue, err) } if n < common.HashLength { - return nil, ErrDecodeHashedValueTooShort + return nil, fmt.Errorf("%w: expected %d, got: %d", ErrDecodeHashedValueTooShort, common.HashLength, n) } return buffer, nil diff --git a/internal/trie/node/decode_test.go b/internal/trie/node/decode_test.go index 5dc8b2f3002..56983161b71 100644 --- a/internal/trie/node/decode_test.go +++ b/internal/trie/node/decode_test.go @@ -121,7 +121,7 @@ func Test_Decode(t *testing.T) { {0b0000_0000}, // less than 32bytes })), errWrapped: ErrDecodeHashedValueTooShort, - errMessage: "cannot decode leaf: hashed storage value too short", + errMessage: "cannot decode leaf: hashed storage value too short: expected 32, got: 1", }, "branch_with_hashed_value_success": { reader: bytes.NewReader(concatByteSlices([][]byte{ @@ -145,7 +145,7 @@ func Test_Decode(t *testing.T) { {0b0000_0000}, })), errWrapped: ErrDecodeHashedValueTooShort, - errMessage: "cannot decode branch: hashed storage value too short", + errMessage: "cannot decode branch: hashed storage value too short: expected 32, got: 1", }, } diff --git a/internal/trie/node/encode.go b/internal/trie/node/encode.go index 29336dd3e61..90eb7b7d940 100644 --- a/internal/trie/node/encode.go +++ b/internal/trie/node/encode.go @@ -51,7 +51,7 @@ func (n *Node) Encode(buffer Buffer) (err error) { if n.StorageValue != nil { if n.HashedValue { if len(n.StorageValue) != common.HashLength { - return ErrEncodeHashedValueTooShort + return fmt.Errorf("%w: expected %d, got: %d", ErrEncodeHashedValueTooShort, common.HashLength, len(n.StorageValue)) } _, err := buffer.Write(n.StorageValue) if err != nil { diff --git a/internal/trie/node/encode_test.go b/internal/trie/node/encode_test.go index c7f3fbe8140..53707e5a3f0 100644 --- a/internal/trie/node/encode_test.go +++ b/internal/trie/node/encode_test.go @@ -165,7 +165,7 @@ func Test_Node_Encode(t *testing.T) { {written: []byte{0x01, 0x23}}, }, wrappedErr: ErrEncodeHashedValueTooShort, - errMessage: "hashed storage value too short", + errMessage: "hashed storage value too short: expected 32, got: 8", }, "branch_header_encoding_error": { node: &Node{ @@ -391,7 +391,7 @@ func Test_Node_Encode(t *testing.T) { }, }, wrappedErr: ErrEncodeHashedValueTooShort, - errMessage: "hashed storage value too short", + errMessage: "hashed storage value too short: expected 32, got: 8", }, }