Skip to content

Commit

Permalink
chore: move txtype-specific builders to network-primitives (#1602)
Browse files Browse the repository at this point in the history
* chore: move txtype-specific builders to network-primitives

* fmt

* add file

* use alloc::Vec
  • Loading branch information
klkvr authored Nov 1, 2024
1 parent 0c09f4c commit ab78ea0
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 111 deletions.
3 changes: 3 additions & 0 deletions crates/network-primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ pub use traits::{BlockResponse, ReceiptResponse, TransactionResponse};

mod block;
pub use block::{BlockTransactionHashes, BlockTransactions, BlockTransactionsKind};

mod tx_builders;
pub use tx_builders::{TransactionBuilder4844, TransactionBuilder7702};
85 changes: 85 additions & 0 deletions crates/network-primitives/src/tx_builders.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
use core::ops::{Deref, DerefMut};

use alloc::vec::Vec;
use alloy_consensus::BlobTransactionSidecar;
use alloy_eips::eip7702::SignedAuthorization;
use alloy_serde::WithOtherFields;

/// Transaction builder type supporting EIP-4844 transaction fields.
pub trait TransactionBuilder4844: Default + Sized + Send + Sync + 'static {
/// Get the max fee per blob gas for the transaction.
fn max_fee_per_blob_gas(&self) -> Option<u128>;

/// Set the max fee per blob gas for the transaction.
fn set_max_fee_per_blob_gas(&mut self, max_fee_per_blob_gas: u128);

/// Builder-pattern method for setting max fee per blob gas .
fn with_max_fee_per_blob_gas(mut self, max_fee_per_blob_gas: u128) -> Self {
self.set_max_fee_per_blob_gas(max_fee_per_blob_gas);
self
}

/// Gets the EIP-4844 blob sidecar of the transaction.
fn blob_sidecar(&self) -> Option<&BlobTransactionSidecar>;

/// Sets the EIP-4844 blob sidecar of the transaction.
///
/// Note: This will also set the versioned blob hashes accordingly:
/// [BlobTransactionSidecar::versioned_hashes]
fn set_blob_sidecar(&mut self, sidecar: BlobTransactionSidecar);

/// Builder-pattern method for setting the EIP-4844 blob sidecar of the transaction.
fn with_blob_sidecar(mut self, sidecar: BlobTransactionSidecar) -> Self {
self.set_blob_sidecar(sidecar);
self
}
}

/// Transaction builder type supporting EIP-7702 transaction fields.
pub trait TransactionBuilder7702: Default + Sized + Send + Sync + 'static {
/// Get the EIP-7702 authorization list for the transaction.
fn authorization_list(&self) -> Option<&Vec<SignedAuthorization>>;

/// Sets the EIP-7702 authorization list.
fn set_authorization_list(&mut self, authorization_list: Vec<SignedAuthorization>);

/// Builder-pattern method for setting the authorization list.
fn with_authorization_list(mut self, authorization_list: Vec<SignedAuthorization>) -> Self {
self.set_authorization_list(authorization_list);
self
}
}

impl<T> TransactionBuilder4844 for WithOtherFields<T>
where
T: TransactionBuilder4844,
{
fn max_fee_per_blob_gas(&self) -> Option<u128> {
self.deref().max_fee_per_blob_gas()
}

fn set_max_fee_per_blob_gas(&mut self, max_fee_per_blob_gas: u128) {
self.deref_mut().set_max_fee_per_blob_gas(max_fee_per_blob_gas)
}

fn blob_sidecar(&self) -> Option<&BlobTransactionSidecar> {
self.deref().blob_sidecar()
}

fn set_blob_sidecar(&mut self, sidecar: BlobTransactionSidecar) {
self.deref_mut().set_blob_sidecar(sidecar)
}
}

