Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: track transaction number in changes #33

Merged
merged 3 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/instruction_handlers/precompiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ fn precompile_call(
let query = LogQuery {
timestamp: Timestamp(0),
key: abi.to_u256(),
tx_number_in_block: Default::default(),
montekki marked this conversation as resolved.
Show resolved Hide resolved
tx_number_in_block: vm.state.transaction_number,
aux_byte: Default::default(),
shard_id: Default::default(),
address: Default::default(),
Expand Down
10 changes: 7 additions & 3 deletions src/instruction_handlers/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@ fn sstore(
let key = Register1::get(args, &mut vm.state);
let value = Register2::get(args, &mut vm.state);

let refund =
vm.world_diff
.write_storage(world, vm.state.current_frame.address, key, value);
let refund = vm.world_diff.write_storage(
world,
vm.state.current_frame.address,
key,
value,
vm.state.transaction_number,
);

assert!(refund <= SSTORE_COST);
vm.state.current_frame.gas += refund;
Expand Down
17 changes: 12 additions & 5 deletions src/world_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use zkevm_opcode_defs::system_params::{
#[derive(Default)]
pub struct WorldDiff {
// These are rolled back on revert or panic (and when the whole VM is rolled back).
storage_changes: RollbackableMap<(H160, U256), U256>,
storage_changes: RollbackableMap<(H160, U256), (u16, U256)>,
paid_changes: RollbackableMap<(H160, U256), u32>,
transient_storage_changes: RollbackableMap<(H160, U256), U256>,
events: RollbackableLog<Event>,
Expand Down Expand Up @@ -68,6 +68,7 @@ impl WorldDiff {
.as_ref()
.get(&(contract, key))
.cloned()
.map(|v| v.1)
.unwrap_or_else(|| world.read_storage(contract, key));

let refund = if world.is_free_storage_slot(&contract, &key)
Expand All @@ -89,8 +90,10 @@ impl WorldDiff {
contract: H160,
key: U256,
value: U256,
tx_number: u16,
) -> u32 {
self.storage_changes.insert((contract, key), value);
self.storage_changes
.insert((contract, key), (tx_number, value));

if world.is_free_storage_slot(&contract, &key) {
return WARM_WRITE_REFUND;
Expand Down Expand Up @@ -124,18 +127,22 @@ impl WorldDiff {
refund
}

pub fn get_storage_state(&self) -> &BTreeMap<(H160, U256), U256> {
pub fn get_storage_state(&self) -> &BTreeMap<(H160, U256), (u16, U256)> {
self.storage_changes.as_ref()
}

pub fn get_storage_changes(&self) -> BTreeMap<(H160, U256), (Option<U256>, U256)> {
#[allow(clippy::type_complexity)]
pub fn get_storage_changes(
&self,
) -> BTreeMap<(H160, U256), (Option<(u16, U256)>, (u16, U256))> {
self.storage_changes.changes_after(0)
}

#[allow(clippy::type_complexity)]
pub fn get_storage_changes_after(
&self,
snapshot: &Snapshot,
) -> BTreeMap<(H160, U256), (Option<U256>, U256)> {
) -> BTreeMap<(H160, U256), (Option<(u16, U256)>, (u16, U256))> {
self.storage_changes.changes_after(snapshot.storage_changes)
}

Expand Down
Loading