From c6613f7785fef21d17e83279a4b838dc8da05efc Mon Sep 17 00:00:00 2001 From: Ignacio Hagopian Date: Mon, 9 Sep 2024 13:47:56 -0300 Subject: [PATCH] verkle: many contract execution fixes Signed-off-by: Ignacio Hagopian --- .../test_contract_execution.py | 127 ++++++++++-------- 1 file changed, 71 insertions(+), 56 deletions(-) diff --git a/tests/verkle/eip4762_verkle_gas_witness/test_contract_execution.py b/tests/verkle/eip4762_verkle_gas_witness/test_contract_execution.py index 01aa4ec48b..73e2dafd89 100644 --- a/tests/verkle/eip4762_verkle_gas_witness/test_contract_execution.py +++ b/tests/verkle/eip4762_verkle_gas_witness/test_contract_execution.py @@ -52,18 +52,17 @@ def code_with_jumps(size, jumps: list[Jump | Jumpi] = []): """ Returns the requested code with defined size, jumps, and PUSHNs. """ - code = Op.PUSH0 * size + code = Op.JUMPDEST * size result_code = bytes() last_offset = 0 for j in jumps: result_code += bytes(code)[last_offset : j.offset] - if isinstance(j, Jump): - jump_code = bytes(Op.JUMP(j.pc)) - elif isinstance(j, Jumpi): + if isinstance(j, Jumpi): jump_code = bytes(Op.JUMPI(j.pc, 1 if j.condition else 0)) + elif isinstance(j, Jump): + jump_code = bytes(Op.JUMP(j.pc)) result_code += jump_code last_offset = j.offset + len(jump_code) - result_code += bytes(Op.JUMPDEST) result_code += bytes(code)[last_offset:] @@ -77,81 +76,97 @@ def code_with_jumps(size, jumps: list[Jump | Jumpi] = []): @pytest.mark.parametrize( "bytecode, gas_limit, witness_code_chunk_ranges", [ - ( # only_code_in_account_header - code_with_jumps(10), - 1_000_000, - [[0, 0]], - ), - ( # chunks_both_in_and_out_account_header - code_with_jumps(128 * 31 + 100), - 1_000_000, - [[0, 131]], - ), + # ( # only_code_in_account_header + # code_with_jumps(10), + # 1_000_000, + # [[0, 0]], + # ), + # ( # chunks_both_in_and_out_account_header + # code_with_jumps(128 * 31 + 100), + # 1_000_000, + # [[0, 131]], + # ), # ( # touches_only_first_byte_code_chunk # code_with_jumps(128 * 31 + 1), # 1_000_000, - # [[0, 129]], + # [[0, 128]], # ), # ( # touches_only_last_byte_code_chunk - # code_with_jumps(128 * 31 + 100, [Jump(10, 128 * 31 + 100)]), + # code_with_jumps(128 * 31 + 100, [Jump(10, 128 * 31 + 9)]), # 1_000_000, - # [[0, 0], [132, 132]], + # [[0, 0], [131, 131]], # ), # ( # pushn_with_data_in_chunk_that_cant_be_paid - # Op.PUSH0 * 30 + Op.PUSH1(42), - # 42, + # bytes(Op.PUSH0 * 30 + Op.PUSH1(42)), + # 21000, # [[0, 0]], # ), # ( # jump_to_jumpdest_in_pushn_data - # Op.PUSH0 * 10 + Op.JUMP(10 + 2 + 1 + 1000) + Op.PUSH0 * 1000 + Op.PUSH1(0x5B), + # bytes( + # Op.PUSH0 * 10 + # + Op.JUMP(10 + 3 + 1 + 1000 + 1) # 3+1=PUSH2+JUMP + # + Op.PUSH0 * 1000 + # + Op.PUSH1(0x5B) + # + Op.PUSH0 * 100 # Add more code, but can't be executed due to the invalid jump + # ), # 1_000_000, - # [[0, 0], [33, 33]], + # [[0, 0], [32, 32]], # ), # ( # jumpi_to_jumpdest_in_pushn_data - # Op.PUSH0 * 10 + Op.JUMPI(10 + 3 + 1 + 1000, 1) + Op.PUSH0 * 1000 + Op.PUSH1(0x5B), + # bytes( + # Op.PUSH0 * 10 + # + Op.JUMPI(10 + 5 + 1 + 1000 + 1, 1) # 5+1=PUSH1+PUSH2+JUMPI + # + Op.PUSH0 * 1000 + # + Op.PUSH1(0x5B) + # + Op.PUSH0 * 100 + # ), # 1_000_000, - # [[0, 0], [33, 33]], + # [[0, 0], [32, 32]], # ), # ( # jump_to_non_jumpdest_destiny - # Op.PUSH0 * 10 + Op.JUMP(10 + 2 + 1 + 1000) + Op.PUSH0 * 1000 + Op.ORIGIN, + # bytes( + # Op.PUSH0 * 10 + Op.JUMP(10 + 3 + 1 + 1000) + Op.PUSH0 * 1000 + Op.ORIGIN + # ), # 3+1=PUSH2+JUMP # 1_000_000, - # [[0, 0], [33, 33]], + # [[0, 0], [32, 32]], # ), # ( # jumpi_to_non_jumpdest_destiny - # Op.PUSH0 * 10 + Op.JUMPI(10 + 3 + 1 + 1000, 1) + Op.PUSH0 * 1000 + Op.ORIGIN, + # bytes( + # Op.PUSH0 * 10 + Op.JUMPI(10 + 5 + 1 + 1000, 1) + Op.PUSH0 * 1000 + Op.ORIGIN + # ), # 5+1=PUSH1+PUSH2+JUMPI # 1_000_000, - # [[0, 0], [33, 33]], + # [[0, 0], [32, 32]], # ), # ( # linear_execution_stopping_at_first_byte_of_next_chunk # code_with_jumps(128 * 31 + 1), # 1_000_000, - # [[0, 129]], + # [[0, 128]], # ), # ( # false_jumpi # code_with_jumps(150 * 31 + 10, [Jumpi(50, 1000, False)]), # 1_000_000, - # [[0, 151]], + # [[0, 150]], # ), # ( # insufficient_gas_for_jump_instruction - # code_with_jumps(150 * 31, [Jump(50, 1000)]), - # 142, - # [[0, 1]], + # code_with_jumps(150 * 31, [Jump(10, 1000)]), + # 21000 + 200 + 10 + 3, + # [[0, 0]], # ), # ( # insufficient_gas_for_jumpi_instruction - # code_with_jumps(150 * 31, [Jumpi(50, 1000, True)]), - # 142, - # [[0, 1]], + # code_with_jumps(150 * 31, [Jumpi(10, 1000, True)]), + # 21000 + 200 + 10 + 3 + 3, + # [[0, 0]], # ), # ( # sufficient_gas_for_jump_instruction_but_not_for_code_chunk - # code_with_jumps(150 * 31, [Jump(50, 1000)]), - # 42, - # [[0, 0], [33, 33]], - # ), - # ( # sufficient_gas_for_jumpi_instruction_but_not_for_code_chunk - # code_with_jumps(150 * 31, [Jumpi(50, 1000, True)]), - # 42, - # [[0, 0], [33, 33]], + # code_with_jumps(150 * 31, [Jump(10, 1000)]), + # 21000 + 200 + 10 + 3 + 8, + # [[0, 0]], # ), + ( # sufficient_gas_for_jumpi_instruction_but_not_for_code_chunk + # code_with_jumps(150 * 31, [Jumpi(10, 1000, True)]), + # 21000 + 200 + 10 + 3 + 3 + 10, + # [[0, 0]], + ), # ( # jump_outside_code_size # code_with_jumps(150 * 31, [Jump(50, 150 * 31 + 42)]), # 1_000_000, @@ -179,8 +194,8 @@ def code_with_jumps(size, jumps: list[Jump | Jumpi] = []): # ), ], ids=[ - "only_code_in_account_header", - "chunks_both_in_and_out_account_header", + # "only_code_in_account_header", + # "chunks_both_in_and_out_account_header", # "touches_only_first_byte_code_chunk", # "touches_only_last_byte_code_chunk", # "pushn_with_data_in_chunk_that_cant_be_paid", @@ -203,8 +218,8 @@ def code_with_jumps(size, jumps: list[Jump | Jumpi] = []): ) def test_contract_execution_foo( blockchain_test: BlockchainTestFiller, - bytecode, - gas_limit, + bytecode: bytes, + gas_limit: int, witness_code_chunk_ranges, ): """ @@ -235,14 +250,14 @@ def test_contract_execution_foo( print("Number of chunks {}".format(len(code_chunks))) witness_check = WitnessCheck() - for address in [TestAddress, TestAddress2, env.fee_recipient]: - witness_check.add_account_full( - address=address, - account=(None if address == env.fee_recipient else pre[address]), - ) - for chunk_ranges in witness_code_chunk_ranges: - for chunk_number in range(chunk_ranges[0], chunk_ranges[1] + 1): - witness_check.add_code_chunk(TestAddress2, chunk_number, code_chunks[chunk_number]) + # for address in [TestAddress, TestAddress2, env.fee_recipient]: + # witness_check.add_account_full( + # address=address, + # account=pre.get(address), + # ) + # for chunk_ranges in witness_code_chunk_ranges: + # for chunk_number in range(chunk_ranges[0], chunk_ranges[1] + 1): + # witness_check.add_code_chunk(TestAddress2, chunk_number, code_chunks[chunk_number]) blocks = [ Block(