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

Numeric type audit: network, consensus, provider, rpc-types #454

Merged
merged 13 commits into from
Apr 5, 2024
8 changes: 4 additions & 4 deletions crates/consensus/src/transaction/eip1559.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ pub struct TxEip1559 {
/// this transaction. This is paid up-front, before any
/// computation is done and may not be increased
/// later; formally Tg.
#[cfg_attr(feature = "serde", serde(with = "alloy_serde::u64_hex_or_decimal"))]
pub gas_limit: u64,
#[cfg_attr(feature = "serde", serde(with = "alloy_serde::u128_hex_or_decimal"))]
pub gas_limit: u128,
/// 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 @@ -273,11 +273,11 @@ impl Transaction for TxEip1559 {
self.nonce
}

fn gas_limit(&self) -> u64 {
fn gas_limit(&self) -> u128 {
self.gas_limit
}

fn gas_price(&self) -> Option<U256> {
fn gas_price(&self) -> Option<u128> {
None
}
}
Expand Down
10 changes: 5 additions & 5 deletions crates/consensus/src/transaction/eip2930.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ pub struct TxEip2930 {
/// this transaction. This is paid up-front, before any
/// computation is done and may not be increased
/// later; formally Tg.
#[cfg_attr(feature = "serde", serde(with = "alloy_serde::u64_hex_or_decimal"))]
pub gas_limit: u64,
#[cfg_attr(feature = "serde", serde(with = "alloy_serde::u128_hex_or_decimal"))]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do any endpoints use decimal here? in other PRs we've avoided allowing decimal when nobody actually uses it

pub gas_limit: u128,
/// The 160-bit address of the message call’s recipient or, for a contract creation
/// transaction, ∅, used here to denote the only member of B0 ; formally Tt.
#[cfg_attr(feature = "serde", serde(default, skip_serializing_if = "TxKind::is_create"))]
Expand Down Expand Up @@ -237,12 +237,12 @@ impl Transaction for TxEip2930 {
self.nonce
}

fn gas_limit(&self) -> u64 {
fn gas_limit(&self) -> u128 {
self.gas_limit
}

fn gas_price(&self) -> Option<U256> {
Some(U256::from(self.gas_price))
fn gas_price(&self) -> Option<u128> {
Some(self.gas_price)
}
}

Expand Down
16 changes: 8 additions & 8 deletions crates/consensus/src/transaction/eip4844.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,14 +232,14 @@ impl Transaction for TxEip4844Variant {
}
}

fn gas_limit(&self) -> u64 {
fn gas_limit(&self) -> u128 {
match self {
TxEip4844Variant::TxEip4844(tx) => tx.gas_limit,
TxEip4844Variant::TxEip4844WithSidecar(tx) => tx.tx().gas_limit,
}
}

fn gas_price(&self) -> Option<U256> {
fn gas_price(&self) -> Option<u128> {
None
}

Expand Down Expand Up @@ -333,8 +333,8 @@ pub struct TxEip4844 {
/// this transaction. This is paid up-front, before any
/// computation is done and may not be increased
/// later; formally Tg.
#[cfg_attr(feature = "serde", serde(with = "alloy_serde::u64_hex_or_decimal"))]
pub gas_limit: u64,
#[cfg_attr(feature = "serde", serde(with = "alloy_serde::u128_hex_or_decimal"))]
pub gas_limit: u128,
/// 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 @@ -716,11 +716,11 @@ impl Transaction for TxEip4844 {
self.nonce
}

fn gas_limit(&self) -> u64 {
fn gas_limit(&self) -> u128 {
self.gas_limit
}

fn gas_price(&self) -> Option<U256> {
fn gas_price(&self) -> Option<u128> {
None
}
}
Expand Down Expand Up @@ -931,11 +931,11 @@ impl Transaction for TxEip4844WithSidecar {
self.tx.chain_id()
}

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

fn gas_price(&self) -> Option<U256> {
fn gas_price(&self) -> Option<u128> {
self.tx.gas_price()
}

Expand Down
2 changes: 1 addition & 1 deletion crates/consensus/src/transaction/envelope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ mod tests {
chain_id: u64::MAX,
nonce: u64::MAX,
gas_price: u128::MAX,
gas_limit: u64::MAX,
gas_limit: u128::MAX,
to: TxKind::Call(Address::random()),
value: U256::MAX,
input: Bytes::new(),
Expand Down
10 changes: 5 additions & 5 deletions crates/consensus/src/transaction/legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ pub struct TxLegacy {
/// this transaction. This is paid up-front, before any
/// computation is done and may not be increased
/// later; formally Tg.
#[cfg_attr(feature = "serde", serde(with = "alloy_serde::u64_hex_or_decimal"))]
pub gas_limit: u64,
#[cfg_attr(feature = "serde", serde(with = "alloy_serde::u128_hex_or_decimal"))]
pub gas_limit: u128,
/// The 160-bit address of the message call’s recipient or, for a contract creation
/// transaction, ∅, used here to denote the only member of B0 ; formally Tt.
#[cfg_attr(feature = "serde", serde(default, skip_serializing_if = "TxKind::is_create"))]
Expand Down Expand Up @@ -213,12 +213,12 @@ impl Transaction for TxLegacy {
self.nonce
}

fn gas_limit(&self) -> u64 {
fn gas_limit(&self) -> u128 {
self.gas_limit
}

fn gas_price(&self) -> Option<U256> {
Some(U256::from(self.gas_price))
fn gas_price(&self) -> Option<u128> {
Some(self.gas_price)
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/consensus/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ pub trait Transaction: std::any::Any + Send + Sync + 'static {
fn nonce(&self) -> u64;

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

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

/// 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 @@ -96,7 +96,7 @@ impl Transaction for TypedTransaction {
}
}

fn gas_limit(&self) -> u64 {
fn gas_limit(&self) -> u128 {
match self {
Self::Legacy(tx) => tx.gas_limit(),
Self::Eip2930(tx) => tx.gas_limit(),
Expand All @@ -105,7 +105,7 @@ impl Transaction for TypedTransaction {
}
}

fn gas_price(&self) -> Option<alloy_primitives::U256> {
fn gas_price(&self) -> Option<u128> {
match self {
Self::Legacy(tx) => tx.gas_price(),
Self::Eip2930(tx) => tx.gas_price(),
Expand Down
4 changes: 2 additions & 2 deletions crates/contract/src/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,15 +286,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: U256) -> Self {
pub fn gas(mut self, gas: u128) -> Self {
self.request.set_gas_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: U256) -> Self {
pub fn gas_price(mut self, gas_price: u128) -> Self {
self.request.set_gas_price(gas_price);
self
}
Expand Down
21 changes: 10 additions & 11 deletions crates/network/src/any/builder.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::ops::{Deref, DerefMut};

use alloy_consensus::BlobTransactionSidecar;
use alloy_primitives::U256;
use alloy_rpc_types::{TransactionRequest, WithOtherFields};

use crate::{
Expand Down Expand Up @@ -57,43 +56,43 @@ impl TransactionBuilder<AnyNetwork> for WithOtherFields<TransactionRequest> {
self.deref_mut().set_value(value)
}

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

fn set_gas_price(&mut self, gas_price: U256) {
fn set_gas_price(&mut self, gas_price: u128) {
self.deref_mut().set_gas_price(gas_price);
}

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

fn set_max_fee_per_gas(&mut self, max_fee_per_gas: U256) {
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);
}

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

fn set_max_priority_fee_per_gas(&mut self, max_priority_fee_per_gas: U256) {
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);
}

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

fn set_max_fee_per_blob_gas(&mut self, max_fee_per_blob_gas: U256) {
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<U256> {
fn gas_limit(&self) -> Option<u128> {
self.deref().gas_limit()
}

fn set_gas_limit(&mut self, gas_limit: U256) {
fn set_gas_limit(&mut self, gas_limit: u128) {
self.deref_mut().set_gas_limit(gas_limit);
}

Expand Down
63 changes: 22 additions & 41 deletions crates/network/src/ethereum/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
use alloy_consensus::{
BlobTransactionSidecar, TxEip1559, TxEip2930, TxEip4844, TxEip4844Variant, TxLegacy,
};
use alloy_primitives::{Address, TxKind, U256};
use alloy_primitives::{Address, TxKind};
use alloy_rpc_types::request::TransactionRequest;

impl TransactionBuilder<Ethereum> for alloy_rpc_types::TransactionRequest {
Expand Down Expand Up @@ -59,43 +59,43 @@ impl TransactionBuilder<Ethereum> for alloy_rpc_types::TransactionRequest {
self.value = Some(value)
}

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

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

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

fn set_max_fee_per_gas(&mut self, max_fee_per_gas: U256) {
fn set_max_fee_per_gas(&mut self, max_fee_per_gas: u128) {
self.max_fee_per_gas = Some(max_fee_per_gas);
}

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

fn set_max_priority_fee_per_gas(&mut self, max_priority_fee_per_gas: U256) {
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);
}

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

fn set_max_fee_per_blob_gas(&mut self, max_fee_per_blob_gas: U256) {
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<U256> {
fn gas_limit(&self) -> Option<u128> {
self.gas
}

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

Expand Down Expand Up @@ -158,12 +158,8 @@ fn build_legacy(request: TransactionRequest) -> Result<TxLegacy, TransactionBuil
nonce: request.nonce.ok_or_else(|| TransactionBuilderError::MissingKey("nonce"))?,
gas_price: request
.gas_price
.ok_or_else(|| TransactionBuilderError::MissingKey("gas_price"))?
.to(),
gas_limit: request
.gas
.ok_or_else(|| TransactionBuilderError::MissingKey("gas_limit"))?
.to(),
.ok_or_else(|| TransactionBuilderError::MissingKey("gas_price"))?,
gas_limit: request.gas.ok_or_else(|| TransactionBuilderError::MissingKey("gas_limit"))?,
to: request.to.into(),
value: request.value.unwrap_or_default(),
input: request.input.into_input().unwrap_or_default(),
Expand All @@ -177,16 +173,11 @@ fn build_1559(request: TransactionRequest) -> Result<TxEip1559, TransactionBuild
nonce: request.nonce.ok_or_else(|| TransactionBuilderError::MissingKey("nonce"))?,
max_priority_fee_per_gas: request
.max_priority_fee_per_gas
.ok_or_else(|| TransactionBuilderError::MissingKey("max_priority_fee_per_gas"))?
.to(),
.ok_or_else(|| TransactionBuilderError::MissingKey("max_priority_fee_per_gas"))?,
max_fee_per_gas: request
.max_fee_per_gas
.ok_or_else(|| TransactionBuilderError::MissingKey("max_fee_per_gas"))?
.to(),
gas_limit: request
.gas
.ok_or_else(|| TransactionBuilderError::MissingKey("gas_limit"))?
.to(),
.ok_or_else(|| TransactionBuilderError::MissingKey("max_fee_per_gas"))?,
gas_limit: request.gas.ok_or_else(|| TransactionBuilderError::MissingKey("gas_limit"))?,
to: request.to.into(),
value: request.value.unwrap_or_default(),
input: request.input.into_input().unwrap_or_default(),
Expand All @@ -201,12 +192,8 @@ fn build_2930(request: TransactionRequest) -> Result<TxEip2930, TransactionBuild
nonce: request.nonce.ok_or_else(|| TransactionBuilderError::MissingKey("nonce"))?,
gas_price: request
.gas_price
.ok_or_else(|| TransactionBuilderError::MissingKey("gas_price"))?
.to(),
gas_limit: request
.gas
.ok_or_else(|| TransactionBuilderError::MissingKey("gas_limit"))?
.to(),
.ok_or_else(|| TransactionBuilderError::MissingKey("gas_price"))?,
gas_limit: request.gas.ok_or_else(|| TransactionBuilderError::MissingKey("gas_limit"))?,
to: request.to.into(),
value: request.value.unwrap_or_default(),
input: request.input.into_input().unwrap_or_default(),
Expand All @@ -219,18 +206,13 @@ fn build_4844(request: TransactionRequest) -> Result<TxEip4844, TransactionBuild
Ok(TxEip4844 {
chain_id: request.chain_id.unwrap_or(1),
nonce: request.nonce.ok_or_else(|| TransactionBuilderError::MissingKey("nonce"))?,
gas_limit: request
.gas
.ok_or_else(|| TransactionBuilderError::MissingKey("gas_limit"))?
.to(),
gas_limit: request.gas.ok_or_else(|| TransactionBuilderError::MissingKey("gas_limit"))?,
max_fee_per_gas: request
.max_fee_per_gas
.ok_or_else(|| TransactionBuilderError::MissingKey("max_fee_per_gas"))?
.to(),
.ok_or_else(|| TransactionBuilderError::MissingKey("max_fee_per_gas"))?,
max_priority_fee_per_gas: request
.max_priority_fee_per_gas
.ok_or_else(|| TransactionBuilderError::MissingKey("max_priority_fee_per_gas"))?
.to(),
.ok_or_else(|| TransactionBuilderError::MissingKey("max_priority_fee_per_gas"))?,
to: request.to.ok_or_else(|| TransactionBuilderError::MissingKey("to"))?,
value: request.value.unwrap_or_default(),
access_list: request.access_list.unwrap_or_default(),
Expand All @@ -239,8 +221,7 @@ fn build_4844(request: TransactionRequest) -> Result<TxEip4844, TransactionBuild
.ok_or_else(|| TransactionBuilderError::MissingKey("blob_versioned_hashes"))?,
max_fee_per_blob_gas: request
.max_fee_per_blob_gas
.ok_or_else(|| TransactionBuilderError::MissingKey("max_fee_per_blob_gas"))?
.to(),
.ok_or_else(|| TransactionBuilderError::MissingKey("max_fee_per_blob_gas"))?,
input: request.input.into_input().unwrap_or_default(),
})
}
Loading
Loading