diff --git a/cairo_zero/kakarot/instructions/system_operations.cairo b/cairo_zero/kakarot/instructions/system_operations.cairo index 020ce57f0..fe797568c 100644 --- a/cairo_zero/kakarot/instructions/system_operations.cairo +++ b/cairo_zero/kakarot/instructions/system_operations.cairo @@ -113,7 +113,6 @@ namespace SystemOperations { let gas_limit = evm.gas_left - gas_limit; if (evm.message.read_only != FALSE) { - let evm = EVM.charge_gas(evm, gas_limit); let (revert_reason_len, revert_reason) = Errors.stateModificationError(); let evm = EVM.stop(evm, revert_reason_len, revert_reason, Errors.EXCEPTIONAL_HALT); return evm; diff --git a/cairo_zero/tests/src/kakarot/precompiles/test_precompiles.py b/cairo_zero/tests/src/kakarot/precompiles/test_precompiles.py index a4e212371..ac3cea87f 100644 --- a/cairo_zero/tests/src/kakarot/precompiles/test_precompiles.py +++ b/cairo_zero/tests/src/kakarot/precompiles/test_precompiles.py @@ -3,11 +3,9 @@ from tests.utils.constants import ( CAIRO_PRECOMPILE_GAS, - FIRST_KAKAROT_PRECOMPILE_ADDRESS, - FIRST_ROLLUP_PRECOMPILE_ADDRESS, - LAST_ETHEREUM_PRECOMPILE_ADDRESS, - LAST_KAKAROT_PRECOMPILE_ADDRESS, - LAST_ROLLUP_PRECOMPILE_ADDRESS, + ETHEREUM_PRECOMPILES, + KAKAROT_PRECOMPILES, + ROLLUP_PRECOMPILES, ) from tests.utils.syscall_handler import SyscallHandler @@ -207,38 +205,22 @@ def test__cairo_precompiles( return class TestIsPrecompile: - @pytest.mark.parametrize( - "address", range(0, LAST_ETHEREUM_PRECOMPILE_ADDRESS + 2) - ) + @pytest.mark.parametrize("address", range(0, ETHEREUM_PRECOMPILES[-1] + 2)) def test__is_precompile_ethereum_precompiles(self, cairo_run, address): result = cairo_run("test__is_precompile", address=address) - assert result == (address in range(1, LAST_ETHEREUM_PRECOMPILE_ADDRESS + 1)) + assert result == (address in ETHEREUM_PRECOMPILES) @pytest.mark.parametrize( "address", - range(FIRST_ROLLUP_PRECOMPILE_ADDRESS, LAST_ROLLUP_PRECOMPILE_ADDRESS + 2), + range(ROLLUP_PRECOMPILES[0], ROLLUP_PRECOMPILES[-1] + 2), ) def test__is_precompile_rollup_precompiles(self, cairo_run, address): result = cairo_run("test__is_precompile", address=address) - assert result == ( - address - in range( - FIRST_ROLLUP_PRECOMPILE_ADDRESS, LAST_ROLLUP_PRECOMPILE_ADDRESS + 1 - ) - ) + assert result == (address in ROLLUP_PRECOMPILES) @pytest.mark.parametrize( - "address", - range( - FIRST_KAKAROT_PRECOMPILE_ADDRESS, LAST_KAKAROT_PRECOMPILE_ADDRESS + 2 - ), + "address", range(KAKAROT_PRECOMPILES[0], KAKAROT_PRECOMPILES[-1] + 2) ) def test__is_precompile_kakarot_precompiles(self, cairo_run, address): result = cairo_run("test__is_precompile", address=address) - assert result == ( - address - in range( - FIRST_KAKAROT_PRECOMPILE_ADDRESS, - LAST_KAKAROT_PRECOMPILE_ADDRESS + 1, - ) - ) + assert result == (address in KAKAROT_PRECOMPILES) diff --git a/cairo_zero/tests/src/kakarot/test_state.py b/cairo_zero/tests/src/kakarot/test_state.py index fd1e43b10..d7dc44456 100644 --- a/cairo_zero/tests/src/kakarot/test_state.py +++ b/cairo_zero/tests/src/kakarot/test_state.py @@ -4,9 +4,8 @@ TX_ACCESS_LIST_ADDRESS_COST, TX_ACCESS_LIST_STORAGE_KEY_COST, ) -from web3 import Web3 -from tests.utils.constants import TRANSACTIONS +from tests.utils.constants import ETHEREUM_PRECOMPILES, TRANSACTIONS from tests.utils.helpers import flatten_tx_access_list, merge_access_list from tests.utils.syscall_handler import SyscallHandler @@ -89,9 +88,9 @@ class TestCachePreaccessedAddresses: @SyscallHandler.patch("IERC20.balanceOf", lambda *_: [0, 1]) def test_should_cache_precompiles(self, cairo_run): state = cairo_run("test__cache_precompiles") - assert list(map(Web3.to_checksum_address, state["accounts"].keys())) == [ - Web3.to_checksum_address(f"0x{i:040x}") for i in range(1, 11) - ] + assert [ + int(address, 16) for address in state["accounts"].keys() + ] == ETHEREUM_PRECOMPILES @SyscallHandler.patch("IERC20.balanceOf", lambda *_: [0, 1]) @pytest.mark.parametrize("transaction", TRANSACTIONS) diff --git a/kakarot_scripts/deployment/evm_deployments.py b/kakarot_scripts/deployment/evm_deployments.py index fef72eafa..19c74d752 100644 --- a/kakarot_scripts/deployment/evm_deployments.py +++ b/kakarot_scripts/deployment/evm_deployments.py @@ -7,6 +7,7 @@ from kakarot_scripts.utils.kakarot import deploy as deploy_evm from kakarot_scripts.utils.kakarot import deploy_and_fund_evm_address from kakarot_scripts.utils.kakarot import dump_deployments as dump_evm_deployments +from kakarot_scripts.utils.kakarot import fund_address from kakarot_scripts.utils.kakarot import get_deployments as get_evm_deployments from kakarot_scripts.utils.starknet import call, call_contract, execute_calls from kakarot_scripts.utils.starknet import get_deployments as get_starknet_deployments @@ -16,6 +17,7 @@ register_lazy_account, remove_lazy_account, ) +from tests.utils.constants import ALL_PRECOMPILES logger = logging.getLogger(__name__) logger.setLevel(logging.INFO) @@ -64,6 +66,10 @@ async def deploy_evm_contracts(): } await invoke("kakarot", "set_coinbase", int(contract.address, 16)) + # %% Pre-fund precompiles + # see https://github.com/ethereum/go-ethereum/blob/5230b06d5151e214e80762eebed9196a670c52b1/core/vm/instructions.go#L404 + for precompile in ALL_PRECOMPILES: + await fund_address(precompile, 1 / 1e18) # %% Tear down dump_evm_deployments(evm_deployments) diff --git a/kakarot_scripts/deployment/main.py b/kakarot_scripts/deployment/main.py index 4891aef9d..2f8f18e8f 100644 --- a/kakarot_scripts/deployment/main.py +++ b/kakarot_scripts/deployment/main.py @@ -17,6 +17,7 @@ deploy_and_fund_evm_address, eth_balance_of, get_contract, + get_starknet_address, ) from kakarot_scripts.utils.starknet import ( call, @@ -26,6 +27,7 @@ register_lazy_account, remove_lazy_account, ) +from tests.utils.constants import ALL_PRECOMPILES logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") logger = logging.getLogger(__name__) @@ -92,6 +94,14 @@ async def main(): f"💰 EVM deployer balance:\n L2: {l2_balance} ETH\n L1: {l1_balance} ETH" ) + # check precompiles received funds + for precompile in ALL_PRECOMPILES: + starknet_address = await get_starknet_address(precompile) + balance = await get_balance(starknet_address) + assert ( + balance > 0 + ), f"Failed to fund precompile {precompile} starknet address {starknet_address}" + # %% diff --git a/tests/utils/constants.py b/tests/utils/constants.py index 93600322a..a510e9346 100644 --- a/tests/utils/constants.py +++ b/tests/utils/constants.py @@ -2,6 +2,7 @@ from time import time import pytest +from ethereum.cancun.vm.precompiled_contracts.mapping import PRE_COMPILED_CONTRACTS from kakarot_scripts.constants import BLOCK_GAS_LIMIT @@ -26,11 +27,12 @@ TRANSACTION_GAS_LIMIT = BLOCK_GAS_LIMIT # PRECOMPILES -LAST_ETHEREUM_PRECOMPILE_ADDRESS = 0x0A -FIRST_ROLLUP_PRECOMPILE_ADDRESS = 0x100 -LAST_ROLLUP_PRECOMPILE_ADDRESS = 0x100 -FIRST_KAKAROT_PRECOMPILE_ADDRESS = 0x75001 -LAST_KAKAROT_PRECOMPILE_ADDRESS = 0x75004 +ETHEREUM_PRECOMPILES = [ + int.from_bytes(address, "big") for address in PRE_COMPILED_CONTRACTS.keys() +] +ROLLUP_PRECOMPILES = [0x100] +KAKAROT_PRECOMPILES = [0x75001, 0x75002, 0x75003, 0x75004] +ALL_PRECOMPILES = [*ETHEREUM_PRECOMPILES, *ROLLUP_PRECOMPILES, *KAKAROT_PRECOMPILES] CAIRO_PRECOMPILE_GAS = 10000