Skip to content

Commit

Permalink
feat: set_code
Browse files Browse the repository at this point in the history
  • Loading branch information
obatirou committed Jan 17, 2025
1 parent 9af49dd commit 3298535
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
14 changes: 14 additions & 0 deletions cairo/ethereum/cancun/state.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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 ();
}
13 changes: 12 additions & 1 deletion cairo/tests/ethereum/cancun/test_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 3298535

Please sign in to comment.