diff --git a/cairo/ethereum/cancun/state.cairo b/cairo/ethereum/cancun/state.cairo index ad447217..e3788e95 100644 --- a/cairo/ethereum/cancun/state.cairo +++ b/cairo/ethereum/cancun/state.cairo @@ -943,3 +943,17 @@ func copy_transient_storage_tries_recursive{ dict_start + AddressTrieBytes32U256DictAccess.SIZE, dict_end ); } + +func set_code{poseidon_ptr: PoseidonBuiltin*, state: State}(address: Address, code: Bytes) { + // Get the current account + let account = get_account(address); + + // Create new account with updated code + tempvar new_account = OptionalAccount( + new AccountStruct(nonce=account.value.nonce, balance=account.value.balance, code=code) + ); + + // Set the updated account + set_account(address, new_account); + return (); +} diff --git a/cairo/tests/ethereum/cancun/test_state.py b/cairo/tests/ethereum/cancun/test_state.py index 8461d97c..abd3f229 100644 --- a/cairo/tests/ethereum/cancun/test_state.py +++ b/cairo/tests/ethereum/cancun/test_state.py @@ -23,11 +23,12 @@ is_account_empty, mark_account_created, set_account, + set_code, set_storage, set_transient_storage, ) from tests.utils.args_gen import State, TransientStorage -from tests.utils.strategies import address, bytes32, state, transient_storage +from tests.utils.strategies import address, bytes32, code, state, transient_storage @composite @@ -167,6 +168,16 @@ def test_is_account_alive(self, cairo_run, data): assert result_cairo == is_account_alive(state, address) assert state_cairo == state + @given( + data=state_and_address_and_optional_key(), + code=code, + ) + def test_set_code(self, cairo_run, data, code: bytes): + state, address = data + state_cairo = cairo_run("set_code", state, address, code) + set_code(state, address, code) + assert state_cairo == state + class TestStateStorage: @given(state_and_address_and_optional_key(key_strategy=bytes32))