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

Commit

Permalink
Merge branch 'main' into feat/remove-starknet-address
Browse files Browse the repository at this point in the history
  • Loading branch information
ClementWalter authored Sep 23, 2024
2 parents ac7ef0f + 27c2f38 commit 1eab764
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 84 deletions.
60 changes: 25 additions & 35 deletions src/kakarot/accounts/account_contract.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,19 @@ func is_initialized{
}

// EOA specific entrypoints

// @notice Executes a transaction from outside the account.
// @dev This function validates the transaction for account-related checks and sends it to the Kakarot contract for execution.
// Further EVM-related checks are performed in the library and in the Kakarot contract.
// @param outside_execution The outside execution context. Actually unused, but required by the SNIP-9 specification.
// @param call_array_len The length of the call array. Must be 1 as multicall is not supported.
// @param call_array An array containing the call data for the transaction.
// @param calldata_len The length of the calldata array.
// @param calldata The calldata for the transaction.
// @param signature_len The length of the signature array.
// @param signature The signature of the transaction.
// @return response_len The length of the response array.
// @return response The response from the Kakarot contract.
@external
func execute_from_outside{
syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, bitwise_ptr: BitwiseBuiltin*, range_check_ptr
Expand All @@ -94,23 +107,7 @@ func execute_from_outside{
) -> (response_len: felt, response: felt*) {
alloc_locals;
let (caller) = get_caller_address();

// Starknet validation
if (outside_execution.caller != 'ANY_CALLER') {
with_attr error_message("Execute from outside: invalid caller") {
assert caller = outside_execution.caller;
}
}
let (block_timestamp) = get_block_timestamp();
let too_early = is_nn(outside_execution.execute_after - block_timestamp);
with_attr error_message("Execute from outside: too early") {
assert too_early = FALSE;
}
let too_late = is_nn(block_timestamp - outside_execution.execute_before);
with_attr error_message("Execute from outside: too late") {
assert too_late = FALSE;
}
with_attr error_message("Execute from outside: multicall not supported") {
with_attr error_message("EOA: multicall not supported") {
assert call_array_len = 1;
}
let (tx_info) = get_tx_info();
Expand Down Expand Up @@ -153,11 +150,7 @@ func execute_from_outside{
}

// @notice Validate a transaction
// @dev The transaction is considered as valid if it is signed with the correct address and is a valid kakarot transaction
// @param call_array_len The length of the call_array
// @param call_array An array containing all the calls of the transaction see: https://docs.openzeppelin.com/contracts-cairo/0.6.0/accounts#call_and_accountcallarray_format
// @param calldata_len The length of the Calldata array
// @param calldata The calldata
// @dev Disabled in favor of `execute_from_outside`. Required by the Native Account Abstraction spec.
@external
func __validate__{
syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, bitwise_ptr: BitwiseBuiltin*, range_check_ptr
Expand All @@ -168,9 +161,7 @@ func __validate__{
return ();
}

// @notice Validate this account class for declaration
// @dev For our use case the account doesn't need to declare contracts
// @param class_hash The account class
// @notice Disabled. Required by the Native Account Abstraction spec.
@external
func __validate_declare__{
syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, bitwise_ptr: BitwiseBuiltin*, range_check_ptr
Expand All @@ -181,14 +172,8 @@ func __validate_declare__{
return ();
}

// @notice Execute the Kakarot transaction
// @dev this is executed only if the __validate__ function succeeded
// @param call_array_len The length of the call_array
// @param call_array An array containing all the calls of the transaction see: https://docs.openzeppelin.com/contracts-cairo/0.6.0/accounts#call_and_accountcallarray_format
// @param calldata_len The length of the Calldata array
// @param calldata The calldata
// @return response_len The length of the response array
// @return response The response from the kakarot contract
// @notice Execute a starknet transaction.
// @dev Disabled in favor of `execute_from_outside`.
@external
func __execute__{
syscall_ptr: felt*,
Expand Down Expand Up @@ -300,6 +285,8 @@ func is_valid_jumpdest{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_che
return (is_valid=is_valid);
}

// @notice Get the code hash of the account.
// @return code_hash The code hash of the account.
@view
func get_code_hash{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (
code_hash: Uint256
Expand All @@ -308,6 +295,8 @@ func get_code_hash{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_p
return (code_hash,);
}

// @notice Set the code hash of the account.
// @param code_hash The code hash of the account.
@external
func set_code_hash{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
code_hash: Uint256
Expand All @@ -329,8 +318,9 @@ func set_authorized_pre_eip155_tx{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*
return ();
}

// @notice Used to preserve caller in Cairo Precompiles
// @dev Reentrency check is done, only get_starknet_address is allowed for Solidity contracts
// @notice Execute a starknet call.
// @dev Used when executing a Cairo Precompile. Used to preserve the caller address.
// Reentrency check is done, only `get_starknet_address` is allowed for Solidity contracts
// to be able to get the corresponding Starknet address in their calldata.
@external
func execute_starknet_call{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
Expand Down
51 changes: 2 additions & 49 deletions tests/src/kakarot/accounts/test_account_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,55 +518,8 @@ def test_should_pass_all_data_len(self, cairo_run, bytecode):

class TestExecuteFromOutsideEntrypoint:

def test_should_raise_when_caller_is_not_any_caller_nor_actual_caller(
self, cairo_run
):
with cairo_error(message="Execute from outside: invalid caller"):
cairo_run(
"test__execute_from_outside_entrypoint",
outside_execution={
"caller": SyscallHandler.caller_address + 1,
"nonce": 0,
"execute_after": 0,
"execute_before": 0,
},
call_array=[],
calldata=[],
signature=[],
)

def test_should_raise_when_call_is_too_early(self, cairo_run):
with cairo_error(message="Execute from outside: too early"):
cairo_run(
"test__execute_from_outside_entrypoint",
outside_execution={
"caller": SyscallHandler.caller_address,
"nonce": 0,
"execute_after": SyscallHandler.block_timestamp + 1,
"execute_before": 0,
},
call_array=[],
calldata=[],
signature=[],
)

def test_should_raise_when_call_is_too_late(self, cairo_run):
with cairo_error(message="Execute from outside: too late"):
cairo_run(
"test__execute_from_outside_entrypoint",
outside_execution={
"caller": SyscallHandler.caller_address,
"nonce": 0,
"execute_after": 0,
"execute_before": SyscallHandler.block_timestamp - 1,
},
call_array=[],
calldata=[],
signature=[],
)

def test_should_raise_when_call_array_is_empty(self, cairo_run):
with cairo_error(message="Execute from outside: multicall not supported"):
with cairo_error(message="EOA: multicall not supported"):
cairo_run(
"test__execute_from_outside_entrypoint",
outside_execution={
Expand All @@ -581,7 +534,7 @@ def test_should_raise_when_call_array_is_empty(self, cairo_run):
)

def test_should_raise_when_call_array_has_more_than_one_call(self, cairo_run):
with cairo_error(message="Execute from outside: multicall not supported"):
with cairo_error(message="EOA: multicall not supported"):
cairo_run(
"test__execute_from_outside_entrypoint",
outside_execution={
Expand Down

0 comments on commit 1eab764

Please sign in to comment.