Skip to content

Commit

Permalink
feat(rpc): create revm env on demand (#13017)
Browse files Browse the repository at this point in the history
Co-authored-by: dkathiriya <[email protected]>
Co-authored-by: Matthias Seitz <[email protected]>
  • Loading branch information
3 people authored Nov 30, 2024
1 parent 3dc6f50 commit 890f082
Show file tree
Hide file tree
Showing 14 changed files with 91 additions and 123 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions book/cli/reth/node.md
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,8 @@ RPC State Cache:
[default: 2000]
--rpc-cache.max-envs <MAX_ENVS>
Max number of bytes for cached env data
--rpc-cache.max-envs <MAX_HEADERS>
Max number of headers in cache
[default: 1000]
Expand Down
10 changes: 5 additions & 5 deletions crates/node/core/src/args/rpc_state_cache.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clap::Args;
use reth_rpc_server_types::constants::cache::{
DEFAULT_BLOCK_CACHE_MAX_LEN, DEFAULT_CONCURRENT_DB_REQUESTS, DEFAULT_ENV_CACHE_MAX_LEN,
DEFAULT_BLOCK_CACHE_MAX_LEN, DEFAULT_CONCURRENT_DB_REQUESTS, DEFAULT_HEADER_CACHE_MAX_LEN,
DEFAULT_RECEIPT_CACHE_MAX_LEN,
};

Expand All @@ -22,12 +22,12 @@ pub struct RpcStateCacheArgs {
)]
pub max_receipts: u32,

/// Max number of bytes for cached env data.
/// Max number of headers in cache.
#[arg(
long = "rpc-cache.max-envs",
default_value_t = DEFAULT_ENV_CACHE_MAX_LEN,
default_value_t = DEFAULT_HEADER_CACHE_MAX_LEN,
)]
pub max_envs: u32,
pub max_headers: u32,

