Skip to content

Commit

Permalink
feat: add nonce to eth_call (#997)
Browse files Browse the repository at this point in the history
<!--- Please provide a general summary of your changes in the title
above -->

<!-- Give an estimate of the time you spent on this PR in terms of work
days.
Did you spend 0.5 days on this PR or rather 2 days?  -->

Resolves #996 

## Pull request type

<!-- Please try to limit your pull request to one type,
submit multiple pull requests if needed. -->

Please check the type of change your PR introduces:

- [x] Bugfix
- [ ] Feature
- [ ] Code style update (formatting, renaming)
- [ ] Refactoring (no functional changes, no api changes)
- [ ] Build related changes
- [ ] Documentation content changes
- [ ] Other (please describe):

BREAKING CHANGES TO API

<!-- Reviewable:start -->
- - -
This change is [<img src="https://reviewable.io/review_button.svg"
height="34" align="absmiddle"
alt="Reviewable"/>](https://reviewable.io/reviews/kkrt-labs/kakarot/997)
<!-- Reviewable:end -->
  • Loading branch information
Eikix authored Feb 27, 2024
1 parent b0c62ee commit b5f385e
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 9 deletions.
1 change: 1 addition & 0 deletions scripts/utils/kakarot.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ async def _wrapper(self, *args, **kwargs):
else int(EVM_ADDRESS, 16)
)
result = await kakarot_contract.functions["eth_call"].call(
nonce=0,
origin=origin,
to={
"is_some": 1,
Expand Down
1 change: 1 addition & 0 deletions src/kakarot/interfaces/interfaces.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ namespace IKakarot {
}

func eth_call(
nonce: felt,
origin: felt,
to: model.Option,
gas_limit: felt,
Expand Down
28 changes: 25 additions & 3 deletions src/kakarot/kakarot.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ from starkware.cairo.common.bool import FALSE, TRUE
from starkware.cairo.common.cairo_builtins import HashBuiltin, BitwiseBuiltin
from starkware.cairo.common.math_cmp import is_not_zero
from starkware.cairo.common.uint256 import Uint256
from starkware.starknet.common.syscalls import get_caller_address, replace_class
from starkware.starknet.common.syscalls import get_caller_address, replace_class, get_tx_info
from starkware.cairo.common.registers import get_fp_and_pc
from openzeppelin.access.ownable.library import Ownable

Expand Down Expand Up @@ -160,6 +160,7 @@ func deploy_externally_owned_account{
func eth_call{
syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr, bitwise_ptr: BitwiseBuiltin*
}(
nonce: felt,
origin: felt,
to: model.Option,
gas_limit: felt,
Expand All @@ -174,7 +175,16 @@ func eth_call{
let fp_and_pc = get_fp_and_pc();
local __fp__: felt* = fp_and_pc.fp_val;
let (evm, state, gas_used) = Kakarot.eth_call(
origin, to, gas_limit, gas_price, &value, data_len, data, access_list_len, access_list
nonce,
origin,
to,
gas_limit,
gas_price,
&value,
data_len,
data,
access_list_len,
access_list,
);
let is_reverted = is_not_zero(evm.reverted);
return (evm.return_data_len, evm.return_data, 1 - is_reverted, gas_used);
Expand Down Expand Up @@ -213,8 +223,20 @@ func eth_send_transaction{
local __fp__: felt* = fp_and_pc.fp_val;
let (local starknet_caller_address) = get_caller_address();
let (local origin) = Kakarot.safe_get_evm_address(starknet_caller_address);

let (tx_info) = get_tx_info();

let (evm, state, gas_used) = Kakarot.eth_call(
origin, to, gas_limit, gas_price, &value, data_len, data, access_list_len, access_list
tx_info.nonce,
origin,
to,
gas_limit,
gas_price,
&value,
data_len,
data,
access_list_len,
access_list,
);
let is_reverted = is_not_zero(evm.reverted);
let result = (evm.return_data_len, evm.return_data, 1 - is_reverted, gas_used);
Expand Down
11 changes: 5 additions & 6 deletions src/kakarot/library.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ namespace Kakarot {
range_check_ptr,
bitwise_ptr: BitwiseBuiltin*,
}(
nonce: felt,
origin: felt,
to: model.Option,
gas_limit: felt,
Expand All @@ -86,7 +87,7 @@ namespace Kakarot {
alloc_locals;
let is_regular_tx = is_not_zero(to.is_some);
let is_deploy_tx = 1 - is_regular_tx;
let evm_contract_address = resolve_to(to, origin);
let evm_contract_address = resolve_to(to, origin, nonce);
let starknet_contract_address = Account.compute_starknet_address(evm_contract_address);
tempvar address = new model.Address(
starknet=starknet_contract_address, evm=evm_contract_address
Expand Down Expand Up @@ -189,21 +190,19 @@ namespace Kakarot {
// @dev When to=None, it's a deploy tx so we first compute the target address
// @param to The transaction to parameter
// @param origin The transaction origin parameter
// @param nonce The transaction nonce parameter, used to compute the target address if it's a deploy tx
// @return the target evm address
func resolve_to{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr,
bitwise_ptr: BitwiseBuiltin*,
}(to: model.Option, origin: felt) -> felt {
}(to: model.Option, origin: felt, nonce: felt) -> felt {
alloc_locals;
if (to.is_some != 0) {
return to.value;
}
// TODO: read the nonce from the provided origin address, otherwise in view mode this will
// TODO: always use a 0 nonce
let (tx_info) = get_tx_info();
let (local evm_contract_address) = CreateHelper.get_create_address(origin, tx_info.nonce);
let (local evm_contract_address) = CreateHelper.get_create_address(origin, nonce);
return evm_contract_address;
}

Expand Down
1 change: 1 addition & 0 deletions tests/end_to_end/test_kakarot.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ async def test_eth_call_should_succeed(
assert status == "✅"

result = await kakarot.functions["eth_call"].call(
nonce=0,
origin=int(evm_address, 16),
to={
"is_some": 1,
Expand Down
1 change: 1 addition & 0 deletions tests/utils/contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ async def _wrapped(self, *args, **kwargs):
}

call_kwargs = {
"nonce": 0,
"origin": caller_evm_address,
**invoke_kwargs,
}
Expand Down

0 comments on commit b5f385e

Please sign in to comment.