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 10 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
32 changes: 16 additions & 16 deletions crates/consensus/src/transaction/eip1559.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{SignableTransaction, Signed, Transaction, TxType};
use alloy_eips::eip2930::AccessList;
use alloy_primitives::{keccak256, Bytes, ChainId, Signature, TxKind, U256};
use alloy_primitives::{aliases::TxNonce, keccak256, Bytes, ChainId, Signature, TxKind, U256};
zerosnacks marked this conversation as resolved.
Show resolved Hide resolved
use alloy_rlp::{BufMut, Decodable, Encodable, Header};
use core::mem;

Expand All @@ -18,7 +18,7 @@ pub struct TxEip1559 {
pub chain_id: ChainId,
/// A scalar value equal to the number of transactions sent by the sender; formally Tn.
#[cfg_attr(feature = "serde", serde(with = "alloy_serde::quantity"))]
pub nonce: u64,
pub nonce: TxNonce,
/// A scalar value equal to the maximum
/// amount of gas that should be used in executing
/// this transaction. This is paid up-front, before any
Expand Down Expand Up @@ -249,7 +249,7 @@ impl TxEip1559 {
#[inline]
pub fn size(&self) -> usize {
mem::size_of::<ChainId>() + // chain_id
mem::size_of::<u64>() + // nonce
mem::size_of::<TxNonce>() + // nonce
mem::size_of::<u64>() + // gas_limit
mem::size_of::<u128>() + // max_fee_per_gas
mem::size_of::<u128>() + // max_priority_fee_per_gas
Expand All @@ -261,23 +261,11 @@ 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)
}

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

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
32 changes: 16 additions & 16 deletions crates/consensus/src/transaction/eip2930.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{SignableTransaction, Signed, Transaction, TxType};
use alloy_eips::eip2930::AccessList;
use alloy_primitives::{keccak256, Bytes, ChainId, Signature, TxKind, U256};
use alloy_primitives::{aliases::TxNonce, keccak256, Bytes, ChainId, Signature, TxKind, U256};
use alloy_rlp::{length_of_length, BufMut, Decodable, Encodable, Header};
use core::mem;

Expand All @@ -18,7 +18,7 @@ pub struct TxEip2930 {
pub chain_id: ChainId,
/// A scalar value equal to the number of transactions sent by the sender; formally Tn.
#[cfg_attr(feature = "serde", serde(with = "alloy_serde::quantity"))]
pub nonce: u64,
pub nonce: TxNonce,
/// A scalar value equal to the number of
/// Wei to be paid per unit of gas for all computation
/// costs incurred as a result of the execution of this transaction; formally Tp.
Expand Down Expand Up @@ -63,7 +63,7 @@ impl TxEip2930 {
#[inline]
pub fn size(&self) -> usize {
mem::size_of::<ChainId>() + // chain_id
mem::size_of::<u64>() + // nonce
mem::size_of::<TxNonce>() + // nonce
mem::size_of::<u128>() + // gas_price
mem::size_of::<u64>() + // gas_limit
self.to.size() + // to
Expand Down Expand Up @@ -225,23 +225,11 @@ 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)
}

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

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
20 changes: 11 additions & 9 deletions crates/consensus/src/transaction/eip4844.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::{SignableTransaction, Signed, Transaction, TxType};

use alloy_eips::{eip2930::AccessList, eip4844::DATA_GAS_PER_BLOB};
use alloy_primitives::{keccak256, Address, Bytes, ChainId, Signature, TxKind, B256, U256};
use alloy_primitives::{
aliases::TxNonce, keccak256, Address, Bytes, ChainId, Signature, TxKind, B256, U256,
};
use alloy_rlp::{length_of_length, BufMut, Decodable, Encodable, Header};
use core::mem;

Expand Down Expand Up @@ -217,7 +219,7 @@ impl Transaction for TxEip4844Variant {
}
}

fn nonce(&self) -> u64 {
fn nonce(&self) -> TxNonce {
match self {
Self::TxEip4844(tx) => tx.nonce,
Self::TxEip4844WithSidecar(tx) => tx.tx().nonce,
Expand Down Expand Up @@ -294,7 +296,7 @@ pub struct TxEip4844 {
pub chain_id: ChainId,
/// A scalar value equal to the number of transactions sent by the sender; formally Tn.
#[cfg_attr(feature = "serde", serde(with = "alloy_serde::quantity"))]
pub nonce: u64,
pub nonce: TxNonce,
/// A scalar value equal to the maximum
/// amount of gas that should be used in executing
/// this transaction. This is paid up-front, before any
Expand Down Expand Up @@ -472,7 +474,7 @@ impl TxEip4844 {
#[inline]
pub fn size(&self) -> usize {
mem::size_of::<ChainId>() + // chain_id
mem::size_of::<u64>() + // nonce
mem::size_of::<TxNonce>() + // nonce
mem::size_of::<u64>() + // gas_limit
mem::size_of::<u128>() + // max_fee_per_gas
mem::size_of::<u128>() + // max_priority_fee_per_gas
Expand Down Expand Up @@ -645,7 +647,7 @@ impl Transaction for TxEip4844 {
Some(self.chain_id)
}

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

Expand Down Expand Up @@ -868,6 +870,10 @@ impl Transaction for TxEip4844WithSidecar {
self.tx.chain_id()
}

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

fn gas_limit(&self) -> u128 {
self.tx.gas_limit()
}
Expand All @@ -876,10 +882,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
28 changes: 14 additions & 14 deletions crates/consensus/src/transaction/legacy.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{SignableTransaction, Signed, Transaction};
use alloy_primitives::{keccak256, Bytes, ChainId, Signature, TxKind, U256};
use alloy_primitives::{aliases::TxNonce, keccak256, Bytes, ChainId, Signature, TxKind, U256};
use alloy_rlp::{length_of_length, BufMut, Decodable, Encodable, Header, Result};
use core::mem;

Expand Down Expand Up @@ -198,23 +198,11 @@ 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
}

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

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
22 changes: 11 additions & 11 deletions crates/consensus/src/transaction/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Transaction types.

use crate::Signed;
use alloy_primitives::{keccak256, ChainId, TxKind, B256, U256};
use alloy_primitives::{aliases::TxNonce, keccak256, ChainId, TxKind, B256, U256};
use core::any;

#[cfg(not(feature = "std"))]
Expand Down Expand Up @@ -36,26 +36,26 @@ 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>;

/// Get `nonce`.
fn nonce(&self) -> u64;
fn nonce(&self) -> TxNonce;

/// Get `gas_limit`.
fn gas_limit(&self) -> u128;

/// 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::{aliases::TxNonce, TxKind};

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

fn nonce(&self) -> u64 {
fn nonce(&self) -> TxNonce {
match self {
Self::Legacy(tx) => tx.nonce(),
Self::Eip2930(tx) => tx.nonce(),
Expand Down
24 changes: 12 additions & 12 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 @@ -119,14 +119,14 @@ impl BlockNumberOrTag {
}

impl From<u64> for BlockNumberOrTag {
fn from(num: u64) -> Self {
Self::Number(num)
fn from(block_number: u64) -> Self {
Self::Number(block_number)
}
}

impl From<U64> for BlockNumberOrTag {
fn from(num: U64) -> Self {
num.to::<u64>().into()
fn from(block_number: U64) -> Self {
block_number.to::<u64>().into()
}
}

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 All @@ -362,8 +362,8 @@ impl Default for BlockId {
}

impl From<u64> for BlockId {
fn from(num: u64) -> Self {
BlockNumberOrTag::Number(num).into()
fn from(block_number: u64) -> Self {
BlockNumberOrTag::Number(block_number).into()
}
}

Expand All @@ -374,8 +374,8 @@ impl From<U64> for BlockId {
}

impl From<BlockNumberOrTag> for BlockId {
fn from(num: BlockNumberOrTag) -> Self {
Self::Number(num)
fn from(block_number: BlockNumberOrTag) -> Self {
Self::Number(block_number)
}
}

Expand Down
Loading
Loading