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

feat(types): modify types to use native u128 where possible #790

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 2 additions & 8 deletions crates/provider/src/alloy/entry_point/v0_6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,10 +314,7 @@ where
user_op: UserOperation,
max_validation_gas: u128,
) -> ProviderResult<(TransactionRequest, StateOverride)> {
let pvg: u128 = user_op
.pre_verification_gas
.uint_try_to()
.context("pre verification gas out of bounds")?;
let pvg: u128 = user_op.pre_verification_gas;
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: we do not need the explicit type definition if its defined at the UO struct

let call = self
.i_entry_point
.simulateValidation(user_op.into())
Expand All @@ -332,10 +329,7 @@ where
max_validation_gas: u128,
block_id: Option<BlockId>,
) -> ProviderResult<Result<ValidationOutput, ValidationRevert>> {
let pvg: u128 = user_op
.pre_verification_gas
.uint_try_to()
.context("pre verification gas out of bounds")?;
let pvg: u128 = user_op.pre_verification_gas;
Copy link
Collaborator

Choose a reason for hiding this comment

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

same here

let blockless = self
.i_entry_point
.simulateValidation(user_op.into())
Expand Down
5 changes: 1 addition & 4 deletions crates/provider/src/alloy/entry_point/v0_7.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,10 +306,7 @@ where
max_validation_gas: u128,
) -> ProviderResult<(TransactionRequest, StateOverride)> {
let addr = *self.i_entry_point.address();
let pvg: u128 = user_op
.pre_verification_gas
.uint_try_to()
.context("pre verification gas out of bounds")?;
let pvg = user_op.pre_verification_gas;
let mut override_ep = StateOverride::default();
add_simulations_override(&mut override_ep, addr);

Expand Down
42 changes: 21 additions & 21 deletions crates/types/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

use std::str::FromStr;

use alloy_primitives::{Address, U256};
use alloy_primitives::Address;
use serde::{Deserialize, Serialize};

const ENTRY_POINT_ADDRESS_V6_0: &str = "0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789";
Expand All @@ -40,24 +40,24 @@ pub struct ChainSpec {
///
/// NOTE: This must take into account when the storage slot was originally 0
/// and is now non-zero, making the overhead slightly higher for most operations.
pub deposit_transfer_overhead: U256,
pub deposit_transfer_overhead: u128,
/// The maximum size of a transaction in bytes
pub max_transaction_size_bytes: usize,
/// Intrinsic gas cost for a transaction
pub transaction_intrinsic_gas: U256,
pub transaction_intrinsic_gas: u128,
/// Per user operation gas cost for v0.6
pub per_user_op_v0_6_gas: U256,
pub per_user_op_v0_6_gas: u128,
/// Per user operation gas cost for v0.7
pub per_user_op_v0_7_gas: U256,
pub per_user_op_v0_7_gas: u128,
/// Per user operation deploy gas cost overhead, to capture
/// deploy costs that are not metered by the entry point
pub per_user_op_deploy_overhead_gas: U256,
pub per_user_op_deploy_overhead_gas: u128,
/// Gas cost for a user operation word in a bundle transaction
pub per_user_op_word_gas: U256,
pub per_user_op_word_gas: u128,
/// Gas cost for a zero byte in calldata
pub calldata_zero_byte_gas: U256,
pub calldata_zero_byte_gas: u128,
/// Gas cost for a non-zero byte in calldata
pub calldata_non_zero_byte_gas: U256,
pub calldata_non_zero_byte_gas: u128,

/*
* Gas estimation
Expand All @@ -81,9 +81,9 @@ pub struct ChainSpec {
/// Type of oracle for estimating priority fees
pub priority_fee_oracle_type: PriorityFeeOracleType,
/// Minimum max priority fee per gas for the network
pub min_max_priority_fee_per_gas: U256,
pub min_max_priority_fee_per_gas: u128,
/// Maximum max priority fee per gas for the network
pub max_max_priority_fee_per_gas: U256,
pub max_max_priority_fee_per_gas: u128,
/// Usage ratio of the chain that determines "congestion"
/// Some chains have artificially high block gas limits but
/// actually cap block gas usage at a lower value.
Expand Down Expand Up @@ -149,22 +149,22 @@ impl Default for ChainSpec {
id: 0,
entry_point_address_v0_6: Address::from_str(ENTRY_POINT_ADDRESS_V6_0).unwrap(),
entry_point_address_v0_7: Address::from_str(ENTRY_POINT_ADDRESS_V7_0).unwrap(),
deposit_transfer_overhead: U256::from(30_000),
transaction_intrinsic_gas: U256::from(21_000),
per_user_op_v0_6_gas: U256::from(18_300),
per_user_op_v0_7_gas: U256::from(19_500),
per_user_op_deploy_overhead_gas: U256::from(0),
per_user_op_word_gas: U256::from(4),
calldata_zero_byte_gas: U256::from(4),
calldata_non_zero_byte_gas: U256::from(16),
deposit_transfer_overhead: 30_000,
transaction_intrinsic_gas: 21_000,
per_user_op_v0_6_gas: 18_300,
per_user_op_v0_7_gas: 19_500,
per_user_op_deploy_overhead_gas: 0,
per_user_op_word_gas: 4,
calldata_zero_byte_gas: 4,
calldata_non_zero_byte_gas: 16,
eip1559_enabled: true,
calldata_pre_verification_gas: false,
l1_gas_oracle_contract_type: L1GasOracleContractType::default(),
l1_gas_oracle_contract_address: Address::ZERO,
include_l1_gas_in_gas_limit: true,
priority_fee_oracle_type: PriorityFeeOracleType::default(),
min_max_priority_fee_per_gas: U256::ZERO,
max_max_priority_fee_per_gas: U256::MAX,
min_max_priority_fee_per_gas: 0,
max_max_priority_fee_per_gas: u128::MAX,
congestion_trigger_usage_ratio_threshold: 0.75,
max_transaction_size_bytes: 131072, // 128 KiB
bundle_max_send_interval_millis: u64::MAX,
Expand Down
62 changes: 39 additions & 23 deletions crates/types/src/user_operation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use std::{fmt::Debug, time::Duration};

use alloy_primitives::{Address, Bytes, B256, U256};
use alloy_sol_types::SolValue;
use rand::{self, RngCore};

/// User Operation types for Entry Point v0.6
pub mod v0_6;
Expand Down Expand Up @@ -89,19 +90,19 @@ pub trait UserOperation: Debug + Clone + Send + Sync + 'static {
fn call_data(&self) -> &Bytes;

/// Returns the call gas limit
fn call_gas_limit(&self) -> U256;
fn call_gas_limit(&self) -> u128;

/// Returns the verification gas limit
fn verification_gas_limit(&self) -> U256;
fn verification_gas_limit(&self) -> u128;

/// Returns the max fee per gas
fn max_fee_per_gas(&self) -> U256;
fn max_fee_per_gas(&self) -> u128;

/// Returns the max priority fee per gas
fn max_priority_fee_per_gas(&self) -> U256;
fn max_priority_fee_per_gas(&self) -> u128;

/// Returns the maximum cost, in wei, of this user operation
fn max_gas_cost(&self) -> U256;
fn max_gas_cost(&self) -> u128;

/*
* Enhanced functions
Expand All @@ -123,24 +124,24 @@ pub trait UserOperation: Debug + Clone + Send + Sync + 'static {
fn heap_size(&self) -> usize;

/// Returns the total verification gas limit
fn total_verification_gas_limit(&self) -> U256;
fn total_verification_gas_limit(&self) -> u128;

/// Returns the required pre-execution buffer
///
/// This should capture all of the gas that is needed to execute the user operation,
/// minus the call gas limit. The entry point will check for this buffer before
/// executing the user operation.
fn required_pre_execution_buffer(&self) -> U256;
fn required_pre_execution_buffer(&self) -> u128;

/// Returns the pre-verification gas
fn pre_verification_gas(&self) -> U256;
fn pre_verification_gas(&self) -> u128;

/// Calculate the static portion of the pre-verification gas for this user operation
fn calc_static_pre_verification_gas(
&self,
chain_spec: &ChainSpec,
include_fixed_gas_overhead: bool,
) -> U256;
) -> u128;

/// Clear the signature field of the user op
///
Expand Down Expand Up @@ -221,7 +222,7 @@ impl UserOperation for UserOperationVariant {
}
}

fn max_gas_cost(&self) -> U256 {
fn max_gas_cost(&self) -> u128 {
match self {
UserOperationVariant::V0_6(op) => op.max_gas_cost(),
UserOperationVariant::V0_7(op) => op.max_gas_cost(),
Expand All @@ -242,35 +243,35 @@ impl UserOperation for UserOperationVariant {
}
}

fn call_gas_limit(&self) -> U256 {
fn call_gas_limit(&self) -> u128 {
match self {
UserOperationVariant::V0_6(op) => op.call_gas_limit(),
UserOperationVariant::V0_7(op) => op.call_gas_limit(),
}
}

fn verification_gas_limit(&self) -> U256 {
fn verification_gas_limit(&self) -> u128 {
match self {
UserOperationVariant::V0_6(op) => op.verification_gas_limit(),
UserOperationVariant::V0_7(op) => op.verification_gas_limit(),
}
}

fn total_verification_gas_limit(&self) -> U256 {
fn total_verification_gas_limit(&self) -> u128 {
match self {
UserOperationVariant::V0_6(op) => op.total_verification_gas_limit(),
UserOperationVariant::V0_7(op) => op.total_verification_gas_limit(),
}
}

fn required_pre_execution_buffer(&self) -> U256 {
fn required_pre_execution_buffer(&self) -> u128 {
match self {
UserOperationVariant::V0_6(op) => op.required_pre_execution_buffer(),
UserOperationVariant::V0_7(op) => op.required_pre_execution_buffer(),
}
}

fn pre_verification_gas(&self) -> U256 {
fn pre_verification_gas(&self) -> u128 {
match self {
UserOperationVariant::V0_6(op) => op.pre_verification_gas(),
UserOperationVariant::V0_7(op) => op.pre_verification_gas(),
Expand All @@ -281,7 +282,7 @@ impl UserOperation for UserOperationVariant {
&self,
chain_spec: &ChainSpec,
include_fixed_gas_overhead: bool,
) -> U256 {
) -> u128 {
match self {
UserOperationVariant::V0_6(op) => {
op.calc_static_pre_verification_gas(chain_spec, include_fixed_gas_overhead)
Expand All @@ -292,14 +293,14 @@ impl UserOperation for UserOperationVariant {
}
}

fn max_fee_per_gas(&self) -> U256 {
fn max_fee_per_gas(&self) -> u128 {
match self {
UserOperationVariant::V0_6(op) => op.max_fee_per_gas(),
UserOperationVariant::V0_7(op) => op.max_fee_per_gas(),
}
}

fn max_priority_fee_per_gas(&self) -> U256 {
fn max_priority_fee_per_gas(&self) -> u128 {
match self {
UserOperationVariant::V0_6(op) => op.max_priority_fee_per_gas(),
UserOperationVariant::V0_7(op) => op.max_priority_fee_per_gas(),
Expand Down Expand Up @@ -395,12 +396,12 @@ pub struct UserOpsPerAggregator<UO: UserOperation> {

pub(crate) fn op_calldata_gas_cost<UO: SolValue>(
uo: UO,
zero_byte_cost: U256,
non_zero_byte_cost: U256,
per_word_cost: U256,
) -> U256 {
zero_byte_cost: u128,
non_zero_byte_cost: u128,
per_word_cost: u128,
) -> u128 {
let encoded_op = uo.abi_encode();
let length_in_words = U256::from((encoded_op.len() + 31) >> 5); // ceil(encoded_op.len() / 32)
let length_in_words: u128 = (encoded_op.len() as u128 + 31) >> 5; // ceil(encoded_op.len() / 32)
let call_data_cost = encoded_op
.iter()
.map(|&x| {
Expand Down Expand Up @@ -430,6 +431,21 @@ pub(crate) fn default_if_none_or_equal<V: Copy + PartialEq>(
v.filter(|v| v != &equal).unwrap_or(default)
}

/// Fills a bytes array of size ARR_SIZE with FILL_SIZE random bytes starting
/// at the beginning
fn random_bytes_array<const ARR_SIZE: usize, const FILL_SIZE: usize>() -> [u8; ARR_SIZE] {
let mut bytes = [0_u8; ARR_SIZE];
rand::thread_rng().fill_bytes(&mut bytes[..FILL_SIZE]);
bytes
}

/// Fills a bytes object with fill_size random bytes
fn random_bytes(fill_size: usize) -> Bytes {
let mut bytes = vec![0_u8; fill_size];
rand::thread_rng().fill_bytes(&mut bytes);
bytes.into()
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
Loading
Loading