Skip to content

Commit 873009f

Browse files
authored
refactor: Remove standalone from_recovered functions and make part of TransactionCompat trait (#13653)
1 parent 923a805 commit 873009f

File tree

7 files changed

+29
-55
lines changed

7 files changed

+29
-55
lines changed

crates/rpc/rpc-eth-api/src/helpers/transaction.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use reth_provider::{
2121
TransactionsProvider,
2222
};
2323
use reth_rpc_eth_types::{utils::binary_search, EthApiError, SignError, TransactionSource};
24-
use reth_rpc_types_compat::transaction::{from_recovered, from_recovered_with_block_context};
24+
use reth_rpc_types_compat::transaction::TransactionCompat;
2525
use reth_transaction_pool::{PoolTransaction, TransactionOrigin, TransactionPool};
2626
use std::sync::Arc;
2727

@@ -221,11 +221,9 @@ pub trait EthTransactions: LoadTransaction<Provider: BlockReaderIdExt> {
221221
index: Some(index as u64),
222222
};
223223

224-
return Ok(Some(from_recovered_with_block_context(
225-
tx.clone().with_signer(*signer),
226-
tx_info,
227-
self.tx_resp_builder(),
228-
)?))
224+
return Ok(Some(
225+
self.tx_resp_builder().fill(tx.clone().with_signer(*signer), tx_info)?,
226+
))
229227
}
230228
}
231229

@@ -250,7 +248,7 @@ pub trait EthTransactions: LoadTransaction<Provider: BlockReaderIdExt> {
250248
RpcNodeCore::pool(self).get_transaction_by_sender_and_nonce(sender, nonce)
251249
{
252250
let transaction = tx.transaction.clone_into_consensus();
253-
return Ok(Some(from_recovered(transaction, self.tx_resp_builder())?));
251+
return Ok(Some(self.tx_resp_builder().fill_pending(transaction)?));
254252
}
255253
}
256254

@@ -301,11 +299,7 @@ pub trait EthTransactions: LoadTransaction<Provider: BlockReaderIdExt> {
301299
base_fee: base_fee_per_gas.map(u128::from),
302300
index: Some(index as u64),
303301
};
304-
from_recovered_with_block_context(
305-
tx.clone().with_signer(*signer),
306-
tx_info,
307-
self.tx_resp_builder(),
308-
)
302+
self.tx_resp_builder().fill(tx.clone().with_signer(*signer), tx_info)
309303
})
310304
})
311305
.ok_or(EthApiError::HeaderNotFound(block_id))?

crates/rpc/rpc-eth-types/src/transaction.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ use alloy_primitives::B256;
66
use alloy_rpc_types_eth::TransactionInfo;
77
use reth_primitives::{RecoveredTx, TransactionSigned};
88
use reth_primitives_traits::SignedTransaction;
9-
use reth_rpc_types_compat::{
10-
transaction::{from_recovered, from_recovered_with_block_context},
11-
TransactionCompat,
12-
};
9+
use reth_rpc_types_compat::TransactionCompat;
1310

1411
/// Represents from where a transaction was fetched.
1512
#[derive(Debug, Clone, Eq, PartialEq)]
@@ -47,7 +44,7 @@ impl<T: SignedTransaction> TransactionSource<T> {
4744
resp_builder: &Builder,
4845
) -> Result<Builder::Transaction, Builder::Error> {
4946
match self {
50-
Self::Pool(tx) => from_recovered(tx, resp_builder),
47+
Self::Pool(tx) => resp_builder.fill_pending(tx),
5148
Self::Block { transaction, index, block_hash, block_number, base_fee } => {
5249
let tx_info = TransactionInfo {
5350
hash: Some(transaction.trie_hash()),
@@ -57,7 +54,7 @@ impl<T: SignedTransaction> TransactionSource<T> {
5754
base_fee: base_fee.map(u128::from),
5855
};
5956

60-
from_recovered_with_block_context(transaction, tx_info, resp_builder)
57+
resp_builder.fill(transaction, tx_info)
6158
}
6259
}
6360
}

crates/rpc/rpc-types-compat/src/block.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use alloy_rpc_types_eth::{
99
use reth_primitives::{transaction::SignedTransactionIntoRecoveredExt, BlockWithSenders};
1010
use reth_primitives_traits::{Block as BlockTrait, BlockBody, SignedTransaction};
1111

12-
use crate::{transaction::from_recovered_with_block_context, TransactionCompat};
12+
use crate::transaction::TransactionCompat;
1313

1414
/// Converts the given primitive block into a [`Block`] response with the given
1515
/// [`BlockTransactionsKind`]
@@ -94,11 +94,7 @@ where
9494
index: Some(idx as u64),
9595
};
9696

97-
from_recovered_with_block_context::<_, T>(
98-
signed_tx_ec_recovered,
99-
tx_info,
100-
tx_resp_builder,
101-
)
97+
tx_resp_builder.fill(signed_tx_ec_recovered, tx_info)
10298
})
10399
.collect::<Result<Vec<_>, T::Error>>()?;
104100

crates/rpc/rpc-types-compat/src/transaction.rs

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,6 @@ use alloy_rpc_types_eth::{request::TransactionRequest, TransactionInfo};
77
use reth_primitives::{RecoveredTx, TransactionSigned};
88
use serde::{Deserialize, Serialize};
99

