reth_rpc_eth_types/
transaction.rsuse 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, from_recovered_with_block_context},
TransactionCompat,
};
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum TransactionSource<T = TransactionSigned> {
Pool(RecoveredTx<T>),
Block {
transaction: RecoveredTx<T>,
index: u64,
block_hash: B256,
block_number: u64,
base_fee: Option<u64>,
},
}
impl<T: SignedTransaction> TransactionSource<T> {
pub fn into_recovered(self) -> RecoveredTx<T> {
self.into()
}
pub fn into_transaction<Builder: TransactionCompat<T>>(
self,
resp_builder: &Builder,
) -> Result<Builder::Transaction, Builder::Error> {
match self {
Self::Pool(tx) => from_recovered(tx, resp_builder),
Self::Block { transaction, index, block_hash, block_number, base_fee } => {
let tx_info = TransactionInfo {
hash: Some(transaction.trie_hash()),
index: Some(index),
block_hash: Some(block_hash),
block_number: Some(block_number),
base_fee: base_fee.map(u128::from),
};
from_recovered_with_block_context(transaction, tx_info, resp_builder)
}
}
}
pub fn split(self) -> (RecoveredTx<T>, TransactionInfo) {
match self {
Self::Pool(tx) => {
let hash = tx.trie_hash();
(tx, TransactionInfo { hash: Some(hash), ..Default::default() })
}
Self::Block { transaction, index, block_hash, block_number, base_fee } => {
let hash = transaction.trie_hash();
(
transaction,
TransactionInfo {
hash: Some(hash),
index: Some(index),
block_hash: Some(block_hash),
block_number: Some(block_number),
base_fee: base_fee.map(u128::from),
},
)
}
}
}
}
impl<T> From<TransactionSource<T>> for RecoveredTx<T> {
fn from(value: TransactionSource<T>) -> Self {
match value {
TransactionSource::Pool(tx) => tx,
TransactionSource::Block { transaction, .. } => transaction,
}
}
}