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: bump revm #44

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[dependencies]
alloy-primitives = { version = "0.8.22", features = ["map"] }
alloy-primitives = { version = "0.8.24", features = ["map"] }
alloy-provider = { version = "0.12", default-features = false }
alloy-rpc-types = { version = "0.12", features = ["eth"] }
alloy-consensus = { version = "0.12", default-features = false }
Expand All @@ -35,10 +35,7 @@ futures = "0.3"

parking_lot = "0.12"

revm = { version = "19.0.0", default-features = false, features = [
"std",
"serde",
] }
revm = { version = "20.0.0", features = ["std", "serde"] }

serde = "1.0"
serde_json = "1.0"
Expand Down
7 changes: 6 additions & 1 deletion deny.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
[advisories]
version = 2
yanked = "warn"
ignore = []
ignore = [
# https://github.com/dtolnay/paste
"RUSTSEC-2024-0436",
]

[bans]
multiple-versions = "warn"
Expand Down Expand Up @@ -37,6 +40,8 @@ exceptions = [
# https://tldrlegal.com/license/creative-commons-cc0-1.0-universal
{ allow = ["CC0-1.0"], name = "tiny-keccak" },
{ allow = ["CC0-1.0"], name = "aurora-engine-modexp" },
{ allow = ["CC0-1.0"], name = "secp256k1" },
{ allow = ["CC0-1.0"], name = "secp256k1-sys" },
]

