Skip to content

Commit

Permalink
refactor from_recovered_with_block_context
Browse files Browse the repository at this point in the history
  • Loading branch information
tonypony220 committed Jan 4, 2025
1 parent e5cba66 commit 91c10e5
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 48 deletions.
7 changes: 0 additions & 7 deletions crates/optimism/rpc/src/eth/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,6 @@ where
type Transaction = Transaction;
type Error = OpEthApiError;

fn fill_pending(
&self,
tx: RecoveredTx<OpTransactionSigned>,
) -> Result<Self::Transaction, Self::Error> {
self.fill(tx, TransactionInfo::default())
}

fn fill(
&self,
tx: RecoveredTx<OpTransactionSigned>,
Expand Down
16 changes: 5 additions & 11 deletions crates/rpc/rpc-eth-api/src/helpers/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use reth_provider::{
TransactionsProvider,
};
use reth_rpc_eth_types::{utils::binary_search, EthApiError, SignError, TransactionSource};
use reth_rpc_types_compat::transaction::{from_recovered_with_block_context, TransactionCompat};
use reth_rpc_types_compat::transaction::TransactionCompat;
use reth_transaction_pool::{PoolTransaction, TransactionOrigin, TransactionPool};
use std::sync::Arc;

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

return Ok(Some(from_recovered_with_block_context(
tx.clone().with_signer(*signer),
tx_info,
self.tx_resp_builder(),
)?))
return Ok(Some(
self.tx_resp_builder().fill(tx.clone().with_signer(*signer), tx_info)?,
))
}
}

Expand Down Expand Up @@ -301,11 +299,7 @@ pub trait EthTransactions: LoadTransaction<Provider: BlockReaderIdExt> {
base_fee: base_fee_per_gas.map(u128::from),
index: Some(index as u64),
};
from_recovered_with_block_context(
tx.clone().with_signer(*signer),
tx_info,
self.tx_resp_builder(),
)
self.tx_resp_builder().fill(tx.clone().with_signer(*signer), tx_info)
})
})
.ok_or(EthApiError::HeaderNotFound(block_id))?
Expand Down
4 changes: 2 additions & 2 deletions crates/rpc/rpc-eth-types/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use alloy_primitives::B256;
use alloy_rpc_types_eth::TransactionInfo;
use reth_primitives::{RecoveredTx, TransactionSigned};
use reth_primitives_traits::SignedTransaction;
use reth_rpc_types_compat::{transaction::from_recovered_with_block_context, TransactionCompat};
use reth_rpc_types_compat::TransactionCompat;

/// Represents from where a transaction was fetched.
#[derive(Debug, Clone, Eq, PartialEq)]
Expand Down Expand Up @@ -54,7 +54,7 @@ impl<T: SignedTransaction> TransactionSource<T> {
base_fee: base_fee.map(u128::from),
};

from_recovered_with_block_context(transaction, tx_info, resp_builder)
resp_builder.fill(transaction, tx_info)
}
}
}
Expand Down
8 changes: 2 additions & 6 deletions crates/rpc/rpc-types-compat/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use alloy_rpc_types_eth::{
use reth_primitives::{transaction::SignedTransactionIntoRecoveredExt, BlockWithSenders};
use reth_primitives_traits::{Block as BlockTrait, BlockBody, SignedTransaction};

use crate::{transaction::from_recovered_with_block_context, TransactionCompat};
use crate::transaction::TransactionCompat;

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

from_recovered_with_block_context::<_, T>(
signed_tx_ec_recovered,
tx_info,
tx_resp_builder,
)
tx_resp_builder.fill(signed_tx_ec_recovered, tx_info)
})
.collect::<Result<Vec<_>, T::Error>>()?;

Expand Down
24 changes: 9 additions & 15 deletions crates/rpc/rpc-types-compat/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,6 @@ use alloy_rpc_types_eth::{request::TransactionRequest, TransactionInfo};
use reth_primitives::{RecoveredTx, TransactionSigned};
use serde::{Deserialize, Serialize};

/// Create a new rpc transaction result for a mined transaction, using the given block hash,
/// number, and tx index fields to populate the corresponding fields in the rpc result.
///
/// The block hash, number, and tx index fields should be from the original block where the
/// transaction was mined.
pub fn from_recovered_with_block_context<Tx, T: TransactionCompat<Tx>>(
tx: RecoveredTx<Tx>,
tx_info: TransactionInfo,
resp_builder: &T,
) -> Result<T::Transaction, T::Error> {
resp_builder.fill(tx, tx_info)
}

/// Builds RPC transaction w.r.t. network.
pub trait TransactionCompat<T = TransactionSigned>:
Send + Sync + Unpin + Clone + fmt::Debug
Expand All @@ -37,10 +24,17 @@ pub trait TransactionCompat<T = TransactionSigned>:
type Error: error::Error + Into<jsonrpsee_types::ErrorObject<'static>>;

/// Wrapper for `fill()` with default `TransactionInfo`
fn fill_pending(&self, tx: RecoveredTx<T>) -> Result<Self::Transaction, Self::Error>;

/// Create a new rpc transaction result for a _pending_ signed transaction, setting block
/// environment related fields to `None`.
fn fill_pending(&self, tx: RecoveredTx<T>) -> Result<Self::Transaction, Self::Error> {
self.fill(tx, TransactionInfo::default())
}

/// Create a new rpc transaction result for a mined transaction, using the given block hash,
/// number, and tx index fields to populate the corresponding fields in the rpc result.
///
/// The block hash, number, and tx index fields should be from the original block where the
/// transaction was mined.
fn fill(
&self,
tx: RecoveredTx<T>,
Expand Down
7 changes: 0 additions & 7 deletions crates/rpc/rpc/src/eth/helpers/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,6 @@ where

type Error = EthApiError;

fn fill_pending(
&self,
tx: RecoveredTx<TransactionSigned>,
) -> Result<Self::Transaction, Self::Error> {
self.fill(tx, TransactionInfo::default())
}

fn fill(
&self,
tx: RecoveredTx<TransactionSigned>,
Expand Down

0 comments on commit 91c10e5

Please sign in to comment.