diff --git a/kakarot_scripts/constants.py b/kakarot_scripts/constants.py index 61dfa7029..8f9408c6e 100644 --- a/kakarot_scripts/constants.py +++ b/kakarot_scripts/constants.py @@ -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): @@ -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, @@ -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): diff --git a/src/kakarot/constants.cairo b/src/kakarot/constants.cairo index 34131f538..c38f29ab2 100644 --- a/src/kakarot/constants.cairo +++ b/src/kakarot/constants.cairo @@ -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: diff --git a/src/kakarot/library.cairo b/src/kakarot/library.cairo index d1e75bc58..acc3e0984 100644 --- a/src/kakarot/library.cairo +++ b/src/kakarot/library.cairo @@ -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, @@ -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); } diff --git a/tests/src/kakarot/test_kakarot.py b/tests/src/kakarot/test_kakarot.py index 0ba9d54c6..6cd1328dd 100644 --- a/tests/src/kakarot/test_kakarot.py +++ b/tests/src/kakarot/test_kakarot.py @@ -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 @@ -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) diff --git a/tests/utils/constants.py b/tests/utils/constants.py index 7807302b2..bdab8bf09 100644 --- a/tests/utils/constants.py +++ b/tests/utils/constants.py @@ -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