Skip to content

Commit

Permalink
fix(trie): Improve error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
dimartiro committed Jun 21, 2023
1 parent 97ed6ef commit 1f370f2
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 8 deletions.
6 changes: 3 additions & 3 deletions internal/trie/node/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions internal/trie/node/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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",
},
}

Expand Down
2 changes: 1 addition & 1 deletion internal/trie/node/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions internal/trie/node/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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",
},
}

Expand Down

0 comments on commit 1f370f2

Please sign in to comment.