reth_rpc_eth_types/
utils.rs

1//! Commonly used code snippets
2
3use super::{EthApiError, EthResult};
4use alloy_consensus::TxReceipt;
5use reth_primitives_traits::{Recovered, SignedTransaction};
6use std::future::Future;
7
8/// Calculates the gas used and next log index for a transaction at the given index
9pub fn calculate_gas_used_and_next_log_index(
10    tx_index: u64,
11    all_receipts: &[impl TxReceipt],
12) -> (u64, usize) {
13    let mut gas_used = 0;
14    let mut next_log_index = 0;
15
16    if tx_index > 0 {
17        for receipt in all_receipts.iter().take(tx_index as usize) {
18            gas_used = receipt.cumulative_gas_used();
19            next_log_index += receipt.logs().len();
20        }
21    }
22
23    (gas_used, next_log_index)
24}
25
26/// Recovers a [`SignedTransaction`] from an enveloped encoded byte stream.
27///
28/// This is a helper function that returns the appropriate RPC-specific error if the input data is
29/// malformed.
30///
31/// This function uses [`alloy_eips::eip2718::Decodable2718::decode_2718_exact`] to ensure
32/// that the entire input buffer is consumed and no trailing bytes are allowed.
33///
34/// See [`alloy_eips::eip2718::Decodable2718::decode_2718_exact`]
35pub fn recover_raw_transaction<T: SignedTransaction>(data: &[u8]) -> EthResult<Recovered<T>> {
36    if data.is_empty() {
37        return Err(EthApiError::EmptyRawTransactionData)
38    }
39
40    let transaction =
41        T::decode_2718_exact(data).map_err(|_| EthApiError::FailedToDecodeSignedTransaction)?;
42
43    SignedTransaction::try_into_recovered(transaction)
44        .or(Err(EthApiError::InvalidTransactionSignature))
45}
46
47/// Performs a binary search within a given block range to find the desired block number.
48///
49/// The binary search is performed by calling the provided asynchronous `check` closure on the
50/// blocks of the range. The closure should return a future representing the result of performing
51/// the desired logic at a given block. The future resolves to an `bool` where:
52/// - `true` indicates that the condition has been matched, but we can try to find a lower block to
53///   make the condition more matchable.
54/// - `false` indicates that the condition not matched, so the target is not present in the current
55///   block and should continue searching in a higher range.
56///
57/// Args:
58/// - `low`: The lower bound of the block range (inclusive).
59/// - `high`: The upper bound of the block range (inclusive).
60/// - `check`: A closure that performs the desired logic at a given block.
61pub async fn binary_search<F, Fut, E>(low: u64, high: u64, check: F) -> Result<u64, E>
62where
63    F: Fn(u64) -> Fut,
64    Fut: Future<Output = Result<bool, E>>,
65{
66    let mut low = low;
67    let mut high = high;
68    let mut num = high;
69
70    while low <= high {
71        let mid = (low + high) / 2;
72        if check(mid).await? {
73            high = mid - 1;
74            num = mid;
75        } else {
76            low = mid + 1
77        }
78    }
79
80    Ok(num)
81}
82
83/// Calculates the blob gas used ratio for a block, accounting for the case where
84/// `max_blob_gas_per_block` is zero.
85///
86/// Returns `0.0` if `blob_gas_used` is `0`, otherwise returns the ratio
87/// `blob_gas_used/max_blob_gas_per_block`.
88pub fn checked_blob_gas_used_ratio(blob_gas_used: u64, max_blob_gas_per_block: u64) -> f64 {
89    if blob_gas_used == 0 {
90        0.0
91    } else {
92        blob_gas_used as f64 / max_blob_gas_per_block as f64
93    }
94}
95
96#[cfg(test)]
97mod tests {
98    use super::*;
99
100    #[tokio::test]
101    async fn test_binary_search() {
102        // in the middle
103        let num: Result<_, ()> =
104            binary_search(1, 10, |mid| Box::pin(async move { Ok(mid >= 5) })).await;
105        assert_eq!(num, Ok(5));
106
107        // in the upper
108        let num: Result<_, ()> =
109            binary_search(1, 10, |mid| Box::pin(async move { Ok(mid >= 7) })).await;
110        assert_eq!(num, Ok(7));
111
112        // in the lower
113        let num: Result<_, ()> =
114            binary_search(1, 10, |mid| Box::pin(async move { Ok(mid >= 1) })).await;
115        assert_eq!(num, Ok(1));
116
117        // higher than the upper
118        let num: Result<_, ()> =
119            binary_search(1, 10, |mid| Box::pin(async move { Ok(mid >= 11) })).await;
120        assert_eq!(num, Ok(10));
121    }
122
123    #[test]
124    fn test_checked_blob_gas_used_ratio() {
125        // No blob gas used, max blob gas per block is 0
126        assert_eq!(checked_blob_gas_used_ratio(0, 0), 0.0);
127        // Blob gas used is zero, max blob gas per block is non-zero
128        assert_eq!(checked_blob_gas_used_ratio(0, 100), 0.0);
129        // Blob gas used is non-zero, max blob gas per block is non-zero
130        assert_eq!(checked_blob_gas_used_ratio(50, 100), 0.5);
131        // Blob gas used is non-zero and equal to max blob gas per block
132        assert_eq!(checked_blob_gas_used_ratio(100, 100), 1.0);
133    }
134}