Skip to content

Commit

Permalink
[KGA-97] Qa report 46 (#1637)
Browse files Browse the repository at this point in the history
  • Loading branch information
obatirou authored Nov 25, 2024
1 parent a8072d8 commit 59a41f9
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 38 deletions.
1 change: 0 additions & 1 deletion cairo_zero/kakarot/instructions/system_operations.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
36 changes: 9 additions & 27 deletions cairo_zero/tests/src/kakarot/precompiles/test_precompiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
9 changes: 4 additions & 5 deletions cairo_zero/tests/src/kakarot/test_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions kakarot_scripts/deployment/evm_deployments.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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)

Expand Down
10 changes: 10 additions & 0 deletions kakarot_scripts/deployment/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
deploy_and_fund_evm_address,
eth_balance_of,
get_contract,
get_starknet_address,
)
from kakarot_scripts.utils.starknet import (
call,
Expand All @@ -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__)
Expand Down Expand Up @@ -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}"


# %%

Expand Down
12 changes: 7 additions & 5 deletions tests/utils/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down

0 comments on commit 59a41f9

Please sign in to comment.