Skip to content

Commit afb8203

Browse files
committed
Integrate with revm interpreter
1 parent 8513c83 commit afb8203

File tree

10 files changed

+449
-4
lines changed

10 files changed

+449
-4
lines changed

Cargo.lock

Lines changed: 98 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ members = [
3131
"crates/dbs/storage",
3232
"crates/cfxcore/vm-interpreter",
3333
"crates/cfxcore/vm-types",
34+
"crates/util/alloy-type-conversions",
3435
"crates/util/cfx-vm-tracer-derive",
3536
"crates/util/dag",
3637
"crates/util/delegate",
@@ -150,6 +151,7 @@ cfx-rpc-primitives = { path = "./crates/rpc/rpc-primitives" }
150151
cfx-rpc-cfx-types = { path = "./crates/rpc/rpc-cfx-types" }
151152
cfx-parity-trace-types = { path = "./crates/cfxcore/parity-trace-types" }
152153
cfx-rpc-eth-api = { path = "./crates/rpc/rpc-eth-api" }
154+
alloy-type-conversions = { path = "./crates/util/alloy-type-conversions" }
153155

154156
serde = { version = "1.0", features = ["derive", "alloc"] }
155157
serde_json = "1.0"
@@ -182,6 +184,11 @@ alloy-primitives = "0.7.2"
182184
alloy-rpc-types = { git = "https://github.com/alloy-rs/alloy", rev = "4e22b9e" }
183185
alloy-rpc-types-trace = { git = "https://github.com/alloy-rs/alloy", rev = "4e22b9e" }
184186
revm = "8.0"
187+
# revm-interpreter = "6.0"
188+
# revm-primitives = "5.0"
189+
revm-interpreter = { git = "https://github.com/Conflux-Chain/revm.git", branch = "dev"}
190+
revm-primitives = { git = "https://github.com/Conflux-Chain/revm.git", branch = "dev"}
191+
185192

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

crates/cfxcore/executor/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ c-kzg = { version = "1.0.2", default-features = false}
5454
once_cell = "1.19"
5555
rayon = { workspace = true }
5656
cfx-parity-trace-types = { workspace = true }
57+
revm-interpreter = { workspace = true }
58+
alloy-type-conversions = { workspace = true }
5759

5860
[dev-dependencies]
5961
cfx-statedb = { workspace = true, features = ["testonly_code"]}

crates/cfxcore/executor/src/context.rs renamed to crates/cfxcore/executor/src/context/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
// Conflux is free software and distributed under GNU General Public License.
33
// See http://www.gnu.org/licenses/
44

5+
mod revm;
6+
7+
pub(crate) use revm::EvmHost;
8+
59
// Transaction execution environment.
610
use crate::{
711
executive::contract_address,
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
#![allow(unused, dead_code)]
2+
3+
use super::Context;
4+
use alloy_type_conversions::*;
5+
use cfx_statedb::Result as DbResult;
6+
use cfx_types::AddressSpaceUtil;
7+
use cfx_vm_types::Context as ContextTrait;
8+
use revm_interpreter::primitives::{Address, Bytes, Env, Log, B256, U256};
9+
10+
pub(crate) struct EvmHost<'a> {
11+
context: Context<'a>,
12+
error: DbResult<()>,
13+
}
14+
15+
impl<'a> EvmHost<'a> {
16+
pub fn new(context: Context<'a>) -> Self {
17+
Self {
18+
context,
19+
error: Ok(()),
20+
}
21+
}
22+
23+
pub fn take_db_error(&mut self) -> DbResult<()> {
24+
std::mem::replace(&mut self.error, Ok(()))
25+
}
26+
}
27+
28+
fn unwrap_db_error(e: cfx_vm_types::Error) -> cfx_statedb::Error {
29+
match e {
30+
cfx_vm_types::Error::StateDbError(e) => e.0,
31+
_ => unreachable!(),
32+
}
33+
}
34+
35+
const COLD: bool = true;
36+
37+
impl<'a> revm_interpreter::Host for EvmHost<'a> {
38+
fn env(&self) -> &Env { todo!() }
39+
40+
fn env_mut(&mut self) -> &mut Env { todo!() }
41+
42+
fn load_account(
43+
&mut self, address: Address,
44+
) -> Option<revm_interpreter::LoadAccountResult> {
45+
match self
46+
.context
47+
.exists_and_not_null(&from_alloy_address(address))
48+
{
49+
Ok(exists) => Some(revm_interpreter::LoadAccountResult {
50+
is_cold: COLD,
51+
is_empty: !exists,
52+
}),
53+
Err(e) => {
54+
self.error = Err(unwrap_db_error(e));
55+
None
56+
}
57+
}
58+
}
59+
60+
fn block_hash(&mut self, number: U256) -> Option<B256> {
61+
match self.context.blockhash(&from_alloy_u256(number)) {
62+
Ok(hash) => Some(to_alloy_h256(hash)),
63+
Err(e) => {
64+
self.error = Err(unwrap_db_error(e));
65+
None
66+
}
67+
}
68+
}
69+
70+
fn balance(&mut self, address: Address) -> Option<(U256, bool)> {
71+
match self.context.balance(&from_alloy_address(address)) {
72+
Ok(balance) => Some((to_alloy_u256(balance), COLD)),
73+
Err(e) => {
74+
self.error = Err(unwrap_db_error(e));
75+
None
76+
}
77+
}
78+
}
79+
80+
fn code(&mut self, address: Address) -> Option<(Bytes, bool)> {
81+
match self.context.extcode(&from_alloy_address(address)) {
82+
Ok(None) => Some((Bytes::new(), COLD)),
83+
Ok(Some(code)) => Some((Bytes::copy_from_slice(&**code), COLD)),
84+
Err(e) => {
85+
self.error = Err(unwrap_db_error(e));
86+
None
87+
}
88+
}
89+
}
90+
91+
fn code_hash(&mut self, address: Address) -> Option<(B256, bool)> {
92+
match self.context.extcodehash(&from_alloy_address(address)) {
93+
Ok(hash) => Some((to_alloy_h256(hash), COLD)),
94+
Err(e) => {
95+
self.error = Err(unwrap_db_error(e));
96+
None
97+
}
98+
}
99+
}
100+
101+
fn sload(&mut self, address: Address, index: U256) -> Option<(U256, bool)> {
102+
let receiver =
103+
from_alloy_address(address).with_space(self.context.space);
104+
let key = index.to_be_bytes::<32>();
105+
match self.context.state.storage_at(&receiver, &key) {
106+
Ok(value) => Some((to_alloy_u256(value), COLD)),
107+
Err(e) => {
108+
self.error = Err(e);
109+
None
110+
}
111+
}
112+
}
113+
114+
fn sstore(
115+
&mut self, address: Address, index: U256, value: U256,
116+
) -> Option<revm_interpreter::SStoreResult> {
117+
// TODO: who checks static flag in revm?
118+
todo!()
119+
}
120+
121+
fn tload(&mut self, address: Address, index: U256) -> U256 { todo!() }
122+
123+
fn tstore(&mut self, address: Address, index: U256, value: U256) { todo!() }
124+
125+
fn log(&mut self, log: Log) { todo!() }
126+
127+
fn selfdestruct(
128+
&mut self, address: Address, target: Address,
129+
) -> Option<revm_interpreter::SelfDestructResult> {
130+
todo!()
131+
}
132+
}

crates/cfxcore/executor/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,7 @@ pub mod spec;
6060
/// meaningful database interfaces for the execution.
6161
pub mod state;
6262

63+
pub mod revm_wrapper;
64+
6365
pub use internal_contract::{InternalContractMap, InternalContractTrait};
6466
pub use observer as executive_observer;

0 commit comments

Comments
 (0)