[[licenses.clarify]]
Expand Down
11 changes: 3 additions & 8 deletions src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ use futures::{
Future, FutureExt,
};
use revm::{
db::DatabaseRef,
database::DatabaseRef,
primitives::{
map::{hash_map::Entry, AddressHashMap, HashMap},
AccountInfo, Bytecode, KECCAK_EMPTY,
KECCAK_EMPTY,
},
state::{AccountInfo, Bytecode},
};
use std::{
collections::VecDeque,
Expand Down Expand Up @@ -927,7 +928,6 @@ mod tests {

let provider = get_http_provider(endpoint);
let meta = BlockchainDbMeta {
cfg_env: Default::default(),
block_env: Default::default(),
hosts: BTreeSet::from([endpoint.to_string()]),
};
Expand Down Expand Up @@ -979,7 +979,6 @@ mod tests {

let provider = get_http_provider(endpoint);
let meta = BlockchainDbMeta {
cfg_env: Default::default(),
block_env: Default::default(),
hosts: BTreeSet::from([endpoint.to_string()]),
};
Expand Down Expand Up @@ -1049,7 +1048,6 @@ mod tests {

let provider = get_http_provider(endpoint);
let meta = BlockchainDbMeta {
cfg_env: Default::default(),
block_env: Default::default(),
hosts: BTreeSet::from([endpoint.to_string()]),
};
Expand Down Expand Up @@ -1112,7 +1110,6 @@ mod tests {

let provider = get_http_provider(endpoint);
let meta = BlockchainDbMeta {
cfg_env: Default::default(),
block_env: Default::default(),
hosts: BTreeSet::from([endpoint.to_string()]),
};
Expand Down Expand Up @@ -1166,7 +1163,6 @@ mod tests {

let provider = get_http_provider(endpoint);
let meta = BlockchainDbMeta {
cfg_env: Default::default(),
block_env: Default::default(),
hosts: BTreeSet::from([endpoint.to_string()]),
};
Expand Down Expand Up @@ -1326,7 +1322,6 @@ mod tests {

let provider = get_http_provider(&endpoint);
let meta = BlockchainDbMeta {
cfg_env: Default::default(),
block_env: Default::default(),
hosts: BTreeSet::from([endpoint.to_string()]),
};
Expand Down
99 changes: 27 additions & 72 deletions src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ use alloy_primitives::{Address, B256, U256};
use alloy_provider::network::TransactionResponse;
use parking_lot::RwLock;
use revm::{
context::BlockEnv,
context_interface::block::BlobExcessGasAndPrice,
primitives::{
map::{AddressHashMap, HashMap},
Account, AccountInfo, AccountStatus, BlobExcessGasAndPrice, BlockEnv, CfgEnv, KECCAK_EMPTY,
KECCAK_EMPTY,
},
state::{Account, AccountInfo, AccountStatus},
DatabaseCommit,
};
use serde::{ser::SerializeMap, Deserialize, Deserializer, Serialize, Serializer};
Expand Down Expand Up @@ -122,29 +125,21 @@ impl BlockchainDb {
/// relevant identifying markers in the context of [BlockchainDb]
#[derive(Clone, Debug, Eq, Serialize, Default)]
pub struct BlockchainDbMeta {
pub cfg_env: CfgEnv,
/// The block environment
pub block_env: BlockEnv,
/// all the hosts used to connect to
/// All the hosts used to connect to
pub hosts: BTreeSet<String>,
}

impl BlockchainDbMeta {
/// Creates a new instance
pub fn new(env: revm::primitives::Env, url: String) -> Self {
pub fn new(block_env: BlockEnv, url: String) -> Self {
let host = Url::parse(&url)
.ok()
.and_then(|url| url.host().map(|host| host.to_string()))
.unwrap_or(url);

Self { cfg_env: env.cfg.clone(), block_env: env.block, hosts: BTreeSet::from([host]) }
}

/// Sets the chain_id in the [CfgEnv] of this instance.
///
/// Remaining fields of [CfgEnv] are left unchanged.
pub const fn with_chain_id(mut self, chain_id: u64) -> Self {
self.cfg_env.chain_id = chain_id;
self
Self { block_env, hosts: BTreeSet::from([host]) }
}

/// Sets the [BlockEnv] of this instance using the provided [alloy_rpc_types::Block]
Expand All @@ -153,12 +148,12 @@ impl BlockchainDbMeta {
block: &alloy_rpc_types::Block<T, H>,
) -> Self {
self.block_env = BlockEnv {
number: U256::from(block.header.number()),
coinbase: block.header.beneficiary(),
timestamp: U256::from(block.header.timestamp()),
number: block.header.number(),
beneficiary: block.header.beneficiary(),
timestamp: block.header.timestamp(),
difficulty: U256::from(block.header.difficulty()),
basefee: block.header.base_fee_per_gas().map(U256::from).unwrap_or_default(),
gas_limit: U256::from(block.header.gas_limit()),
basefee: block.header.base_fee_per_gas().unwrap_or_default(),
gas_limit: block.header.gas_limit(),
prevrandao: block.header.mix_hash(),
blob_excess_gas_and_price: Some(BlobExcessGasAndPrice::new(
block.header.excess_blob_gas().unwrap_or_default(),
Expand All @@ -179,13 +174,8 @@ impl BlockchainDbMeta {
self
}

/// Sets [CfgEnv] of this instance
pub fn set_cfg_env(mut self, cfg_env: revm::primitives::CfgEnv) {
self.cfg_env = cfg_env;
}

/// Sets the [BlockEnv] of this instance
pub fn set_block_env(mut self, block_env: revm::primitives::BlockEnv) {
pub fn set_block_env(mut self, block_env: revm::context::BlockEnv) {
self.block_env = block_env;
}
}
Expand All @@ -194,7 +184,7 @@ impl BlockchainDbMeta {
// case for http vs ws endpoints
impl PartialEq for BlockchainDbMeta {
fn eq(&self, other: &Self) -> bool {
self.cfg_env == other.cfg_env && self.block_env == other.block_env
self.block_env == other.block_env
}
}

Expand All @@ -203,46 +193,13 @@ impl<'de> Deserialize<'de> for BlockchainDbMeta {
where
D: Deserializer<'de>,
{
/// A backwards compatible representation of [revm::primitives::CfgEnv]
///
/// This prevents deserialization errors of cache files caused by breaking changes to the
/// default [revm::primitives::CfgEnv], for example enabling an optional feature.
/// By hand rolling deserialize impl we can prevent cache file issues
struct CfgEnvBackwardsCompat {
inner: revm::primitives::CfgEnv,
}

impl<'de> Deserialize<'de> for CfgEnvBackwardsCompat {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let mut value = serde_json::Value::deserialize(deserializer)?;

// we check for breaking changes here
if let Some(obj) = value.as_object_mut() {
let default_value =
serde_json::to_value(revm::primitives::CfgEnv::default()).unwrap();
for (key, value) in default_value.as_object().unwrap() {
if !obj.contains_key(key) {
obj.insert(key.to_string(), value.clone());
}
}
}

let cfg_env: revm::primitives::CfgEnv =
serde_json::from_value(value).map_err(serde::de::Error::custom)?;
Ok(Self { inner: cfg_env })
}
}

/// A backwards compatible representation of [revm::primitives::BlockEnv]
///
/// This prevents deserialization errors of cache files caused by breaking changes to the
/// default [revm::primitives::BlockEnv], for example enabling an optional feature.
/// By hand rolling deserialize impl we can prevent cache file issues
struct BlockEnvBackwardsCompat {
inner: revm::primitives::BlockEnv,
inner: revm::context::BlockEnv,
}

impl<'de> Deserialize<'de> for BlockEnvBackwardsCompat {
Expand All @@ -255,15 +212,15 @@ impl<'de> Deserialize<'de> for BlockchainDbMeta {
// we check for any missing fields here
if let Some(obj) = value.as_object_mut() {
let default_value =
serde_json::to_value(revm::primitives::BlockEnv::default()).unwrap();
serde_json::to_value(revm::context::BlockEnv::default()).unwrap();
for (key, value) in default_value.as_object().unwrap() {
if !obj.contains_key(key) {
obj.insert(key.to_string(), value.clone());
}
}
}

let cfg_env: revm::primitives::BlockEnv =
let cfg_env: revm::context::BlockEnv =
serde_json::from_value(value).map_err(serde::de::Error::custom)?;
Ok(Self { inner: cfg_env })
}
Expand All @@ -272,7 +229,6 @@ impl<'de> Deserialize<'de> for BlockchainDbMeta {
// custom deserialize impl to not break existing cache files
#[derive(Deserialize)]
struct Meta {
cfg_env: CfgEnvBackwardsCompat,
block_env: BlockEnvBackwardsCompat,
/// all the hosts used to connect to
#[serde(alias = "host")]
Expand All @@ -286,9 +242,8 @@ impl<'de> Deserialize<'de> for BlockchainDbMeta {
Single(String),
}

let Meta { cfg_env, block_env, hosts } = Meta::deserialize(deserializer)?;
let Meta { block_env, hosts } = Meta::deserialize(deserializer)?;
Ok(Self {
cfg_env: cfg_env.inner,
block_env: block_env.inner,
hosts: match hosts {
Hosts::Multi(hosts) => hosts,
Expand Down Expand Up @@ -557,12 +512,12 @@ mod tests {
"disable_base_fee": false
},
"block_env": {
"number": "0xed3ddf",
"number": 15547871,
"coinbase": "0x0000000000000000000000000000000000000000",
"timestamp": "0x6324bc3f",
"timestamp": 1663351871,
"difficulty": "0x0",
"basefee": "0x2e5fda223",
"gas_limit": "0x1c9c380",
"basefee": 12448539171,
"gas_limit": 30000000,
"prevrandao": "0x0000000000000000000000000000000000000000000000000000000000000000"
},
"hosts": [
Expand Down Expand Up @@ -636,11 +591,11 @@ mod tests {
"optimism": false
},
"block_env": {
"number": "0x11c99bc",
"number": 18651580,
"coinbase": "0x4838b106fce9647bdf1e7877bf73ce8b0bad5f97",
"timestamp": "0x65627003",
"gas_limit": "0x1c9c380",
"basefee": "0x64288ff1f",
"timestamp": 1700950019,
"gas_limit": 30000000,
"basefee": 26886078239,
"difficulty": "0xc6b1a299886016dea3865689f8393b9bf4d8f4fe8c0ad25f0058b3569297c057",
"prevrandao": "0xc6b1a299886016dea3865689f8393b9bf4d8f4fe8c0ad25f0058b3569297c057",
"blob_excess_gas_and_price": {
Expand Down
2 changes: 2 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,5 @@ impl From<Infallible> for DatabaseError {
match value {}
}
}

impl revm::database::DBErrorMarker for DatabaseError {}
50 changes: 49 additions & 1 deletion test-data/storage.json
Original file line number Diff line number Diff line change
@@ -1 +1,49 @@
{"meta":{"cfg_env":{"chain_id":1,"spec_id":"LATEST","perf_all_precompiles_have_balance":false,"memory_limit":4294967295,"perf_analyse_created_bytecodes":"Analyse","limit_contract_code_size":24576,"disable_coinbase_tip":false},"block_env":{"number":"0xdc42b8","coinbase":"0x0000000000000000000000000000000000000000","timestamp":"0x1","difficulty":"0x0","basefee":"0x0","gas_limit":"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"},"hosts":["mainnet.infura.io"]},"accounts":{"0x63091244180ae240c87d1f528f5f269134cb07b3":{"balance":"0x0","code_hash":"0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470","code":null,"nonce":0}},"storage":{"0x63091244180ae240c87d1f528f5f269134cb07b3":{"0x0":"0x0","0x1":"0x0","0x2":"0x0","0x3":"0x0","0x4":"0x0","0x5":"0x0","0x6":"0x0","0x7":"0x0","0x8":"0x0","0x9":"0x0"}},"block_hashes":{}}
{
"meta":{
"cfg_env":{
"chain_id":1,
"spec_id":"LATEST",
"perf_all_precompiles_have_balance":false,
"memory_limit":4294967295,
"perf_analyse_created_bytecodes":"Analyse",
"limit_contract_code_size":24576,
"disable_coinbase_tip":false
},
"block_env":{
"number":14435000,
"coinbase":"0x0000000000000000000000000000000000000000",
"timestamp":0,
"difficulty":"0",
"basefee":0,
"gas_limit":18446744073709551615
},
"hosts":[
"mainnet.infura.io"
]
},
"accounts":{
"0x63091244180ae240c87d1f528f5f269134cb07b3":{
"balance":"0x0",
"code_hash":"0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470",
"code":null,
"nonce":0
}
},
"storage":{
"0x63091244180ae240c87d1f528f5f269134cb07b3":{
"0x0":"0x0",
"0x1":"0x0",
"0x2":"0x0",
"0x3":"0x0",
"0x4":"0x0",
"0x5":"0x0",
"0x6":"0x0",
"0x7":"0x0",
"0x8":"0x0",
"0x9":"0x0"
}
},
"block_hashes":{

}
}
Loading