Skip to content

Commit

Permalink
chore: rm pooledtx element type (paradigmxyz#13286)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored and emhane committed Dec 13, 2024
1 parent 4b5c01e commit 4933f42
Show file tree
Hide file tree
Showing 13 changed files with 214 additions and 993 deletions.
16 changes: 6 additions & 10 deletions bin/reth/src/commands/debug_cmd/build_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ use reth_fs_util as fs;
use reth_node_api::{BlockTy, EngineApiMessageVersion, PayloadBuilderAttributes};
use reth_node_ethereum::{EthEvmConfig, EthExecutorProvider};
use reth_primitives::{
BlobTransaction, BlockExt, PooledTransactionsElement, SealedBlockFor, SealedBlockWithSenders,
SealedHeader, Transaction, TransactionSigned,
BlockExt, SealedBlockFor, SealedBlockWithSenders, SealedHeader, Transaction, TransactionSigned,
};
use reth_provider::{
providers::{BlockchainProvider, ProviderNodeTypes},
Expand Down Expand Up @@ -190,14 +189,11 @@ impl<C: ChainSpecParser<ChainSpec = ChainSpec>> Command<C> {
let sidecar: BlobTransactionSidecar =
blobs_bundle.pop_sidecar(blob_versioned_hashes.len());

// first construct the tx, calculating the length of the tx with sidecar before
// insertion
let tx = BlobTransaction::try_from_signed(
transaction.as_ref().clone(),
sidecar.clone(),
)
.expect("should not fail to convert blob tx if it is already eip4844");
let pooled = PooledTransactionsElement::BlobTransaction(tx);
let pooled = transaction
.clone()
.into_signed()
.try_into_pooled_eip4844(sidecar.clone())
.expect("should not fail to convert blob tx if it is already eip4844");
let encoded_length = pooled.encode_2718_len();

// insert the blob into the store
Expand Down
7 changes: 7 additions & 0 deletions crates/primitives-traits/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ proptest-arbitrary-interop = { workspace = true, optional = true }
alloy-primitives = { workspace = true, features = ["arbitrary"] }
alloy-consensus = { workspace = true, features = ["arbitrary"] }

secp256k1 = { workspace = true, features = [
"recovery",
"global-context",
"rand"
] }
bincode.workspace = true
proptest-arbitrary-interop.workspace = true
proptest.workspace = true
Expand Down Expand Up @@ -92,6 +97,8 @@ arbitrary = [
"alloy-eips/arbitrary",
"revm-primitives/arbitrary",
"reth-codecs?/arbitrary",
"secp256k1?/global-context",
"secp256k1?/rand",
"op-alloy-consensus?/arbitrary"
]
serde-bincode-compat = [
Expand Down
27 changes: 25 additions & 2 deletions crates/primitives-traits/src/size.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use alloy_consensus::{Header, TxEip1559, TxEip2930, TxEip4844, TxEip7702, TxLegacy, TxType};
use alloy_consensus::{
transaction::PooledTransaction, Header, TxEip1559, TxEip2930, TxEip4844, TxEip4844WithSidecar,
TxEip7702, TxLegacy, TxType,
};
use alloy_primitives::{PrimitiveSignature as Signature, TxHash};
use revm_primitives::Log;

Expand Down Expand Up @@ -45,7 +48,15 @@ macro_rules! impl_in_mem_size {
};
}

impl_in_mem_size!(Header, TxLegacy, TxEip2930, TxEip1559, TxEip7702, TxEip4844);
impl_in_mem_size!(
Header,
TxLegacy,
TxEip2930,
TxEip1559,
TxEip7702,
TxEip4844,
TxEip4844WithSidecar
);

#[cfg(feature = "op")]
impl_in_mem_size_size_of!(op_alloy_consensus::OpTxType);
Expand All @@ -59,6 +70,18 @@ impl InMemorySize for alloy_consensus::Receipt {
}
}

impl InMemorySize for PooledTransaction {
fn size(&self) -> usize {
match self {
Self::Legacy(tx) => tx.size(),
Self::Eip2930(tx) => tx.size(),
Self::Eip1559(tx) => tx.size(),
Self::Eip4844(tx) => tx.size(),
Self::Eip7702(tx) => tx.size(),
}
}
}

#[cfg(feature = "op")]
impl InMemorySize for op_alloy_consensus::OpDepositReceipt {
fn size(&self) -> usize {
Expand Down
49 changes: 46 additions & 3 deletions crates/primitives-traits/src/transaction/signed.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
//! API of a signed transaction.
use crate::{FillTxEnv, InMemorySize, MaybeCompact, MaybeSerde};
use crate::{
crypto::secp256k1::{recover_signer, recover_signer_unchecked},
FillTxEnv, InMemorySize, MaybeCompact, MaybeSerde,
};
use alloc::{fmt, vec::Vec};
use alloy_consensus::{transaction::PooledTransaction, SignableTransaction};
use alloy_eips::eip2718::{Decodable2718, Encodable2718};
use alloy_primitives::{keccak256, Address, PrimitiveSignature, TxHash, B256};
use alloy_primitives::{keccak256, Address, PrimitiveSignature as Signature, TxHash, B256};
use core::hash::Hash;

/// Helper trait that unifies all behaviour required by block to support full node operations.
Expand Down Expand Up @@ -34,7 +38,7 @@ pub trait SignedTransaction:
fn tx_hash(&self) -> &TxHash;

/// Returns reference to signature.
fn signature(&self) -> &PrimitiveSignature;
fn signature(&self) -> &Signature;

/// Returns whether this transaction type can be __broadcasted__ as full transaction over the
/// network.
Expand Down Expand Up @@ -76,3 +80,42 @@ pub trait SignedTransaction:
keccak256(self.encoded_2718())
}
}

impl SignedTransaction for PooledTransaction {
fn tx_hash(&self) -> &TxHash {
match self {
Self::Legacy(tx) => tx.hash(),
Self::Eip2930(tx) => tx.hash(),
Self::Eip1559(tx) => tx.hash(),
Self::Eip7702(tx) => tx.hash(),
Self::Eip4844(tx) => tx.hash(),
}
}

fn signature(&self) -> &Signature {
match self {
Self::Legacy(tx) => tx.signature(),
Self::Eip2930(tx) => tx.signature(),
Self::Eip1559(tx) => tx.signature(),
Self::Eip7702(tx) => tx.signature(),
Self::Eip4844(tx) => tx.signature(),
}
}

fn recover_signer(&self) -> Option<Address> {
let signature_hash = self.signature_hash();
recover_signer(self.signature(), signature_hash)
}

fn recover_signer_unchecked_with_buf(&self, buf: &mut Vec<u8>) -> Option<Address> {
match self {
Self::Legacy(tx) => tx.tx().encode_for_signing(buf),
Self::Eip2930(tx) => tx.tx().encode_for_signing(buf),
Self::Eip1559(tx) => tx.tx().encode_for_signing(buf),
Self::Eip7702(tx) => tx.tx().encode_for_signing(buf),
Self::Eip4844(tx) => tx.tx().encode_for_signing(buf),
}
let signature_hash = keccak256(buf);
recover_signer_unchecked(self.signature(), signature_hash)
}
}
6 changes: 3 additions & 3 deletions crates/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ pub use static_file::StaticFileSegment;

pub use transaction::{
util::secp256k1::{public_key_to_address, recover_signer_unchecked, sign_message},
BlobTransaction, InvalidTransactionError, PooledTransactionsElement,
PooledTransactionsElementEcRecovered, RecoveredTx, Transaction, TransactionMeta,
TransactionSigned, TransactionSignedEcRecovered, TxType,
InvalidTransactionError, PooledTransactionsElement, PooledTransactionsElementEcRecovered,
RecoveredTx, Transaction, TransactionMeta, TransactionSigned, TransactionSignedEcRecovered,
TxType,
};

pub use alloy_consensus::ReceiptWithBloom;
Expand Down
80 changes: 66 additions & 14 deletions crates/primitives/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,45 @@
use alloc::vec::Vec;
use alloy_consensus::{
transaction::RlpEcdsaTx, SignableTransaction, Signed, Transaction as _, TxEip1559, TxEip2930,
TxEip4844, TxEip4844Variant, TxEip7702, TxLegacy, Typed2718, TypedTransaction,
transaction::{PooledTransaction, RlpEcdsaTx},
SignableTransaction, Signed, Transaction as _, TxEip1559, TxEip2930, TxEip4844,
TxEip4844Variant, TxEip4844WithSidecar, TxEip7702, TxLegacy, Typed2718, TypedTransaction,
};
use alloy_eips::{
eip2718::{Decodable2718, Eip2718Error, Eip2718Result, Encodable2718},
eip2930::AccessList,
eip4844::BlobTransactionSidecar,
eip7702::SignedAuthorization,
};
use alloy_primitives::{
keccak256, Address, Bytes, ChainId, PrimitiveSignature as Signature, TxHash, TxKind, B256, U256,
};
use alloy_rlp::{Decodable, Encodable, Error as RlpError, Header};
pub use compat::FillTxEnv;
use core::hash::{Hash, Hasher};
use derive_more::{AsRef, Deref};
pub use meta::TransactionMeta;
use once_cell as _;
#[cfg(not(feature = "std"))]
use once_cell::sync::{Lazy as LazyLock, OnceCell as OnceLock};
#[cfg(feature = "optimism")]
use op_alloy_consensus::DepositTransaction;
#[cfg(feature = "optimism")]
use op_alloy_consensus::TxDeposit;
use rayon::prelude::{IntoParallelIterator, ParallelIterator};
use reth_primitives_traits::{InMemorySize, SignedTransaction};
use revm_primitives::{AuthorizationList, TxEnv};
use serde::{Deserialize, Serialize};
#[cfg(feature = "std")]
use std::sync::{LazyLock, OnceLock};

pub use compat::FillTxEnv;
pub use meta::TransactionMeta;
pub use pooled::{PooledTransactionsElement, PooledTransactionsElementEcRecovered};
use rayon::prelude::{IntoParallelIterator, ParallelIterator};
pub use reth_primitives_traits::{
transaction::error::{
InvalidTransactionError, TransactionConversionError, TryFromRecoveredTransactionError,
},
WithEncoded,
};
pub use sidecar::BlobTransaction;
use reth_primitives_traits::{InMemorySize, SignedTransaction};
use revm_primitives::{AuthorizationList, TxEnv};
use serde::{Deserialize, Serialize};
pub use signature::{recover_signer, recover_signer_unchecked};
#[cfg(feature = "std")]
use std::sync::{LazyLock, OnceLock};
pub use tx_type::TxType;

/// Handling transaction signature operations, including signature recovery,
Expand All @@ -52,7 +52,6 @@ pub(crate) mod access_list;
mod compat;
mod meta;
mod pooled;
mod sidecar;
mod tx_type;

#[cfg(any(test, feature = "reth-codec"))]
Expand Down Expand Up @@ -857,7 +856,7 @@ impl TransactionSigned {
/// Tries to convert a [`TransactionSigned`] into a [`PooledTransactionsElement`].
///
/// This function used as a helper to convert from a decoded p2p broadcast message to
/// [`PooledTransactionsElement`]. Since [`BlobTransaction`] is disallowed to be broadcasted on
/// [`PooledTransactionsElement`]. Since EIP4844 variants are disallowed to be broadcasted on
/// p2p, return an err if `tx` is [`Transaction::Eip4844`].
pub fn try_into_pooled(self) -> Result<PooledTransactionsElement, Self> {
let hash = self.hash();
Expand All @@ -882,6 +881,32 @@ impl TransactionSigned {
}
}

/// Converts from an EIP-4844 [`RecoveredTx`] to a
/// [`PooledTransactionsElementEcRecovered`] with the given sidecar.
///
/// Returns an `Err` containing the original `TransactionSigned` if the transaction is not
/// EIP-4844.
pub fn try_into_pooled_eip4844(
self,
sidecar: BlobTransactionSidecar,
) -> Result<PooledTransactionsElement, Self> {
let hash = self.hash();
Ok(match self {
// If the transaction is an EIP-4844 transaction...
Self { transaction: Transaction::Eip4844(tx), signature, .. } => {
// Construct a pooled eip488 tx with the provided sidecar.
PooledTransactionsElement::Eip4844(Signed::new_unchecked(
TxEip4844WithSidecar { tx, sidecar },
signature,
hash,
))
}
// If the transaction is not EIP-4844, return an error with the original
// transaction.
_ => return Err(self),
})
}

/// Transaction hash. Used to identify transaction.
pub fn hash(&self) -> TxHash {
*self.tx_hash()
Expand Down Expand Up @@ -1165,6 +1190,26 @@ impl From<RecoveredTx> for TransactionSigned {
}
}

impl TryFrom<TransactionSigned> for PooledTransaction {
type Error = TransactionConversionError;

fn try_from(tx: TransactionSigned) -> Result<Self, Self::Error> {
tx.try_into_pooled().map_err(|_| TransactionConversionError::UnsupportedForP2P)
}
}

impl From<PooledTransaction> for TransactionSigned {
fn from(tx: PooledTransaction) -> Self {
match tx {
PooledTransaction::Legacy(signed) => signed.into(),
PooledTransaction::Eip2930(signed) => signed.into(),
PooledTransaction::Eip1559(signed) => signed.into(),
PooledTransaction::Eip4844(signed) => signed.into(),
PooledTransaction::Eip7702(signed) => signed.into(),
}
}
}

impl Encodable for TransactionSigned {
/// This encodes the transaction _with_ the signature, and an rlp header.
///
Expand Down Expand Up @@ -1406,6 +1451,13 @@ impl From<Signed<Transaction>> for TransactionSigned {
}
}

impl From<Signed<TxEip4844WithSidecar>> for TransactionSigned {
fn from(value: Signed<TxEip4844WithSidecar>) -> Self {
let (tx, sig, hash) = value.into_parts();
Self::new(tx.tx.into(), sig, hash)
}
}

impl From<TransactionSigned> for Signed<Transaction> {
fn from(value: TransactionSigned) -> Self {
let (tx, sig, hash) = value.into_parts();
Expand Down
Loading

0 comments on commit 4933f42

Please sign in to comment.