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

Commit

Permalink
fix: fix python tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Eikix committed Nov 23, 2023
1 parent 93c813b commit c7f62b0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
20 changes: 15 additions & 5 deletions tests/src/kakarot/instructions/test_system_operations.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ from starkware.cairo.common.bool import TRUE, FALSE
from starkware.cairo.common.cairo_builtins import HashBuiltin, BitwiseBuiltin
from starkware.cairo.common.math import split_felt, assert_not_zero
from starkware.cairo.common.uint256 import Uint256, uint256_sub
from starkware.starknet.common.syscalls import get_contract_address

// Local dependencies
from data_availability.starknet import Starknet
Expand Down Expand Up @@ -615,7 +614,7 @@ func test__exec_create__should_return_a_new_context_with_bytecode_from_memory_at

// Fill the stack with exec_create args
let stack: model.Stack* = Stack.init();
tempvar value = new Uint256(0, 0);
tempvar value = new Uint256(1, 0);
tempvar offset = new Uint256(3, 0);
tempvar size = new Uint256(4, 0);
let stack = Stack.push(stack, size);
Expand All @@ -632,7 +631,7 @@ func test__exec_create__should_return_a_new_context_with_bytecode_from_memory_at
let bytecode_len = 0;
let (bytecode: felt*) = alloc();
// As this test contract is mocking the contract account we have to set this contract address as the starknet_contract_address.
let (contract_address: felt) = get_contract_address();
let (contract_address: felt) = Account.compute_starknet_address(evm_caller_address);
let ctx = TestHelpers.init_context_at_address_with_stack(
contract_address, evm_caller_address, bytecode_len, bytecode, stack
);
Expand Down Expand Up @@ -680,6 +679,11 @@ func test__exec_create__should_return_a_new_context_with_bytecode_from_memory_at
account.code_len, account.code, return_data_len, sub_ctx.return_data
);

tempvar sender_address = new model.Address(contract_address, evm_caller_address);
let (state, sender) = State.get_account(state, sender_address);
assert [sender.balance] = Uint256(0, 0);
assert [account.balance] = Uint256(1, 0);

return ();
}

Expand Down Expand Up @@ -709,7 +713,7 @@ func test__exec_create2__should_return_a_new_context_with_bytecode_from_memory_a

// Fill the stack with exec_create2 args
let stack: model.Stack* = Stack.init();
tempvar value = new Uint256(0, 0);
tempvar value = new Uint256(1, 0);
let stack = Stack.push_uint128(stack, nonce);
let stack = Stack.push_uint128(stack, bytecode_size);
let stack = Stack.push_uint128(stack, bytecode_offset);
Expand All @@ -722,7 +726,8 @@ func test__exec_create2__should_return_a_new_context_with_bytecode_from_memory_a
let stack = Stack.push(stack, memory_offset);
let bytecode_len = 0;
let (bytecode: felt*) = alloc();
let (contract_address: felt) = get_contract_address();
let (contract_address: felt) = Account.compute_starknet_address(evm_caller_address);
tempvar sender_address = new model.Address(contract_address, evm_caller_address);
let ctx = TestHelpers.init_context_at_address_with_stack(
contract_address, evm_caller_address, bytecode_len, bytecode, stack
);
Expand Down Expand Up @@ -769,5 +774,10 @@ func test__exec_create2__should_return_a_new_context_with_bytecode_from_memory_a
account.code_len, account.code, return_data_len, sub_ctx.return_data
);

let (state, sender) = State.get_account(state, sender_address);

assert [sender.balance] = Uint256(0, 0);
assert [account.balance] = Uint256(1, 0);

return ();
}
26 changes: 20 additions & 6 deletions tests/src/kakarot/instructions/test_system_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,21 @@ async def test_should_return_a_new_context_based_on_calling_ctx_stack(
await mint(ZERO_ACCOUNT, 2)
await system_operations.test__exec_delegatecall__should_return_a_new_context_based_on_calling_ctx_stack().call()

async def test_create(self, system_operations):
async def test_create(self, system_operations, eth):
salt = 0
# given we start with the first anvil test account
evm_caller_address = to_checksum_address(
0xF39FD6E51AAD88F6F4CE6AB8827279CFFFB92266
)
expected_create_addr = get_create_address(evm_caller_address, salt)

starknet_contract_address = (
await system_operations.compute_starknet_address(
int(evm_caller_address, 16)
).call()
).result.contract_address
await eth.mint(starknet_contract_address, int_to_uint256(1)).execute()

await system_operations.test__exec_create__should_return_a_new_context_with_bytecode_from_memory_at_expected_address(
int(evm_caller_address, 16),
salt,
Expand All @@ -133,7 +140,7 @@ async def test_create_has_deterministic_address(self, system_operations, nonce):
int(expected_create_addr, 16),
).call()

async def test_create2(self, system_operations):
async def test_create2(self, system_operations, eth):
# we store a memory word in memory
# and have our bytecode as the memory read from an offset and size
# we take that word at an offset and size and use it as the bytecode to determine the expected create2 evm contract address
Expand All @@ -143,17 +150,24 @@ async def test_create2(self, system_operations):
size = 4
salt = 5
padded_salt = salt.to_bytes(32, byteorder="big")
evm_caller_address_int = 15
evm_caller_address_bytes = evm_caller_address_int.to_bytes(20, byteorder="big")
evm_caller_address = to_checksum_address(evm_caller_address_bytes)
evm_caller_address = to_checksum_address(
0xF39FD6E51AAD88F6F4CE6AB8827279CFFFB92266
)
bytecode = to_bytes(memory_word)[offset : offset + size]

starknet_contract_address = (
await system_operations.compute_starknet_address(
int(evm_caller_address, 16)
).call()
).result.contract_address
await eth.mint(starknet_contract_address, int_to_uint256(1)).execute()

expected_create2_addr = get_create2_address(
evm_caller_address, padded_salt, bytecode
)

await system_operations.test__exec_create2__should_return_a_new_context_with_bytecode_from_memory_at_expected_address(
evm_caller_address_int,
int(evm_caller_address, 16),
offset,
size,
salt,
Expand Down

0 comments on commit c7f62b0

Please sign in to comment.