Skip to content

Commit

Permalink
storage stats
Browse files Browse the repository at this point in the history
  • Loading branch information
montekki committed Aug 21, 2024
1 parent 4f7d9f5 commit 0ebaa09
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/instruction_handlers/decommit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::{
addressing_modes::{Arguments, Destination, Register1, Register2, Source},
fat_pointer::FatPointer,
instruction::ExecutionStatus,
vm::STORAGE_READ_STORAGE_APPLICATION_CYCLES,
Instruction, VirtualMachine, World,
};
use eravm_stable_interface::{opcodes, Tracer};
Expand Down Expand Up @@ -35,6 +36,8 @@ fn decommit<T: Tracer>(
let (program, is_fresh) = vm.world_diff.decommit_opcode(world, code_hash);
if !is_fresh {
vm.state.current_frame.gas += extra_cost;
} else {
vm.state.storage_application_cycles += STORAGE_READ_STORAGE_APPLICATION_CYCLES as usize;
}

let heap = vm.state.heaps.allocate_with_content(program.as_ref());
Expand Down
14 changes: 13 additions & 1 deletion src/instruction_handlers/far_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::{
fat_pointer::FatPointer,
instruction::ExecutionStatus,
predication::Flags,
vm::STORAGE_READ_STORAGE_APPLICATION_CYCLES,
Instruction, VirtualMachine, World,
};
use eravm_stable_interface::{
Expand All @@ -18,8 +19,9 @@ use eravm_stable_interface::{
};
use u256::U256;
use zkevm_opcode_defs::{
ethereum_types::Address,
system_params::{EVM_SIMULATOR_STIPEND, MSG_VALUE_SIMULATOR_ADDITIVE_COST},
ADDRESS_MSG_VALUE,
ADDRESS_MSG_VALUE, DEPLOYER_SYSTEM_CONTRACT_ADDRESS_LOW,
};

/// A call to another contract.
Expand Down Expand Up @@ -57,6 +59,16 @@ fn far_call<T: Tracer, M: TypeLevelCallingMode, const IS_STATIC: bool, const IS_
};

let failing_part = (|| {
let deployer_system_contract_address =
Address::from_low_u64_be(DEPLOYER_SYSTEM_CONTRACT_ADDRESS_LOW as u64);
if !vm
.world_diff
.read_storage_slots_ct
.contains(&(deployer_system_contract_address, destination_address))
{
vm.state.storage_application_cycles +=
STORAGE_READ_STORAGE_APPLICATION_CYCLES as usize;
}
let decommit_result = vm.world_diff.decommit(
world,
destination_address,
Expand Down
21 changes: 21 additions & 0 deletions src/instruction_handlers/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{
Arguments, Destination, Register1, Register2, Source, SLOAD_COST, SSTORE_COST,
},
instruction::ExecutionStatus,
vm::{STORAGE_READ_STORAGE_APPLICATION_CYCLES, STORAGE_WRITE_STORAGE_APPLICATION_CYCLES},
Instruction, VirtualMachine, World,
};
use eravm_stable_interface::{opcodes, Tracer};
Expand All @@ -16,6 +17,14 @@ fn sstore<T: Tracer>(
instruction_boilerplate::<opcodes::StorageWrite, _>(vm, world, tracer, |vm, args, world| {
let key = Register1::get(args, &mut vm.state);
let value = Register2::get(args, &mut vm.state);
if !vm
.world_diff
.written_storage_slots_ct
.contains(&(vm.state.current_frame.address, key))
{
vm.state.storage_application_cycles +=
STORAGE_WRITE_STORAGE_APPLICATION_CYCLES as usize;
}

let refund = vm
.world_diff
Expand Down Expand Up @@ -52,6 +61,18 @@ fn sload<T: Tracer>(
) -> ExecutionStatus {
instruction_boilerplate::<opcodes::StorageRead, _>(vm, world, tracer, |vm, args, world| {
let key = Register1::get(args, &mut vm.state);

if !vm
.world_diff
.read_storage_slots_ct
.contains(&(vm.state.current_frame.address, key))
&& !vm
.world_diff
.written_storage_slots_ct
.contains(&(vm.state.current_frame.address, key))
{
vm.state.storage_application_cycles += STORAGE_READ_STORAGE_APPLICATION_CYCLES as usize;
}
let (value, refund) =
vm.world_diff
.read_storage(world, vm.state.current_frame.address, key);
Expand Down
6 changes: 6 additions & 0 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub struct State<T> {
pub secp256v1_verify_cycles: usize,

pub code_decommitter_cycles: usize,
pub storage_application_cycles: usize,

pub(crate) context_u128: u128,
}
Expand Down Expand Up @@ -90,6 +91,7 @@ impl<T> State<T> {
sha256_cycles: 0,
secp256v1_verify_cycles: 0,
code_decommitter_cycles: 0,
storage_application_cycles: 0,
}
}

Expand Down Expand Up @@ -136,6 +138,7 @@ impl<T> State<T> {
sha256_cycles: self.sha256_cycles,
secp256v1_verify_cycles: self.secp256v1_verify_cycles,
code_decommitter_cycles: self.code_decommitter_cycles,
storage_application_cycles: self.storage_application_cycles,
}
}

Expand All @@ -153,6 +156,7 @@ impl<T> State<T> {
sha256_cycles,
secp256v1_verify_cycles,
code_decommitter_cycles,
storage_application_cycles,
} = snapshot;

for heap in self.current_frame.rollback(bootloader_frame) {
Expand All @@ -169,6 +173,7 @@ impl<T> State<T> {
self.sha256_cycles = sha256_cycles;
self.secp256v1_verify_cycles = secp256v1_verify_cycles;
self.code_decommitter_cycles = code_decommitter_cycles;
self.storage_application_cycles = storage_application_cycles
}

pub(crate) fn delete_history(&mut self) {
Expand Down Expand Up @@ -227,6 +232,7 @@ pub(crate) struct StateSnapshot {
sha256_cycles: usize,
secp256v1_verify_cycles: usize,
code_decommitter_cycles: usize,
storage_application_cycles: usize,

context_u128: u128,
}
3 changes: 3 additions & 0 deletions src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ use eravm_stable_interface::opcodes::TypeLevelCallingMode;
use eravm_stable_interface::{opcodes, CallingMode, HeapId, Tracer};
use u256::H160;

pub(crate) const STORAGE_READ_STORAGE_APPLICATION_CYCLES: u32 = 1;
pub(crate) const STORAGE_WRITE_STORAGE_APPLICATION_CYCLES: u32 = 2;

#[derive(Debug)]
pub struct Settings {
pub default_aa_code_hash: [u8; 32],
Expand Down
16 changes: 16 additions & 0 deletions src/world_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ pub struct WorldDiff {

// 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 @@ -41,6 +44,9 @@ 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 @@ -102,6 +108,7 @@ 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))
{
Expand Down Expand Up @@ -129,6 +136,7 @@ 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.storage_refunds.push(WARM_WRITE_REFUND);
self.pubdata_costs.push(0);
Expand Down Expand Up @@ -305,6 +313,9 @@ 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 @@ -318,6 +329,11 @@ 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 0ebaa09

Please sign in to comment.