Skip to content

Commit

Permalink
New Invoker / Callstack (#216)
Browse files Browse the repository at this point in the history
* Generalize RuntimeState in standard invoker

* Generalize interrupt

* [WIP] New call stack design

* Finish new invoker / callstack design
  • Loading branch information
sorpaas committed Nov 17, 2023
1 parent 1f05d4c commit 5c94026
Show file tree
Hide file tree
Showing 13 changed files with 769 additions and 715 deletions.
4 changes: 4 additions & 0 deletions interpreter/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ pub enum ExitFatal {
UnhandledInterrupt,
/// The environment explicitly set call errors as fatal error.
ExceptionAsFatal(ExitException),
/// Already exited.
AlreadyExited,
/// Unfinished execution.
Unfinished,

/// Other fatal errors.
Other(Cow<'static, str>),
Expand Down
10 changes: 8 additions & 2 deletions interpreter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,13 @@ impl<S> Machine<S> {
{
let position = self.position;
if position >= self.code.len() {
return Err(Capture::Exit(ExitSucceed::Stopped.into()));
return Err(Capture::Exit(ExitFatal::AlreadyExited.into()));
}

let opcode = Opcode(self.code[position]);
let control = etable[opcode.as_usize()](self, handle, opcode, self.position);

match control {
let mut ret = match control {
Control::Continue => {
self.position += 1;
Ok(())
Expand All @@ -181,7 +181,13 @@ impl<S> Machine<S> {
self.position = position + 1;
Err(Capture::Trap(opcode))
}
};

if position >= self.code.len() {
ret = Err(Capture::Exit(ExitSucceed::Stopped.into()));
}

ret
}

/// Pick the next opcode.
Expand Down
23 changes: 14 additions & 9 deletions jsontests/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ use crate::types::*;
use evm::backend::in_memory::{
InMemoryAccount, InMemoryBackend, InMemoryEnvironment, InMemoryLayer,
};
use evm::standard::{Config, Etable, Gasometer, Invoker};
use evm::standard::{Config, Etable, Gasometer, Invoker, TransactArgs};
use evm::utils::u256_to_h256;
use evm::RuntimeState;
use primitive_types::U256;
use std::collections::{BTreeMap, BTreeSet};

Expand Down Expand Up @@ -62,15 +63,19 @@ pub fn run_test(_filename: &str, _test_name: &str, test: Test) -> Result<(), Err

let etable = Etable::runtime();
let invoker = Invoker::new(&config);
let _result = invoker.transact_call::<Gasometer, _>(
test.transaction.sender,
test.transaction.to,
test.transaction.value,
test.transaction.data,
test.transaction.gas_limit,
test.transaction.gas_price,
Vec::new(),
let _result = evm::transact::<RuntimeState, Gasometer, _, _, _, _>(
TransactArgs::Call {
caller: test.transaction.sender,
address: test.transaction.to,
value: test.transaction.value,
data: test.transaction.data,
gas_limit: test.transaction.gas_limit,
gas_price: test.transaction.gas_price,
access_list: Vec::new(),
},
Some(4),
&mut backend,
&invoker,
&etable,
);

Expand Down
10 changes: 5 additions & 5 deletions src/backend/in_memory.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
ExitError, ExitException, Log, RuntimeBackend, RuntimeBaseBackend, RuntimeEnvironment,
TransactionalBackend, TransactionalMergeStrategy,
ExitError, ExitException, Log, MergeStrategy, RuntimeBackend, RuntimeBaseBackend,
RuntimeEnvironment, TransactionalBackend,
};
use alloc::collections::{BTreeMap, BTreeSet};
use primitive_types::{H160, H256, U256};
Expand Down Expand Up @@ -249,14 +249,14 @@ impl TransactionalBackend for InMemoryBackend {
self.layers.push(layer);
}

fn pop_substate(&mut self, strategy: TransactionalMergeStrategy) {
fn pop_substate(&mut self, strategy: MergeStrategy) {
let layer = self.layers.pop().expect("current layer exist");

match strategy {
TransactionalMergeStrategy::Commit => {
MergeStrategy::Commit => {
*self.current_layer_mut() = layer;
}
TransactionalMergeStrategy::Discard => (),
MergeStrategy::Discard | MergeStrategy::Revert => (),
}
}
}
7 changes: 1 addition & 6 deletions src/backend/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
pub mod in_memory;

pub enum TransactionalMergeStrategy {
Commit,
Discard,
}

pub trait TransactionalBackend {
fn push_substate(&mut self);
fn pop_substate(&mut self, strategy: TransactionalMergeStrategy);
fn pop_substate(&mut self, strategy: crate::MergeStrategy);
}
Loading

0 comments on commit 5c94026

Please sign in to comment.