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

feat: expose evm internal fns #62

Merged
merged 12 commits into from
Oct 12, 2021
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ serde = { version = "1.0", default-features = false, features = ["derive"], opti
codec = { package = "parity-scale-codec", version = "2.0", default-features = false, features = ["derive"], optional = true }
ethereum = { version = "~0.8", default-features = false }
environmental = { version = "1.1.2", default-features = false, optional = true }
auto_impl = "0.4.1"

[dev-dependencies]
criterion = "0.3"
Expand Down
4 changes: 2 additions & 2 deletions gasometer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub struct Snapshot {
}

/// EVM gasometer.
#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct Gasometer<'config> {
gas_limit: u64,
config: &'config Config,
Expand Down Expand Up @@ -674,7 +674,7 @@ pub fn dynamic_opcode_cost<H: Handler>(
}

/// Holds the gas consumption for a Gasometer instance.
#[derive(Clone)]
#[derive(Clone, Debug)]
struct Inner<'config> {
memory_gas: u64,
used_gas: u64,
Expand Down
1 change: 1 addition & 0 deletions runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ evm-core = { version = "0.30", path = "../core", default-features = false }
primitive-types = { version = "0.10", default-features = false }
sha3 = { version = "0.8", default-features = false }
environmental = { version = "1.1.2", default-features = false, optional = true}
auto_impl = "0.4.1"

[features]
default = ["std"]
Expand Down
1 change: 1 addition & 0 deletions runtime/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub struct Transfer {
}

/// EVM context handler.
#[auto_impl::auto_impl(&mut, Box)]
pub trait Handler {
/// Type of `CREATE` interrupt.
type CreateInterrupt;
Expand Down
5 changes: 5 additions & 0 deletions src/backend/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ impl<'vicinity> MemoryBackend<'vicinity> {
pub fn state(&self) -> &BTreeMap<H160, MemoryAccount> {
&self.state
}

/// Get a mutable reference to the underlying `BTreeMap` storing the state.
pub fn state_mut(&mut self) -> &mut BTreeMap<H160, MemoryAccount> {
&mut self.state
}
}

impl<'vicinity> Backend for MemoryBackend<'vicinity> {
Expand Down
1 change: 1 addition & 0 deletions src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub enum Apply<I> {
}

/// EVM backend.
#[auto_impl::auto_impl(&, Arc, Box)]
pub trait Backend {
/// Gas price.
fn gas_price(&self) -> U256;
Expand Down
6 changes: 4 additions & 2 deletions src/executor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
mod stack;

pub use self::stack::{
MemoryStackState, Precompile, PrecompileOutput, StackExecutor, StackExitKind, StackState,
StackSubstateMetadata,
MemoryStackAccount, MemoryStackState, MemoryStackSubstate, Precompile, PrecompileOutput,
StackExecutor, StackExitKind, StackState, StackSubstateMetadata,
};

pub use ethereum::Log;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this though?

Copy link
Contributor Author

@gakonst gakonst Sep 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because of this. Ideally I'd like to avoid having to import ethereum explicitly in my crate and have to manage its version & make sure it's in line with sputnik's.

25 changes: 15 additions & 10 deletions src/executor/stack/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod state;

pub use self::state::{MemoryStackState, MemoryStackSubstate, StackState};
pub use self::state::{MemoryStackAccount, MemoryStackState, MemoryStackSubstate, StackState};

use crate::gasometer::{self, Gasometer, StorageTarget};
use crate::{
Expand Down Expand Up @@ -43,18 +43,18 @@ pub enum StackExitKind {
Failed,
}

#[derive(Default)]
#[derive(Default, Clone, Debug)]
struct Accessed {
accessed_addresses: BTreeSet<H160>,
accessed_storage: BTreeSet<(H160, H256)>,
}

impl Accessed {
fn access_address(&mut self, address: H160) {
pub fn access_address(&mut self, address: H160) {
self.accessed_addresses.insert(address);
}

fn access_addresses<I>(&mut self, addresses: I)
pub fn access_addresses<I>(&mut self, addresses: I)
where
I: Iterator<Item = H160>,
{
Expand All @@ -63,7 +63,7 @@ impl Accessed {
}
}

fn access_storages<I>(&mut self, storages: I)
pub fn access_storages<I>(&mut self, storages: I)
where
I: Iterator<Item = (H160, H256)>,
{
Expand All @@ -73,6 +73,7 @@ impl Accessed {
}
}

#[derive(Clone, Debug)]
pub struct StackSubstateMetadata<'config> {
gasometer: Gasometer<'config>,
is_static: bool,
Expand Down Expand Up @@ -152,13 +153,13 @@ impl<'config> StackSubstateMetadata<'config> {
self.depth
}

fn access_address(&mut self, address: H160) {
pub fn access_address(&mut self, address: H160) {
if let Some(accessed) = &mut self.accessed {
accessed.access_address(address)
}
}

fn access_addresses<I>(&mut self, addresses: I)
pub fn access_addresses<I>(&mut self, addresses: I)
where
I: Iterator<Item = H160>,
{
Expand All @@ -167,13 +168,13 @@ impl<'config> StackSubstateMetadata<'config> {
}
}

fn access_storage(&mut self, address: H160, key: H256) {
pub fn access_storage(&mut self, address: H160, key: H256) {
if let Some(accessed) = &mut self.accessed {
accessed.accessed_storage.insert((address, key));
}
}

fn access_storages<I>(&mut self, storages: I)
pub fn access_storages<I>(&mut self, storages: I)
where
I: Iterator<Item = (H160, H256)>,
{
Expand Down Expand Up @@ -216,6 +217,10 @@ impl<'config, S: StackState<'config>> StackExecutor<'config, S> {
self.config
}

pub fn precompile(&self) -> &Precompile {
&self.precompile
}

/// Create a new stack-based executor with given precompiles.
pub fn new_with_precompile(state: S, config: &'config Config, precompile: Precompile) -> Self {
Self {
Expand Down Expand Up @@ -471,7 +476,7 @@ impl<'config, S: StackState<'config>> StackExecutor<'config, S> {
}
}

fn initialize_with_access_list(&mut self, access_list: Vec<(H160, Vec<H256>)>) {
pub fn initialize_with_access_list(&mut self, access_list: Vec<(H160, Vec<H256>)>) {
let addresses = access_list.iter().map(|a| a.0);
self.state.metadata_mut().access_addresses(addresses);

Expand Down
11 changes: 9 additions & 2 deletions src/executor/stack/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ use core::mem;
use primitive_types::{H160, H256, U256};

#[derive(Clone, Debug)]
struct MemoryStackAccount {
pub struct MemoryStackAccount {
pub basic: Basic,
pub code: Option<Vec<u8>>,
pub reset: bool,
}

#[derive(Clone, Debug)]
pub struct MemoryStackSubstate<'config> {
metadata: StackSubstateMetadata<'config>,
parent: Option<Box<MemoryStackSubstate<'config>>>,
Expand All @@ -37,6 +38,10 @@ impl<'config> MemoryStackSubstate<'config> {
}
}

pub fn logs(&self) -> &[Log] {
&self.logs
}

pub fn metadata(&self) -> &StackSubstateMetadata<'config> {
&self.metadata
}
Expand Down Expand Up @@ -165,7 +170,7 @@ impl<'config> MemoryStackSubstate<'config> {
Ok(())
}

fn known_account(&self, address: H160) -> Option<&MemoryStackAccount> {
pub fn known_account(&self, address: H160) -> Option<&MemoryStackAccount> {
if let Some(account) = self.accounts.get(&address) {
Some(account)
} else if let Some(parent) = self.parent.as_ref() {
Expand Down Expand Up @@ -384,6 +389,7 @@ impl<'config> MemoryStackSubstate<'config> {
}
}

#[auto_impl::auto_impl(&mut, Box)]
pub trait StackState<'config>: Backend {
fn metadata(&self) -> &StackSubstateMetadata<'config>;
fn metadata_mut(&mut self) -> &mut StackSubstateMetadata<'config>;
Expand All @@ -409,6 +415,7 @@ pub trait StackState<'config>: Backend {
fn touch(&mut self, address: H160);
}

#[derive(Clone, Debug)]
pub struct MemoryStackState<'backend, 'config, B> {
backend: &'backend B,
substate: MemoryStackSubstate<'config>,
Expand Down