Skip to content

Commit

Permalink
Merge branch 'main' into crate/rpc_client
Browse files Browse the repository at this point in the history
  • Loading branch information
error2215 authored May 28, 2024
2 parents 1804efc + e901768 commit 7599a2f
Show file tree
Hide file tree
Showing 19 changed files with 168 additions and 141 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ Crates used in Core or that are used by other crates
- [ ] eips
- [x] genesis - in work (Misha)
- [x] json-rpc - in work (Misha)
- [ ] network - in work (Misha)
- [x] network - in work (Misha)
- [x] node-bindings - in work (Misha)
- [ ] provider - in work (Misha)
- [ ] pubsub - in work (Misha)
- [ ] rpc-client - in work (Misha)
- [ ] rpc-types
- [x] rpc-client - in work (Misha)
- [ ] rpc-types - in work (Misha)
- [ ] rpc-types-anvil
- [ ] rpc-types-engine
- [ ] rpc-types-trace
Expand Down
2 changes: 1 addition & 1 deletion crates/consensus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@ serde = [
"alloy-primitives/serde",
"dep:alloy-serde",
"alloy-eips/serde",
]
]
8 changes: 4 additions & 4 deletions crates/contract/src/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ impl<T: Transport + Clone, P: Provider<T, N>, 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
}

Expand Down Expand Up @@ -326,15 +326,15 @@ impl<T: Transport + Clone, P: Provider<T, N>, 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
}

/// Sets the `gas_price` field in the transaction to the provided value
/// 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
}

Expand Down Expand Up @@ -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'"
);
Expand Down
2 changes: 1 addition & 1 deletion crates/network/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ alloy-signer-wallet.workspace = true
tokio.workspace = true

[features]
k256 = ["alloy-primitives/k256", "alloy-consensus/k256"]
k256 = ["alloy-primitives/k256", "alloy-consensus/k256"]
36 changes: 22 additions & 14 deletions crates/network/src/any/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ use alloy_rpc_types::{AccessList, TransactionRequest, WithOtherFields};
use crate::{any::AnyNetwork, BuildResult, Network, TransactionBuilder, TransactionBuilderError};

