Skip to content

Commit

Permalink
Compliance fix 3 (#220)
Browse files Browse the repository at this point in the history
  • Loading branch information
sorpaas authored Nov 19, 2023
1 parent 9f5645d commit fc97fb7
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 6 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,7 @@ jobs:
jsontests/res/ethtests/GeneralStateTests/stExample/ \
jsontests/res/ethtests/GeneralStateTests/stSLoadTest/ \
jsontests/res/ethtests/GeneralStateTests/VMTests/vmArithmeticTest/ \
jsontests/res/ethtests/GeneralStateTests/VMTests/vmBitwiseLogicOperation/
jsontests/res/ethtests/GeneralStateTests/VMTests/vmBitwiseLogicOperation/ \
jsontests/res/ethtests/GeneralStateTests/VMTests/vmIOandFlowOperations/ \
jsontests/res/ethtests/GeneralStateTests/VMTests/vmLogTest/
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@
target
**/result
tests.bin
Cargo.lock
Cargo.lock
perf.data
perf.data.old
9 changes: 9 additions & 0 deletions interpreter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ impl<S> Machine<S> {
}
}

#[inline]
/// Step the machine N times.
pub fn stepn<H, Tr, F>(
&mut self,
Expand Down Expand Up @@ -152,6 +153,10 @@ impl<S> Machine<S> {
where
F: Fn(&mut Machine<S>, &mut H, Opcode, usize) -> Control<Tr>,
{
if self.is_empty() {
return Err(Capture::Exit(ExitSucceed::Stopped.into()));
}

let position = self.position;
if position >= self.code.len() {
return Err(Capture::Exit(ExitFatal::AlreadyExited.into()));
Expand Down Expand Up @@ -189,6 +194,10 @@ impl<S> Machine<S> {
self.code.get(self.position).map(|opcode| Opcode(*opcode))
}

pub fn is_empty(&self) -> bool {
self.code.is_empty()
}

pub fn advance(&mut self) {
if self.position == self.code.len() {
return;
Expand Down
24 changes: 21 additions & 3 deletions src/standard/gasometer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ mod utils;

use crate::standard::Config;
use crate::{
ExitError, ExitException, Gasometer as GasometerT, Machine, MergeStrategy, Opcode,
ExitError, ExitException, ExitFatal, Gasometer as GasometerT, Machine, MergeStrategy, Opcode,
RuntimeBackend, RuntimeState, Stack,
};
use core::cmp::max;
use core::cmp::{max, min};
use primitive_types::{H160, H256, U256};

pub trait TransactGasometer<'config, S: AsRef<RuntimeState>>: Sized {
Expand All @@ -25,6 +25,8 @@ pub trait TransactGasometer<'config, S: AsRef<RuntimeState>>: Sized {
access_list: &Vec<(H160, Vec<H256>)>,
config: &'config Config,
) -> Result<Self, ExitError>;

fn effective_gas(&self) -> U256;
}

pub struct Gasometer<'config> {
Expand All @@ -36,6 +38,7 @@ pub struct Gasometer<'config> {
}

impl<'config> Gasometer<'config> {
#[inline]
pub fn perform<R, F: FnOnce(&mut Self) -> Result<R, ExitError>>(
&mut self,
f: F,
Expand Down Expand Up @@ -125,6 +128,17 @@ impl<'config, S: AsRef<RuntimeState>> TransactGasometer<'config, S> for Gasomete
s.record_cost(transaction_cost)?;
Ok(s)
}

fn effective_gas(&self) -> U256 {
U256::from(
self.gas_limit
- (self.total_used_gas()
- min(
self.total_used_gas() / self.config.max_refund_quotient,
self.refunded_gas,
)),
)
}
}

impl<'config, S: AsRef<RuntimeState>, H: RuntimeBackend> GasometerT<S, H> for Gasometer<'config> {
Expand All @@ -134,8 +148,12 @@ impl<'config, S: AsRef<RuntimeState>, H: RuntimeBackend> GasometerT<S, H> for Ga
is_static: bool,
handler: &H,
) -> Result<(), ExitError> {
if machine.is_empty() {
return Ok(());
}

self.perform(|gasometer| {
let opcode = machine.peek_opcode().ok_or(ExitException::OutOfGas)?;
let opcode = machine.peek_opcode().ok_or(ExitFatal::AlreadyExited)?;

if let Some(cost) = consts::STATIC_COST_TABLE[opcode.as_usize()] {
gasometer.record_cost(cost)?;
Expand Down
2 changes: 1 addition & 1 deletion src/standard/invoker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ where
mut machine: GasedMachine<S, G>,
handler: &mut H,
) -> Result<Self::TransactValue, ExitError> {
let left_gas = machine.gasometer.gas();
let left_gas = machine.gasometer.effective_gas();

let work = || -> Result<Self::TransactValue, ExitError> {
if result.is_ok() {
Expand Down

0 comments on commit fc97fb7

Please sign in to comment.