chore(rpc): only forward calls to upstream if against latest block state

This commit is contained in:
Quertyy
2025-07-07 02:20:52 +02:00
parent 0c640ab2f2
commit d0d7ab8cb8
2 changed files with 75 additions and 32 deletions

View File

@ -1,6 +1,10 @@
use alloy_eips::BlockId;
use alloy_primitives::{Bytes, U256};
use alloy_rpc_types_eth::{state::StateOverride, transaction::TransactionRequest, BlockOverrides};
use alloy_rpc_types_eth::{
state::{EvmOverrides, StateOverride},
transaction::TransactionRequest,
BlockOverrides,
};
use jsonrpsee::{
http_client::{HttpClient, HttpClientBuilder},
proc_macros::rpc,
@ -8,6 +12,7 @@ use jsonrpsee::{
types::{error::INTERNAL_ERROR_CODE, ErrorObject},
};
use jsonrpsee_core::{async_trait, client::ClientT, ClientError, RpcResult};
use reth_rpc_eth_api::helpers::EthCall;
#[rpc(server, namespace = "eth")]
pub(crate) trait CallForwarderApi {
@ -32,21 +37,25 @@ pub(crate) trait CallForwarderApi {
) -> RpcResult<U256>;
}
pub struct CallForwarderExt {
client: HttpClient,
pub struct CallForwarderExt<EthApi> {
upstream_client: HttpClient,
eth_api: EthApi,
}
impl CallForwarderExt {
pub fn new(upstream_rpc_url: String) -> Self {
let client =
impl<EthApi> CallForwarderExt<EthApi> {
pub fn new(upstream_rpc_url: String, eth_api: EthApi) -> Self {
let upstream_client =
HttpClientBuilder::default().build(upstream_rpc_url).expect("Failed to build client");
Self { client }
Self { upstream_client, eth_api }
}
}
#[async_trait]
impl CallForwarderApiServer for CallForwarderExt {
impl<EthApi> CallForwarderApiServer for CallForwarderExt<EthApi>
where
EthApi: EthCall + Send + Sync + 'static,
{
async fn call(
&self,
request: TransactionRequest,
@ -54,9 +63,9 @@ impl CallForwarderApiServer for CallForwarderExt {
state_overrides: Option<StateOverride>,
block_overrides: Option<Box<BlockOverrides>>,
) -> RpcResult<Bytes> {
let result = self
.client
.clone()
let is_latest = block_number.as_ref().map(|b| b.is_latest()).unwrap_or(true);
let result = if is_latest {
self.upstream_client
.request(
"eth_call",
rpc_params![request, block_number, state_overrides, block_overrides],
@ -69,7 +78,20 @@ impl CallForwarderApiServer for CallForwarderExt {
format!("Failed to call: {e:?}"),
Some(()),
),
})?;
})?
} else {
EthCall::call(
&self.eth_api,
request,
block_number,
EvmOverrides::new(state_overrides, block_overrides),
)
.await
.map_err(|e| {
ErrorObject::owned(INTERNAL_ERROR_CODE, format!("Failed to call: {e:?}"), Some(()))
})?
};
Ok(result)
}
@ -79,9 +101,9 @@ impl CallForwarderApiServer for CallForwarderExt {
block_number: Option<BlockId>,
state_override: Option<StateOverride>,
) -> RpcResult<U256> {
let result = self
.client
.clone()
let is_latest = block_number.as_ref().map(|b| b.is_latest()).unwrap_or(true);
let result = if is_latest {
self.upstream_client
.request("eth_estimateGas", rpc_params![request, block_number, state_override])
.await
.map_err(|e| match e {
@ -91,7 +113,24 @@ impl CallForwarderApiServer for CallForwarderExt {
format!("Failed to estimate gas: {e:?}"),
Some(()),
),
})?;
})?
} else {
EthCall::estimate_gas_at(
&self.eth_api,
request,
block_number.unwrap_or_default(),
state_override,
)
.await
.map_err(|e| {
ErrorObject::owned(
INTERNAL_ERROR_CODE,
format!("Failed to estimate gas: {e:?}"),
Some(()),
)
})?
};
Ok(result)
}
}

View File

@ -45,7 +45,11 @@ fn main() -> eyre::Result<()> {
if ext.forward_call {
ctx.modules.replace_configured(
call_forwarder::CallForwarderExt::new(upstream_rpc_url.clone()).into_rpc(),
call_forwarder::CallForwarderExt::new(
upstream_rpc_url.clone(),
ctx.registry.eth_api().clone(),
)
.into_rpc(),
)?;
info!("Call/gas estimation will be forwarded to {}", upstream_rpc_url);
}