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
58 changes: 53 additions & 5 deletions crates/engine/execution-cache/src/cached_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1159,8 +1159,9 @@ impl ExecutionCache {
// because we can't efficiently invalidate all storage slots for a single address.
// This should only happen on pre-Dencun networks.
//
// If the original account had no code (was an EOA or a not yet deployed contract), we
// just remove the account from cache - no storage exists for it.
// If the original account had no code (was an EOA or a not yet deployed contract), a
// destroyed account can be removed from the cache. A re-created account still has
// post-block storage and info that must be written below.
if account.was_destroyed() {
let had_code =
account.original_info.as_ref().is_some_and(|info| !info.is_empty_code_hash());
Expand All @@ -1178,8 +1179,10 @@ impl ExecutionCache {
return Ok(())
}

self.0.account_cache.remove(addr);
continue;
if account.info.is_none() {
self.0.account_cache.remove(addr);
continue;
}
}

// If we have an account that was modified, but it has a `None` account info, some wild
Expand Down Expand Up @@ -1311,7 +1314,7 @@ mod tests {
use super::*;
use alloy_primitives::{map::HashMap, U256};
use reth_provider::test_utils::{ExtendedAccount, MockEthProvider};
use reth_revm::db::{AccountStatus, BundleAccount};
use reth_revm::db::{states::StorageSlot, AccountStatus, BundleAccount};
use reth_storage_api::StateProvider;
use revm::state::AccountInfo;

Expand Down Expand Up @@ -1512,6 +1515,51 @@ mod tests {
assert!(caches.0.storage_cache.get(&(addr1, storage_key)).is_some());
}

#[test]
fn test_insert_state_recreated_account_updates_cached_storage() {
let caches = ExecutionCache::new(1000);
let addr = Address::random();
let other_addr = Address::random();
let key = StorageKey::from(U256::from(1));
let other_key = StorageKey::from(U256::from(2));
let provider = MockEthProvider::default();
provider.extend_accounts(vec![(addr, ExtendedAccount::new(0, U256::from(100)))]);
let state_provider =
CachedStateProvider::new_prewarm(provider.into_evm_state_provider(), caches.clone());
assert_eq!(state_provider.storage(addr, key).unwrap(), None);
assert_eq!(caches.0.storage_cache.get(&(addr, key)), Some(U256::ZERO));
drop(state_provider);
caches.insert_storage(other_addr, other_key, Some(U256::from(42)));

let current_info =
AccountInfo { nonce: 1, code_hash: B256::random(), ..Default::default() };
let bundle = BundleState {
state: HashMap::from_iter([(
addr,
BundleAccount::new(
Some(AccountInfo {
balance: U256::from(100),
code_hash: alloy_primitives::KECCAK256_EMPTY,
..Default::default()
}),
Some(current_info.clone()),
HashMap::from_iter([(
U256::from(1),
StorageSlot::new_changed(U256::ZERO, U256::from(7)),
)]),
AccountStatus::DestroyedChanged,
),
)]),
..Default::default()
};

assert!(caches.insert_state(&bundle).is_ok());
assert_eq!(caches.0.storage_cache.get(&(addr, key)), Some(U256::from(7)));
let cached_account = caches.0.account_cache.get(&addr).flatten().unwrap();
assert_eq!(cached_account.bytecode_hash, Some(current_info.code_hash));
assert_eq!(caches.0.storage_cache.get(&(other_addr, other_key)), Some(U256::from(42)));
}

#[test]
fn test_insert_state_destroyed_account_no_original_info_removes_only_account() {
let caches = ExecutionCache::new(1000);
Expand Down
28 changes: 27 additions & 1 deletion crates/net/eth-wire-types/src/snap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,8 @@ pub enum SnapProtocolMessage {
BlockAccessLists(BlockAccessListsMessage),
}

/// Error decoding an inbound `snap` protocol message from its framed bytes.
/// Error decoding an inbound `snap` protocol message from its framed bytes, or matching a response
/// to its request.
#[derive(thiserror::Error, Debug)]
pub enum SnapProtocolError {
/// The payload was empty and carried no message id.
Expand All @@ -344,6 +345,31 @@ pub enum SnapProtocolError {
/// Decoding the RLP message body failed.
#[error("RLP error: {0}")]
Rlp(#[from] alloy_rlp::Error),
/// The response answers no request sent on this connection.
///
/// The response is rejected before its body is decoded.
#[error("received unsolicited {message_id:?} response with request id {request_id}")]
UnsolicitedResponse {
/// The message id of the response.
message_id: SnapMessageId,
/// The request id the response carried.
request_id: u64,
},
/// A list in the response is longer than its request allows: more entries than requested, or
/// more range proof nodes than two boundary paths can hold.
///
/// The response is rejected before its body is decoded.
#[error(
"{message_id:?} response with request id {request_id} holds a list longer than the {max_items} items its request allows"
)]
ResponseExceedsRequest {
/// The message id of the response.
message_id: SnapMessageId,
/// The request id the response carried.
request_id: u64,
/// The longest the list may be.
max_items: usize,
},
}

impl SnapProtocolMessage {
Expand Down
28 changes: 27 additions & 1 deletion crates/net/eth-wire/src/errors/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
};
use alloy_chains::Chain;
use alloy_primitives::B256;
use reth_eth_wire_types::{snap::SnapProtocolError, EthVersion};
use reth_eth_wire_types::{snap::SnapProtocolError, EthMessageID, EthVersion};
use reth_ethereum_forks::ValidationError;
use reth_primitives_traits::{GotExpected, GotExpectedBoxed};
use std::io;
Expand Down Expand Up @@ -52,6 +52,30 @@ pub enum EthStreamError {
/// The identifier of the unknown Ethereum message.
message_id: u8,
},
/// Received a response that answers no request sent on this stream.
///
/// The response is rejected before its items are decoded.
#[error("received unsolicited {message_id:?} response with request id {request_id}")]
UnsolicitedResponse {
/// The message id of the response.
message_id: EthMessageID,
/// The request id the response carried.
request_id: u64,
},
/// Received a response with more items than its request asked for.
///
/// The response is rejected before its items are decoded.
#[error(
"{message_id:?} response with request id {request_id} exceeds the {max_items} requested items"
)]
ResponseExceedsRequest {
/// The message id of the response.
message_id: EthMessageID,
/// The request id the response carried.
request_id: u64,
/// The number of items the request asked for.
max_items: usize,
},
}

// === impl EthStreamError ===
Expand All @@ -78,6 +102,8 @@ impl EthStreamError {
Self::MessageTooBig(_) |
Self::TransactionHashesInvalidLenOfFields { .. } |
Self::UnsupportedMessage { .. } |
Self::UnsolicitedResponse { .. } |
Self::ResponseExceedsRequest { .. } |
Self::P2PStreamError(
P2PStreamError::Rlp(_) |
P2PStreamError::Snap(_) |
Expand Down
Loading