Skip to content

Commit

Permalink
New callstack implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
sorpaas committed Nov 6, 2023
1 parent 0924e66 commit 48d8511
Show file tree
Hide file tree
Showing 23 changed files with 324 additions and 3,006 deletions.
25 changes: 2 additions & 23 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: Rust

on:
push:
branches: [ master ]
branches: [ master, next ]
pull_request:
branches: [ master ]
branches: [ master, next ]

env:
CARGO_TERM_COLOR: always
Expand All @@ -16,32 +16,11 @@ jobs:
- uses: actions/checkout@v2
- name: Rustfmt
run: cargo fmt --all -- --check
- name: Clippy
run: cargo clippy --all
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Build
run: cargo build --verbose
- name: Build for feature (tracing)
run: cargo build --features tracing --verbose
- name: Run tests
run: cargo test --verbose
jsontests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
repository: "rust-blockchain/evm-tests"
submodules: recursive
- name: Submodules
run: |
cd evm
git remote set-url origin "$GITHUB_SERVER_URL/$GITHUB_REPOSITORY"
git fetch origin $GITHUB_SHA
git checkout $GITHUB_SHA
- name: Run tests
run: |
cd jsontests
cargo test --release --verbose
5 changes: 0 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ evm-gasometer = { version = "0.40", path = "gasometer", default-features = false
criterion = "0.4"
hex = "0.4"

[[bench]]
name = "loop"
harness = false

[features]
default = ["std"]
std = [
Expand Down Expand Up @@ -67,5 +63,4 @@ with-serde = [
members = [
"interpreter",
"gasometer",
"fuzzer",
]
68 changes: 0 additions & 68 deletions benches/loop.rs

This file was deleted.

16 changes: 0 additions & 16 deletions fuzzer/Cargo.toml

This file was deleted.

18 changes: 0 additions & 18 deletions fuzzer/README.md

This file was deleted.

72 changes: 0 additions & 72 deletions fuzzer/src/main.rs

This file was deleted.

11 changes: 6 additions & 5 deletions gasometer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub use crate::config::Config;
pub use crate::standard::StandardGasometer;

use core::ops::{Add, AddAssign, Sub, SubAssign};
use evm_interpreter::{Capture, Control, Etable, ExitError, ExitResult, Machine, Opcode, Trap};
use evm_interpreter::{Capture, Control, Etable, ExitError, ExitResult, Machine, Opcode};
use primitive_types::U256;

pub trait Gas:
Expand All @@ -32,6 +32,7 @@ impl Gas for U256 {}
pub enum GasometerMergeStrategy {
Commit,
Revert,
Discard,
}

pub trait Gasometer<S, H>: Sized {
Expand All @@ -50,16 +51,16 @@ pub trait Gasometer<S, H>: Sized {
fn merge(&mut self, other: Self, strategy: GasometerMergeStrategy);
}

pub fn run_with_gasometer<S, H, G, F>(
pub fn run_with_gasometer<S, H, Tr, G, F>(
machine: &mut Machine<S>,
gasometer: &mut G,
handler: &mut H,
is_static: bool,
etable: &Etable<S, H, F>,
) -> Capture<ExitResult, Trap>
etable: &Etable<S, H, Tr, F>,
) -> Capture<ExitResult, Tr>
where
G: Gasometer<S, H>,
F: Fn(&mut Machine<S>, &mut H, Opcode, usize) -> Control,
F: Fn(&mut Machine<S>, &mut H, Opcode, usize) -> Control<Tr>,
{
loop {
match gasometer.record_stepn(&machine, handler, is_static) {
Expand Down
1 change: 1 addition & 0 deletions gasometer/src/standard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ impl<'config, H: Handler> Gasometer<RuntimeState, H> for StandardGasometer<'conf
GasometerMergeStrategy::Revert => {
self.used_gas -= Gasometer::<RuntimeState, H>::gas(&other);
}
GasometerMergeStrategy::Discard => (),
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions interpreter/src/call_create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ pub struct Transfer {
pub value: U256,
}

pub enum CallCreateTrapData {
Call(CallTrapData),
Create(CreateTrapData),
}

pub struct CallTrapData {
pub target: H160,
pub transfer: Option<Transfer>,
Expand Down
54 changes: 30 additions & 24 deletions interpreter/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ impl CallCreateTrap for Opcode {
}
}

/// EVM context handler.
#[auto_impl::auto_impl(&mut, Box)]
pub trait Handler {
pub trait RuntimeBackend {
/// Get balance of address.
fn balance(&self, address: H160) -> U256;
/// Get code size of address.
Expand All @@ -50,12 +48,24 @@ pub trait Handler {
/// Get original storage value of address at index.
fn original_storage(&self, address: H160, index: H256) -> H256;

/// Get the gas left value.
fn gas_left(&self) -> U256;
/// Get the gas price value.
fn gas_price(&self) -> U256;
/// Get execution origin.
fn origin(&self) -> H160;
/// Check whether an address exists.
fn exists(&self, address: H160) -> bool;
/// Check whether an address has already been deleted.
fn deleted(&self, address: H160) -> bool;
/// Checks if the address or (address, index) pair has been previously accessed.
fn is_cold(&self, address: H160, index: Option<H256>) -> bool;
/// Mark an address or (address, index) pair as hot.
fn mark_hot(&mut self, address: H160, index: Option<H256>) -> Result<(), ExitError>;
/// Set storage value of address at index.
fn set_storage(&mut self, address: H160, index: H256, value: H256) -> Result<(), ExitError>;

/// Create a log owned by address with given topics and data.
fn log(&mut self, address: H160, topics: Vec<H256>, data: Vec<u8>) -> Result<(), ExitError>;
/// Mark an address to be deleted, with funds transferred to target.
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.
Expand All @@ -74,20 +84,16 @@ pub trait Handler {
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;
}

/// Check whether an address exists.
fn exists(&self, address: H160) -> bool;
/// Check whether an address has already been deleted.
fn deleted(&self, address: H160) -> bool;
/// Checks if the address or (address, index) pair has been previously accessed.
fn is_cold(&self, address: H160, index: Option<H256>) -> bool;
/// Mark an address or (address, index) pair as hot.
fn mark_hot(&mut self, address: H160, index: Option<H256>) -> Result<(), ExitError>;

/// Set storage value of address at index.
fn set_storage(&mut self, address: H160, index: H256, value: H256) -> Result<(), ExitError>;
/// Create a log owned by address with given topics and data.
fn log(&mut self, address: H160, topics: Vec<H256>, data: Vec<u8>) -> Result<(), ExitError>;
/// Mark an address to be deleted, with funds transferred to target.
fn mark_delete(&mut self, address: H160, target: H160) -> Result<(), ExitError>;
pub trait RuntimeGasometerBackend {
/// Get the gas left value.
fn gas_left(&self) -> U256;
}

/// EVM context handler.
pub trait Handler: RuntimeBackend + RuntimeEnvironmentalBackend + RuntimeGasometerBackend {}
9 changes: 9 additions & 0 deletions src/backend.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
pub enum TransactionalMergeStrategy {
Commit,
Discard,
}

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

0 comments on commit 48d8511

Please sign in to comment.