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

Make runtime opcode handler generic over S and add missing gasometer extra_check #196

Merged
merged 2 commits into from
Nov 5, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 4 additions & 4 deletions gasometer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,23 @@ impl Gas for u64 {}
impl Gas for U256 {}

#[derive(Clone, Copy)]
pub enum MergeStrategy {
pub enum GasometerMergeStrategy {
Commit,
Revert,
Discard,
}

pub trait Gasometer<S, H>: Sized {
type Gas: Gas;
type Config;

fn new(gas_limit: Self::Gas, machine: &Machine<S>, config: Self::Config) -> Self;
fn record(
fn record_stepn(
self,
machine: &Machine<S>,
handler: &H,
is_static: bool,
) -> Result<(Self, usize), ExitError>;
fn record_codedeposit(self, len: usize) -> Result<Self, ExitError>;
fn gas(&self) -> Self::Gas;
fn merge(&mut self, other: Self, strategy: MergeStrategy);
fn merge(&mut self, other: Self, strategy: GasometerMergeStrategy);
}
31 changes: 25 additions & 6 deletions gasometer/src/standard.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{consts, costs, Config, Gasometer, MergeStrategy};
use crate::{consts, costs, Config, Gasometer, GasometerMergeStrategy};
use core::cmp::max;
use evm_interpreter::{ExitError, ExitException, Handler, Machine, Opcode, RuntimeState, Stack};
use primitive_types::{H160, H256, U256};
Expand Down Expand Up @@ -42,7 +42,7 @@ impl<'config, H: Handler> Gasometer<RuntimeState, H> for StandardGasometer<'conf
}
}

fn record(
fn record_stepn(
mut self,
machine: &Machine<RuntimeState>,
handler: &H,
Expand Down Expand Up @@ -77,25 +77,33 @@ impl<'config, H: Handler> Gasometer<RuntimeState, H> for StandardGasometer<'conf
self.memory_gas = max(self.memory_gas, memory_cost);
}
}

let after_gas = Gasometer::<RuntimeState, H>::gas(&self);
gas.extra_check(after_gas, self.config)?;
}

Ok((self, 1))
}

fn record_codedeposit(mut self, len: usize) -> Result<Self, ExitError> {
let cost = len as u64 * consts::G_CODEDEPOSIT;
self = self.record_cost(cost)?;
Ok(self)
}

fn gas(&self) -> u64 {
self.gas_limit - self.memory_gas - self.used_gas
}

fn merge(&mut self, other: Self, strategy: MergeStrategy) {
fn merge(&mut self, other: Self, strategy: GasometerMergeStrategy) {
match strategy {
MergeStrategy::Commit => {
GasometerMergeStrategy::Commit => {
self.used_gas -= Gasometer::<RuntimeState, H>::gas(&other);
self.refunded_gas += other.refunded_gas;
}
MergeStrategy::Revert => {
GasometerMergeStrategy::Revert => {
self.used_gas -= Gasometer::<RuntimeState, H>::gas(&other);
}
MergeStrategy::Discard => (),
}
}
}
Expand Down Expand Up @@ -573,6 +581,17 @@ impl GasCost {
_ => 0,
}
}

/// Extra check of the cost.
pub fn extra_check(&self, after_gas: u64, config: &Config) -> Result<(), ExitException> {
match *self {
GasCost::Call { gas, .. } => costs::call_extra_check(gas, after_gas, config),
GasCost::CallCode { gas, .. } => costs::call_extra_check(gas, after_gas, config),
GasCost::DelegateCall { gas, .. } => costs::call_extra_check(gas, after_gas, config),
GasCost::StaticCall { gas, .. } => costs::call_extra_check(gas, after_gas, config),
_ => Ok(()),
}
}
}

/// Memory cost.
Expand Down
Loading