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

Commit

Permalink
Remove State.read_balance
Browse files Browse the repository at this point in the history
  • Loading branch information
ClementWalter committed Nov 22, 2023
1 parent b597d61 commit d3f3afb
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 63 deletions.
Empty file.
6 changes: 0 additions & 6 deletions src/data_availability/starknet.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,6 @@ namespace Internals {
return ();

Check warning on line 105 in src/data_availability/starknet.cairo

View check run for this annotation

Codecov / codecov/patch

src/data_availability/starknet.cairo#L103-L105

Added lines #L103 - L105 were not covered by tests
}

// Skip account if it has indeed never been fetched
// but only touched for balance read
if (accounts_start.new_value == 0) {
return _commit_accounts(accounts_start + DictAccess.SIZE, accounts_end);
}

let (starknet_address) = Account.compute_starknet_address(accounts_start.key);
let account = cast(accounts_start.new_value, Account.Summary*);
_commit_account(account, starknet_address);

Check warning on line 110 in src/data_availability/starknet.cairo

View check run for this annotation

Codecov / codecov/patch

src/data_availability/starknet.cairo#L108-L110

Added lines #L108 - L110 were not covered by tests
Expand Down
4 changes: 2 additions & 2 deletions src/kakarot/instructions/block_information.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ namespace Internals {
func selfbalance{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
ctx: model.ExecutionContext*
) -> (model.ExecutionContext*, Uint256) {
let (state, balance) = State.read_balance(ctx.state, ctx.call_context.address);
let (state, account) = State.get_account(ctx.state, ctx.call_context.address);

Check warning on line 184 in src/kakarot/instructions/block_information.cairo

View check run for this annotation

Codecov / codecov/patch

src/kakarot/instructions/block_information.cairo#L184

Added line #L184 was not covered by tests
let ctx = ExecutionContext.update_state(ctx, state);
return (ctx, balance);
return (ctx, [account.balance]);

Check warning on line 186 in src/kakarot/instructions/block_information.cairo

View check run for this annotation

Codecov / codecov/patch

src/kakarot/instructions/block_information.cairo#L186

Added line #L186 was not covered by tests
}
}
8 changes: 4 additions & 4 deletions src/kakarot/instructions/environmental_information.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ namespace EnvironmentalInformation {
let evm_address = Helpers.uint256_to_felt([address_uint256]);
let (starknet_address) = Account.compute_starknet_address(evm_address);
tempvar address = new model.Address(starknet_address, evm_address);
let (state, balance) = State.read_balance(ctx.state, address);
let stack = Stack.push_uint256(stack, balance);
let (state, account) = State.get_account(ctx.state, address);
let stack = Stack.push_uint256(stack, [account.balance]);

let ctx = ExecutionContext.update_stack(ctx, stack);
let ctx = ExecutionContext.update_state(ctx, state);
Expand Down Expand Up @@ -565,8 +565,8 @@ namespace EnvironmentalInformation {

let (state, account) = State.get_account(ctx.state, address);
let has_code_or_nonce = Account.has_code_or_nonce(account);
let (state, balance) = State.read_balance(state, address);
let account_exists = has_code_or_nonce + balance.low;
let (state, account) = State.get_account(state, address);
let account_exists = has_code_or_nonce + account.balance.low;
// Relevant cases:
// https://github.com/ethereum/go-ethereum/blob/master/core/vm/instructions.go#L392
if (account_exists == 0) {
Expand Down
4 changes: 2 additions & 2 deletions src/kakarot/instructions/system_operations.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -351,9 +351,9 @@ namespace SystemOperations {

let (recipient_starknet_address) = Account.compute_starknet_address(recipient_evm_address);
tempvar recipient = new model.Address(recipient_starknet_address, recipient_evm_address);
let (state, balance) = State.read_balance(ctx.state, ctx.call_context.address);
let (state, account) = State.get_account(ctx.state, ctx.call_context.address);

Check warning on line 354 in src/kakarot/instructions/system_operations.cairo

View check run for this annotation

Codecov / codecov/patch

src/kakarot/instructions/system_operations.cairo#L354

Added line #L354 was not covered by tests
let transfer = model.Transfer(
sender=ctx.call_context.address, recipient=recipient, amount=balance
sender=ctx.call_context.address, recipient=recipient, amount=[account.balance]

Check warning on line 356 in src/kakarot/instructions/system_operations.cairo

View check run for this annotation

Codecov / codecov/patch

src/kakarot/instructions/system_operations.cairo#L356

Added line #L356 was not covered by tests
);
let (state, success) = State.add_transfer(state, transfer);

Expand Down
38 changes: 0 additions & 38 deletions src/kakarot/state.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -245,32 +245,6 @@ namespace State {
);
return (state, success);
}

// @notice Get the balance of a given address
// @dev Try to read from local dict, and read from ETH contract otherwise
// @param self The pointer to the State
// @param address The pointer to the Address
func read_balance{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
self: model.State*, address: model.Address*
) -> (state: model.State*, balance: Uint256) {
let accounts = self.accounts;
let (pointer) = dict_read{dict_ptr=accounts}(key=address.evm);
tempvar self = new model.State(
accounts_start=self.accounts_start,
accounts=accounts,
events_len=self.events_len,
events=self.events,
transfers_len=self.transfers_len,
transfers=self.transfers,
);
if (pointer != 0) {
let account = cast(pointer, model.Account*);
return (self, [account.balance]);
} else {
let balance = Account.read_balance(address);
return (self, balance);
}
}
}

namespace Internals {
Expand All @@ -284,12 +258,6 @@ namespace Internals {
return ();
}

// Skip account if it has indeed never been fetched
// but only touched for balance read
if (accounts_start.new_value == 0) {
return _finalize_accounts(accounts_start + DictAccess.SIZE, accounts_end);
}

let account = cast(accounts_start.new_value, model.Account*);
let account_summary = Account.copy(account);
dict_write{dict_ptr=accounts}(
Expand All @@ -309,12 +277,6 @@ namespace Internals {
return ();
}

// Skip account if it has indeed never been fetched
// but only touched for balance read
if (accounts_start.new_value == 0) {
return _finalize_accounts(accounts_start + DictAccess.SIZE, accounts_end);
}

let account = cast(accounts_start.new_value, model.Account*);
let account_summary = Account.finalize(account);
dict_write{dict_ptr=accounts}(
Expand Down
21 changes: 10 additions & 11 deletions tests/src/kakarot/instructions/test_system_operations.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ func test__exec_call__should_transfer_value{
tempvar caller_address = new model.Address(
caller_starknet_contract_address, caller_evm_contract_address
);
let (state, caller_balance_prev) = State.read_balance(ctx.state, caller_address);
let (state, caller_account_prev) = State.get_account(ctx.state, caller_address);
let ctx = ExecutionContext.update_state(ctx, state);

// When
Expand All @@ -302,11 +302,12 @@ func test__exec_call__should_transfer_value{
tempvar callee_address = new model.Address(
callee_starknet_contract_address, callee_evm_contract_address
);
let (state, callee_balance) = State.read_balance(state, callee_address);
let (state, caller_balance_new) = State.read_balance(state, caller_address);
let (caller_diff_balance) = uint256_sub(caller_balance_prev, caller_balance_new);

assert callee_balance = Uint256(2, 0);
let (state, callee_account) = State.get_account(state, callee_address);
let (state, caller_account_new) = State.get_account(state, caller_address);
let (caller_diff_balance) = uint256_sub(
[caller_account_prev.balance], [caller_account_new.balance]
);
assert [callee_account.balance] = Uint256(2, 0);
assert caller_diff_balance = Uint256(2, 0);
return ();
}
Expand Down Expand Up @@ -443,7 +444,7 @@ func test__exec_callcode__should_transfer_value{
let ctx = MemoryOperations.exec_mstore(ctx);

// Get the balance of caller pre-call
let (state, caller_pre_balance) = State.read_balance(ctx.state, caller_address);
let (state, caller_pre_account) = State.get_account(ctx.state, caller_address);
let ctx = ExecutionContext.update_state(ctx, state);

// When
Expand All @@ -452,11 +453,9 @@ func test__exec_callcode__should_transfer_value{
// Then
// get balances of caller and callee post-call
let state = sub_ctx.state;
let (state, caller_post_balance) = State.read_balance(state, caller_address);
let (state, callee_balance) = State.read_balance(state, callee_address);
let (caller_diff_balance) = uint256_sub(caller_pre_balance, caller_post_balance);
let (state, caller_post_account) = State.get_account(state, caller_address);

assert caller_post_balance = caller_pre_balance;
assert caller_post_account.balance = caller_pre_account.balance;
return ();
}

Expand Down

0 comments on commit d3f3afb

Please sign in to comment.