Skip to content
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
5 changes: 2 additions & 3 deletions core/src/opcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ impl Opcode {
}

impl fmt::Display for Opcode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
//write!(f, "{}", "urgh...")
let op = match self {
&Opcode::STOP => "STOP",
Expand Down Expand Up @@ -423,6 +423,5 @@ impl fmt::Display for Opcode {
_ => "UNKNOWNOP",
};
write!(f, "{}", op)
}
}
}

8 changes: 8 additions & 0 deletions src/backend/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ impl<'vicinity> Backend for MemoryBackend<'vicinity> {
.unwrap_or_default()
}

fn code_as_json(&self, address: H160) -> Vec<u8> {
self.code(address)
}

fn storage(&self, address: H160, index: H256) -> H256 {
self.state
.get(&address)
Expand All @@ -152,6 +156,10 @@ impl<'vicinity> Backend for MemoryBackend<'vicinity> {
fn original_storage(&self, address: H160, index: H256) -> Option<H256> {
Some(self.storage(address, index))
}

fn substate_as_json(&self, _address: H160, _vname: &str, _indices: &[String]) -> Vec<u8> {
Vec::new()
}
}

impl<'vicinity> ApplyBackend for MemoryBackend<'vicinity> {
Expand Down
6 changes: 5 additions & 1 deletion src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,14 @@ pub trait Backend {
fn basic(&self, address: H160) -> Basic;
/// Get account code.
fn code(&self, address: H160) -> Vec<u8>;
/// Get storage value of address at index.
/// Get account code formatted as json (if possible)
fn code_as_json(&self, address: H160) -> Vec<u8>;
/// Get storage value of address at inasex.
fn storage(&self, address: H160, index: H256) -> H256;
/// Get original storage value of address at index, if available.
fn original_storage(&self, address: H160, index: H256) -> Option<H256>;
/// Get contract substate with given array of indices
fn substate_as_json(&self, address: H160, vname: &str, indices: &[String]) -> Vec<u8>;
}

/// EVM backend that can apply changes.
Expand Down
35 changes: 24 additions & 11 deletions src/executor/stack/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,20 +285,23 @@ pub trait PrecompileHandle {
/// Record a log.
fn log(&mut self, address: H160, topics: Vec<H256>, data: Vec<u8>) -> Result<(), ExitError>;

/// Retreive the code address (what is the address of the precompile being called).
/// Retrieve the code address (what is the address of the precompile being called).
fn code_address(&self) -> H160;

/// Retreive the input data the precompile is called with.
/// Retrieve the input data the precompile is called with.
fn input(&self) -> &[u8];

/// Retreive the context in which the precompile is executed.
/// Retrieve the context in which the precompile is executed.
fn context(&self) -> &Context;

/// Is the precompile call is done statically.
fn is_static(&self) -> bool;

/// Retreive the gas limit of this call.
/// Retrieve the gas limit of this call.
fn gas_limit(&self) -> Option<u64>;

/// Retrieve backend
fn backend(&self) -> &dyn Backend;
}

/// A precompile result.
Expand Down Expand Up @@ -335,8 +338,13 @@ impl PrecompileSet for () {
/// * Is static
///
/// In case of success returns the output and the cost.
pub type PrecompileFn =
fn(&[u8], Option<u64>, &Context, bool) -> Result<(PrecompileOutput, u64), PrecompileFailure>;
pub type PrecompileFn = fn(
&[u8],
Option<u64>,
&Context,
&dyn Backend,
bool,
) -> Result<(PrecompileOutput, u64), PrecompileFailure>;

impl PrecompileSet for BTreeMap<H160, PrecompileFn> {
fn execute(&self, handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {
Expand All @@ -347,8 +355,9 @@ impl PrecompileSet for BTreeMap<H160, PrecompileFn> {
let gas_limit = handle.gas_limit();
let context = handle.context();
let is_static = handle.is_static();
let backend = handle.backend();

match (*precompile)(input, gas_limit, context, is_static) {
match (*precompile)(input, gas_limit, context, backend, is_static) {
Ok((output, cost)) => {
handle.record_cost(cost)?;
Ok(output)
Expand Down Expand Up @@ -1330,17 +1339,17 @@ impl<'inner, 'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> Pr
Handler::log(self.executor, address, topics, data)
}

/// Retreive the code address (what is the address of the precompile being called).
/// Retrieve the code address (what is the address of the precompile being called).
fn code_address(&self) -> H160 {
self.code_address
}

/// Retreive the input data the precompile is called with.
/// Retrieve the input data the precompile is called with.
fn input(&self) -> &[u8] {
self.input
}

/// Retreive the context in which the precompile is executed.
/// Retrieve the context in which the precompile is executed.
fn context(&self) -> &Context {
self.context
}
Expand All @@ -1350,8 +1359,12 @@ impl<'inner, 'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> Pr
self.is_static
}

/// Retreive the gas limit of this call.
/// Retrieve the gas limit of this call.
fn gas_limit(&self) -> Option<u64> {
self.gas_limit
}

fn backend(&self) -> &dyn Backend {
&self.executor.state
}
}
8 changes: 8 additions & 0 deletions src/executor/stack/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,10 @@ impl<'backend, 'config, B: Backend> Backend for MemoryStackState<'backend, 'conf
.unwrap_or_else(|| self.backend.code(address))
}

fn code_as_json(&self, address: H160) -> Vec<u8> {
self.backend.code_as_json(address)
}

fn storage(&self, address: H160, key: H256) -> H256 {
self.substate
.known_storage(address, key)
Expand All @@ -490,6 +494,10 @@ impl<'backend, 'config, B: Backend> Backend for MemoryStackState<'backend, 'conf

self.backend.original_storage(address, key)
}

fn substate_as_json(&self, address: H160, vname: &str, indices: &[String]) -> Vec<u8> {
self.backend.substate_as_json(address, vname, indices)
}
}

impl<'backend, 'config, B: Backend> StackState<'config> for MemoryStackState<'backend, 'config, B> {
Expand Down