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

chore(other): use type aliases where possible to improve clarity #859

Merged
merged 20 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
7 changes: 2 additions & 5 deletions crates/consensus/src/receipt/receipts.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::receipt::{Eip658Value, TxReceipt};
use alloy_primitives::{Bloom, Log};
use alloy_primitives::{Bloom, Log, U128};
use alloy_rlp::{length_of_length, BufMut, Decodable, Encodable};
use core::borrow::Borrow;

Expand Down Expand Up @@ -43,10 +43,7 @@ where
let key = if self.status.is_eip658() { "status" } else { "root" };
s.serialize_field(key, &self.status)?;

s.serialize_field(
"cumulativeGasUsed",
&alloy_primitives::U128::from(self.cumulative_gas_used),
)?;
s.serialize_field("cumulativeGasUsed", &U128::from(self.cumulative_gas_used))?;
s.serialize_field("logs", &self.logs)?;

s.end()
Expand Down
24 changes: 12 additions & 12 deletions crates/consensus/src/transaction/eip1559.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,18 +261,6 @@ impl TxEip1559 {
}

impl Transaction for TxEip1559 {
fn input(&self) -> &[u8] {
&self.input
}

fn to(&self) -> TxKind {
self.to
}

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

fn chain_id(&self) -> Option<ChainId> {
Some(self.chain_id)
}
Expand All @@ -288,6 +276,18 @@ impl Transaction for TxEip1559 {
fn gas_price(&self) -> Option<u128> {
None
}

fn to(&self) -> TxKind {
self.to
}

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

fn input(&self) -> &[u8] {
&self.input
}
zerosnacks marked this conversation as resolved.
Show resolved Hide resolved
}

impl SignableTransaction<Signature> for TxEip1559 {
Expand Down
24 changes: 12 additions & 12 deletions crates/consensus/src/transaction/eip2930.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,18 +225,6 @@ impl TxEip2930 {
}

impl Transaction for TxEip2930 {
fn input(&self) -> &[u8] {
&self.input
}

fn to(&self) -> TxKind {
self.to
}

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

fn chain_id(&self) -> Option<ChainId> {
Some(self.chain_id)
}
Expand All @@ -252,6 +240,18 @@ impl Transaction for TxEip2930 {
fn gas_price(&self) -> Option<u128> {
Some(self.gas_price)
}

fn to(&self) -> TxKind {
self.to
}

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

fn input(&self) -> &[u8] {
&self.input
}
}

impl SignableTransaction<Signature> for TxEip2930 {
Expand Down
8 changes: 4 additions & 4 deletions crates/consensus/src/transaction/eip4844.rs
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,10 @@ impl Transaction for TxEip4844WithSidecar {
self.tx.chain_id()
}

fn nonce(&self) -> u64 {
self.tx.nonce()
}

fn gas_limit(&self) -> u128 {
self.tx.gas_limit()
}
Expand All @@ -876,10 +880,6 @@ impl Transaction for TxEip4844WithSidecar {
self.tx.gas_price()
}

fn nonce(&self) -> u64 {
self.tx.nonce()
}

fn to(&self) -> TxKind {
self.tx.to()
}
Expand Down
24 changes: 12 additions & 12 deletions crates/consensus/src/transaction/legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,18 +198,6 @@ impl TxLegacy {
}

impl Transaction for TxLegacy {
fn input(&self) -> &[u8] {
&self.input
}

fn to(&self) -> TxKind {
self.to
}

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

fn chain_id(&self) -> Option<ChainId> {
self.chain_id
}
Expand All @@ -225,6 +213,18 @@ impl Transaction for TxLegacy {
fn gas_price(&self) -> Option<u128> {
Some(self.gas_price)
}

fn to(&self) -> TxKind {
self.to
}

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

fn input(&self) -> &[u8] {
&self.input
}
}

impl SignableTransaction<Signature> for TxLegacy {
Expand Down
18 changes: 9 additions & 9 deletions crates/consensus/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,6 @@ pub use typed::TypedTransaction;
/// Represents a minimal EVM transaction.
#[doc(alias = "Tx")]
pub trait Transaction: any::Any + Send + Sync + 'static {
/// Get `data`.
fn input(&self) -> &[u8];

/// Get `to`.
fn to(&self) -> TxKind;

/// Get `value`.
fn value(&self) -> U256;

/// Get `chain_id`.
fn chain_id(&self) -> Option<ChainId>;

Expand All @@ -56,6 +47,15 @@ pub trait Transaction: any::Any + Send + Sync + 'static {

/// Get `gas_price`.
fn gas_price(&self) -> Option<u128>;

/// Get `to`.
fn to(&self) -> TxKind;

/// Get `value`.
fn value(&self) -> U256;

/// Get `data`.
fn input(&self) -> &[u8];
}

/// A signable transaction.
Expand Down
4 changes: 2 additions & 2 deletions crates/consensus/src/transaction/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
transaction::eip4844::{TxEip4844, TxEip4844Variant, TxEip4844WithSidecar},
Transaction, TxEip1559, TxEip2930, TxEnvelope, TxLegacy, TxType,
};
use alloy_primitives::TxKind;
use alloy_primitives::{ChainId, TxKind};

/// The TypedTransaction enum represents all Ethereum transaction request types.
///
Expand Down Expand Up @@ -115,7 +115,7 @@ impl TypedTransaction {
}

impl Transaction for TypedTransaction {
fn chain_id(&self) -> Option<alloy_primitives::ChainId> {
fn chain_id(&self) -> Option<ChainId> {
match self {
Self::Legacy(tx) => tx.chain_id(),
Self::Eip2930(tx) => tx.chain_id(),
Expand Down
8 changes: 4 additions & 4 deletions crates/eips/src/eip1898.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use serde::{
#[cfg_attr(feature = "serde", serde(rename = "camelCase"))]
pub struct RpcBlockHash {
/// A block hash
pub block_hash: B256,
pub block_hash: BlockHash,
/// Whether the block must be a canonical block
pub require_canonical: Option<bool>,
}
Expand Down Expand Up @@ -273,7 +273,7 @@ pub enum BlockId {

impl BlockId {
/// Returns the block hash if it is [BlockId::Hash]
pub const fn as_block_hash(&self) -> Option<B256> {
pub const fn as_block_hash(&self) -> Option<BlockHash> {
match self {
Self::Hash(hash) => Some(hash.block_hash),
Self::Number(_) => None,
Expand Down Expand Up @@ -345,12 +345,12 @@ impl BlockId {
}

/// Create a new block hash instance.
pub const fn hash(block_hash: B256) -> Self {
pub const fn hash(block_hash: BlockHash) -> Self {
Self::Hash(RpcBlockHash { block_hash, require_canonical: None })
}

/// Create a new block hash instance that requires the block to be canonical.
pub const fn hash_canonical(block_hash: B256) -> Self {
pub const fn hash_canonical(block_hash: BlockHash) -> Self {
Self::Hash(RpcBlockHash { block_hash, require_canonical: Some(true) })
}
}
Expand Down
20 changes: 10 additions & 10 deletions crates/network/src/any/builder.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use std::ops::{Deref, DerefMut};

use alloy_consensus::BlobTransactionSidecar;
use alloy_primitives::Bytes;
use alloy_primitives::{Address, Bytes, ChainId, TxKind, U256};
use alloy_rpc_types_eth::{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> {
fn chain_id(&self) -> Option<ChainId> {
self.deref().chain_id()
}

fn set_chain_id(&mut self, chain_id: alloy_primitives::ChainId) {
fn set_chain_id(&mut self, chain_id: ChainId) {
self.deref_mut().set_chain_id(chain_id)
}

Expand All @@ -23,39 +23,39 @@ impl TransactionBuilder<AnyNetwork> for WithOtherFields<TransactionRequest> {
self.deref_mut().set_nonce(nonce)
}

fn input(&self) -> Option<&alloy_primitives::Bytes> {
fn input(&self) -> Option<&Bytes> {
self.deref().input()
}

fn set_input<T: Into<Bytes>>(&mut self, input: T) {
self.deref_mut().set_input(input);
}

fn from(&self) -> Option<alloy_primitives::Address> {
fn from(&self) -> Option<Address> {
self.deref().from()
}

fn set_from(&mut self, from: alloy_primitives::Address) {
fn set_from(&mut self, from: Address) {
self.deref_mut().set_from(from);
}

fn kind(&self) -> Option<alloy_primitives::TxKind> {
fn kind(&self) -> Option<TxKind> {
self.deref().kind()
}

fn clear_kind(&mut self) {
self.deref_mut().clear_kind()
}

fn set_kind(&mut self, kind: alloy_primitives::TxKind) {
fn set_kind(&mut self, kind: TxKind) {
self.deref_mut().set_kind(kind)
}

fn value(&self) -> Option<alloy_primitives::U256> {
fn value(&self) -> Option<U256> {
self.deref().value()
}

fn set_value(&mut self, value: alloy_primitives::U256) {
fn set_value(&mut self, value: U256) {
self.deref_mut().set_value(value)
}

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 @@ -9,7 +9,7 @@
use alloy_consensus::TxReceipt;
use alloy_eips::eip2718::{Eip2718Envelope, Eip2718Error};
use alloy_json_rpc::RpcObject;
use alloy_primitives::{Address, B256, U256};
use alloy_primitives::{Address, TxHash, U256};
use core::fmt::{Debug, Display};

mod transaction;
Expand Down Expand Up @@ -44,7 +44,7 @@ pub trait ReceiptResponse {
pub trait TransactionResponse {
/// Hash of the transaction
#[doc(alias = "transaction_hash")]
fn tx_hash(&self) -> B256;
fn tx_hash(&self) -> TxHash;

/// Sender of the transaction
fn from(&self) -> Address;
Expand Down
8 changes: 4 additions & 4 deletions crates/network/src/transaction/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub trait TransactionBuilder<N: Network>: Default + Sized + Send + Sync + 'stati
fn set_chain_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 {
fn with_chain_id(mut self, chain_id: ChainId) -> Self {
self.set_chain_id(chain_id);
self
}
Expand Down Expand Up @@ -101,16 +101,16 @@ pub trait TransactionBuilder<N: Network>: Default + Sized + Send + Sync + 'stati
}

/// Get the kind of transaction.
fn kind(&self) -> Option<alloy_primitives::TxKind>;
fn kind(&self) -> Option<TxKind>;

/// Clear the kind of transaction.
fn clear_kind(&mut self);

/// Set the kind of transaction.
fn set_kind(&mut self, kind: alloy_primitives::TxKind);
fn set_kind(&mut self, kind: TxKind);

/// Builder-pattern method for setting the kind of transaction.
fn with_kind(mut self, kind: alloy_primitives::TxKind) -> Self {
fn with_kind(mut self, kind: TxKind) -> Self {
self.set_kind(kind);
self
}
Expand Down
10 changes: 5 additions & 5 deletions crates/node-bindings/src/anvil.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Utilities for launching an Anvil instance.

use alloy_primitives::{hex, Address};
use alloy_primitives::{hex, Address, ChainId};
use k256::{ecdsa::SigningKey, SecretKey as K256SecretKey};
use std::{
io::{BufRead, BufReader},
Expand All @@ -25,7 +25,7 @@ pub struct AnvilInstance {
private_keys: Vec<K256SecretKey>,
addresses: Vec<Address>,
port: u16,
chain_id: Option<u64>,
chain_id: Option<ChainId>,
}

impl AnvilInstance {
Expand Down Expand Up @@ -55,8 +55,8 @@ impl AnvilInstance {
}

/// Returns the chain of the anvil instance
pub fn chain_id(&self) -> u64 {
const ANVIL_HARDHAT_CHAIN_ID: u64 = 31_337;
pub fn chain_id(&self) -> ChainId {
const ANVIL_HARDHAT_CHAIN_ID: ChainId = 31_337;
self.chain_id.unwrap_or(ANVIL_HARDHAT_CHAIN_ID)
}

Expand Down Expand Up @@ -150,7 +150,7 @@ pub struct Anvil {
// If the block_time is an integer, f64::to_string() will output without a decimal point
// which allows this to be backwards compatible.
block_time: Option<f64>,
chain_id: Option<u64>,
chain_id: Option<ChainId>,
mnemonic: Option<String>,
fork: Option<String>,
fork_block_number: Option<u64>,
Expand Down
Loading