Skip to content

Commit

Permalink
perf: remove getBlock request in feeHistory (#414)
Browse files Browse the repository at this point in the history
* perf: remove getBlock request in feeHistory

* clippy

* check 0 fee

* Update crates/rpc-types/src/eth/fee.rs

Co-authored-by: Oliver Nordbjerg <[email protected]>

* Update crates/rpc-types/src/eth/fee.rs

Co-authored-by: Oliver Nordbjerg <[email protected]>

---------

Co-authored-by: Oliver Nordbjerg <[email protected]>
  • Loading branch information
mattsse and onbjerg authored Mar 29, 2024
1 parent c7dacad commit 1fc24a7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
28 changes: 18 additions & 10 deletions crates/provider/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -831,20 +831,28 @@ pub trait Provider<N: Network, T: Transport + Clone = BoxTransport>: Send + Sync
&self,
estimator: Option<EstimatorFunction>,
) -> TransportResult<Eip1559Estimation> {
let (bf, fee_history) = futures::try_join!(
self.get_block_by_number(BlockNumberOrTag::Latest, false),
self.get_fee_history(
let fee_history = self
.get_fee_history(
U256::from(utils::EIP1559_FEE_ESTIMATION_PAST_BLOCKS),
BlockNumberOrTag::Latest,
&[utils::EIP1559_FEE_ESTIMATION_REWARD_PERCENTILE],
)
)?;

let base_fee_per_gas = bf
.ok_or(RpcError::NullResp)?
.header
.base_fee_per_gas
.ok_or(RpcError::UnsupportedFeature("eip1559"))?;
.await?;

// if the base fee of the Latest block is 0 then we need check if the latest block even has
// a base fee/supports EIP1559
let base_fee_per_gas = match fee_history.latest_block_base_fee() {
Some(base_fee) if !base_fee.is_zero() => base_fee,
_ => {
// empty response, fetch basefee from latest block directly
self.get_block_by_number(BlockNumberOrTag::Latest, false)
.await?
.ok_or(RpcError::NullResp)?
.header
.base_fee_per_gas
.ok_or(RpcError::UnsupportedFeature("eip1559"))?
}
};

Ok(estimator.unwrap_or(utils::eip1559_default_estimator)(
base_fee_per_gas,
Expand Down
23 changes: 22 additions & 1 deletion crates/rpc-types/src/eth/fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ pub struct FeeHistory {
}

impl FeeHistory {
/// Returns the base fee of the latest block in the `eth_feeHistory` request.
pub fn latest_block_base_fee(&self) -> Option<U256> {
// the base fee of requested block is the second last element in the
// list
self.base_fee_per_gas.iter().rev().nth(1).copied()
}

/// Returns the base fee of the next block.
pub fn next_block_base_fee(&self) -> Option<U256> {
self.base_fee_per_gas.last().copied()
Expand All @@ -72,7 +79,7 @@ impl FeeHistory {
/// Returns the blob base fee of the next block.
///
/// If the next block is pre- EIP-4844, this will return `None`.
pub fn next_blob_base_fee(&self) -> Option<U256> {
pub fn next_block_blob_base_fee(&self) -> Option<U256> {
self.base_fee_per_blob_gas
.last()
.filter(|fee| {
Expand All @@ -81,4 +88,18 @@ impl FeeHistory {
})
.copied()
}

/// Returns the blob fee of the latest block in the `eth_feeHistory` request.
pub fn latest_block_blob_base_fee(&self) -> Option<U256> {
// the blob fee requested block is the second last element in the list
self.base_fee_per_blob_gas
.iter()
.rev()
.nth(1)
.filter(|fee| {
// skip zero value that is returned for pre-EIP-4844 blocks
!fee.is_zero()
})
.copied()
}
}

0 comments on commit 1fc24a7

Please sign in to comment.