impl TransactionBuilder<AnyNetwork> for WithOtherFields<TransactionRequest> {
fn chain_id(&self) -> Option<alloy_primitives::ChainId> {
self.deref().chain_id()
fn network_id(&self) -> Option<alloy_primitives::ChainId> {
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<u64> {
Expand All @@ -31,11 +31,11 @@ impl TransactionBuilder<AnyNetwork> for WithOtherFields<TransactionRequest> {
self.deref_mut().set_input(input);
}

fn from(&self) -> Option<alloy_primitives::Address> {
fn from(&self) -> Option<alloy_primitives::IcanAddress> {
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);
}

Expand All @@ -59,12 +59,20 @@ impl TransactionBuilder<AnyNetwork> for WithOtherFields<TransactionRequest> {
self.deref_mut().set_value(value)
}

fn gas_price(&self) -> Option<u128> {
self.deref().gas_price()
fn signature(&self) -> Option<alloy_primitives::B1368> {
self.deref().signature()
}

fn set_gas_price(&mut self, gas_price: u128) {
self.deref_mut().set_gas_price(gas_price);
fn set_signature(&mut self, signature: alloy_primitives::B1368) {
self.deref_mut().set_signature(signature)
}

fn energy_limit(&self) -> Option<u128> {
self.deref().energy_limit()
}

fn set_energy_limit(&mut self, energy_limit: u128) {
self.deref_mut().set_energy_limit(energy_limit);
}

fn max_fee_per_gas(&self) -> Option<u128> {
Expand All @@ -91,12 +99,12 @@ impl TransactionBuilder<AnyNetwork> for WithOtherFields<TransactionRequest> {
self.deref_mut().set_max_fee_per_blob_gas(max_fee_per_blob_gas)
}

fn gas_limit(&self) -> Option<u128> {
self.deref().gas_limit()
fn energy_price(&self) -> Option<u128> {
self.deref().energy_price()
}

fn set_gas_limit(&mut self, gas_limit: u128) {
self.deref_mut().set_gas_limit(gas_limit);
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.
Expand Down
2 changes: 1 addition & 1 deletion crates/network/src/any/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl Network for AnyNetwork {
}

impl ReceiptResponse for AnyTransactionReceipt {
fn contract_address(&self) -> Option<alloy_primitives::Address> {
fn contract_address(&self) -> Option<alloy_primitives::IcanAddress> {
self.contract_address
}
}
49 changes: 28 additions & 21 deletions crates/network/src/ethereum/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, B1368, U256};
use alloy_rpc_types::{request::TransactionRequest, AccessList};

impl TransactionBuilder<Ethereum> for TransactionRequest {
fn chain_id(&self) -> Option<ChainId> {
self.chain_id
fn network_id(&self) -> Option<ChainId> {
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<u64> {
Expand All @@ -30,11 +30,11 @@ impl TransactionBuilder<Ethereum> for TransactionRequest {
self.input.input = Some(input.into());
}

fn from(&self) -> Option<Address> {
fn from(&self) -> Option<IcanAddress> {
self.from
}

fn set_from(&mut self, from: Address) {
fn set_from(&mut self, from: IcanAddress) {
self.from = Some(from);
}

Expand All @@ -58,12 +58,20 @@ impl TransactionBuilder<Ethereum> for TransactionRequest {
self.value = Some(value)
}

fn gas_price(&self) -> Option<u128> {
self.gas_price
fn signature(&self) -> Option<B1368> {
self.signature
}

fn set_gas_price(&mut self, gas_price: u128) {
self.gas_price = Some(gas_price);
fn set_signature(&mut self, signature: B1368) {
self.signature = Some(signature)
}

fn energy_price(&self) -> Option<u128> {
self.energy_price
}

fn set_energy_price(&mut self, energy_price: u128) {
self.energy_price = Some(energy_price);
}

fn max_fee_per_gas(&self) -> Option<u128> {
Expand All @@ -90,12 +98,12 @@ impl TransactionBuilder<Ethereum> for TransactionRequest {
self.max_fee_per_blob_gas = Some(max_fee_per_blob_gas)
}

fn gas_limit(&self) -> Option<u128> {
fn energy_limit(&self) -> Option<u128> {
self.gas
}

fn set_gas_limit(&mut self, gas_limit: u128) {
self.gas = Some(gas_limit);
fn set_energy_limit(&mut self, energy_limit: u128) {
self.gas = Some(energy_limit);
}

fn access_list(&self) -> Option<&AccessList> {
Expand Down Expand Up @@ -128,7 +136,7 @@ impl TransactionBuilder<Ethereum> 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 {
Expand All @@ -137,7 +145,6 @@ impl TransactionBuilder<Ethereum> for TransactionRequest {

// chain_id and from may be none.
let common = self.gas.is_some() && self.nonce.is_some();

let legacy = self.gas_price.is_some();
let eip2930 = legacy && self.access_list().is_some();

Expand Down Expand Up @@ -205,7 +212,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)
Expand All @@ -225,7 +232,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)
Expand All @@ -241,7 +248,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);
Expand All @@ -268,7 +275,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();

Expand Down Expand Up @@ -306,7 +313,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();

Expand Down
2 changes: 1 addition & 1 deletion crates/network/src/ethereum/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl Network for Ethereum {
}

impl ReceiptResponse for alloy_rpc_types::TransactionReceipt {
fn contract_address(&self) -> Option<alloy_primitives::Address> {
fn contract_address(&self) -> Option<alloy_primitives::IcanAddress> {
self.contract_address
}
}
18 changes: 9 additions & 9 deletions crates/network/src/ethereum/signer.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
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};

/// A signer capable of signing any transaction for the Ethereum network.
#[derive(Clone, Default)]
pub struct EthereumSigner {
default: Address,
secp_signers: BTreeMap<Address, Arc<dyn TxSigner<Signature> + Send + Sync>>,
default: IcanAddress,
secp_signers: BTreeMap<IcanAddress, Arc<dyn TxSigner<Signature> + Send + Sync>>,
}

impl std::fmt::Debug for EthereumSigner {
Expand Down Expand Up @@ -75,14 +75,14 @@ impl EthereumSigner {
/// Get the signer for the given address.
pub fn signer_by_address(
&self,
address: Address,
address: IcanAddress,
) -> Option<Arc<dyn TxSigner<Signature> + Send + Sync + 'static>> {
self.secp_signers.get(&address).cloned()
}

async fn sign_transaction_inner(
&self,
sender: Address,
sender: IcanAddress,
tx: &mut dyn SignableTransaction<Signature>,
) -> alloy_signer::Result<Signature> {
self.signer_by_address(sender)
Expand All @@ -100,21 +100,21 @@ impl<N> NetworkSigner<N> for EthereumSigner
where
N: Network<UnsignedTx = TypedTransaction, TxEnvelope = TxEnvelope>,
{
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<Item = Address> {
fn signer_addresses(&self) -> impl Iterator<Item = IcanAddress> {
self.secp_signers.keys().copied()
}

async fn sign_transaction_from(
&self,
sender: Address,
sender: IcanAddress,
tx: TypedTransaction,
) -> alloy_signer::Result<TxEnvelope> {
match tx {
Expand Down
4 changes: 2 additions & 2 deletions crates/network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Address>;
fn contract_address(&self) -> Option<IcanAddress>;
}

/// Captures type info for network-specific RPC requests/responses.
Expand Down
Loading

0 comments on commit 7599a2f

Please sign in to comment.