From 7cb0d59db8838711fe7c8c743ea47eee139e94be Mon Sep 17 00:00:00 2001 From: Mykhailo Slyvka Date: Wed, 15 May 2024 18:46:44 +0300 Subject: [PATCH 1/4] update network crate --- README.md | 4 +- crates/consensus/Cargo.toml | 1 + crates/consensus/src/transaction/typed.rs | 3 + crates/contract/src/call.rs | 8 +- crates/network/Cargo.toml | 1 + crates/network/src/any/builder.rs | 65 ++++++++------ crates/network/src/any/mod.rs | 2 +- crates/network/src/ethereum/builder.rs | 86 +++++++++++------- crates/network/src/ethereum/mod.rs | 2 +- crates/network/src/ethereum/signer.rs | 21 +++-- crates/network/src/lib.rs | 4 +- crates/network/src/transaction/builder.rs | 89 ++++++++++++------- crates/network/src/transaction/signer.rs | 14 +-- crates/provider/src/fillers/chain_id.rs | 6 +- crates/provider/src/fillers/gas.rs | 28 +++--- crates/provider/src/fillers/nonce.rs | 4 +- crates/provider/src/fillers/signer.rs | 2 +- crates/provider/src/provider/trait.rs | 2 +- crates/rpc-types/src/eth/transaction/mod.rs | 8 +- .../rpc-types/src/eth/transaction/request.rs | 46 +++++----- 20 files changed, 229 insertions(+), 167 deletions(-) diff --git a/README.md b/README.md index 1c880dd9c..447fa0f91 100644 --- a/README.md +++ b/README.md @@ -6,12 +6,12 @@ Crates used in Core or that are used by other crates - [ ] eips - [x] genesis - in work (Misha) - [ ] json-rpc -- [ ] network - in work (Misha) +- [x] network - in work (Misha) #todo fix tests - [ ] node-bindings - [ ] provider - [ ] pubsub - [ ] rpc-client -- [ ] rpc-types +- [ ] rpc-types - in work (Misha) - [ ] rpc-types-anvil - [ ] rpc-types-engine - [ ] rpc-types-trace diff --git a/crates/consensus/Cargo.toml b/crates/consensus/Cargo.toml index 964d82d6e..ea387b501 100644 --- a/crates/consensus/Cargo.toml +++ b/crates/consensus/Cargo.toml @@ -57,3 +57,4 @@ serde = [ "dep:alloy-serde", "alloy-eips/serde", ] +typed_tx = [] diff --git a/crates/consensus/src/transaction/typed.rs b/crates/consensus/src/transaction/typed.rs index e8aea0cab..993a7fa31 100644 --- a/crates/consensus/src/transaction/typed.rs +++ b/crates/consensus/src/transaction/typed.rs @@ -19,12 +19,15 @@ pub enum TypedTransaction { #[cfg_attr(feature = "serde", serde(rename = "0x00", alias = "0x0"))] Legacy(TxLegacy), /// EIP-2930 transaction + #[cfg(feature = "typed_tx")] #[cfg_attr(feature = "serde", serde(rename = "0x01", alias = "0x1"))] Eip2930(TxEip2930), /// EIP-1559 transaction + #[cfg(feature = "typed_tx")] #[cfg_attr(feature = "serde", serde(rename = "0x02", alias = "0x2"))] Eip1559(TxEip1559), /// EIP-4844 transaction + #[cfg(feature = "typed_tx")] #[cfg_attr(feature = "serde", serde(rename = "0x03", alias = "0x3"))] Eip4844(TxEip4844Variant), } diff --git a/crates/contract/src/call.rs b/crates/contract/src/call.rs index f1dfb2db0..ef6181735 100644 --- a/crates/contract/src/call.rs +++ b/crates/contract/src/call.rs @@ -291,7 +291,7 @@ impl, D: CallDecoder, N: Network> CallBu /// Sets the `chain_id` field in the transaction to the provided value pub fn chain_id(mut self, chain_id: ChainId) -> Self { - self.request.set_chain_id(chain_id); + self.request.set_network_id(chain_id); self } @@ -326,7 +326,7 @@ impl, D: CallDecoder, N: Network> CallBu /// Sets the `gas` field in the transaction to the provided value pub fn gas(mut self, gas: u128) -> Self { - self.request.set_gas_limit(gas); + self.request.set_energy_limit(gas); self } @@ -334,7 +334,7 @@ impl, D: CallDecoder, N: Network> CallBu /// If the internal transaction is an EIP-1559 one, then it sets both /// `max_fee_per_gas` and `max_priority_fee_per_gas` to the same value pub fn gas_price(mut self, gas_price: u128) -> Self { - self.request.set_gas_price(gas_price); + self.request.set_energy_price(gas_price); self } @@ -612,7 +612,7 @@ mod tests { fn change_chain_id() { let call_builder = build_call_builder().chain_id(1337); assert_eq!( - call_builder.request.chain_id.expect("chain_id should be set"), + call_builder.request.network_id.expect("chain_id should be set"), 1337, "chain_id of request should be '1337'" ); diff --git a/crates/network/Cargo.toml b/crates/network/Cargo.toml index ed09faa6d..b0cadb7bc 100644 --- a/crates/network/Cargo.toml +++ b/crates/network/Cargo.toml @@ -30,3 +30,4 @@ tokio.workspace = true [features] k256 = ["alloy-primitives/k256", "alloy-consensus/k256"] +typed_tx = [] diff --git a/crates/network/src/any/builder.rs b/crates/network/src/any/builder.rs index 6607fc581..3a9eae6fd 100644 --- a/crates/network/src/any/builder.rs +++ b/crates/network/src/any/builder.rs @@ -7,12 +7,12 @@ use alloy_rpc_types::{AccessList, TransactionRequest, WithOtherFields}; use crate::{any::AnyNetwork, BuildResult, Network, TransactionBuilder, TransactionBuilderError}; impl TransactionBuilder for WithOtherFields { - fn chain_id(&self) -> Option { - self.deref().chain_id() + fn network_id(&self) -> Option { + self.deref().network_id() } - fn set_chain_id(&mut self, chain_id: alloy_primitives::ChainId) { - self.deref_mut().set_chain_id(chain_id) + fn set_network_id(&mut self, chain_id: alloy_primitives::ChainId) { + self.deref_mut().set_network_id(chain_id) } fn nonce(&self) -> Option { @@ -31,11 +31,11 @@ impl TransactionBuilder for WithOtherFields { self.deref_mut().set_input(input); } - fn from(&self) -> Option { + fn from(&self) -> Option { self.deref().from() } - fn set_from(&mut self, from: alloy_primitives::Address) { + fn set_from(&mut self, from: alloy_primitives::IcanAddress) { self.deref_mut().set_from(from); } @@ -59,84 +59,97 @@ impl TransactionBuilder for WithOtherFields { self.deref_mut().set_value(value) } - fn gas_price(&self) -> Option { - self.deref().gas_price() + fn energy_price(&self) -> Option { + self.deref().energy_price() } - fn set_gas_price(&mut self, gas_price: u128) { - self.deref_mut().set_gas_price(gas_price); + fn set_energy_price(&mut self, gas_price: u128) { + self.deref_mut().set_energy_price(gas_price); } + fn energy_limit(&self) -> Option { + self.deref().energy_limit() + } + + fn set_energy_limit(&mut self, energy_limit: u128) { + self.deref_mut().set_energy_limit(energy_limit); + } + + #[cfg(feature = "typed_tx")] fn max_fee_per_gas(&self) -> Option { self.deref().max_fee_per_gas() } + #[cfg(feature = "typed_tx")] fn set_max_fee_per_gas(&mut self, max_fee_per_gas: u128) { self.deref_mut().set_max_fee_per_gas(max_fee_per_gas); } + #[cfg(feature = "typed_tx")] fn max_priority_fee_per_gas(&self) -> Option { self.deref().max_priority_fee_per_gas() } + #[cfg(feature = "typed_tx")] fn set_max_priority_fee_per_gas(&mut self, max_priority_fee_per_gas: u128) { self.deref_mut().set_max_priority_fee_per_gas(max_priority_fee_per_gas); } + #[cfg(feature = "typed_tx")] fn max_fee_per_blob_gas(&self) -> Option { self.deref().max_fee_per_blob_gas() } + #[cfg(feature = "typed_tx")] 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 gas_limit(&self) -> Option { - self.deref().gas_limit() - } - - fn set_gas_limit(&mut self, gas_limit: u128) { - self.deref_mut().set_gas_limit(gas_limit); - } - /// Get the EIP-2930 access list for the transaction. + #[cfg(feature = "typed_tx")] fn access_list(&self) -> Option<&AccessList> { self.deref().access_list() } /// Sets the EIP-2930 access list. + #[cfg(feature = "typed_tx")] fn set_access_list(&mut self, access_list: AccessList) { self.deref_mut().set_access_list(access_list) } + #[cfg(feature = "typed_tx")] fn blob_sidecar(&self) -> Option<&BlobTransactionSidecar> { self.deref().blob_sidecar() } + #[cfg(feature = "typed_tx")] fn set_blob_sidecar(&mut self, sidecar: BlobTransactionSidecar) { self.deref_mut().set_blob_sidecar(sidecar) } + #[cfg(feature = "typed_tx")] fn complete_type(&self, ty: ::TxType) -> Result<(), Vec<&'static str>> { self.deref().complete_type(ty.try_into().map_err(|_| vec!["supported tx type"])?) } - fn can_build(&self) -> bool { - self.deref().can_build() - } - - fn can_submit(&self) -> bool { - self.deref().can_submit() - } - + #[cfg(feature = "typed_tx")] fn output_tx_type(&self) -> ::TxType { self.deref().output_tx_type().into() } + #[cfg(feature = "typed_tx")] fn output_tx_type_checked(&self) -> Option<::TxType> { self.deref().output_tx_type_checked().map(Into::into) } + fn can_build(&self) -> bool { + self.deref().can_build() + } + + fn can_submit(&self) -> bool { + self.deref().can_submit() + } + fn prep_for_submission(&mut self) { self.deref_mut().prep_for_submission() } diff --git a/crates/network/src/any/mod.rs b/crates/network/src/any/mod.rs index e12b3a8cb..6c011a9e7 100644 --- a/crates/network/src/any/mod.rs +++ b/crates/network/src/any/mod.rs @@ -77,7 +77,7 @@ impl Network for AnyNetwork { } impl ReceiptResponse for AnyTransactionReceipt { - fn contract_address(&self) -> Option { + fn contract_address(&self) -> Option { self.contract_address } } diff --git a/crates/network/src/ethereum/builder.rs b/crates/network/src/ethereum/builder.rs index 02c8d2cee..a74e2b054 100644 --- a/crates/network/src/ethereum/builder.rs +++ b/crates/network/src/ethereum/builder.rs @@ -2,16 +2,16 @@ use crate::{ BuildResult, Ethereum, Network, NetworkSigner, TransactionBuilder, TransactionBuilderError, }; use alloy_consensus::{BlobTransactionSidecar, TxType, TypedTransaction}; -use alloy_primitives::{Address, Bytes, ChainId, TxKind, U256}; +use alloy_primitives::{Bytes, ChainId, IcanAddress, TxKind, U256}; use alloy_rpc_types::{request::TransactionRequest, AccessList}; impl TransactionBuilder for TransactionRequest { - fn chain_id(&self) -> Option { - self.chain_id + fn network_id(&self) -> Option { + self.network_id } - fn set_chain_id(&mut self, chain_id: ChainId) { - self.chain_id = Some(chain_id); + fn set_network_id(&mut self, network_id: ChainId) { + self.network_id = Some(network_id); } fn nonce(&self) -> Option { @@ -30,11 +30,11 @@ impl TransactionBuilder for TransactionRequest { self.input.input = Some(input.into()); } - fn from(&self) -> Option
{ + fn from(&self) -> Option { self.from } - fn set_from(&mut self, from: Address) { + fn set_from(&mut self, from: IcanAddress) { self.from = Some(from); } @@ -58,63 +58,74 @@ impl TransactionBuilder for TransactionRequest { self.value = Some(value) } - fn gas_price(&self) -> Option { - self.gas_price + fn energy_price(&self) -> Option { + self.energy_price } - fn set_gas_price(&mut self, gas_price: u128) { - self.gas_price = Some(gas_price); + fn set_energy_price(&mut self, energy_price: u128) { + self.energy_price = Some(energy_price); } + fn energy_limit(&self) -> Option { + self.gas + } + + fn set_energy_limit(&mut self, energy_limit: u128) { + self.gas = Some(energy_limit); + } + + #[cfg(feature = "typed_tx")] fn max_fee_per_gas(&self) -> Option { self.max_fee_per_gas } + #[cfg(feature = "typed_tx")] fn set_max_fee_per_gas(&mut self, max_fee_per_gas: u128) { self.max_fee_per_gas = Some(max_fee_per_gas); } + #[cfg(feature = "typed_tx")] fn max_priority_fee_per_gas(&self) -> Option { self.max_priority_fee_per_gas } + #[cfg(feature = "typed_tx")] fn set_max_priority_fee_per_gas(&mut self, max_priority_fee_per_gas: u128) { self.max_priority_fee_per_gas = Some(max_priority_fee_per_gas); } + #[cfg(feature = "typed_tx")] fn max_fee_per_blob_gas(&self) -> Option { self.max_fee_per_blob_gas } + #[cfg(feature = "typed_tx")] 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 gas_limit(&self) -> Option { - self.gas - } - - fn set_gas_limit(&mut self, gas_limit: u128) { - self.gas = Some(gas_limit); - } - + #[cfg(feature = "typed_tx")] fn access_list(&self) -> Option<&AccessList> { self.access_list.as_ref() } + #[cfg(feature = "typed_tx")] fn set_access_list(&mut self, access_list: AccessList) { self.access_list = Some(access_list); } + #[cfg(feature = "typed_tx")] fn blob_sidecar(&self) -> Option<&BlobTransactionSidecar> { self.sidecar.as_ref() } + #[cfg(feature = "typed_tx")] fn set_blob_sidecar(&mut self, sidecar: BlobTransactionSidecar) { self.sidecar = Some(sidecar); self.populate_blob_hashes(); } + #[cfg(feature = "typed_tx")] fn complete_type(&self, ty: TxType) -> Result<(), Vec<&'static str>> { match ty { TxType::Legacy => self.complete_legacy(), @@ -137,28 +148,37 @@ impl TransactionBuilder for TransactionRequest { // chain_id and from may be none. let common = self.gas.is_some() && self.nonce.is_some(); + let legacy = self.energy_price.is_some(); + + #[cfg(feature = "typed_tx")] + { + let eip2930 = legacy && self.access_list().is_some(); + let eip1559 = self.max_fee_per_gas.is_some() && self.max_priority_fee_per_gas.is_some(); + let eip4844 = eip1559 && self.sidecar.is_some() && self.to.is_some(); + common && (legacy || eip2930 || eip1559 || eip4844) + } - let legacy = self.gas_price.is_some(); - let eip2930 = legacy && self.access_list().is_some(); - - let eip1559 = self.max_fee_per_gas.is_some() && self.max_priority_fee_per_gas.is_some(); - - let eip4844 = eip1559 && self.sidecar.is_some() && self.to.is_some(); - common && (legacy || eip2930 || eip1559 || eip4844) + common && legacy } + #[cfg(feature = "typed_tx")] fn output_tx_type(&self) -> TxType { self.preferred_type() } + #[cfg(feature = "typed_tx")] fn output_tx_type_checked(&self) -> Option { self.buildable_type() } fn prep_for_submission(&mut self) { self.transaction_type = Some(self.preferred_type() as u8); - self.trim_conflicting_keys(); - self.populate_blob_hashes(); + + #[cfg(feature = "typed_tx")] + { + self.trim_conflicting_keys(); + self.populate_blob_hashes(); + } } fn build_unsigned(self) -> BuildResult { @@ -205,7 +225,7 @@ mod tests { fn test_4844_when_sidecar() { let request = TransactionRequest::default() .with_nonce(1) - .with_gas_limit(0) + .with_energy_limit(0) .with_max_fee_per_gas(0) .with_max_priority_fee_per_gas(0) .with_to(Address::ZERO) @@ -225,7 +245,7 @@ mod tests { fn test_2930_when_access_list() { let request = TransactionRequest::default() .with_nonce(1) - .with_gas_limit(0) + .with_energy_limit(0) .with_max_fee_per_gas(0) .with_max_priority_fee_per_gas(0) .with_to(Address::ZERO) @@ -241,7 +261,7 @@ mod tests { fn test_default_to_1559() { let request = TransactionRequest::default() .with_nonce(1) - .with_gas_limit(0) + .with_energy_limit(0) .with_max_fee_per_gas(0) .with_max_priority_fee_per_gas(0) .with_to(Address::ZERO); @@ -268,7 +288,7 @@ mod tests { #[test] fn test_invalid_legacy_fields() { - let request = TransactionRequest::default().with_gas_price(0); + let request = TransactionRequest::default().with_energy_price(0); let error = request.clone().build_unsigned().unwrap_err(); @@ -306,7 +326,7 @@ mod tests { fn test_invalid_2930_fields() { let request = TransactionRequest::default() .with_access_list(AccessList::default()) - .with_gas_price(Default::default()); + .with_energy_price(Default::default()); let error = request.clone().build_unsigned().unwrap_err(); diff --git a/crates/network/src/ethereum/mod.rs b/crates/network/src/ethereum/mod.rs index e1080c546..754ad1303 100644 --- a/crates/network/src/ethereum/mod.rs +++ b/crates/network/src/ethereum/mod.rs @@ -32,7 +32,7 @@ impl Network for Ethereum { } impl ReceiptResponse for alloy_rpc_types::TransactionReceipt { - fn contract_address(&self) -> Option { + fn contract_address(&self) -> Option { self.contract_address } } diff --git a/crates/network/src/ethereum/signer.rs b/crates/network/src/ethereum/signer.rs index ec6627b3f..8b42f0fd4 100644 --- a/crates/network/src/ethereum/signer.rs +++ b/crates/network/src/ethereum/signer.rs @@ -1,6 +1,6 @@ use crate::{Network, NetworkSigner, TxSigner}; use alloy_consensus::{SignableTransaction, TxEnvelope, TypedTransaction}; -use alloy_primitives::Address; +use alloy_primitives::IcanAddress; use alloy_signer::Signature; use async_trait::async_trait; use std::{collections::BTreeMap, sync::Arc}; @@ -8,8 +8,8 @@ use std::{collections::BTreeMap, sync::Arc}; /// A signer capable of signing any transaction for the Ethereum network. #[derive(Clone, Default)] pub struct EthereumSigner { - default: Address, - secp_signers: BTreeMap + Send + Sync>>, + default: IcanAddress, + secp_signers: BTreeMap + Send + Sync>>, } impl std::fmt::Debug for EthereumSigner { @@ -75,14 +75,14 @@ impl EthereumSigner { /// Get the signer for the given address. pub fn signer_by_address( &self, - address: Address, + address: IcanAddress, ) -> Option + Send + Sync + 'static>> { self.secp_signers.get(&address).cloned() } async fn sign_transaction_inner( &self, - sender: Address, + sender: IcanAddress, tx: &mut dyn SignableTransaction, ) -> alloy_signer::Result { self.signer_by_address(sender) @@ -100,21 +100,21 @@ impl NetworkSigner for EthereumSigner where N: Network, { - fn default_signer_address(&self) -> Address { + fn default_signer_address(&self) -> IcanAddress { self.default } - fn has_signer_for(&self, address: &Address) -> bool { + fn has_signer_for(&self, address: &IcanAddress) -> bool { self.secp_signers.contains_key(address) } - fn signer_addresses(&self) -> impl Iterator { + fn signer_addresses(&self) -> impl Iterator { self.secp_signers.keys().copied() } async fn sign_transaction_from( &self, - sender: Address, + sender: IcanAddress, tx: TypedTransaction, ) -> alloy_signer::Result { match tx { @@ -122,14 +122,17 @@ where let sig = self.sign_transaction_inner(sender, &mut t).await?; Ok(t.into_signed(sig).into()) } + #[cfg(feature = "typed_tx")] TypedTransaction::Eip2930(mut t) => { let sig = self.sign_transaction_inner(sender, &mut t).await?; Ok(t.into_signed(sig).into()) } + #[cfg(feature = "typed_tx")] TypedTransaction::Eip1559(mut t) => { let sig = self.sign_transaction_inner(sender, &mut t).await?; Ok(t.into_signed(sig).into()) } + #[cfg(feature = "typed_tx")] TypedTransaction::Eip4844(mut t) => { let sig = self.sign_transaction_inner(sender, &mut t).await?; Ok(t.into_signed(sig).into()) diff --git a/crates/network/src/lib.rs b/crates/network/src/lib.rs index eda7e8c44..c1821b71d 100644 --- a/crates/network/src/lib.rs +++ b/crates/network/src/lib.rs @@ -18,7 +18,7 @@ use alloy_consensus::TxReceipt; use alloy_eips::eip2718::{Eip2718Envelope, Eip2718Error}; use alloy_json_rpc::RpcObject; -use alloy_primitives::Address; +use alloy_primitives::IcanAddress; use core::fmt::{Debug, Display}; mod transaction; @@ -42,7 +42,7 @@ pub use alloy_eips::eip2718; /// [`TxReceipt`]: alloy_consensus::TxReceipt pub trait ReceiptResponse { /// Address of the created contract, or `None` if the transaction was not a deployment. - fn contract_address(&self) -> Option
; + fn contract_address(&self) -> Option; } /// Captures type info for network-specific RPC requests/responses. diff --git a/crates/network/src/transaction/builder.rs b/crates/network/src/transaction/builder.rs index 63a27eedd..e632860ad 100644 --- a/crates/network/src/transaction/builder.rs +++ b/crates/network/src/transaction/builder.rs @@ -1,7 +1,7 @@ use super::signer::NetworkSigner; use crate::Network; use alloy_consensus::BlobTransactionSidecar; -use alloy_primitives::{Address, Bytes, ChainId, TxKind, U256}; +use alloy_primitives::{Bytes, ChainId, IcanAddress, TxKind, U256}; use alloy_rpc_types::AccessList; use alloy_sol_types::SolCall; use futures_utils_wasm::impl_future; @@ -51,15 +51,15 @@ impl TransactionBuilderError { /// Transaction builders should be able to construct all available transaction types on a given /// network. pub trait TransactionBuilder: Default + Sized + Send + Sync + 'static { - /// Get the chain ID for the transaction. - fn chain_id(&self) -> Option; + /// Get the network ID for the transaction. + fn network_id(&self) -> Option; - /// Set the chain ID for the transaction. - fn set_chain_id(&mut self, chain_id: ChainId); + /// Set the network ID for the transaction. + fn set_network_id(&mut self, chain_id: ChainId); - /// Builder-pattern method for setting the chain ID. - fn with_chain_id(mut self, chain_id: alloy_primitives::ChainId) -> Self { - self.set_chain_id(chain_id); + /// Builder-pattern method for setting the network ID. + fn with_network_id(mut self, network_id: alloy_primitives::ChainId) -> Self { + self.set_network_id(chain_id); self } @@ -88,13 +88,13 @@ pub trait TransactionBuilder: Default + Sized + Send + Sync + 'stati } /// Get the sender for the transaction. - fn from(&self) -> Option
; + fn from(&self) -> Option; /// Set the sender for the transaction. - fn set_from(&mut self, from: Address); + fn set_from(&mut self, from: IcanAddress); /// Builder-pattern method for setting the sender. - fn with_from(mut self, from: Address) -> Self { + fn with_from(mut self, from: IcanAddress) -> Self { self.set_from(from); self } @@ -115,7 +115,7 @@ pub trait TransactionBuilder: Default + Sized + Send + Sync + 'stati } /// Get the recipient for the transaction. - fn to(&self) -> Option
{ + fn to(&self) -> Option { if let Some(TxKind::Call(addr)) = self.kind() { return Some(addr); } @@ -123,12 +123,12 @@ pub trait TransactionBuilder: Default + Sized + Send + Sync + 'stati } /// Set the recipient for the transaction. - fn set_to(&mut self, to: Address) { + fn set_to(&mut self, to: IcanAddress) { self.set_kind(to.into()); } /// Builder-pattern method for setting the recipient. - fn with_to(mut self, to: Address) -> Self { + fn with_to(mut self, to: IcanAddress) -> Self { self.set_to(to); self } @@ -177,7 +177,7 @@ pub trait TransactionBuilder: Default + Sized + Send + Sync + 'stati /// /// Returns `None` if the transaction is not a contract creation (the `to` field is set), or if /// the `from` or `nonce` fields are not set. - fn calculate_create_address(&self) -> Option
{ + fn calculate_create_address(&self) -> Option { if !self.kind().is_some_and(|to| to.is_create()) { return None; } @@ -198,103 +198,121 @@ pub trait TransactionBuilder: Default + Sized + Send + Sync + 'stati self } - /// Get the legacy gas price for the transaction. - fn gas_price(&self) -> Option; + /// Get the legacy energy price for the transaction. + fn energy_price(&self) -> Option; - /// Set the legacy gas price for the transaction. - fn set_gas_price(&mut self, gas_price: u128); + /// Set the legacy energy price for the transaction. + fn set_energy_price(&mut self, energy_price: u128); - /// Builder-pattern method for setting the legacy gas price. - fn with_gas_price(mut self, gas_price: u128) -> Self { - self.set_gas_price(gas_price); + /// Builder-pattern method for setting the legacy energy price. + fn with_energy_price(mut self, energy_price: u128) -> Self { + self.set_energy_price(gas_price); self } + /// Get the energy limit for the transaction. + fn energy_limit(&self) -> Option; + + /// Set the energy limit for the transaction. + fn set_energy_limit(&mut self, energy_limit: u128); + + /// Builder-pattern method for setting the energy limit. + fn with_energy_limit(mut self, energy_limit: u128) -> Self { + self.set_energy_limit(energy_limit); + self + } + + #[cfg(feature = "typed_tx")] /// Get the max fee per gas for the transaction. fn max_fee_per_gas(&self) -> Option; + #[cfg(feature = "typed_tx")] /// Set the max fee per gas for the transaction. fn set_max_fee_per_gas(&mut self, max_fee_per_gas: u128); + #[cfg(feature = "typed_tx")] /// Builder-pattern method for setting max fee per gas . fn with_max_fee_per_gas(mut self, max_fee_per_gas: u128) -> Self { self.set_max_fee_per_gas(max_fee_per_gas); self } + #[cfg(feature = "typed_tx")] /// Get the max priority fee per gas for the transaction. fn max_priority_fee_per_gas(&self) -> Option; + #[cfg(feature = "typed_tx")] /// Set the max priority fee per gas for the transaction. fn set_max_priority_fee_per_gas(&mut self, max_priority_fee_per_gas: u128); + #[cfg(feature = "typed_tx")] /// Builder-pattern method for setting max priority fee per gas. fn with_max_priority_fee_per_gas(mut self, max_priority_fee_per_gas: u128) -> Self { self.set_max_priority_fee_per_gas(max_priority_fee_per_gas); self } + #[cfg(feature = "typed_tx")] /// Get the max fee per blob gas for the transaction. fn max_fee_per_blob_gas(&self) -> Option; + #[cfg(feature = "typed_tx")] /// 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); + #[cfg(feature = "typed_tx")] /// 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 } - /// Get the gas limit for the transaction. - fn gas_limit(&self) -> Option; - - /// Set the gas limit for the transaction. - fn set_gas_limit(&mut self, gas_limit: u128); - - /// Builder-pattern method for setting the gas limit. - fn with_gas_limit(mut self, gas_limit: u128) -> Self { - self.set_gas_limit(gas_limit); - self - } - + #[cfg(feature = "typed_tx")] /// Get the EIP-2930 access list for the transaction. fn access_list(&self) -> Option<&AccessList>; + #[cfg(feature = "typed_tx")] /// Sets the EIP-2930 access list. fn set_access_list(&mut self, access_list: AccessList); + #[cfg(feature = "typed_tx")] /// Builder-pattern method for setting the access list. fn with_access_list(mut self, access_list: AccessList) -> Self { self.set_access_list(access_list); self } + #[cfg(feature = "typed_tx")] /// Gets the EIP-4844 blob sidecar of the transaction. fn blob_sidecar(&self) -> Option<&BlobTransactionSidecar>; + #[cfg(feature = "typed_tx")] /// 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); + #[cfg(feature = "typed_tx")] /// 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 } + #[cfg(feature = "typed_tx")] /// Check if all necessary keys are present to build the specified type, /// returning a list of missing keys. fn complete_type(&self, ty: N::TxType) -> Result<(), Vec<&'static str>>; + #[cfg(feature = "typed_tx")] /// Check if all necessary keys are present to build the currently-preferred /// transaction type, returning a list of missing keys. fn complete_preferred(&self) -> Result<(), Vec<&'static str>> { self.complete_type(self.output_tx_type()) } + #[cfg(feature = "typed_tx")] /// Assert that the builder prefers a certain transaction type. This does /// not indicate that the builder is ready to build. This function uses a /// `dbg_assert_eq!` to check the builder status, and will have no affect @@ -303,6 +321,7 @@ pub trait TransactionBuilder: Default + Sized + Send + Sync + 'stati debug_assert_eq!(self.output_tx_type(), ty); } + #[cfg(feature = "typed_tx")] /// Assert that the builder prefers a certain transaction type. This does /// not indicate that the builder is ready to build. This function uses a /// `dbg_assert_eq!` to check the builder status, and will have no affect @@ -320,10 +339,12 @@ pub trait TransactionBuilder: Default + Sized + Send + Sync + 'stati /// a valid transaction. fn can_build(&self) -> bool; + #[cfg(feature = "typed_tx")] /// Returns the transaction type that this builder will attempt to build. /// This does not imply that the builder is ready to build. fn output_tx_type(&self) -> N::TxType; + #[cfg(feature = "typed_tx")] /// Returns the transaction type that this builder will build. `None` if /// the builder is not ready to build. fn output_tx_type_checked(&self) -> Option; diff --git a/crates/network/src/transaction/signer.rs b/crates/network/src/transaction/signer.rs index a9d704936..b7a626840 100644 --- a/crates/network/src/transaction/signer.rs +++ b/crates/network/src/transaction/signer.rs @@ -1,6 +1,6 @@ use crate::{Network, TransactionBuilder}; use alloy_consensus::SignableTransaction; -use alloy_primitives::Address; +use alloy_primitives::IcanAddress; use async_trait::async_trait; use futures_utils_wasm::impl_future; @@ -20,19 +20,19 @@ pub trait NetworkSigner: std::fmt::Debug + Send + Sync { /// Get the default signer address. This address should be used /// in [`NetworkSigner::sign_transaction_from`] when no specific signer is /// specified. - fn default_signer_address(&self) -> Address; + fn default_signer_address(&self) -> IcanAddress; /// Return true if the signer contains a credential for the given address. - fn has_signer_for(&self, address: &Address) -> bool; + fn has_signer_for(&self, address: &IcanAddress) -> bool; /// Return an iterator of all signer addresses. - fn signer_addresses(&self) -> impl Iterator; + fn signer_addresses(&self) -> impl Iterator; /// Asynchronously sign an unsigned transaction, with a specified /// credential. async fn sign_transaction_from( &self, - sender: Address, + sender: IcanAddress, tx: N::UnsignedTx, ) -> alloy_signer::Result; @@ -74,7 +74,7 @@ pub trait NetworkSigner: std::fmt::Debug + Send + Sync { #[cfg_attr(not(target_arch = "wasm32"), async_trait)] pub trait TxSigner { /// Get the address of the signer. - fn address(&self) -> Address; + fn address(&self) -> IcanAddress; /// Asynchronously sign an unsigned transaction. async fn sign_transaction( @@ -100,7 +100,7 @@ pub trait TxSigner { /// [`ChainId`]: alloy_primitives::ChainId pub trait TxSignerSync { /// Get the address of the signer. - fn address(&self) -> Address; + fn address(&self) -> IcanAddress; /// Synchronously sign an unsigned transaction. fn sign_transaction_sync( diff --git a/crates/provider/src/fillers/chain_id.rs b/crates/provider/src/fillers/chain_id.rs index 262d478c4..c8135cbcd 100644 --- a/crates/provider/src/fillers/chain_id.rs +++ b/crates/provider/src/fillers/chain_id.rs @@ -56,7 +56,7 @@ impl TxFiller for ChainIdFiller { type Fillable = u64; fn status(&self, tx: &N::TransactionRequest) -> FillerControlFlow { - if tx.chain_id().is_some() { + if tx.network_id().is_some() { FillerControlFlow::Finished } else { FillerControlFlow::Ready @@ -88,8 +88,8 @@ impl TxFiller for ChainIdFiller { mut tx: SendableTx, ) -> TransportResult> { if let Some(builder) = tx.as_mut_builder() { - if builder.chain_id().is_none() { - builder.set_chain_id(fillable) + if builder.network_id().is_none() { + builder.set_network_id(fillable) } }; Ok(tx) diff --git a/crates/provider/src/fillers/gas.rs b/crates/provider/src/fillers/gas.rs index 9470f6e15..936c9a7da 100644 --- a/crates/provider/src/fillers/gas.rs +++ b/crates/provider/src/fillers/gas.rs @@ -75,13 +75,13 @@ impl GasFiller { T: Transport + Clone, N: Network, { - let gas_price_fut = if let Some(gas_price) = tx.gas_price() { + let gas_price_fut = if let Some(gas_price) = tx.energy_price() { async move { Ok(gas_price) }.left_future() } else { async { provider.get_gas_price().await }.right_future() }; - let gas_limit_fut = if let Some(gas_limit) = tx.gas_limit() { + let gas_limit_fut = if let Some(gas_limit) = tx.energy_limit() { async move { Ok(gas_limit) }.left_future() } else { async { provider.estimate_gas(tx, Default::default()).await }.right_future() @@ -102,7 +102,7 @@ impl GasFiller { T: Transport + Clone, N: Network, { - let gas_limit_fut = if let Some(gas_limit) = tx.gas_limit() { + let gas_limit_fut = if let Some(gas_limit) = tx.energy_limit() { async move { Ok(gas_limit) }.left_future() } else { async { provider.estimate_gas(tx, Default::default()).await }.right_future() @@ -132,7 +132,7 @@ impl GasFiller { T: Transport + Clone, N: Network, { - let gas_limit_fut = if let Some(gas_limit) = tx.gas_limit() { + let gas_limit_fut = if let Some(gas_limit) = tx.energy_limit() { async move { Ok(gas_limit) }.left_future() } else { async { provider.estimate_gas(tx, Default::default()).await }.right_future() @@ -175,7 +175,7 @@ impl TxFiller for GasFiller { fn status(&self, tx: &::TransactionRequest) -> FillerControlFlow { // legacy and eip2930 tx - if tx.gas_price().is_some() && tx.gas_limit().is_some() { + if tx.energy_price().is_some() && tx.energy_limit().is_some() { return FillerControlFlow::Finished; } @@ -183,7 +183,7 @@ impl TxFiller for GasFiller { if tx.max_fee_per_blob_gas().is_some() && tx.max_fee_per_gas().is_some() && tx.max_priority_fee_per_gas().is_some() - && tx.gas_limit().is_some() + && tx.energy_limit().is_some() { return FillerControlFlow::Finished; } @@ -192,7 +192,7 @@ impl TxFiller for GasFiller { if tx.blob_sidecar().is_none() && tx.max_fee_per_gas().is_some() && tx.max_priority_fee_per_gas().is_some() - && tx.gas_limit().is_some() + && tx.energy_limit().is_some() { return FillerControlFlow::Finished; } @@ -209,7 +209,7 @@ impl TxFiller for GasFiller { P: Provider, T: Transport + Clone, { - if tx.gas_price().is_some() || tx.access_list().is_some() { + if tx.energy_price().is_some() || tx.access_list().is_some() { self.prepare_legacy(provider, tx).await } else if tx.blob_sidecar().is_some() { self.prepare_4844(provider, tx).await @@ -231,16 +231,16 @@ impl TxFiller for GasFiller { if let Some(builder) = tx.as_mut_builder() { match fillable { GasFillable::Legacy { gas_limit, gas_price } => { - builder.set_gas_limit(gas_limit); - builder.set_gas_price(gas_price); + builder.set_energy_limit(gas_limit); + builder.set_energy_price(gas_price); } GasFillable::Eip1559 { gas_limit, estimate } => { - builder.set_gas_limit(gas_limit); + builder.set_energy_limit(gas_limit); builder.set_max_fee_per_gas(estimate.max_fee_per_gas); builder.set_max_priority_fee_per_gas(estimate.max_priority_fee_per_gas); } GasFillable::Eip4844 { gas_limit, estimate, max_fee_per_blob_gas } => { - builder.set_gas_limit(gas_limit); + builder.set_energy_limit(gas_limit); builder.set_max_fee_per_gas(estimate.max_fee_per_gas); builder.set_max_priority_fee_per_gas(estimate.max_priority_fee_per_gas); builder.set_max_fee_per_blob_gas(max_fee_per_blob_gas); @@ -268,7 +268,7 @@ mod tests { from: Some(from), value: Some(U256::from(100)), to: Some(address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045").into()), - chain_id: Some(31337), + network_id: Some(31337), ..Default::default() }; @@ -291,7 +291,7 @@ mod tests { from: Some(from), value: Some(U256::from(100)), to: Some(address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045").into()), - gas_price: Some(gas_price), + energy_price: Some(gas_price), ..Default::default() }; diff --git a/crates/provider/src/fillers/nonce.rs b/crates/provider/src/fillers/nonce.rs index e531c625e..e66fddfb6 100644 --- a/crates/provider/src/fillers/nonce.rs +++ b/crates/provider/src/fillers/nonce.rs @@ -126,7 +126,7 @@ mod tests { let tx = TransactionRequest { value: Some(U256::from(100)), to: Some(address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045").into()), - gas_price: Some(20e9 as u128), + energy_price: Some(20e9 as u128), gas: Some(21000), ..Default::default() }; @@ -144,7 +144,7 @@ mod tests { from: Some(from), value: Some(U256::from(100)), to: Some(address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045").into()), - gas_price: Some(20e9 as u128), + energy_price: Some(20e9 as u128), gas: Some(21000), ..Default::default() }; diff --git a/crates/provider/src/fillers/signer.rs b/crates/provider/src/fillers/signer.rs index f479b60ce..a09d0ab37 100644 --- a/crates/provider/src/fillers/signer.rs +++ b/crates/provider/src/fillers/signer.rs @@ -118,7 +118,7 @@ mod tests { nonce: Some(0), value: Some(U256::from(100)), to: Some(address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045").into()), - gas_price: Some(20e9 as u128), + energy_price: Some(20e9 as u128), gas: Some(21000), ..Default::default() }; diff --git a/crates/provider/src/provider/trait.rs b/crates/provider/src/provider/trait.rs index 4ef991bcc..4db1c032e 100644 --- a/crates/provider/src/provider/trait.rs +++ b/crates/provider/src/provider/trait.rs @@ -1054,7 +1054,7 @@ mod tests { let tx = TransactionRequest { value: Some(U256::from(100)), to: Some(address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045").into()), - gas_price: Some(20e9 as u128), + energy_price: Some(20e9 as u128), gas: Some(21000), ..Default::default() }; diff --git a/crates/rpc-types/src/eth/transaction/mod.rs b/crates/rpc-types/src/eth/transaction/mod.rs index 7de871733..6b72c98eb 100644 --- a/crates/rpc-types/src/eth/transaction/mod.rs +++ b/crates/rpc-types/src/eth/transaction/mod.rs @@ -155,11 +155,11 @@ impl Transaction { from: Some(self.from), to, gas: Some(self.gas), - gas_price, + energy_price: gas_price, value: Some(self.value), input: self.input.into(), nonce: Some(self.nonce), - chain_id: self.chain_id, + network_id: self.chain_id, access_list: self.access_list, transaction_type: self.transaction_type, max_fee_per_gas: self.max_fee_per_gas, @@ -404,7 +404,7 @@ mod tests { let tx = serde_json::from_str::(rpc_tx).unwrap(); let request = tx.into_request(); - assert!(request.gas_price.is_some()); + assert!(request.energy_price.is_some()); assert!(request.max_fee_per_gas.is_none()); } @@ -416,7 +416,7 @@ mod tests { let tx = serde_json::from_str::(rpc_tx).unwrap(); let request = tx.into_request(); - assert!(request.gas_price.is_none()); + assert!(request.energy_price.is_none()); assert!(request.max_fee_per_gas.is_some()); } } diff --git a/crates/rpc-types/src/eth/transaction/request.rs b/crates/rpc-types/src/eth/transaction/request.rs index 9a01fc963..1cd29cdbd 100644 --- a/crates/rpc-types/src/eth/transaction/request.rs +++ b/crates/rpc-types/src/eth/transaction/request.rs @@ -25,7 +25,7 @@ pub struct TransactionRequest { skip_serializing_if = "Option::is_none", with = "alloy_serde::num::u128_opt_via_ruint" )] - pub gas_price: Option, + pub energy_price: Option, /// The max base fee per gas the sender is willing to pay. #[serde( default, @@ -73,7 +73,7 @@ pub struct TransactionRequest { skip_serializing_if = "Option::is_none", with = "alloy_serde::num::u64_opt_via_ruint" )] - pub chain_id: Option, + pub network_id: Option, /// An EIP-2930 access list, which lowers cost for accessing accounts and storages in the list. See [EIP-2930](https://eips.ethereum.org/EIPS/eip-2930) for more information. #[serde(default, skip_serializing_if = "Option::is_none")] pub access_list: Option, @@ -161,7 +161,7 @@ impl TransactionRequest { /// The returns `gas_price` (legacy) if set or `max_fee_per_gas` (EIP1559) #[inline] pub fn fee_cap(&self) -> Option { - self.gas_price.or(self.max_fee_per_gas) + self.energy_price.or(self.max_fee_per_gas) } /// Populate the `blob_versioned_hashes` key, if a sidecar exists. No @@ -212,9 +212,9 @@ impl TransactionRequest { let checked_to = self.to.expect("checked in complete_legacy."); TxLegacy { - chain_id: self.chain_id, + chain_id: self.network_id, nonce: self.nonce.expect("checked in complete_legacy"), - gas_price: self.gas_price.expect("checked in complete_legacy"), + gas_price: self.energy_price.expect("checked in complete_legacy"), gas_limit: self.gas.expect("checked in complete_legacy"), to: checked_to, value: self.value.unwrap_or_default(), @@ -232,7 +232,7 @@ impl TransactionRequest { let checked_to = self.to.expect("checked in complete_1559."); TxEip1559 { - chain_id: self.chain_id.unwrap_or(1), + chain_id: self.network_id.unwrap_or(1), nonce: self.nonce.expect("checked in invalid_common_fields"), max_priority_fee_per_gas: self .max_priority_fee_per_gas @@ -256,9 +256,9 @@ impl TransactionRequest { let checked_to = self.to.expect("checked in complete_2930."); TxEip2930 { - chain_id: self.chain_id.unwrap_or(1), + chain_id: self.network_id.unwrap_or(1), nonce: self.nonce.expect("checked in complete_2930"), - gas_price: self.gas_price.expect("checked in complete_2930"), + gas_price: self.energy_price.expect("checked in complete_2930"), gas_limit: self.gas.expect("checked in complete_2930"), to: checked_to, value: self.value.unwrap_or_default(), @@ -285,7 +285,7 @@ impl TransactionRequest { TxEip4844WithSidecar { sidecar: self.sidecar.expect("checked in complete_4844"), tx: TxEip4844 { - chain_id: self.chain_id.unwrap_or(1), + chain_id: self.network_id.unwrap_or(1), nonce: self.nonce.expect("checked in complete_4844"), gas_limit: self.gas.expect("checked in complete_4844"), max_fee_per_gas: self.max_fee_per_gas.expect("checked in complete_4844"), @@ -319,7 +319,7 @@ impl TransactionRequest { } fn check_legacy_fields(&self, missing: &mut Vec<&'static str>) { - if self.gas_price.is_none() { + if self.energy_price.is_none() { missing.push("gas_price"); } } @@ -356,12 +356,12 @@ impl TransactionRequest { self.sidecar = None; } TxType::Eip1559 => { - self.gas_price = None; + self.energy_price = None; self.blob_versioned_hashes = None; self.sidecar = None; } TxType::Eip4844 => { - self.gas_price = None; + self.energy_price = None; } } } @@ -376,9 +376,9 @@ impl TransactionRequest { pub const fn preferred_type(&self) -> TxType { if self.sidecar.is_some() || self.max_fee_per_blob_gas.is_some() { TxType::Eip4844 - } else if self.access_list.is_some() && self.gas_price.is_some() { + } else if self.access_list.is_some() && self.energy_price.is_some() { TxType::Eip2930 - } else if self.gas_price.is_some() { + } else if self.energy_price.is_some() { TxType::Legacy } else { TxType::Eip1559 @@ -600,12 +600,12 @@ impl From for TransactionRequest { fn from(tx: TxLegacy) -> Self { Self { to: if let TxKind::Call(to) = tx.to { Some(to.into()) } else { None }, - gas_price: Some(tx.gas_price), + energy_price: Some(tx.gas_price), gas: Some(tx.gas_limit), value: Some(tx.value), input: tx.input.into(), nonce: Some(tx.nonce), - chain_id: tx.chain_id, + network_id: tx.chain_id, transaction_type: Some(0), ..Default::default() } @@ -616,12 +616,12 @@ impl From for TransactionRequest { fn from(tx: TxEip2930) -> Self { Self { to: if let TxKind::Call(to) = tx.to { Some(to.into()) } else { None }, - gas_price: Some(tx.gas_price), + energy_price: Some(tx.gas_price), gas: Some(tx.gas_limit), value: Some(tx.value), input: tx.input.into(), nonce: Some(tx.nonce), - chain_id: Some(tx.chain_id), + network_id: Some(tx.chain_id), access_list: Some(tx.access_list), transaction_type: Some(1), ..Default::default() @@ -639,7 +639,7 @@ impl From for TransactionRequest { value: Some(tx.value), input: tx.input.into(), nonce: Some(tx.nonce), - chain_id: Some(tx.chain_id), + network_id: Some(tx.chain_id), access_list: Some(tx.access_list), transaction_type: Some(2), ..Default::default() @@ -658,7 +658,7 @@ impl From for TransactionRequest { value: Some(tx.value), input: tx.input.into(), nonce: Some(tx.nonce), - chain_id: Some(tx.chain_id), + network_id: Some(tx.chain_id), access_list: Some(tx.access_list), blob_versioned_hashes: Some(tx.blob_versioned_hashes), transaction_type: Some(3), @@ -680,7 +680,7 @@ impl From for TransactionRequest { value: Some(tx.value), input: tx.input.into(), nonce: Some(tx.nonce), - chain_id: Some(tx.chain_id), + network_id: Some(tx.chain_id), access_list: Some(tx.access_list), blob_versioned_hashes: Some(tx.blob_versioned_hashes), sidecar: Some(sidecar), @@ -847,11 +847,11 @@ mod tests { let chain_id_as_num = format!(r#"{{"chainId": {} }}"#, chain_id); let req1 = serde_json::from_str::(&chain_id_as_num).unwrap(); - assert_eq!(req1.chain_id.unwrap(), chain_id); + assert_eq!(req1.network_id.unwrap(), chain_id); let chain_id_as_hex = format!(r#"{{"chainId": "0x{:x}" }}"#, chain_id); let req2 = serde_json::from_str::(&chain_id_as_hex).unwrap(); - assert_eq!(req2.chain_id.unwrap(), chain_id); + assert_eq!(req2.network_id.unwrap(), chain_id); } #[test] From 27266ba86ca777941e6af0135973a835dbf03d9c Mon Sep 17 00:00:00 2001 From: Mykhailo Slyvka Date: Thu, 16 May 2024 11:18:22 +0300 Subject: [PATCH 2/4] remove typed_tx feature --- crates/consensus/Cargo.toml | 3 +- crates/consensus/src/transaction/typed.rs | 3 -- crates/network/Cargo.toml | 3 +- crates/network/src/any/builder.rs | 29 +++++------------- crates/network/src/ethereum/builder.rs | 37 +++++------------------ crates/network/src/ethereum/signer.rs | 3 -- crates/network/src/transaction/builder.rs | 21 ------------- 7 files changed, 18 insertions(+), 81 deletions(-) diff --git a/crates/consensus/Cargo.toml b/crates/consensus/Cargo.toml index ea387b501..9194c868e 100644 --- a/crates/consensus/Cargo.toml +++ b/crates/consensus/Cargo.toml @@ -56,5 +56,4 @@ serde = [ "alloy-primitives/serde", "dep:alloy-serde", "alloy-eips/serde", -] -typed_tx = [] +] \ No newline at end of file diff --git a/crates/consensus/src/transaction/typed.rs b/crates/consensus/src/transaction/typed.rs index 993a7fa31..e8aea0cab 100644 --- a/crates/consensus/src/transaction/typed.rs +++ b/crates/consensus/src/transaction/typed.rs @@ -19,15 +19,12 @@ pub enum TypedTransaction { #[cfg_attr(feature = "serde", serde(rename = "0x00", alias = "0x0"))] Legacy(TxLegacy), /// EIP-2930 transaction - #[cfg(feature = "typed_tx")] #[cfg_attr(feature = "serde", serde(rename = "0x01", alias = "0x1"))] Eip2930(TxEip2930), /// EIP-1559 transaction - #[cfg(feature = "typed_tx")] #[cfg_attr(feature = "serde", serde(rename = "0x02", alias = "0x2"))] Eip1559(TxEip1559), /// EIP-4844 transaction - #[cfg(feature = "typed_tx")] #[cfg_attr(feature = "serde", serde(rename = "0x03", alias = "0x3"))] Eip4844(TxEip4844Variant), } diff --git a/crates/network/Cargo.toml b/crates/network/Cargo.toml index b0cadb7bc..d16ffab38 100644 --- a/crates/network/Cargo.toml +++ b/crates/network/Cargo.toml @@ -29,5 +29,4 @@ alloy-signer-wallet.workspace = true tokio.workspace = true [features] -k256 = ["alloy-primitives/k256", "alloy-consensus/k256"] -typed_tx = [] +k256 = ["alloy-primitives/k256", "alloy-consensus/k256"] \ No newline at end of file diff --git a/crates/network/src/any/builder.rs b/crates/network/src/any/builder.rs index 3a9eae6fd..2fcf5b21c 100644 --- a/crates/network/src/any/builder.rs +++ b/crates/network/src/any/builder.rs @@ -59,14 +59,6 @@ impl TransactionBuilder for WithOtherFields { self.deref_mut().set_value(value) } - fn energy_price(&self) -> Option { - self.deref().energy_price() - } - - fn set_energy_price(&mut self, gas_price: u128) { - self.deref_mut().set_energy_price(gas_price); - } - fn energy_limit(&self) -> Option { self.deref().energy_limit() } @@ -75,69 +67,64 @@ impl TransactionBuilder for WithOtherFields { self.deref_mut().set_energy_limit(energy_limit); } - #[cfg(feature = "typed_tx")] fn max_fee_per_gas(&self) -> Option { self.deref().max_fee_per_gas() } - #[cfg(feature = "typed_tx")] fn set_max_fee_per_gas(&mut self, max_fee_per_gas: u128) { self.deref_mut().set_max_fee_per_gas(max_fee_per_gas); } - #[cfg(feature = "typed_tx")] fn max_priority_fee_per_gas(&self) -> Option { self.deref().max_priority_fee_per_gas() } - #[cfg(feature = "typed_tx")] fn set_max_priority_fee_per_gas(&mut self, max_priority_fee_per_gas: u128) { self.deref_mut().set_max_priority_fee_per_gas(max_priority_fee_per_gas); } - #[cfg(feature = "typed_tx")] fn max_fee_per_blob_gas(&self) -> Option { self.deref().max_fee_per_blob_gas() } - #[cfg(feature = "typed_tx")] 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 energy_price(&self) -> Option { + self.deref().energy_price() + } + + fn set_energy_price(&mut self, gas_price: u128) { + self.deref_mut().set_energy_price(gas_price); + } + /// Get the EIP-2930 access list for the transaction. - #[cfg(feature = "typed_tx")] fn access_list(&self) -> Option<&AccessList> { self.deref().access_list() } /// Sets the EIP-2930 access list. - #[cfg(feature = "typed_tx")] fn set_access_list(&mut self, access_list: AccessList) { self.deref_mut().set_access_list(access_list) } - #[cfg(feature = "typed_tx")] fn blob_sidecar(&self) -> Option<&BlobTransactionSidecar> { self.deref().blob_sidecar() } - #[cfg(feature = "typed_tx")] fn set_blob_sidecar(&mut self, sidecar: BlobTransactionSidecar) { self.deref_mut().set_blob_sidecar(sidecar) } - #[cfg(feature = "typed_tx")] fn complete_type(&self, ty: ::TxType) -> Result<(), Vec<&'static str>> { self.deref().complete_type(ty.try_into().map_err(|_| vec!["supported tx type"])?) } - #[cfg(feature = "typed_tx")] fn output_tx_type(&self) -> ::TxType { self.deref().output_tx_type().into() } - #[cfg(feature = "typed_tx")] fn output_tx_type_checked(&self) -> Option<::TxType> { self.deref().output_tx_type_checked().map(Into::into) } diff --git a/crates/network/src/ethereum/builder.rs b/crates/network/src/ethereum/builder.rs index a74e2b054..ca4cc652f 100644 --- a/crates/network/src/ethereum/builder.rs +++ b/crates/network/src/ethereum/builder.rs @@ -74,58 +74,47 @@ impl TransactionBuilder for TransactionRequest { self.gas = Some(energy_limit); } - #[cfg(feature = "typed_tx")] fn max_fee_per_gas(&self) -> Option { self.max_fee_per_gas } - #[cfg(feature = "typed_tx")] fn set_max_fee_per_gas(&mut self, max_fee_per_gas: u128) { self.max_fee_per_gas = Some(max_fee_per_gas); } - #[cfg(feature = "typed_tx")] fn max_priority_fee_per_gas(&self) -> Option { self.max_priority_fee_per_gas } - #[cfg(feature = "typed_tx")] fn set_max_priority_fee_per_gas(&mut self, max_priority_fee_per_gas: u128) { self.max_priority_fee_per_gas = Some(max_priority_fee_per_gas); } - #[cfg(feature = "typed_tx")] fn max_fee_per_blob_gas(&self) -> Option { self.max_fee_per_blob_gas } - #[cfg(feature = "typed_tx")] 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) } - #[cfg(feature = "typed_tx")] fn access_list(&self) -> Option<&AccessList> { self.access_list.as_ref() } - #[cfg(feature = "typed_tx")] fn set_access_list(&mut self, access_list: AccessList) { self.access_list = Some(access_list); } - #[cfg(feature = "typed_tx")] fn blob_sidecar(&self) -> Option<&BlobTransactionSidecar> { self.sidecar.as_ref() } - #[cfg(feature = "typed_tx")] fn set_blob_sidecar(&mut self, sidecar: BlobTransactionSidecar) { self.sidecar = Some(sidecar); self.populate_blob_hashes(); } - #[cfg(feature = "typed_tx")] fn complete_type(&self, ty: TxType) -> Result<(), Vec<&'static str>> { match ty { TxType::Legacy => self.complete_legacy(), @@ -148,37 +137,27 @@ impl TransactionBuilder for TransactionRequest { // chain_id and from may be none. let common = self.gas.is_some() && self.nonce.is_some(); - let legacy = self.energy_price.is_some(); - - #[cfg(feature = "typed_tx")] - { - let eip2930 = legacy && self.access_list().is_some(); - let eip1559 = self.max_fee_per_gas.is_some() && self.max_priority_fee_per_gas.is_some(); - let eip4844 = eip1559 && self.sidecar.is_some() && self.to.is_some(); - common && (legacy || eip2930 || eip1559 || eip4844) - } + let legacy = self.gas_price.is_some(); + let eip2930 = legacy && self.access_list().is_some(); + + let eip1559 = self.max_fee_per_gas.is_some() && self.max_priority_fee_per_gas.is_some(); - common && legacy + let eip4844 = eip1559 && self.sidecar.is_some() && self.to.is_some(); + common && (legacy || eip2930 || eip1559 || eip4844) } - #[cfg(feature = "typed_tx")] fn output_tx_type(&self) -> TxType { self.preferred_type() } - #[cfg(feature = "typed_tx")] fn output_tx_type_checked(&self) -> Option { self.buildable_type() } fn prep_for_submission(&mut self) { self.transaction_type = Some(self.preferred_type() as u8); - - #[cfg(feature = "typed_tx")] - { - self.trim_conflicting_keys(); - self.populate_blob_hashes(); - } + self.trim_conflicting_keys(); + self.populate_blob_hashes(); } fn build_unsigned(self) -> BuildResult { diff --git a/crates/network/src/ethereum/signer.rs b/crates/network/src/ethereum/signer.rs index 8b42f0fd4..d2904638e 100644 --- a/crates/network/src/ethereum/signer.rs +++ b/crates/network/src/ethereum/signer.rs @@ -122,17 +122,14 @@ where let sig = self.sign_transaction_inner(sender, &mut t).await?; Ok(t.into_signed(sig).into()) } - #[cfg(feature = "typed_tx")] TypedTransaction::Eip2930(mut t) => { let sig = self.sign_transaction_inner(sender, &mut t).await?; Ok(t.into_signed(sig).into()) } - #[cfg(feature = "typed_tx")] TypedTransaction::Eip1559(mut t) => { let sig = self.sign_transaction_inner(sender, &mut t).await?; Ok(t.into_signed(sig).into()) } - #[cfg(feature = "typed_tx")] TypedTransaction::Eip4844(mut t) => { let sig = self.sign_transaction_inner(sender, &mut t).await?; Ok(t.into_signed(sig).into()) diff --git a/crates/network/src/transaction/builder.rs b/crates/network/src/transaction/builder.rs index e632860ad..27a81a9aa 100644 --- a/crates/network/src/transaction/builder.rs +++ b/crates/network/src/transaction/builder.rs @@ -222,97 +222,79 @@ pub trait TransactionBuilder: Default + Sized + Send + Sync + 'stati self } - #[cfg(feature = "typed_tx")] /// Get the max fee per gas for the transaction. fn max_fee_per_gas(&self) -> Option; - #[cfg(feature = "typed_tx")] /// Set the max fee per gas for the transaction. fn set_max_fee_per_gas(&mut self, max_fee_per_gas: u128); - #[cfg(feature = "typed_tx")] /// Builder-pattern method for setting max fee per gas . fn with_max_fee_per_gas(mut self, max_fee_per_gas: u128) -> Self { self.set_max_fee_per_gas(max_fee_per_gas); self } - #[cfg(feature = "typed_tx")] /// Get the max priority fee per gas for the transaction. fn max_priority_fee_per_gas(&self) -> Option; - #[cfg(feature = "typed_tx")] /// Set the max priority fee per gas for the transaction. fn set_max_priority_fee_per_gas(&mut self, max_priority_fee_per_gas: u128); - #[cfg(feature = "typed_tx")] /// Builder-pattern method for setting max priority fee per gas. fn with_max_priority_fee_per_gas(mut self, max_priority_fee_per_gas: u128) -> Self { self.set_max_priority_fee_per_gas(max_priority_fee_per_gas); self } - #[cfg(feature = "typed_tx")] /// Get the max fee per blob gas for the transaction. fn max_fee_per_blob_gas(&self) -> Option; - #[cfg(feature = "typed_tx")] /// 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); - #[cfg(feature = "typed_tx")] /// 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 } - #[cfg(feature = "typed_tx")] /// Get the EIP-2930 access list for the transaction. fn access_list(&self) -> Option<&AccessList>; - #[cfg(feature = "typed_tx")] /// Sets the EIP-2930 access list. fn set_access_list(&mut self, access_list: AccessList); - #[cfg(feature = "typed_tx")] /// Builder-pattern method for setting the access list. fn with_access_list(mut self, access_list: AccessList) -> Self { self.set_access_list(access_list); self } - #[cfg(feature = "typed_tx")] /// Gets the EIP-4844 blob sidecar of the transaction. fn blob_sidecar(&self) -> Option<&BlobTransactionSidecar>; - #[cfg(feature = "typed_tx")] /// 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); - #[cfg(feature = "typed_tx")] /// 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 } - #[cfg(feature = "typed_tx")] /// Check if all necessary keys are present to build the specified type, /// returning a list of missing keys. fn complete_type(&self, ty: N::TxType) -> Result<(), Vec<&'static str>>; - #[cfg(feature = "typed_tx")] /// Check if all necessary keys are present to build the currently-preferred /// transaction type, returning a list of missing keys. fn complete_preferred(&self) -> Result<(), Vec<&'static str>> { self.complete_type(self.output_tx_type()) } - #[cfg(feature = "typed_tx")] /// Assert that the builder prefers a certain transaction type. This does /// not indicate that the builder is ready to build. This function uses a /// `dbg_assert_eq!` to check the builder status, and will have no affect @@ -321,7 +303,6 @@ pub trait TransactionBuilder: Default + Sized + Send + Sync + 'stati debug_assert_eq!(self.output_tx_type(), ty); } - #[cfg(feature = "typed_tx")] /// Assert that the builder prefers a certain transaction type. This does /// not indicate that the builder is ready to build. This function uses a /// `dbg_assert_eq!` to check the builder status, and will have no affect @@ -339,12 +320,10 @@ pub trait TransactionBuilder: Default + Sized + Send + Sync + 'stati /// a valid transaction. fn can_build(&self) -> bool; - #[cfg(feature = "typed_tx")] /// Returns the transaction type that this builder will attempt to build. /// This does not imply that the builder is ready to build. fn output_tx_type(&self) -> N::TxType; - #[cfg(feature = "typed_tx")] /// Returns the transaction type that this builder will build. `None` if /// the builder is not ready to build. fn output_tx_type_checked(&self) -> Option; From e835de1b893d79c0ee680d8fb635140cfa25b4e1 Mon Sep 17 00:00:00 2001 From: Mykhailo Slyvka Date: Thu, 16 May 2024 11:21:23 +0300 Subject: [PATCH 3/4] revert changes --- crates/network/src/any/builder.rs | 16 +++++++-------- crates/network/src/ethereum/builder.rs | 16 +++++++-------- crates/network/src/transaction/builder.rs | 24 +++++++++++------------ 3 files changed, 28 insertions(+), 28 deletions(-) diff --git a/crates/network/src/any/builder.rs b/crates/network/src/any/builder.rs index 2fcf5b21c..75c917452 100644 --- a/crates/network/src/any/builder.rs +++ b/crates/network/src/any/builder.rs @@ -121,14 +121,6 @@ impl TransactionBuilder for WithOtherFields { self.deref().complete_type(ty.try_into().map_err(|_| vec!["supported tx type"])?) } - fn output_tx_type(&self) -> ::TxType { - self.deref().output_tx_type().into() - } - - fn output_tx_type_checked(&self) -> Option<::TxType> { - self.deref().output_tx_type_checked().map(Into::into) - } - fn can_build(&self) -> bool { self.deref().can_build() } @@ -137,6 +129,14 @@ impl TransactionBuilder for WithOtherFields { self.deref().can_submit() } + fn output_tx_type(&self) -> ::TxType { + self.deref().output_tx_type().into() + } + + fn output_tx_type_checked(&self) -> Option<::TxType> { + self.deref().output_tx_type_checked().map(Into::into) + } + fn prep_for_submission(&mut self) { self.deref_mut().prep_for_submission() } diff --git a/crates/network/src/ethereum/builder.rs b/crates/network/src/ethereum/builder.rs index ca4cc652f..affe0c22d 100644 --- a/crates/network/src/ethereum/builder.rs +++ b/crates/network/src/ethereum/builder.rs @@ -66,14 +66,6 @@ impl TransactionBuilder for TransactionRequest { self.energy_price = Some(energy_price); } - fn energy_limit(&self) -> Option { - self.gas - } - - fn set_energy_limit(&mut self, energy_limit: u128) { - self.gas = Some(energy_limit); - } - fn max_fee_per_gas(&self) -> Option { self.max_fee_per_gas } @@ -98,6 +90,14 @@ impl TransactionBuilder for TransactionRequest { self.max_fee_per_blob_gas = Some(max_fee_per_blob_gas) } + fn energy_limit(&self) -> Option { + self.gas + } + + fn set_energy_limit(&mut self, energy_limit: u128) { + self.gas = Some(energy_limit); + } + fn access_list(&self) -> Option<&AccessList> { self.access_list.as_ref() } diff --git a/crates/network/src/transaction/builder.rs b/crates/network/src/transaction/builder.rs index 27a81a9aa..c30da8172 100644 --- a/crates/network/src/transaction/builder.rs +++ b/crates/network/src/transaction/builder.rs @@ -210,18 +210,6 @@ pub trait TransactionBuilder: Default + Sized + Send + Sync + 'stati self } - /// Get the energy limit for the transaction. - fn energy_limit(&self) -> Option; - - /// Set the energy limit for the transaction. - fn set_energy_limit(&mut self, energy_limit: u128); - - /// Builder-pattern method for setting the energy limit. - fn with_energy_limit(mut self, energy_limit: u128) -> Self { - self.set_energy_limit(energy_limit); - self - } - /// Get the max fee per gas for the transaction. fn max_fee_per_gas(&self) -> Option; @@ -258,6 +246,18 @@ pub trait TransactionBuilder: Default + Sized + Send + Sync + 'stati self } + /// Get the energy limit for the transaction. + fn energy_limit(&self) -> Option; + + /// Set the energy limit for the transaction. + fn set_energy_limit(&mut self, energy_limit: u128); + + /// Builder-pattern method for setting the energy limit. + fn with_energy_limit(mut self, energy_limit: u128) -> Self { + self.set_energy_limit(energy_limit); + self + } + /// Get the EIP-2930 access list for the transaction. fn access_list(&self) -> Option<&AccessList>; From 1ef1c96e96dd6602bdde9987bd163b8c3d678012 Mon Sep 17 00:00:00 2001 From: Mykhailo Slyvka Date: Mon, 20 May 2024 11:43:53 +0300 Subject: [PATCH 4/4] add signature to tx builder --- crates/network/src/any/builder.rs | 8 ++++++++ crates/network/src/ethereum/builder.rs | 12 ++++++++++-- crates/network/src/transaction/builder.rs | 14 +++++++++++++- 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/crates/network/src/any/builder.rs b/crates/network/src/any/builder.rs index 75c917452..e9b6345d6 100644 --- a/crates/network/src/any/builder.rs +++ b/crates/network/src/any/builder.rs @@ -59,6 +59,14 @@ impl TransactionBuilder for WithOtherFields { self.deref_mut().set_value(value) } + fn signature(&self) -> Option { + self.deref().signature() + } + + fn set_signature(&mut self, signature: alloy_primitives::B1368) { + self.deref_mut().set_signature(signature) + } + fn energy_limit(&self) -> Option { self.deref().energy_limit() } diff --git a/crates/network/src/ethereum/builder.rs b/crates/network/src/ethereum/builder.rs index affe0c22d..f793b909b 100644 --- a/crates/network/src/ethereum/builder.rs +++ b/crates/network/src/ethereum/builder.rs @@ -2,7 +2,7 @@ use crate::{ BuildResult, Ethereum, Network, NetworkSigner, TransactionBuilder, TransactionBuilderError, }; use alloy_consensus::{BlobTransactionSidecar, TxType, TypedTransaction}; -use alloy_primitives::{Bytes, ChainId, IcanAddress, TxKind, U256}; +use alloy_primitives::{Bytes, ChainId, IcanAddress, TxKind, B1368, U256}; use alloy_rpc_types::{request::TransactionRequest, AccessList}; impl TransactionBuilder for TransactionRequest { @@ -58,6 +58,14 @@ impl TransactionBuilder for TransactionRequest { self.value = Some(value) } + fn signature(&self) -> Option { + self.signature + } + + fn set_signature(&mut self, signature: B1368) { + self.signature = Some(signature) + } + fn energy_price(&self) -> Option { self.energy_price } @@ -128,7 +136,7 @@ impl TransactionBuilder for TransactionRequest { // value and data may be None. If they are, they will be set to default. // gas fields and nonce may be None, if they are, they will be populated // with default values by the RPC server - self.from.is_some() + self.from.is_some() && self.signature.is_some() } fn can_build(&self) -> bool { diff --git a/crates/network/src/transaction/builder.rs b/crates/network/src/transaction/builder.rs index c30da8172..8e29a2ed7 100644 --- a/crates/network/src/transaction/builder.rs +++ b/crates/network/src/transaction/builder.rs @@ -1,7 +1,7 @@ use super::signer::NetworkSigner; use crate::Network; use alloy_consensus::BlobTransactionSidecar; -use alloy_primitives::{Bytes, ChainId, IcanAddress, TxKind, U256}; +use alloy_primitives::{Bytes, ChainId, IcanAddress, TxKind, B1368, U256}; use alloy_rpc_types::AccessList; use alloy_sol_types::SolCall; use futures_utils_wasm::impl_future; @@ -198,6 +198,18 @@ pub trait TransactionBuilder: Default + Sized + Send + Sync + 'stati self } + /// Get the signature for the transaction. + fn signature(&self) -> Option; + + /// Set the signature for the transaction. + fn set_signature(&mut self, signature: B1368); + + /// Builder-pattern method for setting the signature. + fn with_signature(mut self, signature: B1368) -> Self { + self.set_signature(signature); + self + } + /// Get the legacy energy price for the transaction. fn energy_price(&self) -> Option;