Skip to content
This repository was archived by the owner on Jan 9, 2025. It is now read-only.

Commit 2473e6b

Browse files
committed
fix: use MAX_SAFE_CHAIN_ID (#1472)
<!--- 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 -->
1 parent 038b3a3 commit 2473e6b

File tree

5 files changed

+21
-7
lines changed

5 files changed

+21
-7
lines changed

kakarot_scripts/constants.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
BLOCK_GAS_LIMIT = 7_000_000
2323
DEFAULT_GAS_PRICE = 1
2424
BEACON_ROOT_ADDRESS = "0x000F3df6D732807Ef1319fB7B8bB8522d0Beac02"
25+
# see https://gist.github.com/rekmarks/a47bd5f2525936c4b8eee31a16345553
26+
MAX_SAFE_CHAIN_ID = 4503599627370476
2527

2628

2729
class NetworkType(Enum):
@@ -192,7 +194,7 @@ class NetworkType(Enum):
192194
if WEB3.is_connected():
193195
chain_id = WEB3.eth.chain_id
194196
else:
195-
chain_id = starknet_chain_id % 2**53
197+
chain_id = starknet_chain_id % MAX_SAFE_CHAIN_ID
196198
except (
197199
requests.exceptions.ConnectionError,
198200
requests.exceptions.MissingSchema,
@@ -202,7 +204,7 @@ class NetworkType(Enum):
202204
f"⚠️ Could not get chain Id from {NETWORK['rpc_url']}: {e}, defaulting to KKRT"
203205
)
204206
starknet_chain_id = int.from_bytes(b"KKRT", "big")
205-
chain_id = starknet_chain_id % 2**53
207+
chain_id = starknet_chain_id % MAX_SAFE_CHAIN_ID
206208

207209

208210
class ChainId(IntEnum):

src/kakarot/constants.cairo

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ namespace Constants {
2222
const EMPTY_CODE_HASH_LOW = 0xe500b653ca82273b7bfad8045d85a470;
2323
const EMPTY_CODE_HASH_HIGH = 0xc5d2460186f7233c927e7db2dcc703c0;
2424
const BURN_ADDRESS = 0xdead;
25+
26+
// See https://gist.github.com/rekmarks/a47bd5f2525936c4b8eee31a16345553
27+
const MAX_SAFE_CHAIN_ID = 4503599627370476;
2528
}
2629

2730
// See model.Opcode:

src/kakarot/library.cairo

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ from starkware.cairo.common.math import split_felt
1313

1414
from backend.starknet import Starknet
1515
from kakarot.account import Account
16+
from kakarot.constants import Constants
1617
from kakarot.storages import (
1718
Kakarot_uninitialized_account_class_hash,
1819
Kakarot_account_contract_class_hash,
@@ -125,7 +126,7 @@ namespace Kakarot {
125126
chain_id: felt
126127
) {
127128
let (tx_info) = get_tx_info();
128-
let (_, chain_id) = unsigned_div_rem(tx_info.chain_id, 2 ** 53);
129+
let (_, chain_id) = unsigned_div_rem(tx_info.chain_id, Constants.MAX_SAFE_CHAIN_ID);
129130
return (chain_id=chain_id);
130131
}
131132

tests/src/kakarot/test_kakarot.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@
1717
from web3.exceptions import NoABIFunctionsFound
1818

1919
from kakarot_scripts.ef_tests.fetch import EF_TESTS_PARSED_DIR
20-
from tests.utils.constants import CHAIN_ID, TRANSACTION_GAS_LIMIT, TRANSACTIONS
20+
from tests.utils.constants import (
21+
CHAIN_ID,
22+
MAX_SAFE_CHAIN_ID,
23+
TRANSACTION_GAS_LIMIT,
24+
TRANSACTIONS,
25+
)
2126
from tests.utils.errors import cairo_error
2227
from tests.utils.helpers import felt_to_signed_int, rlp_encode_signed_data
2328
from tests.utils.syscall_handler import SyscallHandler, parse_state
@@ -494,10 +499,12 @@ def test_failing_contract(self, cairo_run):
494499

495500
class TestEthChainIdEntrypoint:
496501
@given(chain_id=integers(min_value=0, max_value=2**64 - 1))
497-
def test_should_return_chain_id_modulo_53(self, cairo_run, chain_id):
502+
def test_should_return_chain_id_modulo_max_safe_chain_id(
503+
self, cairo_run, chain_id
504+
):
498505
with patch.dict(SyscallHandler.tx_info, {"chain_id": chain_id}):
499506
res = cairo_run("test__eth_chain_id")
500-
assert res == chain_id % 2**53
507+
assert res == chain_id % MAX_SAFE_CHAIN_ID
501508

502509
class TestEthSendRawTransactionEntrypoint:
503510
@SyscallHandler.patch("Pausable_paused", 1)

tests/utils/constants.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33

44
import pytest
55

6-
from kakarot_scripts.constants import BLOCK_GAS_LIMIT
6+
from kakarot_scripts.constants import BLOCK_GAS_LIMIT, MAX_SAFE_CHAIN_ID
77

88
BLOCK_GAS_LIMIT = BLOCK_GAS_LIMIT
99
MIN_BASE_FEE_PER_BLOB_GAS = 1
1010

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

1415
# Class hash of the cairo1 helpers
1516
CAIRO1_HELPERS_CLASS_HASH = 0xDEADBEEFABDE1E11A5

0 commit comments

Comments
 (0)