Skip to content

Commit

Permalink
Integrate with revm interpreter
Browse files Browse the repository at this point in the history
  • Loading branch information
ChenxingLi committed Oct 16, 2024
1 parent 8513c83 commit afb8203
Show file tree
Hide file tree
Showing 10 changed files with 449 additions and 4 deletions.
101 changes: 98 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ members = [
"crates/dbs/storage",
"crates/cfxcore/vm-interpreter",
"crates/cfxcore/vm-types",
"crates/util/alloy-type-conversions",
"crates/util/cfx-vm-tracer-derive",
"crates/util/dag",
"crates/util/delegate",
Expand Down Expand Up @@ -150,6 +151,7 @@ cfx-rpc-primitives = { path = "./crates/rpc/rpc-primitives" }
cfx-rpc-cfx-types = { path = "./crates/rpc/rpc-cfx-types" }
cfx-parity-trace-types = { path = "./crates/cfxcore/parity-trace-types" }
cfx-rpc-eth-api = { path = "./crates/rpc/rpc-eth-api" }
alloy-type-conversions = { path = "./crates/util/alloy-type-conversions" }

serde = { version = "1.0", features = ["derive", "alloc"] }
serde_json = "1.0"
Expand Down Expand Up @@ -182,6 +184,11 @@ alloy-primitives = "0.7.2"
alloy-rpc-types = { git = "https://github.com/alloy-rs/alloy", rev = "4e22b9e" }
alloy-rpc-types-trace = { git = "https://github.com/alloy-rs/alloy", rev = "4e22b9e" }
revm = "8.0"
# revm-interpreter = "6.0"
# revm-primitives = "5.0"
revm-interpreter = { git = "https://github.com/Conflux-Chain/revm.git", branch = "dev"}
revm-primitives = { git = "https://github.com/Conflux-Chain/revm.git", branch = "dev"}


bls-signatures = { git = "https://github.com/Conflux-Chain/bls-signatures.git", rev = "fb52187df92d27c365642cb7e7b2aaf60437cf9c", default-features = false, features = ["multicore", "blst"] }

Expand Down
2 changes: 2 additions & 0 deletions crates/cfxcore/executor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ c-kzg = { version = "1.0.2", default-features = false}
once_cell = "1.19"
rayon = { workspace = true }
cfx-parity-trace-types = { workspace = true }
revm-interpreter = { workspace = true }
alloy-type-conversions = { workspace = true }

[dev-dependencies]
cfx-statedb = { workspace = true, features = ["testonly_code"]}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
// Conflux is free software and distributed under GNU General Public License.
// See http://www.gnu.org/licenses/

mod revm;

pub(crate) use revm::EvmHost;

// Transaction execution environment.
use crate::{
executive::contract_address,
Expand Down
132 changes: 132 additions & 0 deletions crates/cfxcore/executor/src/context/revm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
#![allow(unused, dead_code)]

use super::Context;
use alloy_type_conversions::*;
use cfx_statedb::Result as DbResult;
use cfx_types::AddressSpaceUtil;
use cfx_vm_types::Context as ContextTrait;
use revm_interpreter::primitives::{Address, Bytes, Env, Log, B256, U256};

pub(crate) struct EvmHost<'a> {
context: Context<'a>,
error: DbResult<()>,
}

impl<'a> EvmHost<'a> {
pub fn new(context: Context<'a>) -> Self {
Self {
context,
error: Ok(()),
}
}

pub fn take_db_error(&mut self) -> DbResult<()> {
std::mem::replace(&mut self.error, Ok(()))
}
}

fn unwrap_db_error(e: cfx_vm_types::Error) -> cfx_statedb::Error {
match e {
cfx_vm_types::Error::StateDbError(e) => e.0,
_ => unreachable!(),
}
}

const COLD: bool = true;

impl<'a> revm_interpreter::Host for EvmHost<'a> {
fn env(&self) -> &Env { todo!() }

fn env_mut(&mut self) -> &mut Env { todo!() }

fn load_account(
&mut self, address: Address,
) -> Option<revm_interpreter::LoadAccountResult> {
match self
.context
.exists_and_not_null(&from_alloy_address(address))
{
Ok(exists) => Some(revm_interpreter::LoadAccountResult {
is_cold: COLD,
is_empty: !exists,
}),
Err(e) => {
self.error = Err(unwrap_db_error(e));
None
}
}
}

fn block_hash(&mut self, number: U256) -> Option<B256> {
match self.context.blockhash(&from_alloy_u256(number)) {
Ok(hash) => Some(to_alloy_h256(hash)),
Err(e) => {
self.error = Err(unwrap_db_error(e));
None
}
}
}

fn balance(&mut self, address: Address) -> Option<(U256, bool)> {
match self.context.balance(&from_alloy_address(address)) {
Ok(balance) => Some((to_alloy_u256(balance), COLD)),
Err(e) => {
self.error = Err(unwrap_db_error(e));
None
}
}
}

fn code(&mut self, address: Address) -> Option<(Bytes, bool)> {
match self.context.extcode(&from_alloy_address(address)) {
Ok(None) => Some((Bytes::new(), COLD)),
Ok(Some(code)) => Some((Bytes::copy_from_slice(&**code), COLD)),
Err(e) => {
self.error = Err(unwrap_db_error(e));
None
}
}
}

fn code_hash(&mut self, address: Address) -> Option<(B256, bool)> {
match self.context.extcodehash(&from_alloy_address(address)) {
Ok(hash) => Some((to_alloy_h256(hash), COLD)),
Err(e) => {
self.error = Err(unwrap_db_error(e));
None
}
}
}

fn sload(&mut self, address: Address, index: U256) -> Option<(U256, bool)> {
let receiver =
from_alloy_address(address).with_space(self.context.space);
let key = index.to_be_bytes::<32>();
match self.context.state.storage_at(&receiver, &key) {
Ok(value) => Some((to_alloy_u256(value), COLD)),
Err(e) => {
self.error = Err(e);
None
}
}
}

fn sstore(
&mut self, address: Address, index: U256, value: U256,
) -> Option<revm_interpreter::SStoreResult> {
// TODO: who checks static flag in revm?
todo!()
}

fn tload(&mut self, address: Address, index: U256) -> U256 { todo!() }

fn tstore(&mut self, address: Address, index: U256, value: U256) { todo!() }

fn log(&mut self, log: Log) { todo!() }

fn selfdestruct(
&mut self, address: Address, target: Address,
) -> Option<revm_interpreter::SelfDestructResult> {
todo!()
}
}
2 changes: 2 additions & 0 deletions crates/cfxcore/executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,7 @@ pub mod spec;
/// meaningful database interfaces for the execution.
pub mod state;

pub mod revm_wrapper;

pub use internal_contract::{InternalContractMap, InternalContractTrait};
pub use observer as executive_observer;
Loading

0 comments on commit afb8203

Please sign in to comment.