Skip to content

Commit

Permalink
[KGA-65] fix: decode legacy chain id overflow (#1581)
Browse files Browse the repository at this point in the history
<!--- Please provide a general summary of your changes in the title
above -->

<!-- Give an estimate of the time you spent on this PR in terms of work
days.
Did you spend 0.5 days on this PR or rather 2 days?  -->

Time spent on this PR:

## Pull request type

<!-- Please try to limit your pull request to one type,
submit multiple pull requests if needed. -->

Please check the type of change your PR introduces:

- [x] Bugfix
- [ ] Feature
- [ ] Code style update (formatting, renaming)
- [ ] Refactoring (no functional changes, no api changes)
- [ ] Build related changes
- [ ] Documentation content changes
- [ ] Other (please describe):

## What is the current behavior?

<!-- Please describe the current behavior that you are modifying,
or link to a relevant issue. -->

Resolves #<Issue number>

## What is the new behavior?

<!-- Please describe the behavior or changes that are being added by
this PR. -->

Assert the chain id does not overflow for legacy post eip 155 tx

<!-- Reviewable:start -->
- - -
This change is [<img src="https://reviewable.io/review_button.svg"
height="34" align="absmiddle"
alt="Reviewable"/>](https://reviewable.io/reviews/kkrt-labs/kakarot/1581)
<!-- Reviewable:end -->
  • Loading branch information
obatirou authored Nov 6, 2024
1 parent b8853b4 commit 158ff0c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
16 changes: 16 additions & 0 deletions cairo_zero/tests/src/utils/test_eth_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@ async def test_should_raise_with_list_items(self, cairo_run):
with cairo_error():
cairo_run("test__decode", data=list(encode(list(transaction.values()))))

async def test_should_raise_if_chain_id_overflow_legacy_transaction(
self, cairo_run
):
transaction = {
"nonce": 0,
"gasPrice": 234567897654321,
"gas": 2_000_000,
"to": "0xF0109fC8DF283027b6285cc889F5aA624EaC1F55",
"value": 1_000_000_000,
"data": b"",
"chainId": 2**252,
}
with cairo_error(message="assert_nn(31 - items[6].data_len);"):
encoded_unsigned_tx = rlp_encode_signed_data(transaction)
cairo_run("test__decode", data=list(encoded_unsigned_tx))

@given(value=st.integers(min_value=2**248))
@pytest.mark.parametrize("transaction", TRANSACTIONS)
@pytest.mark.parametrize(
Expand Down
5 changes: 5 additions & 0 deletions cairo_zero/utils/eth_transaction.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,23 @@ namespace EthTransaction {

// pre eip-155 txs have 6 fields, post eip-155 txs have 9 fields
if (items_len == 6) {
tempvar range_check_ptr = range_check_ptr;
tempvar is_some = 0;
tempvar chain_id = 0;
} else {
assert items_len = 9;
assert items[6].is_list = FALSE;
assert items[7].is_list = FALSE;
assert items[8].is_list = FALSE;

assert_nn(31 - items[6].data_len);
let chain_id = Helpers.bytes_to_felt(items[6].data_len, items[6].data);

tempvar range_check_ptr = range_check_ptr;
tempvar is_some = 1;
tempvar chain_id = chain_id;
}
let range_check_ptr = [ap - 3];
let is_some = [ap - 2];
let chain_id = [ap - 1];

Expand Down

0 comments on commit 158ff0c

Please sign in to comment.