Skip to content
Merged
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
23 changes: 12 additions & 11 deletions src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ type FullBlockSender =
OneshotSender<DatabaseResult<WithOtherFields<Block<WithOtherFields<Transaction>>>>>;
type TransactionSender = OneshotSender<DatabaseResult<WithOtherFields<Transaction>>>;

type AddressData = Map<Address, AccountInfo>;
type StorageData = Map<Address, StorageInfo>;
use alloy_primitives::map::AddressHashMap;
type AddressData = AddressHashMap<AccountInfo>;
type StorageData = AddressHashMap<StorageInfo>;
type BlockHashData = Map<U256, B256>;

/// Request variants that are executed by the provider
Expand Down Expand Up @@ -935,7 +936,7 @@ mod tests {
code: None,
code_hash: KECCAK_EMPTY,
};
let mut account_data: AddressData = Map::new();
let mut account_data: AddressData = Map::default();
account_data.insert(address, new_acc.clone());

backend.insert_or_update_address(account_data);
Expand Down Expand Up @@ -999,8 +1000,8 @@ mod tests {
// some rng contract from etherscan
let address: Address = "63091244180ae240c87d1f528f5f269134cb07b3".parse().unwrap();

let mut storage_data: StorageData = Map::new();
let mut storage_info: StorageInfo = Map::new();
let mut storage_data: StorageData = Map::default();
let mut storage_info: StorageInfo = Map::default();
storage_info.insert(U256::from(20), U256::from(10));
storage_info.insert(U256::from(30), U256::from(15));
storage_info.insert(U256::from(40), U256::from(20));
Expand Down Expand Up @@ -1062,7 +1063,7 @@ mod tests {
// some rng contract from etherscan
// let address: Address = "63091244180ae240c87d1f528f5f269134cb07b3".parse().unwrap();

let mut block_hash_data: BlockHashData = Map::new();
let mut block_hash_data: BlockHashData = Map::default();
block_hash_data.insert(U256::from(1), B256::from(U256::from(1)));
block_hash_data.insert(U256::from(2), B256::from(U256::from(2)));
block_hash_data.insert(U256::from(3), B256::from(U256::from(3)));
Expand Down Expand Up @@ -1123,8 +1124,8 @@ mod tests {
// some rng contract from etherscan
let address: Address = "63091244180ae240c87d1f528f5f269134cb07b3".parse().unwrap();

let mut storage_data: StorageData = Map::new();
let mut storage_info: StorageInfo = Map::new();
let mut storage_data: StorageData = Map::default();
let mut storage_info: StorageInfo = Map::default();
storage_info.insert(U256::from(1), U256::from(10));
storage_info.insert(U256::from(2), U256::from(15));
storage_info.insert(U256::from(3), U256::from(20));
Expand All @@ -1143,7 +1144,7 @@ mod tests {
// nullify the code
new_acc.code = Some(Bytecode::new_raw(([10, 20, 30, 40]).into()));

let mut account_data: AddressData = Map::new();
let mut account_data: AddressData = Map::default();
account_data.insert(address, new_acc.clone());

backend.insert_or_update_address(account_data);
Expand Down Expand Up @@ -1194,8 +1195,8 @@ mod tests {

let json_db = BlockchainDb::new(meta, Some(cache_path));

let mut storage_data: StorageData = Map::new();
let mut storage_info: StorageInfo = Map::new();
let mut storage_data: StorageData = Map::default();
let mut storage_info: StorageInfo = Map::default();
storage_info.insert(U256::from(1), U256::from(10));
storage_info.insert(U256::from(2), U256::from(15));
storage_info.insert(U256::from(3), U256::from(20));
Expand Down
73 changes: 62 additions & 11 deletions src/cache.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
//! Cache related abstraction
use alloy_primitives::{Address, B256, U256};
use alloy_provider::network::{HeaderResponse, TransactionResponse};
use parking_lot::RwLock;
use revm::{
primitives::{Account, AccountInfo, AccountStatus, HashMap as Map, KECCAK_EMPTY},
primitives::{
map::{AddressHashMap, HashMap},
Account, AccountInfo, AccountStatus, BlobExcessGasAndPrice, BlockEnv, CfgEnv,
HashMap as Map, KECCAK_EMPTY,
},
DatabaseCommit,
};
use serde::{ser::SerializeMap, Deserialize, Deserializer, Serialize, Serializer};
use std::{
collections::{BTreeSet, HashMap},
collections::BTreeSet,
fs,
io::{BufWriter, Write},
path::{Path, PathBuf},
Expand Down Expand Up @@ -84,12 +89,12 @@ impl BlockchainDb {
}

/// Returns the map that holds the account related info
pub fn accounts(&self) -> &RwLock<Map<Address, AccountInfo>> {
pub fn accounts(&self) -> &RwLock<AddressHashMap<AccountInfo>> {
&self.db.accounts
}

/// Returns the map that holds the storage related info
pub fn storage(&self) -> &RwLock<Map<Address, StorageInfo>> {
pub fn storage(&self) -> &RwLock<AddressHashMap<StorageInfo>> {
&self.db.storage
}

Expand All @@ -115,10 +120,10 @@ impl BlockchainDb {
}

/// relevant identifying markers in the context of [BlockchainDb]
#[derive(Clone, Debug, Eq, Serialize)]
#[derive(Clone, Debug, Eq, Serialize, Default)]
pub struct BlockchainDbMeta {
pub cfg_env: revm::primitives::CfgEnv,
pub block_env: revm::primitives::BlockEnv,
pub cfg_env: CfgEnv,
pub block_env: BlockEnv,
/// all the hosts used to connect to
pub hosts: BTreeSet<String>,
}
Expand All @@ -133,6 +138,52 @@ impl BlockchainDbMeta {

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
}

/// Sets the [BlockEnv] of this instance using the provided [alloy_rpc_types::Block]
pub fn with_block<T: TransactionResponse>(mut self, block: &alloy_rpc_types::Block<T>) -> Self {
self.block_env = BlockEnv {
number: U256::from(block.header.number()),
coinbase: block.header.coinbase(),
timestamp: U256::from(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()),
prevrandao: block.header.mix_hash(),
blob_excess_gas_and_price: Some(BlobExcessGasAndPrice::new(
block.header.excess_blob_gas.map(|gas| gas as u64).unwrap_or_default(),
)),
};

self
}

/// Infers the host from the provided url and adds it to the set of hosts
pub fn with_url(mut self, url: &str) -> Self {
let host = Url::parse(url)
.ok()
.and_then(|url| url.host().map(|host| host.to_string()))
.unwrap_or(url.to_string());
self.hosts.insert(host);
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) {
self.block_env = block_env;
}
}

// ignore hosts to not invalidate the cache when different endpoints are used, as it's commonly the
Expand Down Expand Up @@ -248,9 +299,9 @@ impl<'de> Deserialize<'de> for BlockchainDbMeta {
#[derive(Debug, Default)]
pub struct MemDb {
/// Account related data
pub accounts: RwLock<Map<Address, AccountInfo>>,
pub accounts: RwLock<AddressHashMap<AccountInfo>>,
/// Storage related data
pub storage: RwLock<Map<Address, StorageInfo>>,
pub storage: RwLock<AddressHashMap<StorageInfo>>,
/// All retrieved block hashes
pub block_hashes: RwLock<Map<U256, B256>>,
}
Expand Down Expand Up @@ -446,8 +497,8 @@ impl<'de> Deserialize<'de> for JsonBlockCacheData {
#[derive(Deserialize)]
struct Data {
meta: BlockchainDbMeta,
accounts: HashMap<Address, AccountInfo>,
storage: HashMap<Address, HashMap<U256, U256>>,
accounts: AddressHashMap<AccountInfo>,
storage: AddressHashMap<HashMap<U256, U256>>,
block_hashes: HashMap<U256, B256>,
}

Expand Down
Loading