Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Commit

Permalink
chore(execution): consume state_changes on state_changes_count from (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ArniStarkware authored Feb 6, 2024
1 parent a995870 commit 91cdbe1
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 24 deletions.
9 changes: 4 additions & 5 deletions crates/blockifier/src/fee/actual_cost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl<'a> ActualCostBuilder<'a> {
self,
execution_resources: &ExecutionResources,
) -> TransactionExecutionResult<ActualCost> {
self.calculate_actual_fee_and_resources(execution_resources, self.n_reverted_steps)
self.calculate_actual_fee_and_resources(execution_resources)
}

// Setters.
Expand Down Expand Up @@ -128,12 +128,11 @@ impl<'a> ActualCostBuilder<'a> {

// Construct the actual cost object using all fields that were set in the builder.
fn calculate_actual_fee_and_resources(
&self,
self,
execution_resources: &ExecutionResources,
n_reverted_steps: usize,
) -> TransactionExecutionResult<ActualCost> {
let state_changes_count = StateChangesCount::from_state_changes_for_fee_charge(
&self.state_changes,
self.state_changes,
self.sender_address,
self.tx_context
.block_context
Expand Down Expand Up @@ -161,7 +160,7 @@ impl<'a> ActualCostBuilder<'a> {

// Add reverted steps to actual_resources' n_steps for correct fee charge.
*actual_resources.0.get_mut(&abi_constants::N_STEPS_RESOURCE.to_string()).unwrap() +=
n_reverted_steps;
self.n_reverted_steps;

let tx_info = &self.tx_context.tx_info;
let actual_fee = if tx_info.enforce_fee()?
Expand Down
5 changes: 2 additions & 3 deletions crates/blockifier/src/state/cached_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -704,14 +704,13 @@ impl From<&StateChanges> for StateChangesCount {
}

impl StateChangesCount {
// TODO(Arni, 13/2/2024) : Change this method so that it would consume state_changes.
pub fn from_state_changes_for_fee_charge(
state_changes: &StateChanges,
state_changes: StateChanges,
sender_address: Option<ContractAddress>,
fee_token_address: ContractAddress,
) -> Self {
let mut storage_updates = state_changes.storage_updates.clone();
let mut modified_contracts = state_changes.get_modified_contracts();
let mut storage_updates = state_changes.storage_updates;

// For account transactions, we need to compute the transaction fee before we can execute
// the fee transfer, and the fee should cover the state changes that happen in the
Expand Down
2 changes: 1 addition & 1 deletion crates/blockifier/src/state/cached_state_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ fn test_from_state_changes_for_fee_charge(
let state_changes =
create_state_changes_for_test(&mut state, sender_address, fee_token_address);
let state_changes_count = StateChangesCount::from_state_changes_for_fee_charge(
&state_changes,
state_changes,
sender_address,
fee_token_address,
);
Expand Down
27 changes: 12 additions & 15 deletions crates/blockifier/src/transaction/account_transactions_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1062,7 +1062,7 @@ fn test_count_actual_storage_changes(
let execution_info = account_tx.execute_raw(&mut state, &block_context, true, true).unwrap();

let fee_1 = execution_info.actual_fee;
let storage_updates_1 = state.get_actual_state_changes().unwrap();
let state_changes_1 = state.get_actual_state_changes().unwrap();

let cell_write_storage_change =
((contract_address, StorageKey(patricia_key!(15_u8))), stark_felt!(1_u8));
Expand All @@ -1084,7 +1084,7 @@ fn test_count_actual_storage_changes(
]);

let state_changes_count_1 = StateChangesCount::from_state_changes_for_fee_charge(
&storage_updates_1,
state_changes_1.clone(),
Some(account_address),
fee_token_address,
);
Expand All @@ -1097,9 +1097,8 @@ fn test_count_actual_storage_changes(
..Default::default()
};

assert_eq!(expected_modified_contracts, storage_updates_1.get_modified_contracts());
assert_eq!(expected_storage_updates_1, storage_updates_1.storage_updates);

assert_eq!(expected_modified_contracts, state_changes_1.get_modified_contracts());
assert_eq!(expected_storage_updates_1, state_changes_1.storage_updates);
assert_eq!(state_changes_count_1, expected_state_changes_count_1);

// Second transaction: storage cell starts and ends with value 1.
Expand All @@ -1111,7 +1110,7 @@ fn test_count_actual_storage_changes(
let execution_info = account_tx.execute_raw(&mut state, &block_context, true, true).unwrap();

let fee_2 = execution_info.actual_fee;
let storage_updates_2 = state.get_actual_state_changes().unwrap();
let state_changes_2 = state.get_actual_state_changes().unwrap();

expected_sequencer_total_fee += Felt252::from(fee_2.0);
expected_sequencer_fee_update.1 = felt_to_stark_felt(&expected_sequencer_total_fee);
Expand All @@ -1124,7 +1123,7 @@ fn test_count_actual_storage_changes(
HashMap::from([account_balance_storage_change, expected_sequencer_fee_update]);

let state_changes_count_2 = StateChangesCount::from_state_changes_for_fee_charge(
&storage_updates_2,
state_changes_2.clone(),
Some(account_address),
fee_token_address,
);
Expand All @@ -1137,9 +1136,8 @@ fn test_count_actual_storage_changes(
..Default::default()
};

assert_eq!(expected_modified_contracts_2, storage_updates_2.get_modified_contracts());
assert_eq!(expected_storage_updates_2, storage_updates_2.storage_updates);

assert_eq!(expected_modified_contracts_2, state_changes_2.get_modified_contracts());
assert_eq!(expected_storage_updates_2, state_changes_2.storage_updates);
assert_eq!(state_changes_count_2, expected_state_changes_count_2);

// Transfer transaction: transfer 1 ETH to recepient.
Expand All @@ -1152,7 +1150,7 @@ fn test_count_actual_storage_changes(
let execution_info = account_tx.execute_raw(&mut state, &block_context, true, true).unwrap();

let fee_transfer = execution_info.actual_fee;
let storage_updates_transfer = state.get_actual_state_changes().unwrap();
let state_changes_transfer = state.get_actual_state_changes().unwrap();
let transfer_receipient_storage_change = (
(fee_token_address, get_fee_token_var_address(contract_address!(recipient))),
felt_to_stark_felt(&transfer_amount),
Expand All @@ -1172,7 +1170,7 @@ fn test_count_actual_storage_changes(
]);

let state_changes_count_3 = StateChangesCount::from_state_changes_for_fee_charge(
&storage_updates_transfer,
state_changes_transfer.clone(),
Some(account_address),
fee_token_address,
);
Expand All @@ -1187,9 +1185,8 @@ fn test_count_actual_storage_changes(

assert_eq!(
expected_modified_contracts_transfer,
storage_updates_transfer.get_modified_contracts()
state_changes_transfer.get_modified_contracts()
);
assert_eq!(expected_storage_update_transfer, storage_updates_transfer.storage_updates);

assert_eq!(expected_storage_update_transfer, state_changes_transfer.storage_updates);
assert_eq!(state_changes_count_3, expected_state_changes_count_3);
}

0 comments on commit 91cdbe1

Please sign in to comment.