impl<T> TransactionBuilder7702 for WithOtherFields<T>
where
T: TransactionBuilder7702,
{
fn authorization_list(&self) -> Option<&Vec<SignedAuthorization>> {
self.deref().authorization_list()
}

fn set_authorization_list(&mut self, authorization_list: Vec<SignedAuthorization>) {
self.deref_mut().set_authorization_list(authorization_list)
}
}
32 changes: 1 addition & 31 deletions crates/network/src/any/builder.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use crate::{
any::AnyNetwork, BuildResult, Network, NetworkWallet, TransactionBuilder,
TransactionBuilder4844, TransactionBuilder7702, TransactionBuilderError,
TransactionBuilderError,
};
use alloy_consensus::BlobTransactionSidecar;
use alloy_eips::eip7702::SignedAuthorization;
use alloy_primitives::{Address, Bytes, ChainId, TxKind, U256};
use alloy_rpc_types_eth::{AccessList, TransactionRequest};
use alloy_serde::WithOtherFields;
Expand Down Expand Up @@ -148,31 +146,3 @@ impl TransactionBuilder<AnyNetwork> for WithOtherFields<TransactionRequest> {
Ok(wallet.sign_request(self).await?)
}
}

impl TransactionBuilder4844 for WithOtherFields<TransactionRequest> {
fn max_fee_per_blob_gas(&self) -> Option<u128> {
self.deref().max_fee_per_blob_gas()
}

fn set_max_fee_per_blob_gas(&mut self, max_fee_per_blob_gas: u128) {
self.deref_mut().set_max_fee_per_blob_gas(max_fee_per_blob_gas)
}

fn blob_sidecar(&self) -> Option<&BlobTransactionSidecar> {
self.deref().blob_sidecar()
}

fn set_blob_sidecar(&mut self, sidecar: BlobTransactionSidecar) {
self.deref_mut().set_blob_sidecar(sidecar)
}
}

impl TransactionBuilder7702 for WithOtherFields<TransactionRequest> {
fn authorization_list(&self) -> Option<&Vec<SignedAuthorization>> {
self.deref().authorization_list()
}

fn set_authorization_list(&mut self, authorization_list: Vec<SignedAuthorization>) {
self.deref_mut().set_authorization_list(authorization_list)
}
}
36 changes: 3 additions & 33 deletions crates/network/src/ethereum/builder.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use crate::{
BuildResult, Ethereum, Network, NetworkWallet, TransactionBuilder, TransactionBuilder4844,
TransactionBuilder7702, TransactionBuilderError,
BuildResult, Ethereum, Network, NetworkWallet, TransactionBuilder, TransactionBuilder7702,
TransactionBuilderError,
};
use alloy_consensus::{BlobTransactionSidecar, TxType, TypedTransaction};
use alloy_eips::eip7702::SignedAuthorization;
use alloy_consensus::{TxType, TypedTransaction};
use alloy_primitives::{Address, Bytes, ChainId, TxKind, U256};
use alloy_rpc_types_eth::{request::TransactionRequest, AccessList};

Expand Down Expand Up @@ -167,35 +166,6 @@ impl TransactionBuilder<Ethereum> for TransactionRequest {
}
}

impl TransactionBuilder4844 for TransactionRequest {
fn max_fee_per_blob_gas(&self) -> Option<u128> {
self.max_fee_per_blob_gas
}

fn set_max_fee_per_blob_gas(&mut self, max_fee_per_blob_gas: u128) {
self.max_fee_per_blob_gas = Some(max_fee_per_blob_gas)
}

fn blob_sidecar(&self) -> Option<&BlobTransactionSidecar> {
self.sidecar.as_ref()
}

fn set_blob_sidecar(&mut self, sidecar: BlobTransactionSidecar) {
self.sidecar = Some(sidecar);
self.populate_blob_hashes();
}
}

impl TransactionBuilder7702 for TransactionRequest {
fn authorization_list(&self) -> Option<&Vec<SignedAuthorization>> {
self.authorization_list.as_ref()
}

fn set_authorization_list(&mut self, authorization_list: Vec<SignedAuthorization>) {
self.authorization_list = Some(authorization_list);
}
}

#[cfg(test)]
mod tests {
use crate::{
Expand Down
49 changes: 2 additions & 47 deletions crates/network/src/transaction/builder.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use super::signer::NetworkWallet;
use crate::Network;
use alloy_consensus::BlobTransactionSidecar;
use alloy_eips::eip7702::SignedAuthorization;
use alloy_primitives::{Address, Bytes, ChainId, TxKind, U256};
use alloy_rpc_types_eth::AccessList;
use alloy_sol_types::SolCall;
use futures_utils_wasm::impl_future;

pub use alloy_network_primitives::{TransactionBuilder4844, TransactionBuilder7702};

/// Result type for transaction builders
pub type BuildResult<T, N> = Result<T, UnbuiltTransactionError<N>>;

Expand Down Expand Up @@ -343,48 +343,3 @@ pub trait TransactionBuilder<N: Network>: Default + Sized + Send + Sync + 'stati
wallet: &W,
) -> impl_future!(<Output = Result<N::TxEnvelope, TransactionBuilderError<N>>>);
}

