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: add cost fn for conditional opts #1823

Merged
merged 1 commit into from
Dec 23, 2024
Merged
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
43 changes: 43 additions & 0 deletions crates/rpc-types-eth/src/erc4337.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,34 @@ pub struct ConditionalOptions {
pub timestamp_max: Option<u64>,
}

impl ConditionalOptions {
/// Computes the aggregate cost of the preconditions; total number of storage lookups required
pub fn cost(&self) -> u64 {
let mut cost = 0;
for account in self.known_accounts.values() {
// default cost to handle empty accounts
cost += 1;
match account {
AccountStorage::RootHash(_) => {
cost += 1;
}
AccountStorage::Slots(slots) => {
cost += slots.len() as u64;
}
}
}

if self.block_number_min.is_some() || self.block_number_max.is_some() {
cost += 1;
}
if self.timestamp_min.is_some() || self.timestamp_max.is_some() {
cost += 1;
}

cost
}
}

/// Represents the expected state of an account for a transaction to be conditionally accepted.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
Expand All @@ -45,6 +73,21 @@ pub enum AccountStorage {
Slots(HashMap<U256, B256>),
}

impl AccountStorage {
/// Returns `true` if the account storage is a root hash.
pub const fn is_root(&self) -> bool {
matches!(self, Self::RootHash(_))
}

/// Returns the slot values if the account storage is a slot map.
pub const fn as_slots(&self) -> Option<&HashMap<U256, B256>> {
match self {
Self::Slots(slots) => Some(slots),
_ => None,
}
}
}

/// [`UserOperation`] in the spec: Entry Point V0.6
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
Expand Down
Loading