Skip to content

Commit

Permalink
chore: enforce window (paradigmxyz#11540)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored and ebo committed Oct 14, 2024
1 parent 9eb8ffa commit 5e3ed06
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
16 changes: 14 additions & 2 deletions crates/rpc/rpc-eth-api/src/helpers/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use reth_errors::RethError;
use reth_evm::ConfigureEvmEnv;
use reth_primitives::{BlockId, Header, KECCAK_EMPTY};
use reth_provider::{
BlockIdReader, ChainSpecProvider, StateProvider, StateProviderBox, StateProviderFactory,
BlockIdReader, BlockNumReader, ChainSpecProvider, StateProvider, StateProviderBox,
StateProviderFactory,
};
use reth_rpc_eth_types::{EthApiError, EthStateCache, PendingBlockEnv, RpcInvalidTransactionError};
use reth_rpc_types_compat::proof::from_primitive_account_proof;
Expand Down Expand Up @@ -132,10 +133,21 @@ pub trait EthState: LoadState + SpawnBlocking {
) -> impl Future<Output = Result<Option<Account>, Self::Error>> + Send {
self.spawn_blocking_io(move |this| {
let state = this.state_at_block_id(block_id)?;

let account = state.basic_account(address).map_err(Self::Error::from_eth_err)?;
let Some(account) = account else { return Ok(None) };

// Check whether the distance to the block exceeds the maximum configured proof window.
let chain_info =
LoadState::provider(&this).chain_info().map_err(Self::Error::from_eth_err)?;
let block_number = LoadState::provider(&this)
.block_number_for_id(block_id)
.map_err(Self::Error::from_eth_err)?
.ok_or(EthApiError::HeaderNotFound(block_id))?;
let max_window = this.max_proof_window();
if chain_info.best_number.saturating_sub(block_number) > max_window {
return Err(EthApiError::ExceedsMaxProofWindow.into())
}

let balance = account.balance;
let nonce = account.nonce;
let code_hash = account.bytecode_hash.unwrap_or(KECCAK_EMPTY);
Expand Down
21 changes: 5 additions & 16 deletions crates/rpc/rpc/src/eth/helpers/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ mod tests {
use alloy_primitives::{Address, StorageKey, StorageValue, U256};
use reth_chainspec::MAINNET;
use reth_evm_ethereum::EthEvmConfig;
use reth_primitives::{constants::ETHEREUM_BLOCK_GAS_LIMIT, KECCAK_EMPTY};
use reth_network_api::noop::NoopNetwork;
use reth_primitives::constants::ETHEREUM_BLOCK_GAS_LIMIT;
use reth_provider::test_utils::{ExtendedAccount, MockEthProvider, NoopProvider};
use reth_rpc_eth_api::helpers::EthState;
use reth_rpc_eth_types::{
Expand All @@ -61,7 +62,7 @@ mod tests {
use reth_transaction_pool::test_utils::{testing_pool, TestPool};
use std::collections::HashMap;

fn noop_eth_api() -> EthApi<NoopProvider, TestPool, (), EthEvmConfig> {
fn noop_eth_api() -> EthApi<NoopProvider, TestPool, NoopNetwork, EthEvmConfig> {
let pool = testing_pool();
let evm_config = EthEvmConfig::new(MAINNET.clone());

Expand All @@ -70,7 +71,7 @@ mod tests {
EthApi::new(
NoopProvider::default(),
pool,
(),
NoopNetwork::default(),
cache.clone(),
GasPriceOracle::new(NoopProvider::default(), Default::default(), cache.clone()),
ETHEREUM_BLOCK_GAS_LIMIT,
Expand Down Expand Up @@ -102,7 +103,7 @@ mod tests {
GasPriceOracle::new(mock_provider, Default::default(), cache.clone()),
ETHEREUM_BLOCK_GAS_LIMIT,
DEFAULT_MAX_SIMULATE_BLOCKS,
DEFAULT_ETH_PROOF_WINDOW,
DEFAULT_ETH_PROOF_WINDOW + 1,
BlockingTaskPool::build().expect("failed to build tracing pool"),
FeeHistoryCache::new(cache, FeeHistoryCacheConfig::default()),
evm_config,
Expand Down Expand Up @@ -139,16 +140,4 @@ mod tests {
let account = eth_api.get_account(address, Default::default()).await.unwrap();
assert!(account.is_none());
}

#[tokio::test]
async fn test_get_account_empty() {
let address = Address::random();
let accounts = HashMap::from([(address, ExtendedAccount::new(0, U256::ZERO))]);
let eth_api = mock_eth_api(accounts);

let account = eth_api.get_account(address, Default::default()).await.unwrap();
let expected_account =
alloy_rpc_types::Account { code_hash: KECCAK_EMPTY, ..Default::default() };
assert_eq!(Some(expected_account), account);
}
}

0 comments on commit 5e3ed06

Please sign in to comment.