Skip to content

Commit 259b620

Browse files
committed
chore(rpc): re-optimize endpoints
Seems this was not included when migrating out of the SDK.
1 parent 3f992a5 commit 259b620

File tree

3 files changed

+30
-17
lines changed

3 files changed

+30
-17
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ resolver = "2"
44

55
[workspace.package]
66
version = "0.8.2"
7-
edition = "2021"
8-
rust-version = "1.82"
7+
edition = "2024"
8+
rust-version = "1.88"
99
authors = ["init4"]
1010
license = "MIT OR Apache-2.0"
1111
homepage = "https://github.com/init4tech/signet-sdk"

crates/rpc/src/ctx.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,21 @@ where
312312
Ok(builder.build())
313313
}
314314

315+
/// Get the [`Header`] for a given block.
316+
pub async fn raw_header(
317+
&self,
318+
t: impl Into<BlockId>,
319+
) -> Result<Option<(B256, Header)>, EthApiError> {
320+
let Some(hash) = self.provider.block_hash_for_id(t.into())? else {
321+
return Ok(None);
322+
};
323+
324+
let header = self.cache.get_header(hash).await.map_err(EthApiError::from)?;
325+
326+
Ok(Some((hash, header)))
327+
}
328+
329+
315330
/// Get the block for a given block, returning the block hash and
316331
/// the block itself.
317332
pub async fn raw_block(
@@ -513,20 +528,18 @@ where
513528
build_signet_receipt(tx, meta, receipt, all_receipts.to_vec()).map(Some)
514529
}
515530

516-
/// Create the [`Block`] object for a specific [`BlockId`].
531+
/// Create the [`Header`] object for a specific [`BlockId`].
517532
pub async fn block_cfg(&self, mut block_id: BlockId) -> Result<Header, EthApiError> {
518533
// If the block is pending, we'll load the latest and
519534
let pending = block_id.is_pending();
520535
if pending {
521536
block_id = BlockId::latest();
522537
}
523538

524-
let Some((_, block)) = self.raw_block(block_id).await? else {
539+
let Some((_, mut header)) = self.raw_header(block_id).await? else {
525540
return Err(EthApiError::HeaderNotFound(block_id));
526541
};
527542

528-
let mut header = block.clone_header();
529-
530543
// Modify the header for pending blocks, to simulate the next block.
531544
if pending {
532545
header.parent_hash = header.hash_slow();

crates/rpc/src/eth/endpoints.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
use crate::{
2+
Pnt,
23
ctx::RpcCtx,
34
eth::{CallErrorData, EthError},
45
interest::{FilterOutput, InterestKind},
56
receipts::build_signet_receipt,
67
util::{await_jh_option, await_jh_option_response, response_tri},
7-
Pnt,
88
};
99
use ajj::{HandlerCtx, ResponsePayload};
1010
use alloy::{
1111
consensus::{BlockHeader, TxEnvelope},
1212
eips::{
13-
eip2718::{Decodable2718, Encodable2718},
1413
BlockId, BlockNumberOrTag,
14+
eip2718::{Decodable2718, Encodable2718},
1515
},
1616
network::Ethereum,
17-
primitives::{Address, B256, U256, U64},
17+
primitives::{Address, B256, U64, U256},
1818
rpc::types::{
19-
pubsub::SubscriptionKind, state::StateOverride, BlockOverrides, Filter, TransactionRequest,
19+
BlockOverrides, Filter, TransactionRequest, pubsub::SubscriptionKind, state::StateOverride,
2020
},
2121
};
2222
use reth::{
@@ -29,7 +29,7 @@ use reth_rpc_eth_api::{RpcBlock, RpcHeader, RpcReceipt, RpcTransaction};
2929
use serde::Deserialize;
3030
use signet_evm::EvmErrored;
3131
use std::borrow::Cow;
32-
use tracing::{debug, trace_span, Instrument};
32+
use tracing::{Instrument, debug, trace_span};
3333
use trevm::revm::context::result::ExecutionResult;
3434

3535
/// Args for `eth_estimateGas` and `eth_call`.
@@ -405,7 +405,7 @@ where
405405
/// - If the gas is below `MIN_TRANSACTION_GAS`, set it to `None`
406406
/// - If the gas is above the `rpc_gas_cap`, set it to the `rpc_gas_cap`
407407
/// - Otherwise, do nothing
408-
fn normalize_gas_stateless(request: &mut TransactionRequest, max_gas: u64) {
408+
const fn normalize_gas_stateless(request: &mut TransactionRequest, max_gas: u64) {
409409
match request.gas {
410410
Some(..trevm::MIN_TRANSACTION_GAS) => request.gas = None,
411411
Some(val) if val > max_gas => request.gas = Some(max_gas),
@@ -443,7 +443,7 @@ where
443443
return ResponsePayload::internal_error_with_message_and_obj(
444444
"error while loading block cfg".into(),
445445
e.to_string().into(),
446-
)
446+
);
447447
}
448448
};
449449

@@ -550,7 +550,7 @@ where
550550
return ResponsePayload::internal_error_with_message_and_obj(
551551
"error while loading block cfg".into(),
552552
e.to_string().into(),
553-
)
553+
);
554554
}
555555
};
556556

@@ -599,13 +599,13 @@ where
599599
Signet: Pnt,
600600
{
601601
let task = async move {
602-
let (block, suggested) = tokio::try_join!(
603-
ctx.signet().raw_block(BlockId::latest()),
602+
let (header, suggested) = tokio::try_join!(
603+
ctx.signet().raw_header(BlockId::latest()),
604604
ctx.signet().gas_oracle().suggest_tip_cap(),
605605
)
606606
.map_err(|e| e.to_string())?;
607607

608-
let base_fee = block.and_then(|b| b.1.header().base_fee_per_gas()).unwrap_or_default();
608+
let base_fee = header.and_then(|h| h.1.base_fee_per_gas()).unwrap_or_default();
609609
Ok(suggested + U256::from(base_fee))
610610
};
611611

0 commit comments

Comments
 (0)