Skip to content

Commit

Permalink
fix: use MAX_SAFE_CHAIN_ID (#1472)
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:

<!-- 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):

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

Resolves #<Issue number>

Use MAX_SAFE_CHAIN_ID for cairo / cairo-zero
See https://gist.github.com/rekmarks/a47bd5f2525936c4b8eee31a16345553

<!-- 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/1472)
<!-- Reviewable:end -->
  • Loading branch information
obatirou committed Oct 8, 2024
1 parent 038b3a3 commit 2473e6b
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 7 deletions.
6 changes: 4 additions & 2 deletions kakarot_scripts/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
BLOCK_GAS_LIMIT = 7_000_000
DEFAULT_GAS_PRICE = 1
BEACON_ROOT_ADDRESS = "0x000F3df6D732807Ef1319fB7B8bB8522d0Beac02"
# see https://gist.github.com/rekmarks/a47bd5f2525936c4b8eee31a16345553
MAX_SAFE_CHAIN_ID = 4503599627370476


class NetworkType(Enum):
Expand Down Expand Up @@ -192,7 +194,7 @@ class NetworkType(Enum):
if WEB3.is_connected():
chain_id = WEB3.eth.chain_id
else:
chain_id = starknet_chain_id % 2**53
chain_id = starknet_chain_id % MAX_SAFE_CHAIN_ID
except (
requests.exceptions.ConnectionError,
requests.exceptions.MissingSchema,
Expand All @@ -202,7 +204,7 @@ class NetworkType(Enum):
f"⚠️ Could not get chain Id from {NETWORK['rpc_url']}: {e}, defaulting to KKRT"
)
starknet_chain_id = int.from_bytes(b"KKRT", "big")
chain_id = starknet_chain_id % 2**53
chain_id = starknet_chain_id % MAX_SAFE_CHAIN_ID


class ChainId(IntEnum):
Expand Down
3 changes: 3 additions & 0 deletions src/kakarot/constants.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ namespace Constants {
const EMPTY_CODE_HASH_LOW = 0xe500b653ca82273b7bfad8045d85a470;
const EMPTY_CODE_HASH_HIGH = 0xc5d2460186f7233c927e7db2dcc703c0;
const BURN_ADDRESS = 0xdead;

// See https://gist.github.com/rekmarks/a47bd5f2525936c4b8eee31a16345553
const MAX_SAFE_CHAIN_ID = 4503599627370476;
}

// See model.Opcode:
Expand Down
3 changes: 2 additions & 1 deletion src/kakarot/library.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ from starkware.cairo.common.math import split_felt

from backend.starknet import Starknet
from kakarot.account import Account
from kakarot.constants import Constants
from kakarot.storages import (
Kakarot_uninitialized_account_class_hash,
Kakarot_account_contract_class_hash,
Expand Down Expand Up @@ -125,7 +126,7 @@ namespace Kakarot {
chain_id: felt
) {
let (tx_info) = get_tx_info();
let (_, chain_id) = unsigned_div_rem(tx_info.chain_id, 2 ** 53);
let (_, chain_id) = unsigned_div_rem(tx_info.chain_id, Constants.MAX_SAFE_CHAIN_ID);
return (chain_id=chain_id);
}

Expand Down
13 changes: 10 additions & 3 deletions tests/src/kakarot/test_kakarot.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@
from web3.exceptions import NoABIFunctionsFound

from kakarot_scripts.ef_tests.fetch import EF_TESTS_PARSED_DIR
from tests.utils.constants import CHAIN_ID, TRANSACTION_GAS_LIMIT, TRANSACTIONS
from tests.utils.constants import (
CHAIN_ID,
MAX_SAFE_CHAIN_ID,
TRANSACTION_GAS_LIMIT,
TRANSACTIONS,
)
from tests.utils.errors import cairo_error
from tests.utils.helpers import felt_to_signed_int, rlp_encode_signed_data
from tests.utils.syscall_handler import SyscallHandler, parse_state
Expand Down Expand Up @@ -494,10 +499,12 @@ def test_failing_contract(self, cairo_run):

class TestEthChainIdEntrypoint:
@given(chain_id=integers(min_value=0, max_value=2**64 - 1))
def test_should_return_chain_id_modulo_53(self, cairo_run, chain_id):
def test_should_return_chain_id_modulo_max_safe_chain_id(
self, cairo_run, chain_id
):
with patch.dict(SyscallHandler.tx_info, {"chain_id": chain_id}):
res = cairo_run("test__eth_chain_id")
assert res == chain_id % 2**53
assert res == chain_id % MAX_SAFE_CHAIN_ID

class TestEthSendRawTransactionEntrypoint:
@SyscallHandler.patch("Pausable_paused", 1)
Expand Down
3 changes: 2 additions & 1 deletion tests/utils/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@

import pytest

from kakarot_scripts.constants import BLOCK_GAS_LIMIT
from kakarot_scripts.constants import BLOCK_GAS_LIMIT, MAX_SAFE_CHAIN_ID

BLOCK_GAS_LIMIT = BLOCK_GAS_LIMIT
MIN_BASE_FEE_PER_BLOB_GAS = 1

CHAIN_ID = int.from_bytes(b"KKRT", "big") # KKRT (0x4b4b5254) in ASCII
BIG_CHAIN_ID = int.from_bytes(b"SN_SEPOLIA", "big")
MAX_SAFE_CHAIN_ID = MAX_SAFE_CHAIN_ID

# Class hash of the cairo1 helpers
CAIRO1_HELPERS_CLASS_HASH = 0xDEADBEEFABDE1E11A5
Expand Down

0 comments on commit 2473e6b

Please sign in to comment.