chore: make transaction type fields private (#13915)

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
This commit is contained in:
Steven
2025-01-29 04:56:07 -06:00
committed by GitHub
parent ed593ae257
commit 2652ec8af5
12 changed files with 63 additions and 25 deletions

View File

@ -169,7 +169,7 @@ impl<C: ChainSpecParser<ChainSpec = ChainSpec>> Command<C> {
.try_clone_into_recovered() .try_clone_into_recovered()
.map_err(|e| eyre::eyre!("failed to recover tx: {e}"))?; .map_err(|e| eyre::eyre!("failed to recover tx: {e}"))?;
let encoded_length = match &transaction.transaction { let encoded_length = match transaction.transaction() {
Transaction::Eip4844(TxEip4844 { blob_versioned_hashes, .. }) => { Transaction::Eip4844(TxEip4844 { blob_versioned_hashes, .. }) => {
let blobs_bundle = blobs_bundle.as_mut().ok_or_else(|| { let blobs_bundle = blobs_bundle.as_mut().ok_or_else(|| {
eyre::eyre!("encountered a blob tx. `--blobs-bundle-path` must be provided") eyre::eyre!("encountered a blob tx. `--blobs-bundle-path` must be provided")

View File

@ -124,4 +124,5 @@ test-utils = [
"reth-prune-types?/test-utils", "reth-prune-types?/test-utils",
"reth-trie-db/test-utils", "reth-trie-db/test-utils",
"reth-trie-parallel/test-utils", "reth-trie-parallel/test-utils",
"reth-ethereum-primitives/test-utils",
] ]

View File

@ -258,7 +258,7 @@ where
// invalid, which removes its dependent transactions from // invalid, which removes its dependent transactions from
// the iterator. This is similar to the gas limit condition // the iterator. This is similar to the gas limit condition
// for regular transactions above. // for regular transactions above.
trace!(target: "payload_builder", tx=?tx.hash, ?sum_blob_gas_used, ?tx_blob_gas, "skipping blob transaction because it would exceed the max data gas per block"); trace!(target: "payload_builder", tx=?tx.hash(), ?sum_blob_gas_used, ?tx_blob_gas, "skipping blob transaction because it would exceed the max data gas per block");
best_txs.mark_invalid( best_txs.mark_invalid(
&pool_tx, &pool_tx,
InvalidPoolTransactionError::ExceedsGasLimit( InvalidPoolTransactionError::ExceedsGasLimit(

View File

@ -51,6 +51,10 @@ alloy-consensus = { workspace = true, features = ["serde", "arbitrary"] }
[features] [features]
default = ["std"] default = ["std"]
test-utils = [
"reth-codecs?/test-utils",
"reth-primitives-traits/test-utils",
]
alloy-compat = ["dep:alloy-network", "dep:alloy-serde", "dep:alloy-rpc-types"] alloy-compat = ["dep:alloy-network", "dep:alloy-serde", "dep:alloy-rpc-types"]
std = [ std = [
"alloy-consensus/std", "alloy-consensus/std",

View File

@ -282,17 +282,19 @@ impl From<TypedTransaction> for Transaction {
/// Signed Ethereum transaction. /// Signed Ethereum transaction.
#[derive(Debug, Clone, Eq, Serialize, Deserialize, derive_more::AsRef, derive_more::Deref)] #[derive(Debug, Clone, Eq, Serialize, Deserialize, derive_more::AsRef, derive_more::Deref)]
#[cfg_attr(any(test, feature = "reth-codec"), reth_codecs::add_arbitrary_tests(rlp))] #[cfg_attr(any(test, feature = "reth-codec"), reth_codecs::add_arbitrary_tests(rlp))]
#[cfg_attr(feature = "test-utils", derive(derive_more::DerefMut))]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct TransactionSigned { pub struct TransactionSigned {
/// Transaction hash /// Transaction hash
#[serde(skip)] #[serde(skip)]
pub hash: OnceLock<TxHash>, hash: OnceLock<TxHash>,
/// The transaction signature values /// The transaction signature values
pub signature: Signature, signature: Signature,
/// Raw transaction info /// Raw transaction info
#[deref] #[deref]
#[as_ref] #[as_ref]
pub transaction: Transaction, #[cfg_attr(feature = "test-utils", deref_mut)]
transaction: Transaction,
} }
impl Default for TransactionSigned { impl Default for TransactionSigned {
@ -328,6 +330,30 @@ impl TransactionSigned {
Self { hash: hash.into(), signature, transaction } Self { hash: hash.into(), signature, transaction }
} }
/// Consumes the type and returns the transaction.
#[inline]
pub fn into_transaction(self) -> Transaction {
self.transaction
}
/// Returns the transaction.
#[inline]
pub const fn transaction(&self) -> &Transaction {
&self.transaction
}
/// Returns the transaction hash.
#[inline]
pub fn hash(&self) -> &B256 {
self.hash.get_or_init(|| self.recalculate_hash())
}
/// Returns the transaction signature.
#[inline]
pub const fn signature(&self) -> &Signature {
&self.signature
}
/// Creates a new signed transaction from the given transaction and signature without the hash. /// Creates a new signed transaction from the given transaction and signature without the hash.
/// ///
/// Note: this only calculates the hash on the first [`TransactionSigned::hash`] call. /// Note: this only calculates the hash on the first [`TransactionSigned::hash`] call.
@ -335,6 +361,11 @@ impl TransactionSigned {
Self { hash: Default::default(), signature, transaction } Self { hash: Default::default(), signature, transaction }
} }
/// Splits the `TransactionSigned` into its transaction and signature.
pub fn split(self) -> (Transaction, Signature) {
(self.transaction, self.signature)
}
/// Converts from an EIP-4844 transaction to a [`PooledTransaction`] with the given sidecar. /// Converts from an EIP-4844 transaction to a [`PooledTransaction`] with the given sidecar.
/// ///
/// Returns an `Err` containing the original `TransactionSigned` if the transaction is not /// Returns an `Err` containing the original `TransactionSigned` if the transaction is not
@ -367,6 +398,12 @@ impl TransactionSigned {
_ => None, _ => None,
} }
} }
/// Provides mutable access to the transaction.
#[cfg(feature = "test-utils")]
pub fn transaction_mut(&mut self) -> &mut Transaction {
&mut self.transaction
}
} }
impl Typed2718 for TransactionSigned { impl Typed2718 for TransactionSigned {
@ -534,13 +571,13 @@ impl Decodable2718 for TransactionSigned {
} }
impl Encodable for TransactionSigned { impl Encodable for TransactionSigned {
fn length(&self) -> usize {
self.network_len()
}
fn encode(&self, out: &mut dyn alloy_rlp::BufMut) { fn encode(&self, out: &mut dyn alloy_rlp::BufMut) {
self.network_encode(out); self.network_encode(out);
} }
fn length(&self) -> usize {
self.network_len()
}
} }
impl Decodable for TransactionSigned { impl Decodable for TransactionSigned {

View File

@ -99,6 +99,7 @@ test-utils = [
"reth-primitives-traits/test-utils", "reth-primitives-traits/test-utils",
"arbitrary", "arbitrary",
"reth-codecs/test-utils", "reth-codecs/test-utils",
"reth-ethereum-primitives/test-utils",
] ]
serde-bincode-compat = [ serde-bincode-compat = [
"alloy-eips/serde-bincode-compat", "alloy-eips/serde-bincode-compat",

View File

@ -45,9 +45,9 @@ where
) -> Result<Self::Transaction, Self::Error> { ) -> Result<Self::Transaction, Self::Error> {
let from = tx.signer(); let from = tx.signer();
let hash = *tx.tx_hash(); let hash = *tx.tx_hash();
let TransactionSigned { transaction, signature, .. } = tx.into_tx(); let signature = *tx.signature();
let inner: TxEnvelope = match transaction { let inner: TxEnvelope = match tx.into_tx().into_transaction() {
reth_primitives::Transaction::Legacy(tx) => { reth_primitives::Transaction::Legacy(tx) => {
Signed::new_unchecked(tx, signature, hash).into() Signed::new_unchecked(tx, signature, hash).into()
} }
@ -63,8 +63,6 @@ where
reth_primitives::Transaction::Eip7702(tx) => { reth_primitives::Transaction::Eip7702(tx) => {
Signed::new_unchecked(tx, signature, hash).into() Signed::new_unchecked(tx, signature, hash).into()
} }
#[allow(unreachable_patterns)]
_ => unreachable!(),
}; };
let TransactionInfo { let TransactionInfo {

View File

@ -672,9 +672,6 @@ mod tests {
for tx_id in body.tx_num_range() { for tx_id in body.tx_num_range() {
let transaction: TransactionSigned = provider let transaction: TransactionSigned = provider
.transaction_by_id_unhashed(tx_id)? .transaction_by_id_unhashed(tx_id)?
.map(|tx| {
TransactionSigned::new_unhashed(tx.transaction, tx.signature)
})
.expect("no transaction entry"); .expect("no transaction entry");
let signer = let signer =
transaction.recover_signer().expect("failed to recover signer"); transaction.recover_signer().expect("failed to recover signer");

View File

@ -330,7 +330,7 @@ mod tests {
writer.append_receipt(*next_tx_num, &receipt).unwrap(); writer.append_receipt(*next_tx_num, &receipt).unwrap();
} else { } else {
// Used as ID for validation // Used as ID for validation
tx.transaction.set_nonce(*next_tx_num); tx.transaction_mut().set_nonce(*next_tx_num);
writer.append_transaction(*next_tx_num, &tx).unwrap(); writer.append_transaction(*next_tx_num, &tx).unwrap();
} }
*next_tx_num += 1; *next_tx_num += 1;

View File

@ -913,7 +913,7 @@ impl TryFrom<Recovered<TransactionSigned>> for MockTransaction {
let size = transaction.size(); let size = transaction.size();
#[allow(unreachable_patterns)] #[allow(unreachable_patterns)]
match transaction.transaction { match transaction.into_transaction() {
Transaction::Legacy(TxLegacy { Transaction::Legacy(TxLegacy {
chain_id, chain_id,
nonce, nonce,

View File

@ -1326,7 +1326,7 @@ impl PoolTransaction for EthPooledTransaction {
/// ///
/// This is also commonly referred to as the "Gas Fee Cap" (`GasFeeCap`). /// This is also commonly referred to as the "Gas Fee Cap" (`GasFeeCap`).
fn max_fee_per_gas(&self) -> u128 { fn max_fee_per_gas(&self) -> u128 {
self.transaction.transaction.max_fee_per_gas() self.transaction.transaction().max_fee_per_gas()
} }
fn access_list(&self) -> Option<&AccessList> { fn access_list(&self) -> Option<&AccessList> {
@ -1337,7 +1337,7 @@ impl PoolTransaction for EthPooledTransaction {
/// ///
/// This will return `None` for non-EIP1559 transactions /// This will return `None` for non-EIP1559 transactions
fn max_priority_fee_per_gas(&self) -> Option<u128> { fn max_priority_fee_per_gas(&self) -> Option<u128> {
self.transaction.transaction.max_priority_fee_per_gas() self.transaction.transaction().max_priority_fee_per_gas()
} }
fn max_fee_per_blob_gas(&self) -> Option<u128> { fn max_fee_per_blob_gas(&self) -> Option<u128> {
@ -1375,7 +1375,7 @@ impl PoolTransaction for EthPooledTransaction {
/// Returns a measurement of the heap usage of this type and all its internals. /// Returns a measurement of the heap usage of this type and all its internals.
fn size(&self) -> usize { fn size(&self) -> usize {
self.transaction.transaction.input().len() self.transaction.transaction().input().len()
} }
/// Returns the transaction type /// Returns the transaction type
@ -1404,7 +1404,7 @@ impl EthPoolTransaction for EthPooledTransaction {
} }
fn blob_count(&self) -> usize { fn blob_count(&self) -> usize {
match &self.transaction.transaction { match self.transaction.transaction() {
Transaction::Eip4844(tx) => tx.blob_versioned_hashes.len(), Transaction::Eip4844(tx) => tx.blob_versioned_hashes.len(),
_ => 0, _ => 0,
} }
@ -1437,14 +1437,14 @@ impl EthPoolTransaction for EthPooledTransaction {
sidecar: &BlobTransactionSidecar, sidecar: &BlobTransactionSidecar,
settings: &KzgSettings, settings: &KzgSettings,
) -> Result<(), BlobTransactionValidationError> { ) -> Result<(), BlobTransactionValidationError> {
match &self.transaction.transaction { match self.transaction.transaction() {
Transaction::Eip4844(tx) => tx.validate_blob(sidecar, settings), Transaction::Eip4844(tx) => tx.validate_blob(sidecar, settings),
_ => Err(BlobTransactionValidationError::NotBlobTransaction(self.tx_type())), _ => Err(BlobTransactionValidationError::NotBlobTransaction(self.tx_type())),
} }
} }
fn authorization_count(&self) -> usize { fn authorization_count(&self) -> usize {
match &self.transaction.transaction { match self.transaction.transaction() {
Transaction::Eip7702(tx) => tx.authorization_list.len(), Transaction::Eip7702(tx) => tx.authorization_list.len(),
_ => 0, _ => 0,
} }

View File

@ -196,7 +196,7 @@ pub fn random_block<R: Rng>(rng: &mut R, number: u64, block_params: BlockParams)
let tx_count = block_params.tx_count.unwrap_or_else(|| rng.gen::<u8>()); let tx_count = block_params.tx_count.unwrap_or_else(|| rng.gen::<u8>());
let transactions: Vec<TransactionSigned> = let transactions: Vec<TransactionSigned> =
(0..tx_count).map(|_| random_signed_tx(rng)).collect(); (0..tx_count).map(|_| random_signed_tx(rng)).collect();
let total_gas = transactions.iter().fold(0, |sum, tx| sum + tx.transaction.gas_limit()); let total_gas = transactions.iter().fold(0, |sum, tx| sum + tx.transaction().gas_limit());
// Generate ommers // Generate ommers
let ommers_count = block_params.ommers_count.unwrap_or_else(|| rng.gen_range(0..2)); let ommers_count = block_params.ommers_count.unwrap_or_else(|| rng.gen_range(0..2));