From 607692cc8f812d185e82769178cb85f2bcd9b181 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Mon, 23 Dec 2024 11:54:58 +0100 Subject: [PATCH] feat: add cost fn for conditional opts (#1823) --- crates/rpc-types-eth/src/erc4337.rs | 43 +++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/crates/rpc-types-eth/src/erc4337.rs b/crates/rpc-types-eth/src/erc4337.rs index 6947e3ef234..c359d031215 100644 --- a/crates/rpc-types-eth/src/erc4337.rs +++ b/crates/rpc-types-eth/src/erc4337.rs @@ -62,6 +62,34 @@ pub struct ConditionalOptions { pub timestamp_max: Option, } +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. /// /// Allows for a user to express their preference of a known prestate at a particular account. Only @@ -79,6 +107,21 @@ pub enum AccountStorage { Slots(HashMap), } +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> { + 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))]