Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(consensus-tx): enable fast is_create #1683

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions crates/consensus/src/transaction/eip1559.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,11 @@ impl Transaction for TxEip1559 {
self.to
}

#[inline]
fn is_create(&self) -> bool {
self.to.is_create()
}

#[inline]
fn value(&self) -> U256 {
self.value
Expand Down
5 changes: 5 additions & 0 deletions crates/consensus/src/transaction/eip2930.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,11 @@ impl Transaction for TxEip2930 {
self.to
}

#[inline]
fn is_create(&self) -> bool {
self.to.is_create()
}

#[inline]
fn value(&self) -> U256 {
self.value
Expand Down
15 changes: 15 additions & 0 deletions crates/consensus/src/transaction/eip4844.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,11 @@ impl Transaction for TxEip4844Variant {
.into()
}

#[inline]
fn is_create(&self) -> bool {
false
}

#[inline]
fn value(&self) -> U256 {
match self {
Expand Down Expand Up @@ -656,6 +661,11 @@ impl Transaction for TxEip4844 {
self.to.into()
}

#[inline]
fn is_create(&self) -> bool {
false
}

#[inline]
fn value(&self) -> U256 {
self.value
Expand Down Expand Up @@ -870,6 +880,11 @@ impl Transaction for TxEip4844WithSidecar {
self.tx.kind()
}

#[inline]
fn is_create(&self) -> bool {
false
}

#[inline]
fn value(&self) -> U256 {
self.tx.value()
Expand Down
5 changes: 5 additions & 0 deletions crates/consensus/src/transaction/eip7702.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,11 @@ impl Transaction for TxEip7702 {
self.to.into()
}

#[inline]
fn is_create(&self) -> bool {
false
}

#[inline]
fn value(&self) -> U256 {
self.value
Expand Down
11 changes: 11 additions & 0 deletions crates/consensus/src/transaction/envelope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,17 @@ impl Transaction for TxEnvelope {
}
}

#[inline]
fn is_create(&self) -> bool {
match self {
Self::Legacy(tx) => tx.tx().is_create(),
Self::Eip2930(tx) => tx.tx().is_create(),
Self::Eip1559(tx) => tx.tx().is_create(),
Self::Eip4844(tx) => tx.tx().is_create(),
Self::Eip7702(tx) => tx.tx().is_create(),
}
}

#[inline]
fn value(&self) -> U256 {
match self {
Expand Down
5 changes: 5 additions & 0 deletions crates/consensus/src/transaction/legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,11 @@ impl Transaction for TxLegacy {
self.to
}

#[inline]
fn is_create(&self) -> bool {
self.to.is_create()
}

#[inline]
fn value(&self) -> U256 {
self.value
Expand Down
10 changes: 10 additions & 0 deletions crates/consensus/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ pub trait Transaction: fmt::Debug + any::Any + Send + Sync + 'static {
/// Returns the transaction kind.
fn kind(&self) -> TxKind;

/// Returns true if the transaction is a contract creation.
/// We don't provide a default implementation via `kind` as it copies the 21-byte
/// [`TxKind`] for this simple check. A proper implementation shouldn't allocate.
fn is_create(&self) -> bool;

/// Get the transaction's address of the contract that will be called, or the address that will
/// receive the transfer.
///
Expand Down Expand Up @@ -284,6 +289,11 @@ impl<T: Transaction> Transaction for alloy_serde::WithOtherFields<T> {
self.inner.kind()
}

#[inline]
fn is_create(&self) -> bool {
self.inner.is_create()
}

#[inline]
fn value(&self) -> U256 {
self.inner.value()
Expand Down
11 changes: 11 additions & 0 deletions crates/consensus/src/transaction/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,17 @@ impl Transaction for TypedTransaction {
}
}

#[inline]
fn is_create(&self) -> bool {
match self {
Self::Legacy(tx) => tx.is_create(),
Self::Eip2930(tx) => tx.is_create(),
Self::Eip1559(tx) => tx.is_create(),
Self::Eip4844(tx) => tx.is_create(),
Self::Eip7702(tx) => tx.is_create(),
}
}

#[inline]
fn value(&self) -> U256 {
match self {
Expand Down
16 changes: 16 additions & 0 deletions crates/network/src/any/either.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,14 @@ impl TransactionTrait for AnyTypedTransaction {
}
}

#[inline]
fn is_create(&self) -> bool {
match self {
Self::Ethereum(inner) => inner.is_create(),
Self::Unknown(inner) => inner.is_create(),
}
}

#[inline]
fn value(&self) -> U256 {
match self {
Expand Down Expand Up @@ -332,6 +340,14 @@ impl TransactionTrait for AnyTxEnvelope {
}
}

#[inline]
fn is_create(&self) -> bool {
match self {
Self::Ethereum(inner) => inner.is_create(),
Self::Unknown(inner) => inner.is_create(),
}
}

#[inline]
fn value(&self) -> U256 {
match self {
Expand Down
10 changes: 10 additions & 0 deletions crates/network/src/any/unknowns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,11 @@ impl alloy_consensus::Transaction for UnknownTypedTransaction {
.unwrap_or_default()
}

#[inline]
fn is_create(&self) -> bool {
self.fields.get("to").map_or(true, |v| v.is_null())
}

#[inline]
fn value(&self) -> U256 {
self.fields.get_deserialized("value").and_then(Result::ok).unwrap_or_default()
Expand Down Expand Up @@ -328,6 +333,11 @@ impl alloy_consensus::Transaction for UnknownTxEnvelope {
self.inner.kind()
}

#[inline]
fn is_create(&self) -> bool {
self.inner.is_create()
}

#[inline]
fn value(&self) -> U256 {
self.inner.value()
Expand Down
4 changes: 4 additions & 0 deletions crates/rpc-types-eth/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@ impl<T: TransactionTrait> TransactionTrait for Transaction<T> {
self.inner.kind()
}

fn is_create(&self) -> bool {
self.inner.is_create()
}

fn value(&self) -> U256 {
self.inner.value()
}
Expand Down