From 2b622e09f2754ae18545341a0cdf14695a5aeeeb Mon Sep 17 00:00:00 2001 From: Oba Date: Mon, 8 Jul 2024 15:00:03 +0200 Subject: [PATCH 1/5] dev: loop profiling --- .../src/PlainOpcodes/PlainOpcodes.sol | 8 ++++++++ tests/src/kakarot/test_kakarot.py | 20 +++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/solidity_contracts/src/PlainOpcodes/PlainOpcodes.sol b/solidity_contracts/src/PlainOpcodes/PlainOpcodes.sol index cd49b0f16..30d4925e6 100644 --- a/solidity_contracts/src/PlainOpcodes/PlainOpcodes.sol +++ b/solidity_contracts/src/PlainOpcodes/PlainOpcodes.sol @@ -207,6 +207,14 @@ contract PlainOpcodes { return hash; } + function loopProfiling(uint256 max) public pure returns (uint256) { + uint256 val = 0; + for (uint256 i = 0; i < max; i++) { + val += i; + } + return val; + } + receive() external payable {} fallback() external payable {} } diff --git a/tests/src/kakarot/test_kakarot.py b/tests/src/kakarot/test_kakarot.py index f6e7db151..ad216128b 100644 --- a/tests/src/kakarot/test_kakarot.py +++ b/tests/src/kakarot/test_kakarot.py @@ -385,3 +385,23 @@ async def test_failing_contract(self, cairo_run): data="0xADD_DATA", ) assert not evm["reverted"] + + class TestLoopProfiling: + @pytest.mark.slow + @SyscallHandler.patch( + "IAccount.is_valid_jumpdest", + lambda addr, data: [1], + ) + def test_loop_profiling(self, get_contract): + plain_opcodes = get_contract("PlainOpcodes", "PlainOpcodes") + initial_state = { + CONTRACT_ADDRESS: { + "code": list(plain_opcodes.bytecode_runtime), + "storage": {}, + "balance": 0, + "nonce": 0, + } + } + with SyscallHandler.patch_state(parse_state(initial_state)): + res = plain_opcodes.loopProfiling(10) + assert res == 45 From d65cb0d0ae80552c754a5ba826c470da25ebaea9 Mon Sep 17 00:00:00 2001 From: Oba Date: Mon, 8 Jul 2024 17:51:22 +0200 Subject: [PATCH 2/5] test: parametrize test_loop_profiling --- tests/src/kakarot/test_kakarot.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/src/kakarot/test_kakarot.py b/tests/src/kakarot/test_kakarot.py index ad216128b..8f3d34443 100644 --- a/tests/src/kakarot/test_kakarot.py +++ b/tests/src/kakarot/test_kakarot.py @@ -388,11 +388,12 @@ async def test_failing_contract(self, cairo_run): class TestLoopProfiling: @pytest.mark.slow + @pytest.mark.parametrize("max", [10, 50, 100, 200]) @SyscallHandler.patch( "IAccount.is_valid_jumpdest", lambda addr, data: [1], ) - def test_loop_profiling(self, get_contract): + def test_loop_profiling(self, get_contract, max): plain_opcodes = get_contract("PlainOpcodes", "PlainOpcodes") initial_state = { CONTRACT_ADDRESS: { @@ -403,5 +404,5 @@ def test_loop_profiling(self, get_contract): } } with SyscallHandler.patch_state(parse_state(initial_state)): - res = plain_opcodes.loopProfiling(10) - assert res == 45 + res = plain_opcodes.loopProfiling(max) + assert res == sum(x for x in range(max)) From e836889051b1a06cff334edcb5e835c04c6a1e14 Mon Sep 17 00:00:00 2001 From: Oba Date: Tue, 9 Jul 2024 09:40:06 +0200 Subject: [PATCH 3/5] test: increase step limit --- tests/fixtures/starknet.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/fixtures/starknet.py b/tests/fixtures/starknet.py index 25cfdf0e3..385741613 100644 --- a/tests/fixtures/starknet.py +++ b/tests/fixtures/starknet.py @@ -179,7 +179,7 @@ def _factory(entrypoint, **kwargs) -> list: static_locals={"debug_info": debug_info(program)}, vm_class=VmWithCoverage, ) - run_resources = RunResources(n_steps=4_000_000) + run_resources = RunResources(n_steps=10_000_000) try: runner.run_until_pc(end, run_resources) except Exception as e: From da5ce103e4dc48af1664cbb3d4d060e955cc39ee Mon Sep 17 00:00:00 2001 From: Oba Date: Tue, 9 Jul 2024 09:40:50 +0200 Subject: [PATCH 4/5] test: noCI for loop profiling --- tests/src/kakarot/test_kakarot.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/src/kakarot/test_kakarot.py b/tests/src/kakarot/test_kakarot.py index 8f3d34443..f4391ffc4 100644 --- a/tests/src/kakarot/test_kakarot.py +++ b/tests/src/kakarot/test_kakarot.py @@ -388,11 +388,9 @@ async def test_failing_contract(self, cairo_run): class TestLoopProfiling: @pytest.mark.slow + @pytest.mark.NoCI @pytest.mark.parametrize("max", [10, 50, 100, 200]) - @SyscallHandler.patch( - "IAccount.is_valid_jumpdest", - lambda addr, data: [1], - ) + @SyscallHandler.patch("IAccount.is_valid_jumpdest", lambda addr, data: [1]) def test_loop_profiling(self, get_contract, max): plain_opcodes = get_contract("PlainOpcodes", "PlainOpcodes") initial_state = { From b70e9966dd7c33fb6cd965ff5877f2fd684072ce Mon Sep 17 00:00:00 2001 From: Oba Date: Tue, 9 Jul 2024 12:06:16 +0200 Subject: [PATCH 5/5] test: rename variable --- tests/src/kakarot/test_kakarot.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/src/kakarot/test_kakarot.py b/tests/src/kakarot/test_kakarot.py index f4391ffc4..1348eb69e 100644 --- a/tests/src/kakarot/test_kakarot.py +++ b/tests/src/kakarot/test_kakarot.py @@ -389,9 +389,9 @@ async def test_failing_contract(self, cairo_run): class TestLoopProfiling: @pytest.mark.slow @pytest.mark.NoCI - @pytest.mark.parametrize("max", [10, 50, 100, 200]) + @pytest.mark.parametrize("steps", [10, 50, 100, 200]) @SyscallHandler.patch("IAccount.is_valid_jumpdest", lambda addr, data: [1]) - def test_loop_profiling(self, get_contract, max): + def test_loop_profiling(self, get_contract, steps): plain_opcodes = get_contract("PlainOpcodes", "PlainOpcodes") initial_state = { CONTRACT_ADDRESS: { @@ -402,5 +402,5 @@ def test_loop_profiling(self, get_contract, max): } } with SyscallHandler.patch_state(parse_state(initial_state)): - res = plain_opcodes.loopProfiling(max) - assert res == sum(x for x in range(max)) + res = plain_opcodes.loopProfiling(steps) + assert res == sum(x for x in range(steps))