10-
/// Create a new rpc transaction result for a mined transaction, using the given block hash,
11-
/// number, and tx index fields to populate the corresponding fields in the rpc result.
12-
///
13-
/// The block hash, number, and tx index fields should be from the original block where the
14-
/// transaction was mined.
15-
pub fn from_recovered_with_block_context<Tx, T: TransactionCompat<Tx>>(
16-
tx: RecoveredTx<Tx>,
17-
tx_info: TransactionInfo,
18-
resp_builder: &T,
19-
) -> Result<T::Transaction, T::Error> {
20-
resp_builder.fill(tx, tx_info)
21-
}
22-
23-
/// Create a new rpc transaction result for a _pending_ signed transaction, setting block
24-
/// environment related fields to `None`.
25-
pub fn from_recovered<Tx, T: TransactionCompat<Tx>>(
26-
tx: RecoveredTx<Tx>,
27-
resp_builder: &T,
28-
) -> Result<T::Transaction, T::Error> {
29-
resp_builder.fill(tx, TransactionInfo::default())
30-
}
31-
3210
/// Builds RPC transaction w.r.t. network.
3311
pub trait TransactionCompat<T = TransactionSigned>:
3412
Send + Sync + Unpin + Clone + fmt::Debug
@@ -45,8 +23,18 @@ pub trait TransactionCompat<T = TransactionSigned>:
4523
/// RPC transaction error type.
4624
type Error: error::Error + Into<jsonrpsee_types::ErrorObject<'static>>;
4725

26+
/// Wrapper for `fill()` with default `TransactionInfo`
4827
/// Create a new rpc transaction result for a _pending_ signed transaction, setting block
4928
/// environment related fields to `None`.
29+
fn fill_pending(&self, tx: RecoveredTx<T>) -> Result<Self::Transaction, Self::Error> {
30+
self.fill(tx, TransactionInfo::default())
31+
}
32+
33+
/// Create a new rpc transaction result for a mined transaction, using the given block hash,
34+
/// number, and tx index fields to populate the corresponding fields in the rpc result.
35+
///
36+
/// The block hash, number, and tx index fields should be from the original block where the
37+
/// transaction was mined.
5038
fn fill(
5139
&self,
5240
tx: RecoveredTx<T>,

crates/rpc/rpc/src/eth/filter.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ use reth_rpc_eth_types::{
2323
EthApiError, EthFilterConfig, EthStateCache, EthSubscriptionIdProvider,
2424
};
2525
use reth_rpc_server_types::{result::rpc_error_with_code, ToRpcResult};
26-
use reth_rpc_types_compat::transaction::from_recovered;
2726
use reth_tasks::TaskSpawner;
2827
use reth_transaction_pool::{NewSubpoolTransactionStream, PoolTransaction, TransactionPool};
2928
use std::{
@@ -637,7 +636,7 @@ where
637636
let mut prepared_stream = self.txs_stream.lock().await;
638637

639638
while let Ok(tx) = prepared_stream.try_recv() {
640-
match from_recovered(tx.transaction.to_consensus(), &self.tx_resp_builder) {
639+
match self.tx_resp_builder.fill_pending(tx.transaction.to_consensus()) {
641640
Ok(tx) => pending_txs.push(tx),
642641
Err(err) => {
643642
error!(target: "rpc",

crates/rpc/rpc/src/eth/pubsub.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use reth_rpc_eth_api::{
1919
};
2020
use reth_rpc_eth_types::logs_utils;
2121
use reth_rpc_server_types::result::{internal_rpc_err, invalid_params_rpc_err};
22-
use reth_rpc_types_compat::transaction::from_recovered;
2322
use reth_tasks::{TaskSpawner, TokioTaskExecutor};
2423
use reth_transaction_pool::{NewTransactionEvent, PoolConsensusTx, TransactionPool};
2524
use serde::Serialize;
@@ -119,10 +118,11 @@ where
119118
Params::Bool(true) => {
120119
// full transaction objects requested
121120
let stream = pubsub.full_pending_transaction_stream().filter_map(|tx| {
122-
let tx_value = match from_recovered(
123-
tx.transaction.to_consensus(),
124-
pubsub.eth_api.tx_resp_builder(),
125-
) {
121+
let tx_value = match pubsub
122+
.eth_api
123+
.tx_resp_builder()
124+
.fill_pending(tx.transaction.to_consensus())
125+
{
126126
Ok(tx) => Some(tx),
127127
Err(err) => {
128128
error!(target = "rpc",

crates/rpc/rpc/src/txpool.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use alloy_rpc_types_txpool::{
99
use async_trait::async_trait;
1010
use jsonrpsee::core::RpcResult;
1111
use reth_rpc_api::TxPoolApiServer;
12-
use reth_rpc_types_compat::{transaction::from_recovered, TransactionCompat};
12+
use reth_rpc_types_compat::TransactionCompat;
1313
use reth_transaction_pool::{
1414
AllPoolTransactions, PoolConsensusTx, PoolTransaction, TransactionPool,
1515
};
@@ -50,7 +50,7 @@ where
5050
{
5151
content.entry(tx.sender()).or_default().insert(
5252
tx.nonce().to_string(),
53-
from_recovered(tx.clone_into_consensus(), resp_builder)?,
53+
resp_builder.fill_pending(tx.clone_into_consensus())?,
5454
);
5555

5656
Ok(())

0 commit comments

Comments
 (0)