/// Max number of concurrent database requests.
#[arg(
Expand All @@ -42,7 +42,7 @@ impl Default for RpcStateCacheArgs {
Self {
max_blocks: DEFAULT_BLOCK_CACHE_MAX_LEN,
max_receipts: DEFAULT_RECEIPT_CACHE_MAX_LEN,
max_envs: DEFAULT_ENV_CACHE_MAX_LEN,
max_headers: DEFAULT_HEADER_CACHE_MAX_LEN,
max_concurrent_db_requests: DEFAULT_CONCURRENT_DB_REQUESTS,
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/rpc/rpc-builder/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl RethRpcServerConfig for RpcServerArgs {
EthStateCacheConfig {
max_blocks: self.rpc_state_cache.max_blocks,
max_receipts: self.rpc_state_cache.max_receipts,
max_envs: self.rpc_state_cache.max_envs,
max_headers: self.rpc_state_cache.max_headers,
max_concurrent_db_requests: self.rpc_state_cache.max_concurrent_db_requests,
}
}
Expand Down
7 changes: 1 addition & 6 deletions crates/rpc/rpc-builder/src/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,7 @@ where
EvmConfig: ConfigureEvm<Header = Header>,
Tasks: TaskSpawner + Clone + 'static,
{
let cache = EthStateCache::spawn_with(
provider.clone(),
config.cache,
executor.clone(),
evm_config.clone(),
);
let cache = EthStateCache::spawn_with(provider.clone(), config.cache, executor.clone());

let new_canonical_blocks = events.canonical_state_stream();
let c = cache.clone();
Expand Down
14 changes: 8 additions & 6 deletions crates/rpc/rpc-builder/tests/it/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,15 +259,15 @@ where
Some(block_number.into()),
)
.await
.unwrap();
.unwrap_err();
EthApiClient::<Transaction, Block, Receipt>::estimate_gas(
client,
call_request.clone(),
Some(block_number.into()),
None,
)
.await
.unwrap();
.unwrap_err();
EthApiClient::<Transaction, Block, Receipt>::call(
client,
call_request.clone(),
Expand All @@ -276,7 +276,7 @@ where
None,
)
.await
.unwrap();
.unwrap_err();
EthApiClient::<Transaction, Block, Receipt>::syncing(client).await.unwrap();
EthApiClient::<Transaction, Block, Receipt>::send_transaction(
client,
Expand Down Expand Up @@ -368,13 +368,15 @@ where
.unwrap_err();
TraceApiClient::trace_call_many(client, vec![], Some(BlockNumberOrTag::Latest.into()))
.await
.unwrap();
.unwrap_err();
TraceApiClient::replay_transaction(client, B256::default(), HashSet::default())
.await
.err()
.unwrap();
TraceApiClient::trace_block(client, block_id).await.unwrap();
TraceApiClient::replay_block_transactions(client, block_id, HashSet::default()).await.unwrap();
TraceApiClient::trace_block(client, block_id).await.unwrap_err();
TraceApiClient::replay_block_transactions(client, block_id, HashSet::default())
.await
.unwrap_err();

TraceApiClient::trace_filter(client, trace_filter).await.unwrap();
}
Expand Down
17 changes: 10 additions & 7 deletions crates/rpc/rpc-eth-api/src/helpers/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use reth_chainspec::{EthChainSpec, EthereumHardforks};
use reth_errors::RethError;
use reth_evm::ConfigureEvmEnv;
use reth_provider::{
BlockIdReader, BlockNumReader, ChainSpecProvider, StateProvider, StateProviderBox,
StateProviderFactory,
BlockIdReader, BlockNumReader, ChainSpecProvider, EvmEnvProvider as _, StateProvider,
StateProviderBox, StateProviderFactory,
};
use reth_rpc_eth_types::{EthApiError, PendingBlockEnv, RpcInvalidTransactionError};
use reth_transaction_pool::TransactionPool;
Expand Down Expand Up @@ -229,12 +229,15 @@ pub trait LoadState:
.block_hash_for_id(at)
.map_err(Self::Error::from_eth_err)?
.ok_or(EthApiError::HeaderNotFound(at))?;
let (cfg, env) = self
.cache()
.get_evm_env(block_hash)
.await

let header =
self.cache().get_header(block_hash).await.map_err(Self::Error::from_eth_err)?;
let evm_config = self.evm_config().clone();
let (cfg, block_env) = self
.provider()
.env_with_header(&header, evm_config)
.map_err(Self::Error::from_eth_err)?;
Ok((cfg, env, block_hash.into()))
Ok((cfg, block_env, block_hash.into()))
}
}
}
Expand Down
1 change: 0 additions & 1 deletion crates/rpc/rpc-eth-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ workspace = true
reth-chainspec.workspace = true
reth-chain-state.workspace = true
reth-errors.workspace = true
reth-evm.workspace = true
reth-execution-types.workspace = true
reth-metrics.workspace = true
reth-primitives = { workspace = true, features = ["secp256k1"] }
Expand Down
8 changes: 4 additions & 4 deletions crates/rpc/rpc-eth-types/src/cache/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use serde::{Deserialize, Serialize};

use reth_rpc_server_types::constants::cache::{
DEFAULT_BLOCK_CACHE_MAX_LEN, DEFAULT_CONCURRENT_DB_REQUESTS, DEFAULT_ENV_CACHE_MAX_LEN,
DEFAULT_BLOCK_CACHE_MAX_LEN, DEFAULT_CONCURRENT_DB_REQUESTS, DEFAULT_HEADER_CACHE_MAX_LEN,
DEFAULT_RECEIPT_CACHE_MAX_LEN,
};

Expand All @@ -19,10 +19,10 @@ pub struct EthStateCacheConfig {
///
/// Default is 2000.
pub max_receipts: u32,
/// Max number of bytes for cached env data.
/// Max number of headers in cache.
///
/// Default is 1000.
pub max_envs: u32,
pub max_headers: u32,
/// Max number of concurrent database requests.
///
/// Default is 512.
Expand All @@ -34,7 +34,7 @@ impl Default for EthStateCacheConfig {
Self {
max_blocks: DEFAULT_BLOCK_CACHE_MAX_LEN,
max_receipts: DEFAULT_RECEIPT_CACHE_MAX_LEN,
max_envs: DEFAULT_ENV_CACHE_MAX_LEN,
max_headers: DEFAULT_HEADER_CACHE_MAX_LEN,
max_concurrent_db_requests: DEFAULT_CONCURRENT_DB_REQUESTS,
}
}
Expand Down
Loading

0 comments on commit 890f082

Please sign in to comment.