Skip to content

Commit

Permalink
rpc: use eth_api() method (paradigmxyz#11516)
Browse files Browse the repository at this point in the history
  • Loading branch information
tcoratger authored and ebo committed Oct 14, 2024
1 parent 831c448 commit e79ec74
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 51 deletions.
45 changes: 18 additions & 27 deletions crates/rpc/rpc/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,8 @@ where
.ok_or(EthApiError::HeaderNotFound(block_id))?;

let ((cfg, block_env, _), block) = futures::try_join!(
self.inner.eth_api.evm_env_at(block_hash.into()),
self.inner.eth_api.block_with_senders(block_id),
self.eth_api().evm_env_at(block_hash.into()),
self.eth_api().block_with_senders(block_id),
)?;

let block = block.ok_or(EthApiError::HeaderNotFound(block_id))?;
Expand All @@ -235,11 +235,11 @@ where
tx_hash: B256,
opts: GethDebugTracingOptions,
) -> Result<GethTrace, Eth::Error> {
let (transaction, block) = match self.inner.eth_api.transaction_and_block(tx_hash).await? {
let (transaction, block) = match self.eth_api().transaction_and_block(tx_hash).await? {
None => return Err(EthApiError::TransactionNotFound.into()),
Some(res) => res,
};
let (cfg, block_env, _) = self.inner.eth_api.evm_env_at(block.hash().into()).await?;
let (cfg, block_env, _) = self.eth_api().evm_env_at(block.hash().into()).await?;

// we need to get the state of the parent block because we're essentially replaying the
// block the transaction is included in
Expand All @@ -248,8 +248,7 @@ where
let block_txs = block.into_transactions_ecrecovered();

let this = self.clone();
self.inner
.eth_api
self.eth_api()
.spawn_with_state_at_block(state_at, move |state| {
// configure env for the target transaction
let tx = transaction.into_recovered();
Expand Down Expand Up @@ -312,8 +311,7 @@ where
GethDebugBuiltInTracerType::FourByteTracer => {
let mut inspector = FourByteInspector::default();
let inspector = self
.inner
.eth_api
.eth_api()
.spawn_with_call_at(call, at, overrides, move |db, env| {
this.eth_api().inspect(db, env, &mut inspector)?;
Ok(inspector)
Expand All @@ -331,8 +329,7 @@ where
);

let frame = self
.inner
.eth_api
.eth_api()
.spawn_with_call_at(call, at, overrides, move |db, env| {
let (res, env) = this.eth_api().inspect(db, env, &mut inspector)?;
let frame = inspector
Expand All @@ -353,8 +350,7 @@ where
);

let frame = self
.inner
.eth_api
.eth_api()
.spawn_with_call_at(call, at, overrides, move |db, env| {
// wrapper is hack to get around 'higher-ranked lifetime error',
// see <https://github.com/rust-lang/rust/issues/100013>
Expand Down Expand Up @@ -434,11 +430,10 @@ where
GethDebugTracerType::JsTracer(code) => {
let config = tracer_config.into_json();

let (_, _, at) = self.inner.eth_api.evm_env_at(at).await?;
let (_, _, at) = self.eth_api().evm_env_at(at).await?;

let res = self
.inner
.eth_api
.eth_api()
.spawn_with_call_at(call, at, overrides, move |db, env| {
// wrapper is hack to get around 'higher-ranked lifetime error', see
// <https://github.com/rust-lang/rust/issues/100013>
Expand All @@ -464,8 +459,7 @@ where
let mut inspector = TracingInspector::new(inspector_config);

let (res, tx_gas_limit, inspector) = self
.inner
.eth_api
.eth_api()
.spawn_with_call_at(call, at, overrides, move |db, env| {
let (res, env) = this.eth_api().inspect(db, env, &mut inspector)?;
Ok((res, env.tx.gas_limit, inspector))
Expand Down Expand Up @@ -499,8 +493,8 @@ where

let target_block = block_number.unwrap_or_default();
let ((cfg, mut block_env, _), block) = futures::try_join!(
self.inner.eth_api.evm_env_at(target_block),
self.inner.eth_api.block_with_senders(target_block),
self.eth_api().evm_env_at(target_block),
self.eth_api().block_with_senders(target_block),
)?;

let opts = opts.unwrap_or_default();
Expand All @@ -524,8 +518,7 @@ where

let this = self.clone();

self.inner
.eth_api
self.eth_api()
.spawn_with_state_at_block(at.into(), move |state| {
// the outer vec for the bundles
let mut all_bundles = Vec::with_capacity(bundles.len());
Expand All @@ -546,7 +539,7 @@ where
),
handler_cfg: cfg.handler_cfg,
};
let (res, _) = this.inner.eth_api.transact(&mut db, env)?;
let (res, _) = this.eth_api().transact(&mut db, env)?;
db.commit(res.state);
}
}
Expand Down Expand Up @@ -604,14 +597,12 @@ where
) -> Result<ExecutionWitness, Eth::Error> {
let this = self.clone();
let block = this
.inner
.eth_api
.eth_api()
.block_with_senders(block_id.into())
.await?
.ok_or(EthApiError::HeaderNotFound(block_id.into()))?;

self.inner
.eth_api
self.eth_api()
.spawn_with_state_at_block(block.parent_hash.into(), move |state_provider| {
let db = StateProviderDatabase::new(&state_provider);
let block_executor = this.inner.block_executor.executor(db);
Expand Down Expand Up @@ -868,7 +859,7 @@ where
///
/// Returns the bytes of the transaction for the given hash.
async fn raw_transaction(&self, hash: B256) -> RpcResult<Option<Bytes>> {
self.inner.eth_api.raw_transaction_by_hash(hash).await.map_err(Into::into)
self.eth_api().raw_transaction_by_hash(hash).await.map_err(Into::into)
}

/// Handler for `debug_getRawTransactions`
Expand Down
16 changes: 10 additions & 6 deletions crates/rpc/rpc/src/eth/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ impl<Eth> EthBundle<Eth> {
pub fn new(eth_api: Eth, blocking_task_guard: BlockingTaskGuard) -> Self {
Self { inner: Arc::new(EthBundleInner { eth_api, blocking_task_guard }) }
}

/// Access the underlying `Eth` API.
pub fn eth_api(&self) -> &Eth {
&self.inner.eth_api
}
}

impl<Eth> EthBundle<Eth>
Expand Down Expand Up @@ -103,7 +108,7 @@ where

let block_id: alloy_rpc_types::BlockId = state_block_number.into();
// Note: the block number is considered the `parent` block: <https://github.com/flashbots/mev-geth/blob/fddf97beec5877483f879a77b7dea2e58a58d653/internal/ethapi/api.go#L2104>
let (cfg, mut block_env, at) = self.inner.eth_api.evm_env_at(block_id).await?;
let (cfg, mut block_env, at) = self.eth_api().evm_env_at(block_id).await?;

// need to adjust the timestamp for the next block
if let Some(timestamp) = timestamp {
Expand All @@ -125,12 +130,12 @@ where
} else if cfg.handler_cfg.spec_id.is_enabled_in(SpecId::LONDON) {
let parent_block = block_env.number.saturating_to::<u64>();
// here we need to fetch the _next_ block's basefee based on the parent block <https://github.com/flashbots/mev-geth/blob/fddf97beec5877483f879a77b7dea2e58a58d653/internal/ethapi/api.go#L2130>
let parent = LoadPendingBlock::provider(&self.inner.eth_api)
let parent = LoadPendingBlock::provider(self.eth_api())
.header_by_number(parent_block)
.map_err(Eth::Error::from_eth_err)?
.ok_or(EthApiError::HeaderNotFound(parent_block.into()))?;
if let Some(base_fee) = parent.next_block_base_fee(
LoadPendingBlock::provider(&self.inner.eth_api)
LoadPendingBlock::provider(self.eth_api())
.chain_spec()
.base_fee_params_at_block(parent_block),
) {
Expand All @@ -142,10 +147,9 @@ where
// use the block number of the request
block_env.number = U256::from(block_number);

let eth_api = self.inner.eth_api.clone();
let eth_api = self.eth_api().clone();

self.inner
.eth_api
self.eth_api()
.spawn_with_state_at_block(at, move |state| {
let coinbase = block_env.coinbase;
let basefee = Some(block_env.basefee.to::<u64>());
Expand Down
30 changes: 12 additions & 18 deletions crates/rpc/rpc/src/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ where
) -> Result<TraceResults, Eth::Error> {
let tx = recover_raw_transaction(tx)?;

let (cfg, block, at) = self.inner.eth_api.evm_env_at(block_id.unwrap_or_default()).await?;
let (cfg, block, at) = self.eth_api().evm_env_at(block_id.unwrap_or_default()).await?;

let env = EnvWithHandlerCfg::new_with_cfg_env(
cfg,
Expand All @@ -130,8 +130,7 @@ where

let config = TracingInspectorConfig::from_parity_config(&trace_types);

self.inner
.eth_api
self.eth_api()
.spawn_trace_at_with_state(env, config, at, move |inspector, res, db| {
inspector
.into_parity_builder()
Expand All @@ -151,7 +150,7 @@ where
block_id: Option<BlockId>,
) -> Result<Vec<TraceResults>, Eth::Error> {
let at = block_id.unwrap_or(BlockId::pending());
let (cfg, block_env, at) = self.inner.eth_api.evm_env_at(at).await?;
let (cfg, block_env, at) = self.eth_api().evm_env_at(at).await?;

let this = self.clone();
// execute all transactions on top of each other and record the traces
Expand Down Expand Up @@ -202,8 +201,7 @@ where
trace_types: HashSet<TraceType>,
) -> Result<TraceResults, Eth::Error> {
let config = TracingInspectorConfig::from_parity_config(&trace_types);
self.inner
.eth_api
self.eth_api()
.spawn_trace_transaction_in_block(hash, config, move |_, inspector, res, db| {
let trace_res = inspector
.into_parity_builder()
Expand Down Expand Up @@ -285,7 +283,7 @@ where
let mut block_traces = Vec::with_capacity(blocks.len());
for block in &blocks {
let matcher = matcher.clone();
let traces = self.inner.eth_api.trace_block_until(
let traces = self.eth_api().trace_block_until(
block.number.into(),
None,
TracingInspectorConfig::default_parity(),
Expand Down Expand Up @@ -345,8 +343,7 @@ where
&self,
hash: B256,
) -> Result<Option<Vec<LocalizedTransactionTrace>>, Eth::Error> {
self.inner
.eth_api
self.eth_api()
.spawn_trace_transaction_in_block(
hash,
TracingInspectorConfig::default_parity(),
Expand All @@ -364,7 +361,7 @@ where
&self,
block_id: BlockId,
) -> Result<Option<Vec<LocalizedTransactionTrace>>, Eth::Error> {
let traces = self.inner.eth_api.trace_block_with(
let traces = self.eth_api().trace_block_with(
block_id,
TracingInspectorConfig::default_parity(),
|tx_info, inspector, _, _, _| {
Expand All @@ -374,7 +371,7 @@ where
},
);

let block = self.inner.eth_api.block(block_id);
let block = self.eth_api().block(block_id);
let (maybe_traces, maybe_block) = futures::try_join!(traces, block)?;

let mut maybe_traces =
Expand All @@ -399,8 +396,7 @@ where
block_id: BlockId,
trace_types: HashSet<TraceType>,
) -> Result<Option<Vec<TraceResultsWithTransactionHash>>, Eth::Error> {
self.inner
.eth_api
self.eth_api()
.trace_block_with(
block_id,
TracingInspectorConfig::from_parity_config(&trace_types),
Expand Down Expand Up @@ -431,8 +427,7 @@ where
&self,
tx_hash: B256,
) -> Result<Option<TransactionOpcodeGas>, Eth::Error> {
self.inner
.eth_api
self.eth_api()
.spawn_trace_transaction_in_block_with_inspector(
tx_hash,
OpcodeGasInspector::default(),
Expand All @@ -456,8 +451,7 @@ where
block_id: BlockId,
) -> Result<Option<BlockOpcodeGas>, Eth::Error> {
let res = self
.inner
.eth_api
.eth_api()
.trace_block_inspector(
block_id,
OpcodeGasInspector::default,
Expand All @@ -473,7 +467,7 @@ where

let Some(transactions) = res else { return Ok(None) };

let Some(block) = self.inner.eth_api.block(block_id).await? else { return Ok(None) };
let Some(block) = self.eth_api().block(block_id).await? else { return Ok(None) };

Ok(Some(BlockOpcodeGas {
block_hash: block.hash(),
Expand Down

0 comments on commit e79ec74

Please sign in to comment.