From 8641e95a07223aff32994ac771c0ed507fec022a Mon Sep 17 00:00:00 2001 From: Fedor Sakharov Date: Thu, 23 May 2024 12:30:06 +0200 Subject: [PATCH 1/3] track transaction number in changes --- src/instruction_handlers/precompiles.rs | 2 +- src/instruction_handlers/storage.rs | 10 +++++++--- src/world_diff.rs | 15 ++++++++++----- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/instruction_handlers/precompiles.rs b/src/instruction_handlers/precompiles.rs index e882b1f1..f21b144d 100644 --- a/src/instruction_handlers/precompiles.rs +++ b/src/instruction_handlers/precompiles.rs @@ -50,7 +50,7 @@ fn precompile_call( let query = LogQuery { timestamp: Timestamp(0), key: abi.to_u256(), - tx_number_in_block: Default::default(), + tx_number_in_block: vm.state.transaction_number, aux_byte: Default::default(), shard_id: Default::default(), address: Default::default(), diff --git a/src/instruction_handlers/storage.rs b/src/instruction_handlers/storage.rs index 8d4611c9..fb7199c1 100644 --- a/src/instruction_handlers/storage.rs +++ b/src/instruction_handlers/storage.rs @@ -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; diff --git a/src/world_diff.rs b/src/world_diff.rs index e94265cb..743bb484 100644 --- a/src/world_diff.rs +++ b/src/world_diff.rs @@ -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, @@ -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) @@ -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; @@ -124,18 +127,20 @@ 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)> { + pub fn get_storage_changes( + &self, + ) -> BTreeMap<(H160, U256), (Option<(u16, U256)>, (u16, U256))> { self.storage_changes.changes_after(0) } pub fn get_storage_changes_after( &self, snapshot: &Snapshot, - ) -> BTreeMap<(H160, U256), (Option, U256)> { + ) -> BTreeMap<(H160, U256), (Option<(u16, U256)>, (u16, U256))> { self.storage_changes.changes_after(snapshot.storage_changes) } From c67df7114e7f5c8765165f4d2c932fa34db9d16a Mon Sep 17 00:00:00 2001 From: Fedor Sakharov Date: Thu, 23 May 2024 12:36:25 +0200 Subject: [PATCH 2/3] fix clippy --- src/world_diff.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/world_diff.rs b/src/world_diff.rs index 743bb484..64e73d83 100644 --- a/src/world_diff.rs +++ b/src/world_diff.rs @@ -131,12 +131,14 @@ impl WorldDiff { self.storage_changes.as_ref() } + #[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, From 23b478cc05f88617549d4646bf499feb139fe160 Mon Sep 17 00:00:00 2001 From: Fedor Sakharov Date: Thu, 23 May 2024 12:40:31 +0200 Subject: [PATCH 3/3] add a comment --- src/instruction_handlers/precompiles.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/instruction_handlers/precompiles.rs b/src/instruction_handlers/precompiles.rs index f21b144d..5d427667 100644 --- a/src/instruction_handlers/precompiles.rs +++ b/src/instruction_handlers/precompiles.rs @@ -50,7 +50,8 @@ fn precompile_call( let query = LogQuery { timestamp: Timestamp(0), key: abi.to_u256(), - tx_number_in_block: vm.state.transaction_number, + // only two first fields are read by the precompile + tx_number_in_block: Default::default(), aux_byte: Default::default(), shard_id: Default::default(), address: Default::default(),