Skip to content

Feat/8293 #8422

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

Closed
wants to merge 5 commits into from
Closed
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
55 changes: 38 additions & 17 deletions crates/anvil/src/eth/backend/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1789,23 +1789,44 @@ impl Backend {

if block_number < self.env.read().block.number {
{
let mut states = self.states.write();

if let Some((state, block)) = self
.get_block(block_number.to::<u64>())
.and_then(|block| Some((states.get(&block.header.hash_slow())?, block)))
{
let block = BlockEnv {
number: U256::from(block.header.number),
coinbase: block.header.beneficiary,
timestamp: U256::from(block.header.timestamp),
difficulty: block.header.difficulty,
prevrandao: Some(block.header.mix_hash),
basefee: U256::from(block.header.base_fee_per_gas.unwrap_or_default()),
gas_limit: U256::from(block.header.gas_limit),
..Default::default()
};
return Ok(f(Box::new(state), block));
// here we first attempt to get historic state via the read lock.
// if not found, fallback into fetching the state from the disk
// and loading the state with a write lock
let states = self.states.read();

if let Some(block) = self.get_block(block_number.to::<u64>()) {
if let Some(state) = states.get(&block.header.hash_slow()) {
let block = BlockEnv {
number: U256::from(block.header.number),
coinbase: block.header.beneficiary,
timestamp: U256::from(block.header.timestamp),
difficulty: block.header.difficulty,
prevrandao: Some(block.header.mix_hash),
basefee: U256::from(block.header.base_fee_per_gas.unwrap_or_default()),
gas_limit: U256::from(block.header.gas_limit),
..Default::default()
};
return Ok(f(Box::new(state), block));
} else {
let mut states = self.states.write();
if let Some(state) =
states.get_and_initialize_from_hash(&block.header.hash_slow())
{
let block = BlockEnv {
number: U256::from(block.header.number),
coinbase: block.header.beneficiary,
timestamp: U256::from(block.header.timestamp),
difficulty: block.header.difficulty,
prevrandao: Some(block.header.mix_hash),
basefee: U256::from(
block.header.base_fee_per_gas.unwrap_or_default(),
),
gas_limit: U256::from(block.header.gas_limit),
..Default::default()
};
return Ok(f(Box::new(state), block));
}
}
}
}

Expand Down
25 changes: 14 additions & 11 deletions crates/anvil/src/eth/backend/mem/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,16 +162,19 @@ impl InMemoryBlockStates {
}

/// Returns the state for the given `hash` if present
pub fn get(&mut self, hash: &B256) -> Option<&StateDb> {
self.states.get(hash).or_else(|| {
if let Some(state) = self.on_disk_states.get_mut(hash) {
if let Some(cached) = self.disk_cache.read(*hash) {
state.init_from_snapshot(cached);
return Some(state);
}
pub fn get(&self, hash: &B256) -> Option<&StateDb> {
self.states.get(hash)
}

/// Intializes state from the disk cache from the given `hash` and returns it
pub fn get_and_initialize_from_hash(&mut self, hash: &B256) -> Option<&StateDb> {
if let Some(state) = self.on_disk_states.get_mut(hash) {
if let Some(cached) = self.disk_cache.read(*hash) {
state.init_from_snapshot(cached);
return Some(state);
}
None
})
}
None
}

/// Sets the maximum number of stats we keep in memory
Expand Down Expand Up @@ -552,7 +555,7 @@ mod tests {
assert_eq!(storage.on_disk_states.len(), 1);
assert!(storage.on_disk_states.contains_key(&one));

let loaded = storage.get(&one).unwrap();
let loaded = storage.get_and_initialize_from_hash(&one).unwrap();

let acc = loaded.basic_ref(addr).unwrap().unwrap();
assert_eq!(acc.balance, U256::from(1337u64));
Expand Down Expand Up @@ -583,7 +586,7 @@ mod tests {
for idx in 0..num_states {
let hash = B256::from(U256::from(idx));
let addr = Address::from_word(hash);
let loaded = storage.get(&hash).unwrap();
let loaded = storage.get_and_initialize_from_hash(&hash).unwrap();
let acc = loaded.basic_ref(addr).unwrap().unwrap();
let balance = (idx * 2) as u64;
assert_eq!(acc.balance, U256::from(balance));
Expand Down