Skip to content

Commit

Permalink
chore: replace TransactionSigned struct inits with new functions
Browse files Browse the repository at this point in the history
  • Loading branch information
htiennv committed Nov 22, 2024
1 parent 3384c84 commit 3b9bfdc
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 81 deletions.
7 changes: 2 additions & 5 deletions crates/net/network/tests/it/big_pooled_txs_req.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,8 @@ async fn test_large_tx_req() {
// replace rng txhash with real txhash
let mut tx = MockTransaction::eip1559();

let ts = TransactionSigned {
hash: Default::default(),
signature: Signature::test_signature(),
transaction: tx.clone().into(),
};
let ts =
TransactionSigned::new_unhashed(tx.clone().into(), Signature::test_signature());
tx.set_hash(ts.recalculate_hash());
tx
})
Expand Down
2 changes: 1 addition & 1 deletion crates/primitives/src/transaction/sidecar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl BlobTransaction {
hash,
))),
transaction => {
let tx = TransactionSigned { transaction, signature, hash: hash.into() };
let tx = TransactionSigned::new(transaction, signature, hash);
Err((tx, sidecar))
}
}
Expand Down
28 changes: 13 additions & 15 deletions crates/rpc/rpc/src/eth/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ impl<Provider, Pool, Network, EvmConfig> EthApiInner<Provider, Pool, Network, Ev
mod tests {
use alloy_consensus::Header;
use alloy_eips::BlockNumberOrTag;
use alloy_primitives::{B256, U64};
use alloy_primitives::{PrimitiveSignature as Signature, B256, U64};
use alloy_rpc_types::FeeHistory;
use jsonrpsee_types::error::INVALID_PARAMS_CODE;
use reth_chainspec::{BaseFeeParams, ChainSpec, EthChainSpec};
Expand Down Expand Up @@ -504,23 +504,21 @@ mod tests {
let random_fee: u128 = rng.gen();

if let Some(base_fee_per_gas) = header.base_fee_per_gas {
let transaction = TransactionSigned {
transaction: reth_primitives::Transaction::Eip1559(
alloy_consensus::TxEip1559 {
max_priority_fee_per_gas: random_fee,
max_fee_per_gas: random_fee + base_fee_per_gas as u128,
..Default::default()
},
),
..Default::default()
};
let transaction = TransactionSigned::new_unhashed(
reth_primitives::Transaction::Eip1559(alloy_consensus::TxEip1559 {
max_priority_fee_per_gas: random_fee,
max_fee_per_gas: random_fee + base_fee_per_gas as u128,
..Default::default()
}),
Signature::test_signature(),
);

transactions.push(transaction);
} else {
let transaction = TransactionSigned {
transaction: reth_primitives::Transaction::Legacy(Default::default()),
..Default::default()
};
let transaction = TransactionSigned::new_unhashed(
reth_primitives::Transaction::Legacy(Default::default()),
Signature::test_signature(),
);

transactions.push(transaction);
}
Expand Down
6 changes: 2 additions & 4 deletions crates/stages/stages/src/stages/sender_recovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -668,10 +668,8 @@ mod tests {
for tx_id in body.tx_num_range() {
let transaction: TransactionSigned = provider
.transaction_by_id_no_hash(tx_id)?
.map(|tx| TransactionSigned {
hash: Default::default(), // we don't require the hash
signature: tx.signature,
transaction: tx.transaction,
.map(|tx| {
TransactionSigned::new_unhashed(tx.transaction, tx.signature)
})
.expect("no transaction entry");
let signer =
Expand Down
23 changes: 7 additions & 16 deletions crates/storage/provider/src/providers/database/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,12 +549,9 @@ impl<TX: DbTx + 'static, N: NodeTypes> DatabaseProvider<TX, N> {
let body = transactions
.into_iter()
.map(|tx| match transaction_kind {
TransactionVariant::NoHash => TransactionSigned {
// Caller explicitly asked for no hash, so we don't calculate it
hash: Default::default(),
signature: tx.signature,
transaction: tx.transaction,
},
TransactionVariant::NoHash => {
TransactionSigned::new_unhashed(tx.transaction, tx.signature)
}
TransactionVariant::WithHash => tx.with_hash(),
})
.collect();
Expand Down Expand Up @@ -1499,11 +1496,9 @@ impl<TX: DbTx + 'static, N: NodeTypes<ChainSpec: EthereumHardforks>> Transaction

fn transaction_by_hash(&self, hash: TxHash) -> ProviderResult<Option<TransactionSigned>> {
if let Some(id) = self.transaction_id(hash)? {
Ok(self.transaction_by_id_no_hash(id)?.map(|tx| TransactionSigned {
hash: hash.into(),
signature: tx.signature,
transaction: tx.transaction,
}))
Ok(self
.transaction_by_id_no_hash(id)?
.map(|tx| TransactionSigned::new(tx.transaction, tx.signature, hash)))
} else {
Ok(None)
}
Expand All @@ -1517,11 +1512,7 @@ impl<TX: DbTx + 'static, N: NodeTypes<ChainSpec: EthereumHardforks>> Transaction
let mut transaction_cursor = self.tx.cursor_read::<tables::TransactionBlocks>()?;
if let Some(transaction_id) = self.transaction_id(tx_hash)? {
if let Some(tx) = self.transaction_by_id_no_hash(transaction_id)? {
let transaction = TransactionSigned {
hash: tx_hash.into(),
signature: tx.signature,
transaction: tx.transaction,
};
let transaction = TransactionSigned::new(tx.transaction, tx.signature, tx_hash);
if let Some(block_number) =
transaction_cursor.seek(transaction_id).map(|b| b.map(|(_, bn)| bn))?
{
Expand Down
20 changes: 10 additions & 10 deletions crates/storage/provider/src/test_utils/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,14 @@ pub(crate) static TEST_BLOCK: LazyLock<SealedBlock> = LazyLock::new(|| SealedBlo
hex!("cf7b274520720b50e6a4c3e5c4d553101f44945396827705518ce17cb7219a42").into(),
),
body: BlockBody {
transactions: vec![TransactionSigned {
hash: b256!("3541dd1d17e76adeb25dcf2b0a9b60a1669219502e58dcf26a2beafbfb550397").into(),
signature: Signature::new(
transactions: vec![TransactionSigned::new(
Transaction::Legacy(TxLegacy {
gas_price: 10,
gas_limit: 400_000,
to: TxKind::Call(hex!("095e7baea6a6c7c4c2dfeb977efac326af552d87").into()),
..Default::default()
}),
Signature::new(
U256::from_str(
"51983300959770368863831494747186777928121405155922056726144551509338672451120",
)
Expand All @@ -101,13 +106,8 @@ pub(crate) static TEST_BLOCK: LazyLock<SealedBlock> = LazyLock::new(|| SealedBlo
.unwrap(),
false,
),
transaction: Transaction::Legacy(TxLegacy {
gas_price: 10,
gas_limit: 400_000,
to: TxKind::Call(hex!("095e7baea6a6c7c4c2dfeb977efac326af552d87").into()),
..Default::default()
}),
}],
b256!("3541dd1d17e76adeb25dcf2b0a9b60a1669219502e58dcf26a2beafbfb550397"),
)],
..Default::default()
},
});
Expand Down
51 changes: 26 additions & 25 deletions crates/transaction-pool/src/blobstore/tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ pub enum BlobStoreUpdates {
#[cfg(test)]
mod tests {
use alloy_consensus::Header;
use alloy_primitives::PrimitiveSignature as Signature;
use reth_execution_types::Chain;
use reth_primitives::{
BlockBody, SealedBlock, SealedBlockWithSenders, SealedHeader, Transaction,
Expand Down Expand Up @@ -127,22 +128,22 @@ mod tests {
),
body: BlockBody {
transactions: vec![
TransactionSigned {
hash: tx1_hash.into(),
transaction: Transaction::Eip4844(Default::default()),
..Default::default()
},
TransactionSigned {
hash: tx2_hash.into(),
transaction: Transaction::Eip4844(Default::default()),
..Default::default()
},
TransactionSigned::new(
Transaction::Eip4844(Default::default()),
Signature::test_signature(),
tx1_hash,
),
TransactionSigned::new(
Transaction::Eip4844(Default::default()),
Signature::test_signature(),
tx2_hash,
),
// Another transaction that is not EIP-4844
TransactionSigned {
hash: B256::random().into(),
transaction: Transaction::Eip7702(Default::default()),
..Default::default()
},
TransactionSigned::new(
Transaction::Eip7702(Default::default()),
Signature::test_signature(),
B256::random(),
),
],
..Default::default()
},
Expand All @@ -160,16 +161,16 @@ mod tests {
),
body: BlockBody {
transactions: vec![
TransactionSigned {
hash: tx3_hash.into(),
transaction: Transaction::Eip1559(Default::default()),
..Default::default()
},
TransactionSigned {
hash: tx2_hash.into(),
transaction: Transaction::Eip2930(Default::default()),
..Default::default()
},
TransactionSigned::new(
Transaction::Eip1559(Default::default()),
Signature::test_signature(),
tx3_hash,
),
TransactionSigned::new(
Transaction::Eip2930(Default::default()),
Signature::test_signature(),
tx2_hash,
),
],
..Default::default()
},
Expand Down
7 changes: 2 additions & 5 deletions crates/transaction-pool/src/test_utils/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -910,11 +910,8 @@ impl From<PooledTransactionsElementEcRecovered> for MockTransaction {

impl From<MockTransaction> for TransactionSignedEcRecovered {
fn from(tx: MockTransaction) -> Self {
let signed_tx = TransactionSigned {
hash: (*tx.hash()).into(),
signature: Signature::test_signature(),
transaction: tx.clone().into(),
};
let signed_tx =
TransactionSigned::new(tx.clone().into(), Signature::test_signature(), *tx.hash());

Self::from_signed_transaction(signed_tx, tx.sender())
}
Expand Down

0 comments on commit 3b9bfdc

Please sign in to comment.