Skip to content

Commit

Permalink
count decommit cycles of Decommit opcode, too
Browse files Browse the repository at this point in the history
  • Loading branch information
joonazan committed Sep 3, 2024
1 parent b6c8d3c commit 58cf727
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 11 deletions.
19 changes: 11 additions & 8 deletions src/decommit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,20 @@ impl WorldDiff {
code_hash: U256,
) -> (Vec<u8>, bool) {
let is_new = !self.decommitted_hashes.add(code_hash);
(world.decommit_code(code_hash), is_new)
let code = world.decommit_code(code_hash);
if is_new {
// Decommitter can process two words per cycle
self.decommit_cycles += (code.len() as u32 + 63) / 64;
}
(code, is_new)
}

pub(crate) fn pay_for_decommit<T, W: World<T>>(
&mut self,
world: &mut W,
decommit: UnpaidDecommit,
gas: &mut u32,
) -> Option<(Program<T, W>, usize)> {
) -> Option<Program<T, W>> {
// We intentionally record a decommitment event even if actual decommitment never happens because of an out-of-gas error.
// This is how the old VM behaves.
let is_new = !self.decommitted_hashes.add(decommit.code_key);
Expand All @@ -106,13 +111,11 @@ impl WorldDiff {
*gas -= decommit.cost;

let decommit = world.decommit(decommit.code_key);
let decommit_cycles = if is_new {
(decommit.code_page().len() + 1) / 2
} else {
0
};
if is_new {
self.decommit_cycles += (decommit.code_page().len() as u32 + 1) / 2;
}

Some((decommit, decommit_cycles))
Some(decommit)
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/instruction_handlers/far_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,9 @@ fn far_call<
.expect("stipend must not cause overflow");

let new_frame_is_static = IS_STATIC || vm.state.current_frame.is_static;
vm.state.cycle_counts.code_decommitter_cycles += program.1;
vm.push_frame::<M>(
u256_into_address(destination_address),
program.0,
program,
new_frame_gas,
stipend,
exception_handler,
Expand Down
1 change: 0 additions & 1 deletion src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ pub struct CycleCounts {
pub ecrecover_cycles: usize,
pub sha256_cycles: usize,
pub secp256v1_verify_cycles: usize,
pub code_decommitter_cycles: usize,
}

impl PartialEq for CycleCounts {
Expand Down
1 change: 1 addition & 0 deletions src/world_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub struct WorldDiff {
storage_initial_values: BTreeMap<(H160, U256), Option<U256>>,

pub storage_application_cycles: u32,
pub decommit_cycles: u32,
}

#[derive(Debug)]
Expand Down

0 comments on commit 58cf727

Please sign in to comment.