/// Transaction builder type supporting EIP-4844 transaction fields.
pub trait TransactionBuilder4844: Default + Sized + Send + Sync + 'static {
/// Get the max fee per blob gas for the transaction.
fn max_fee_per_blob_gas(&self) -> Option<u128>;

/// Set the max fee per blob gas for the transaction.
fn set_max_fee_per_blob_gas(&mut self, max_fee_per_blob_gas: u128);

/// Builder-pattern method for setting max fee per blob gas .
fn with_max_fee_per_blob_gas(mut self, max_fee_per_blob_gas: u128) -> Self {
self.set_max_fee_per_blob_gas(max_fee_per_blob_gas);
self
}

/// Gets the EIP-4844 blob sidecar of the transaction.
fn blob_sidecar(&self) -> Option<&BlobTransactionSidecar>;

/// Sets the EIP-4844 blob sidecar of the transaction.
///
/// Note: This will also set the versioned blob hashes accordingly:
/// [BlobTransactionSidecar::versioned_hashes]
fn set_blob_sidecar(&mut self, sidecar: BlobTransactionSidecar);

/// Builder-pattern method for setting the EIP-4844 blob sidecar of the transaction.
fn with_blob_sidecar(mut self, sidecar: BlobTransactionSidecar) -> Self {
self.set_blob_sidecar(sidecar);
self
}
}

/// Transaction builder type supporting EIP-7702 transaction fields.
pub trait TransactionBuilder7702: Default + Sized + Send + Sync + 'static {
/// Get the EIP-7702 authorization list for the transaction.
fn authorization_list(&self) -> Option<&Vec<SignedAuthorization>>;

/// Sets the EIP-7702 authorization list.
fn set_authorization_list(&mut self, authorization_list: Vec<SignedAuthorization>);

/// Builder-pattern method for setting the authorization list.
fn with_authorization_list(mut self, authorization_list: Vec<SignedAuthorization>) -> Self {
self.set_authorization_list(authorization_list);
self
}
}
30 changes: 30 additions & 0 deletions crates/rpc-types-eth/src/transaction/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use alloy_consensus::{
TxEip7702, TxEnvelope, TxLegacy, TxType, TypedTransaction,
};
use alloy_eips::eip7702::SignedAuthorization;
use alloy_network_primitives::{TransactionBuilder4844, TransactionBuilder7702};
use alloy_primitives::{Address, Bytes, ChainId, TxKind, B256, U256};
use core::hash::Hash;

Expand Down Expand Up @@ -657,6 +658,35 @@ impl TransactionRequest {
}
}

impl TransactionBuilder4844 for TransactionRequest {
fn max_fee_per_blob_gas(&self) -> Option<u128> {
self.max_fee_per_blob_gas
}

fn set_max_fee_per_blob_gas(&mut self, max_fee_per_blob_gas: u128) {
self.max_fee_per_blob_gas = Some(max_fee_per_blob_gas)
}

fn blob_sidecar(&self) -> Option<&BlobTransactionSidecar> {
self.sidecar.as_ref()
}

fn set_blob_sidecar(&mut self, sidecar: BlobTransactionSidecar) {
self.sidecar = Some(sidecar);
self.populate_blob_hashes();
}
}

impl TransactionBuilder7702 for TransactionRequest {
fn authorization_list(&self) -> Option<&Vec<SignedAuthorization>> {
self.authorization_list.as_ref()
}

fn set_authorization_list(&mut self, authorization_list: Vec<SignedAuthorization>) {
self.authorization_list = Some(authorization_list);
}
}

/// Helper type that supports both `data` and `input` fields that map to transaction input data.
///
/// This is done for compatibility reasons where older implementations used `data` instead of the
Expand Down

0 comments on commit ab78ea0

Please sign in to comment.