feat: add FromRecoveredTransaction conversion trait (#207)

This commit is contained in:
Matthias Seitz
2022-11-15 16:24:13 +01:00
committed by GitHub
parent b60ced1de1
commit 391a509443
4 changed files with 74 additions and 13 deletions

View File

@ -11,7 +11,10 @@ use rand::{
distributions::{Uniform, WeightedIndex},
prelude::Distribution,
};
use reth_primitives::{Address, TxHash, H256, U256};
use reth_primitives::{
Address, FromRecoveredTransaction, Transaction, TransactionSignedEcRecovered, TxHash, H256,
U256,
};
use std::{ops::Range, sync::Arc, time::Instant};
pub type MockTxPool = TxPool<MockOrdering>;
@ -333,6 +336,48 @@ impl PoolTransaction for MockTransaction {
}
}
impl FromRecoveredTransaction for MockTransaction {
fn from_recovered_transaction(tx: TransactionSignedEcRecovered) -> Self {
let sender = tx.signer();
let transaction = tx.into_signed();
let hash = transaction.hash;
match transaction.transaction {
Transaction::Legacy { chain_id, nonce, gas_price, gas_limit, to, value, input } => {
MockTransaction::Legacy {
hash,
sender,
nonce,
gas_price: gas_price.into(),
gas_limit,
value,
}
}
Transaction::Eip1559 {
chain_id,
nonce,
gas_limit,
max_fee_per_gas,
max_priority_fee_per_gas,
to,
value,
input,
access_list,
} => MockTransaction::Eip1559 {
hash,
sender,
nonce,
max_fee_per_gas: max_fee_per_gas.into(),
max_priority_fee_per_gas: max_priority_fee_per_gas.into(),
gas_limit,
value,
},
Transaction::Eip2930 { .. } => {
unimplemented!()
}
}
}
}
#[derive(Default)]
pub struct MockTransactionFactory {
pub ids: SenderIdentifiers,

View File

@ -1,6 +1,6 @@
use crate::{error::PoolResult, pool::state::SubPool, validate::ValidPoolTransaction, BlockID};
use futures::{channel::mpsc::Receiver, future::Shared};
use reth_primitives::{Address, TxHash, H256, U256};
use reth_primitives::{Address, FromRecoveredTransaction, TxHash, H256, U256};
use std::{fmt, sync::Arc};
/// General purpose abstraction fo a transaction-pool.
@ -174,7 +174,7 @@ impl<T> BestTransactions for std::iter::Empty<T> {
}
/// Trait for transaction types used inside the pool
pub trait PoolTransaction: fmt::Debug + Send + Sync + 'static {
pub trait PoolTransaction: fmt::Debug + Send + Sync + FromRecoveredTransaction {
/// Hash of the transaction.
fn hash(&self) -> &TxHash;