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

Commit

Permalink
Use true corresponding starknet address for precompiles (#804)
Browse files Browse the repository at this point in the history
Time spent on this PR: 0.15

## Pull request type

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):

## What is the current behavior?

Some ef tests are crashing the Cairo VM with errors likes `Error
message: ERC20 cannot send to address 0`.

## What is the new behavior?

The precompile sub_ctx uses the true corresponding starknet address.
Transfer is added in a CALL only if value > 0.
  • Loading branch information
ClementWalter authored Nov 9, 2023
1 parent 4c2ef03 commit 19d628d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
6 changes: 3 additions & 3 deletions scripts/utils/kakarot.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ async def eth_send_transaction(
return receipt, response, success


async def _compute_starknet_address(address: Union[str, int]):
async def compute_starknet_address(address: Union[str, int]):
evm_address = int(address, 16) if isinstance(address, str) else address
kakarot_contract = await _get_starknet_contract("kakarot")
return (
Expand All @@ -364,7 +364,7 @@ async def deploy_and_fund_evm_address(evm_address: str, amount: float):


async def fund_address(address: Union[str, int], amount: float):
starknet_address = await _compute_starknet_address(address)
starknet_address = await compute_starknet_address(address)
logger.info(
f"ℹ️ Funding EVM address {address} at Starknet address {hex(starknet_address)}"
)
Expand Down Expand Up @@ -421,7 +421,7 @@ async def store_bytecode(bytecode: Union[str, bytes], **kwargs):


async def get_bytecode(address: Union[int, str]):
starknet_address = await _compute_starknet_address(address)
starknet_address = await compute_starknet_address(address)
return bytes(
(
await _call_starknet(
Expand Down
7 changes: 6 additions & 1 deletion src/kakarot/instructions/system_operations.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ from starkware.cairo.common.cairo_keccak.keccak import cairo_keccak_bigend, fina
from starkware.cairo.common.math import split_felt, unsigned_div_rem
from starkware.cairo.common.math_cmp import is_le, is_not_zero, is_nn
from starkware.cairo.common.memcpy import memcpy
from starkware.cairo.common.uint256 import Uint256
from starkware.cairo.common.uint256 import Uint256, uint256_eq
from starkware.starknet.common.syscalls import (
deploy as deploy_syscall,
get_contract_address,
Expand Down Expand Up @@ -234,6 +234,11 @@ namespace SystemOperations {

let (value_high, value_low) = split_felt(sub_ctx.call_context.value);
tempvar value = Uint256(value_low, value_high);
let (is_zero) = uint256_eq(value, Uint256(0, 0));
if (is_zero != 0) {
return sub_ctx;
}

let calling_context = sub_ctx.call_context.calling_context;
let transfer = model.Transfer(
calling_context.call_context.address, sub_ctx.call_context.address, value
Expand Down
4 changes: 3 additions & 1 deletion src/kakarot/precompiles/precompiles.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ from starkware.cairo.common.default_dict import default_dict_new
from starkware.cairo.common.math_cmp import is_le, is_not_zero

// Internal dependencies
from kakarot.account import Account
from kakarot.constants import Constants
from kakarot.execution_context import ExecutionContext
from kakarot.errors import Errors
Expand Down Expand Up @@ -52,7 +53,8 @@ namespace Precompiles {
// Build returned execution context
let stack = Stack.init();
let memory = Memory.init();
tempvar address = new model.Address(0, evm_address);
let (starknet_address) = Account.compute_starknet_address(evm_address);
tempvar address = new model.Address(starknet_address, evm_address);
tempvar call_context = new model.CallContext(
bytecode=cast(0, felt*),
bytecode_len=0,
Expand Down

0 comments on commit 19d628d

Please sign in to comment.