Skip to content

Commit

Permalink
use existing write and read storage structures
Browse files Browse the repository at this point in the history
  • Loading branch information
montekki committed Aug 23, 2024
1 parent 30a3338 commit cf7b22b
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/instruction_handlers/far_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ fn far_call<
Address::from_low_u64_be(DEPLOYER_SYSTEM_CONTRACT_ADDRESS_LOW as u64);
if !vm
.world_diff
.read_storage_slots_ct
.read_storage_slots
.contains(&(deployer_system_contract_address, destination_address))
{
vm.state.storage_application_cycles +=
Expand Down
6 changes: 3 additions & 3 deletions src/instruction_handlers/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn sstore<T: Tracer, W: World<T>>(
let value = Register2::get(args, &mut vm.state);
if !vm
.world_diff
.written_storage_slots_ct
.written_storage_slots
.contains(&(vm.state.current_frame.address, key))
{
vm.state.storage_application_cycles +=
Expand Down Expand Up @@ -64,11 +64,11 @@ fn sload<T: Tracer, W: World<T>>(

if !vm
.world_diff
.read_storage_slots_ct
.read_storage_slots
.contains(&(vm.state.current_frame.address, key))
&& !vm
.world_diff
.written_storage_slots_ct
.written_storage_slots
.contains(&(vm.state.current_frame.address, key))
{
vm.state.storage_application_cycles += STORAGE_READ_STORAGE_APPLICATION_CYCLES as usize;
Expand Down
23 changes: 4 additions & 19 deletions src/world_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,11 @@ pub struct WorldDiff {

// The fields below are only rolled back when the whole VM is rolled back.
pub(crate) decommitted_hashes: RollbackableSet<U256>,
read_storage_slots: RollbackableSet<(H160, U256)>,
written_storage_slots: RollbackableSet<(H160, U256)>,
pub read_storage_slots: RollbackableSet<(H160, U256)>,
pub written_storage_slots: RollbackableSet<(H160, U256)>,

// This is never rolled back. It is just a cache to avoid asking these from DB every time.
storage_initial_values: BTreeMap<(H160, U256), Option<U256>>,

pub read_storage_slots_ct: RollbackableSet<(H160, U256)>,
pub written_storage_slots_ct: RollbackableSet<(H160, U256)>,
}

#[derive(Debug)]
Expand All @@ -44,9 +41,6 @@ pub struct ExternalSnapshot {
written_storage_slots: <RollbackableMap<(H160, U256), ()> as Rollback>::Snapshot,
storage_refunds: <RollbackableLog<u32> as Rollback>::Snapshot,
pubdata_costs: <RollbackableLog<i32> as Rollback>::Snapshot,

read_storage_slots_ct: <RollbackableMap<(H160, U256), ()> as Rollback>::Snapshot,
written_storage_slots_ct: <RollbackableMap<(H160, U256), ()> as Rollback>::Snapshot,
}

/// There is no address field because nobody is interested in events that don't come
Expand Down Expand Up @@ -108,15 +102,14 @@ impl WorldDiff {
.copied()
.unwrap_or_else(|| world.read_storage(contract, key).unwrap_or_default());

self.read_storage_slots_ct.add((contract, key));
let refund = if world.is_free_storage_slot(&contract, &key)
|| self.read_storage_slots.contains(&(contract, key))
{
WARM_READ_REFUND
} else {
self.read_storage_slots.add((contract, key));
0
};
self.read_storage_slots.add((contract, key));
self.pubdata_costs.push(0);
(value, refund)
}
Expand All @@ -136,8 +129,8 @@ impl WorldDiff {
.entry((contract, key))
.or_insert_with(|| world.read_storage(contract, key));

self.written_storage_slots_ct.add((contract, key));
if world.is_free_storage_slot(&contract, &key) {
self.written_storage_slots.add((contract, key));
self.storage_refunds.push(WARM_WRITE_REFUND);
self.pubdata_costs.push(0);
return WARM_WRITE_REFUND;
Expand Down Expand Up @@ -313,9 +306,6 @@ impl WorldDiff {
written_storage_slots: self.written_storage_slots.snapshot(),
storage_refunds: self.storage_refunds.snapshot(),
pubdata_costs: self.pubdata_costs.snapshot(),

read_storage_slots_ct: self.read_storage_slots_ct.snapshot(),
written_storage_slots_ct: self.written_storage_slots_ct.snapshot(),
}
}

Expand All @@ -329,11 +319,6 @@ impl WorldDiff {
.rollback(snapshot.read_storage_slots);
self.written_storage_slots
.rollback(snapshot.written_storage_slots);

self.read_storage_slots_ct
.rollback(snapshot.read_storage_slots_ct);
self.written_storage_slots_ct
.rollback(snapshot.written_storage_slots_ct);
}

pub(crate) fn delete_history(&mut self) {
Expand Down

0 comments on commit cf7b22b

Please sign in to comment.