Skip to content

Commit

Permalink
bug: RLP::decode() should return an empty string instead of "0" when …
Browse files Browse the repository at this point in the history
…decoding 0x80 (#539)

* corrected decode

* corrected decode default value and test_data

* set test_data as they were + added empty string parsing to 0 for integers

* boyscouted check on payload too long

* removed payloadtoolong error and comment
  • Loading branch information
Quentash authored Nov 23, 2023
1 parent 6262325 commit 876ccef
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
11 changes: 9 additions & 2 deletions crates/utils/src/rlp.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,8 @@ impl RLPImpl of RLPTrait {

match rlp_type {
RLPType::String => {
// checking for default value `0`
if (len == 0) {
output.append(RLPItem::String(array![0].span()));
output.append(RLPItem::String(array![].span()));
} else {
output.append(RLPItem::String(input.slice(offset, len)));
}
Expand Down Expand Up @@ -192,6 +191,10 @@ impl RLPHelpersImpl of RLPHelpersTrait {
fn parse_u128_from_string(self: RLPItem) -> Result<u128, RLPHelpersError> {
match self {
RLPItem::String(bytes) => {
// Empty strings means 0
if bytes.len() == 0 {
return Result::Ok(0);
}
let value = U128Impl::from_bytes(bytes).ok_or(RLPHelpersError::FailedParsingU128)?;
Result::Ok(value)
},
Expand All @@ -202,6 +205,10 @@ impl RLPHelpersImpl of RLPHelpersTrait {
fn parse_u256_from_string(self: RLPItem) -> Result<u256, RLPHelpersError> {
match self {
RLPItem::String(bytes) => {
// Empty strings means 0
if bytes.len() == 0 {
return Result::Ok(0);
}
let value = U256Impl::from_bytes(bytes).ok_or(RLPHelpersError::FailedParsingU256)?;
Result::Ok(value)
},
Expand Down
6 changes: 3 additions & 3 deletions crates/utils/src/tests/test_data.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fn legacy_rlp_encoded_tx() -> Span<u8> {
239,
1,
128,
128
128,
]
.span()
}
Expand Down Expand Up @@ -110,7 +110,7 @@ fn eip_2930_encoded_tx() -> Span<u8> {
171,
205,
239,
192
192,
]
.span()
}
Expand Down Expand Up @@ -169,7 +169,7 @@ fn eip_1559_encoded_tx() -> Span<u8> {
171,
205,
239,
192
192,
]
.span()
}
4 changes: 2 additions & 2 deletions crates/utils/src/tests/test_rlp.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ fn test_rlp_decode_string_default_value() {
let mut arr = array![0x80];

let rlp_item = RLPTrait::decode(arr.span()).unwrap();
let expected = RLPItem::String(array![0].span());
let expected = RLPItem::String(array![].span());

assert(rlp_item.len() == 1, 'item length not 1');
assert(*rlp_item[0] == expected, 'default value not 0');
Expand Down Expand Up @@ -2121,7 +2121,7 @@ fn test_rlp_decode_long_list() {
.span()
);

let mut expected_16 = RLPItem::String(array![0].span());
let mut expected_16 = RLPItem::String(array![].span());

let mut expected = array![
expected_0,
Expand Down

0 comments on commit 876ccef

Please sign in to comment.