Skip to content

Commit

Permalink
Refactor: consistent params and generics order
Browse files Browse the repository at this point in the history
  • Loading branch information
sorpaas committed Nov 7, 2023
1 parent 2337db3 commit ffb6009
Show file tree
Hide file tree
Showing 15 changed files with 224 additions and 148 deletions.
3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ scale-info = { version = "2.3", default-features = false, features = ["derive"],
serde = { version = "1.0", default-features = false, features = ["derive"], optional = true }

evm-interpreter = { version = "0.2.0-dev", path = "interpreter", default-features = false }
evm-gasometer = { version = "0.40", path = "gasometer", default-features = false }

[dev-dependencies]
criterion = "0.4"
Expand All @@ -42,7 +41,6 @@ std = [
"scale-info/std",
"serde/std",
"evm-interpreter/std",
"evm-gasometer/std",
]
with-codec = [
"scale-codec",
Expand All @@ -62,5 +60,4 @@ with-serde = [
[workspace]
members = [
"interpreter",
"gasometer",
]
21 changes: 0 additions & 21 deletions gasometer/Cargo.toml

This file was deleted.

2 changes: 1 addition & 1 deletion interpreter/src/eval/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ pub fn gas<S: AsRef<RuntimeState>, H: Handler, Tr>(
machine: &mut Machine<S>,
handler: &H,
) -> Control<Tr> {
push_u256!(machine, handler.gas_left());
push_u256!(machine, handler.gas());

Control::Continue
}
Expand Down
2 changes: 1 addition & 1 deletion interpreter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub use crate::error::{Capture, ExitError, ExitException, ExitFatal, ExitResult,
pub use crate::eval::{Control, Efn, Etable};
pub use crate::memory::Memory;
pub use crate::opcode::Opcode;
pub use crate::runtime::{CallCreateTrap, Context, Handler, RuntimeMachine, RuntimeState};
pub use crate::runtime::{CallCreateTrap, Context, Handler, RuntimeState};
pub use crate::stack::Stack;
pub use crate::valids::Valids;

Expand Down
146 changes: 115 additions & 31 deletions interpreter/src/runtime.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use crate::{ExitError, Opcode};
use primitive_types::{H160, H256, U256};

/// Runtime machine.
pub type RuntimeMachine = crate::Machine<RuntimeState>;

/// Runtime state.
#[derive(Clone, Debug)]
pub struct RuntimeState {
Expand Down Expand Up @@ -35,6 +32,29 @@ impl CallCreateTrap for Opcode {
}

pub trait RuntimeBackend {
/// Get environmental block hash.
fn block_hash(&self, number: U256) -> H256;
/// Get environmental block number.
fn block_number(&self) -> U256;
/// Get environmental coinbase.
fn block_coinbase(&self) -> H160;
/// Get environmental block timestamp.
fn block_timestamp(&self) -> U256;
/// Get environmental block difficulty.
fn block_difficulty(&self) -> U256;
/// Get environmental block randomness.
fn block_randomness(&self) -> Option<H256>;
/// Get environmental gas limit.
fn block_gas_limit(&self) -> U256;
/// Environmental block base fee.
fn block_base_fee_per_gas(&self) -> U256;
/// Get environmental chain ID.
fn chain_id(&self) -> U256;
/// Get the gas price value.
fn gas_price(&self) -> U256;
/// Get execution origin.
fn origin(&self) -> H160;

/// Get balance of address.
fn balance(&self, address: H160) -> U256;
/// Get code size of address.
Expand Down Expand Up @@ -65,35 +85,99 @@ pub trait RuntimeBackend {
fn mark_delete(&mut self, address: H160, target: H160) -> Result<(), ExitError>;
}

pub trait RuntimeEnvironmentalBackend {
/// Get environmental block hash.
fn block_hash(&self, number: U256) -> H256;
/// Get environmental block number.
fn block_number(&self) -> U256;
/// Get environmental coinbase.
fn block_coinbase(&self) -> H160;
/// Get environmental block timestamp.
fn block_timestamp(&self) -> U256;
/// Get environmental block difficulty.
fn block_difficulty(&self) -> U256;
/// Get environmental block randomness.
fn block_randomness(&self) -> Option<H256>;
/// Get environmental gas limit.
fn block_gas_limit(&self) -> U256;
/// Environmental block base fee.
fn block_base_fee_per_gas(&self) -> U256;
/// Get environmental chain ID.
fn chain_id(&self) -> U256;
/// Get the gas price value.
fn gas_price(&self) -> U256;
/// Get execution origin.
fn origin(&self) -> H160;
pub trait RuntimeGasometer {
/// Get the gas left value.
fn gas(&self) -> U256;
}

pub trait RuntimeGasometerBackend {
/// Get the gas left value.
fn gas_left(&self) -> U256;
/// Handler trait for runtime.
///
/// The handler is generally expected to be a `(backend, gasometer)` tuple, with extensions added
/// to `backend`.
pub trait Handler: RuntimeBackend + RuntimeGasometer {}

impl<'b, 'g, G: RuntimeGasometer, H: RuntimeBackend> RuntimeBackend for (&'b mut G, &'g mut H) {
fn block_hash(&self, number: U256) -> H256 {
self.1.block_hash(number)
}
fn block_number(&self) -> U256 {
self.1.block_number()
}
fn block_coinbase(&self) -> H160 {
self.1.block_coinbase()
}
fn block_timestamp(&self) -> U256 {
self.1.block_timestamp()
}
fn block_difficulty(&self) -> U256 {
self.1.block_difficulty()
}
fn block_randomness(&self) -> Option<H256> {
self.1.block_randomness()
}
fn block_gas_limit(&self) -> U256 {
self.1.block_gas_limit()
}
fn block_base_fee_per_gas(&self) -> U256 {
self.1.block_base_fee_per_gas()
}
fn chain_id(&self) -> U256 {
self.1.chain_id()
}
fn gas_price(&self) -> U256 {
self.1.gas_price()
}
fn origin(&self) -> H160 {
self.1.origin()
}

fn balance(&self, address: H160) -> U256 {
self.1.balance(address)
}
fn code_size(&self, address: H160) -> U256 {
self.1.code_size(address)
}
fn code_hash(&self, address: H160) -> H256 {
self.1.code_hash(address)
}
fn code(&self, address: H160) -> Vec<u8> {
self.1.code(address)
}
fn storage(&self, address: H160, index: H256) -> H256 {
self.1.storage(address, index)
}
fn original_storage(&self, address: H160, index: H256) -> H256 {
self.1.original_storage(address, index)
}

fn exists(&self, address: H160) -> bool {
self.1.exists(address)
}
fn deleted(&self, address: H160) -> bool {
self.1.deleted(address)
}
fn is_cold(&self, address: H160, index: Option<H256>) -> bool {
self.1.is_cold(address, index)
}
fn mark_hot(&mut self, address: H160, index: Option<H256>) -> Result<(), ExitError> {
self.1.mark_hot(address, index)
}
fn set_storage(&mut self, address: H160, index: H256, value: H256) -> Result<(), ExitError> {
self.1.set_storage(address, index, value)
}

fn log(&mut self, address: H160, topics: Vec<H256>, data: Vec<u8>) -> Result<(), ExitError> {
self.1.log(address, topics, data)
}
fn mark_delete(&mut self, address: H160, target: H160) -> Result<(), ExitError> {
self.1.mark_delete(address, target)
}
}

impl<'b, 'g, G: RuntimeGasometer, H: RuntimeBackend> RuntimeGasometer for (&'b mut G, &'g mut H) {
fn gas(&self) -> U256 {
self.0.gas()
}
}

/// EVM context handler.
pub trait Handler: RuntimeBackend + RuntimeEnvironmentalBackend + RuntimeGasometerBackend {}
impl<'b, 'g, G: RuntimeGasometer, H: RuntimeBackend> Handler for (&'b mut G, &'g mut H) {}
Loading

0 comments on commit ffb6009

Please sign in to comment.