evm-fork-cache 0.2.1

Forked EVM state cache, snapshots, overlays, and simulation utilities for EVM search
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
//! EIP-2930 access list builder with L2 profitability accounting.
//!
//! The caller supplies the addresses and storage slots to include; this module
//! decides *whether attaching the list is profitable*, not *which slots are
//! interesting* (it carries no protocol-specific slot knowledge). The trade-off
//! is purely economic: pre-declaring an account/slot warms it (cheaper EIP-2929
//! execution) but costs L1 data to post the list. Slots whose serialized form is
//! mostly zero bytes (e.g. small, low-entropy values) are cheap to post; dense,
//! high-entropy 32-byte keys are expensive on L1 — so the value of a given entry
//! depends on its bytes, which is why the include/exclude decision is left to the
//! caller and the profitability check below.
//!
//! On L2, automatically disables itself when L1 fees rise high enough that the
//! L1 data cost exceeds the L2 execution savings. Arbitrum uses `ArbGasInfo`
//! pricing with exact EIP-2930 RLP data gas; OP Stack chains use
//! `GasPriceOracle.getL1Fee(bytes)` to compare whole transactions with and
//! without the access list.
//!
//! On L1 (Ethereum): Access lists always save gas (no L1 data posting overhead),
//! so use `into_access_list_always()` to skip the profitability check.

use alloy_eips::{
    BlockNumberOrTag,
    eip2930::{AccessList, AccessListItem},
};
use alloy_network::Network;
use alloy_primitives::{Address, B256, Bytes, U256, address};
use alloy_provider::Provider;
use alloy_rlp::Encodable;
use alloy_rpc_types_eth::{TransactionInput, TransactionRequest};
use alloy_sol_types::{SolCall, sol};
use revm::context::result::ExecutionResult;
use tracing::{debug, info};

use crate::cache::EvmCache;
use crate::errors::{AccessListError, AccessListResult as Result};

/// Arbitrum ArbGasInfo precompile.
const ARB_GAS_INFO: Address = address!("000000000000000000000000000000000000006C");

/// Optimism GasPriceOracle predeploy (Bedrock+).
///
/// Fixed predeploy address on every OP Stack chain. Queried for the L1 base fee
/// ([`query_l1_base_fee_for_chain`]) and the full Ecotone L1 data fee
/// ([`compute_op_l1_fee`]).
pub const OP_GAS_PRICE_ORACLE: Address = address!("420000000000000000000000000000000000000F");

/// Chain fee model used by helpers that only need to identify the chain's L1
/// base-fee oracle.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ChainType {
    /// Ethereum L1-like chains where access lists do not incur rollup data fees.
    L1,
    /// Arbitrum-style rollups with ArbGasInfo pricing.
    Arbitrum,
    /// OP Stack rollups with GasPriceOracle pricing.
    OpStack,
}

/// Pricing inputs used when deciding whether to include a simulation access list.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AccessListPricing {
    /// Ethereum L1-like chains where access lists do not incur rollup data fees.
    L1,
    /// Arbitrum-style rollups priced through the `ArbGasInfo` precompile.
    Arbitrum,
    /// OP Stack rollups priced by comparing oracle L1 fees for full tx bytes.
    OpStack {
        /// Serialized unsigned transaction bytes without an access list.
        tx_without_access_list: Bytes,
        /// Serialized unsigned transaction bytes with the candidate access list.
        tx_with_access_list: Bytes,
    },
}

sol! {
    #[sol(rpc)]
    interface ArbGasInfo {
        function getPricesInWei() external view returns (
            uint256 perL2Tx,
            uint256 perL1CalldataUnit,
            uint256 perStorageUnit,
            uint256 perArbGas,
            uint256 perL1Surplus,
            uint256 baseFee
        );
        function getL1BaseFeeEstimate() external view returns (uint256);
    }

    #[sol(rpc)]
    interface OpGasPriceOracle {
        function l1BaseFee() external view returns (uint256);
        function getL1Fee(bytes _data) external view returns (uint256);
    }
}

/// An EIP-2930 access list builder with L2 profitability accounting.
///
/// The caller decides which addresses/slots to add (via
/// [`add_address`](Self::add_address) / [`add_storage_key`](Self::add_storage_key));
/// the builder itself applies no per-entry selection. The `into_access_list_*`
/// finalizers decide whether attaching the *whole* list is profitable, comparing
/// its L1 data-posting cost against the L2 warm-access savings.
pub struct SmartAccessList {
    items: Vec<AccessListItem>,
}

impl SmartAccessList {
    /// Create an empty smart access-list builder.
    ///
    /// Populate it with [`SmartAccessList::add_address`] and
    /// [`SmartAccessList::add_storage_key`], then finalize with one of the
    /// `into_access_list_*` methods.
    pub fn new() -> Self {
        Self { items: Vec::new() }
    }

    /// Create a builder from precomputed EIP-2930 items.
    ///
    /// The items are taken as-is; this constructor does not deduplicate
    /// addresses or storage keys (unlike [`SmartAccessList::add_address`] and
    /// [`SmartAccessList::add_storage_key`]). Pass items that are already
    /// distinct, or rely on downstream encoders to fold duplicates.
    pub fn from_items(items: Vec<AccessListItem>) -> Self {
        Self { items }
    }

    /// Add an address to the access list (address-only, no specific storage keys).
    /// Useful for contracts that are accessed on every call.
    pub fn add_address(&mut self, address: Address) {
        if !self.items.iter().any(|item| item.address == address) {
            self.items.push(AccessListItem {
                address,
                storage_keys: Vec::new(),
            });
        }
    }

    /// Add one storage key for an address, deduplicating both address and key.
    pub fn add_storage_key(&mut self, address: Address, storage_key: B256) {
        if let Some(item) = self.items.iter_mut().find(|item| item.address == address) {
            push_unique(&mut item.storage_keys, storage_key);
        } else {
            self.items.push(AccessListItem {
                address,
                storage_keys: vec![storage_key],
            });
        }
    }

    /// Return the access list unconditionally.
    ///
    /// On L1 chains there is no L1 data posting overhead, so access lists
    /// always save gas (100 gas per warm-vs-cold SLOAD). Returns `None`
    /// only when the list is empty.
    pub fn into_access_list_always(self) -> Option<AccessList> {
        if self.items.is_empty() {
            return None;
        }
        info!(
            items = self.items.len(),
            "Using access list unconditionally (L1 mode)"
        );
        Some(AccessList(self.items))
    }

    /// Evaluate Arbitrum profitability against current L1/L2 gas prices and
    /// return the access list only if it saves money.
    ///
    /// Queries the Arbitrum `ArbGasInfo` precompile for pricing, then compares
    /// the L2 execution savings against the estimated L1 data cost of posting
    /// the serialized list:
    ///
    /// - **L2 savings**: `100 gas * entry_count * perArbGas`, where each address
    ///   and each storage key counts as one entry (the EIP-2929 warm-vs-cold
    ///   access discount).
    /// - **L1 cost**: `l1_data_gas * l1_base_fee`, where `l1_data_gas` is the
    ///   exact per-byte calldata gas ([`l1_data_gas_for_bytes`]) of the EIP-2930
    ///   RLP-encoded access list.
    ///
    /// # Errors
    ///
    /// Returns `Err` if the provider/pricing queries fail.
    ///
    /// Returns `Ok(None)` when:
    /// - the list is empty,
    /// - either the L2 or L1 gas price reads as zero, or
    /// - the estimated L1 cost meets or exceeds the L2 savings (not profitable).
    pub async fn into_access_list_if_profitable<P: Provider>(
        self,
        provider: &P,
    ) -> Result<Option<AccessList>> {
        if self.items.is_empty() {
            return Ok(None);
        }

        // Query ArbGasInfo for current pricing
        let arb = ArbGasInfo::new(ARB_GAS_INFO, provider);
        let prices_call = arb.getPricesInWei();
        let prices = prices_call
            .call()
            .await
            .map_err(|e| AccessListError::query("ArbGasInfo prices", e))?;
        let l1_fee_call = arb.getL1BaseFeeEstimate();
        let l1_base_fee = l1_fee_call
            .call()
            .await
            .map_err(|e| AccessListError::query("ArbGasInfo L1 base fee", e))?;

        let l2_gas_price = prices.perArbGas;

        if l2_gas_price.is_zero() || l1_base_fee.is_zero() {
            debug!("L1 or L2 gas price is zero, skipping access list");
            return Ok(None);
        }

        let access_list = AccessList(self.items);
        if log_access_list_profitability(
            &access_list,
            l2_gas_price,
            l1_base_fee,
            "Access list profitability check",
        ) {
            Ok(Some(access_list))
        } else {
            Ok(None)
        }
    }
}

/// Evaluate whether an existing access list is profitable on Arbitrum.
///
/// Each access list entry saves L2 execution gas (warm vs cold access) but
/// costs L1 data posting gas for its serialized bytes. This function queries
/// `ArbGasInfo`, computes the exact EIP-2930 RLP data gas, and returns the
/// access list only if profitable.
///
/// This is the free-function counterpart to
/// [`SmartAccessList::into_access_list_if_profitable`] for a pre-built
/// [`AccessList`]; the two share the same cost model and break-even comparison.
///
/// # Errors
///
/// Returns `Err` if the provider/pricing queries fail.
///
/// Returns `Ok(None)` when:
/// - the list is empty,
/// - either the L2 or L1 gas price reads as zero, or
/// - the estimated L1 cost meets or exceeds the L2 savings (not profitable).
pub async fn access_list_if_profitable<P: Provider>(
    access_list: AccessList,
    provider: &P,
) -> Result<Option<AccessList>> {
    if access_list.0.is_empty() {
        return Ok(None);
    }

    // Query ArbGasInfo for current pricing
    let arb = ArbGasInfo::new(ARB_GAS_INFO, provider);
    let prices = arb
        .getPricesInWei()
        .call()
        .await
        .map_err(|e| AccessListError::query("ArbGasInfo prices", e))?;
    let l1_base_fee = arb
        .getL1BaseFeeEstimate()
        .call()
        .await
        .map_err(|e| AccessListError::query("ArbGasInfo L1 base fee", e))?;

    let l2_gas_price = prices.perArbGas;

    if l2_gas_price.is_zero() || l1_base_fee.is_zero() {
        debug!("L1 or L2 gas price is zero, skipping access list");
        return Ok(None);
    }

    if log_access_list_profitability(
        &access_list,
        l2_gas_price,
        l1_base_fee,
        "Simulation access list profitability check",
    ) {
        Ok(Some(access_list))
    } else {
        Ok(None)
    }
}

/// Select the appropriate access list strategy based on pricing inputs.
///
/// - **L1**: Always include the simulation access list (no L1 data cost penalty).
///   Returns `None` only if the list is empty.
/// - **Arbitrum**: Include only when warm-access savings exceed the exact
///   EIP-2930 RLP data cost priced through `ArbGasInfo`.
/// - **OP Stack**: Include only when warm-access savings exceed the incremental
///   `GasPriceOracle.getL1Fee(bytes)` fee between the transaction without and
///   with the access list.
pub async fn resolve_access_list<P: Provider>(
    sim_access_list: AccessList,
    provider: &P,
    pricing: AccessListPricing,
) -> Result<Option<AccessList>> {
    if sim_access_list.0.is_empty() {
        return Ok(None);
    }

    match pricing {
        AccessListPricing::L1 => Ok(Some(sim_access_list)),
        AccessListPricing::Arbitrum => access_list_if_profitable(sim_access_list, provider).await,
        AccessListPricing::OpStack {
            tx_without_access_list,
            tx_with_access_list,
        } => {
            access_list_if_profitable_op_stack(
                sim_access_list,
                provider,
                tx_without_access_list,
                tx_with_access_list,
            )
            .await
        }
    }
}

async fn access_list_if_profitable_op_stack<P: Provider>(
    access_list: AccessList,
    provider: &P,
    tx_without_access_list: Bytes,
    tx_with_access_list: Bytes,
) -> Result<Option<AccessList>> {
    let l2_gas_price = U256::from(
        provider
            .get_gas_price()
            .await
            .map_err(|e| AccessListError::query("OP Stack provider gas price", e))?,
    );

    let l1_fee_without = query_op_l1_fee(provider, tx_without_access_list)
        .await
        .map_err(|e| AccessListError::Query {
            operation: "OP Stack GasPriceOracle L1 fee without access list",
            details: e.to_string(),
        })?;
    let l1_fee_with = query_op_l1_fee(provider, tx_with_access_list)
        .await
        .map_err(|e| AccessListError::Query {
            operation: "OP Stack GasPriceOracle L1 fee with access list",
            details: e.to_string(),
        })?;

    let incremental_l1_fee = l1_fee_with.saturating_sub(l1_fee_without);
    let total_entries = access_list_entry_count(&access_list);
    let l2_savings_wei = U256::from(total_entries) * U256::from(100) * l2_gas_price;
    let profitable = l2_savings_wei > incremental_l1_fee;

    info!(
        entries = total_entries,
        items = access_list.0.len(),
        l2_savings_wei = %l2_savings_wei,
        l1_fee_without_wei = %l1_fee_without,
        l1_fee_with_wei = %l1_fee_with,
        incremental_l1_fee_wei = %incremental_l1_fee,
        l2_gas_price_gwei = %format_gwei(l2_gas_price),
        profitable,
        "OP Stack access list profitability check"
    );

    if profitable {
        Ok(Some(access_list))
    } else {
        Ok(None)
    }
}

async fn query_op_l1_fee<P: Provider>(provider: &P, tx_data: Bytes) -> Result<U256> {
    let calldata = OpGasPriceOracle::getL1FeeCall { _data: tx_data }.abi_encode();
    let tx = TransactionRequest::default()
        .to(OP_GAS_PRICE_ORACLE)
        .input(TransactionInput::from(calldata));

    provider
        .client()
        .request("eth_call", (tx, BlockNumberOrTag::Latest))
        .await
        .map_err(|e| AccessListError::query("OP Stack GasPriceOracle.getL1Fee eth_call", e))
}

/// Query the current L1 base fee estimate, dispatching to the correct predeploy
/// based on chain type. Returns `U256::ZERO` for L1 chains or on failure.
pub async fn query_l1_base_fee_for_chain<P, N>(provider: &P, chain_type: ChainType) -> U256
where
    P: Provider<N>,
    N: Network,
{
    match chain_type {
        ChainType::Arbitrum => {
            let arb = ArbGasInfo::new(ARB_GAS_INFO, provider);
            match arb.getL1BaseFeeEstimate().call().await {
                Ok(fee) => fee,
                Err(e) => {
                    debug!(error = %e, "Failed to query L1 base fee from ArbGasInfo");
                    U256::ZERO
                }
            }
        }
        ChainType::OpStack => {
            let oracle = OpGasPriceOracle::new(OP_GAS_PRICE_ORACLE, provider);
            match oracle.l1BaseFee().call().await {
                Ok(fee) => fee,
                Err(e) => {
                    debug!(error = %e, "Failed to query L1 base fee from OP GasPriceOracle");
                    U256::ZERO
                }
            }
        }
        ChainType::L1 => U256::ZERO,
    }
}

/// Compute the OP stack L1 data fee for a given transaction calldata.
///
/// Calls `GasPriceOracle.getL1Fee(bytes)` which handles the full Ecotone fee
/// model internally (base fee scalars, blob base fee, compression). This gives
/// the actual L1 data posting cost in wei, unlike the Arbitrum formula which
/// simply multiplies `calldata_gas * l1_base_fee`.
///
/// Returns `U256::ZERO` on any failure (e.g. predeploy not available).
pub fn compute_op_l1_fee(cache: &mut EvmCache, calldata: &[u8]) -> U256 {
    let encoded = OpGasPriceOracle::getL1FeeCall {
        _data: calldata.to_vec().into(),
    }
    .abi_encode();

    match cache.call_raw(Address::ZERO, OP_GAS_PRICE_ORACLE, encoded.into(), false) {
        Ok(ExecutionResult::Success { output, .. }) => {
            let out = output.into_data();
            OpGasPriceOracle::getL1FeeCall::abi_decode_returns(&out).unwrap_or(U256::ZERO)
        }
        Ok(_) => {
            debug!("GasPriceOracle.getL1Fee() reverted");
            U256::ZERO
        }
        Err(e) => {
            debug!(error = %e, "Failed to call GasPriceOracle.getL1Fee()");
            U256::ZERO
        }
    }
}

impl Default for SmartAccessList {
    fn default() -> Self {
        Self::new()
    }
}

fn push_unique(vec: &mut Vec<B256>, val: B256) {
    if !vec.contains(&val) {
        vec.push(val);
    }
}

/// L1 calldata gas for a byte slice: zero bytes = 4 gas, non-zero = 16 gas.
///
/// This is the post-EIP-2028 calldata pricing used to approximate the L1 data
/// cost of serialized access-list entries. It counts the raw bytes only and
/// does not add any RLP framing overhead.
///
/// # Examples
///
/// ```
/// use evm_fork_cache::access_list::l1_data_gas_for_bytes;
///
/// // All-zero 32-byte slot: 32 * 4 = 128 gas.
/// assert_eq!(l1_data_gas_for_bytes(&[0u8; 32]), 128);
/// // All-non-zero 20-byte address: 20 * 16 = 320 gas.
/// assert_eq!(l1_data_gas_for_bytes(&[0xFFu8; 20]), 320);
/// // Empty slice costs nothing.
/// assert_eq!(l1_data_gas_for_bytes(&[]), 0);
/// ```
pub fn l1_data_gas_for_bytes(data: &[u8]) -> u64 {
    data.iter()
        .map(|&b| if b == 0 { 4u64 } else { 16u64 })
        .sum()
}

/// Exact L1 calldata gas for the EIP-2930 RLP encoding of an access list.
pub fn access_list_rlp_data_gas(access_list: &AccessList) -> u64 {
    let mut encoded = Vec::with_capacity(access_list.length());
    access_list.encode(&mut encoded);
    l1_data_gas_for_bytes(&encoded)
}

fn access_list_entry_count(access_list: &AccessList) -> u64 {
    access_list
        .0
        .iter()
        .map(|item| 1 + item.storage_keys.len() as u64)
        .sum()
}

fn log_access_list_profitability(
    access_list: &AccessList,
    l2_gas_price: U256,
    l1_base_fee: U256,
    message: &'static str,
) -> bool {
    let total_entries = access_list_entry_count(access_list);
    let total_l1_data_gas = access_list_rlp_data_gas(access_list);
    let l2_savings_wei = U256::from(total_entries) * U256::from(100) * l2_gas_price;
    let l1_cost_wei = U256::from(total_l1_data_gas) * l1_base_fee;
    let profitable = l2_savings_wei > l1_cost_wei;

    info!(
        entries = total_entries,
        items = access_list.0.len(),
        l1_data_gas = total_l1_data_gas,
        l2_savings_wei = %l2_savings_wei,
        l1_cost_wei = %l1_cost_wei,
        l2_gas_price_gwei = %format_gwei(l2_gas_price),
        l1_base_fee_gwei = %format_gwei(l1_base_fee),
        profitable,
        check = message,
        "Access list profitability check"
    );

    profitable
}

/// Filter already-warm and excluded addresses from an access list, then apply
/// it to the transaction request.
///
/// Removes entries for:
/// - `sender` — always warm as tx origin per EIP-2929
/// - `tx.to` — always warm as the destination per EIP-2929
/// - Any addresses in `exclude` — caller-excluded addresses
///
/// After filtering, sets the access list on `tx` (skipped if the list is empty).
pub fn apply_access_list(
    tx: &mut alloy_rpc_types_eth::TransactionRequest,
    access_list: &mut AccessList,
    sender: Address,
    exclude: &[Address],
) {
    let tx_to = tx.to.as_ref().and_then(|t| t.to().copied());
    access_list.0.retain(|item| {
        if Some(item.address) == tx_to || item.address == sender {
            return false;
        }
        if exclude.contains(&item.address) {
            return false;
        }
        true
    });
    if !access_list.0.is_empty() {
        *tx = std::mem::take(tx).access_list(access_list.clone());
    }
}

fn format_gwei(wei: U256) -> String {
    let gwei = wei / U256::from(1_000_000_000u64);
    let remainder = (wei % U256::from(1_000_000_000u64))
        .try_into()
        .unwrap_or(0u64);
    format!("{}.{:03}", gwei, remainder / 1_000_000)
}

#[cfg(test)]
mod tests {
    use super::*;
    use alloy_primitives::Bytes;

    #[test]
    fn add_address_deduplicates_address_only_entries() {
        let address = Address::repeat_byte(0xAA);
        let mut al = SmartAccessList::new();

        al.add_address(address);
        al.add_address(address);

        let access_list = al.into_access_list_always().expect("non-empty");
        assert_eq!(access_list.0.len(), 1);
        assert_eq!(access_list.0[0].address, address);
        assert!(access_list.0[0].storage_keys.is_empty());
    }

    #[test]
    fn add_storage_key_deduplicates_keys() {
        let address = Address::repeat_byte(0xBB);
        let key = B256::from(U256::from(4));
        let mut al = SmartAccessList::new();

        al.add_storage_key(address, key);
        al.add_storage_key(address, key);

        let access_list = al.into_access_list_always().expect("should return Some");
        assert_eq!(access_list.0.len(), 1);
        assert_eq!(access_list.0[0].address, address);
        assert_eq!(access_list.0[0].storage_keys, vec![key]);
    }

    #[test]
    fn into_access_list_always_returns_none_when_empty() {
        let al = SmartAccessList::new();
        assert!(al.into_access_list_always().is_none());
    }

    #[test]
    fn l1_gas_for_zero_bytes_is_cheap() {
        let key = [0u8; 32];
        assert_eq!(l1_data_gas_for_bytes(&key), 128);
    }

    #[test]
    fn l1_gas_for_nonzero_address_bytes_is_expensive() {
        let addr = Address::repeat_byte(0xFF);
        assert_eq!(l1_data_gas_for_bytes(addr.as_slice()), 320);
    }

    #[test]
    fn access_list_rlp_data_gas_uses_exact_eip2930_encoding() {
        let access_list = AccessList(vec![AccessListItem {
            address: Address::ZERO,
            storage_keys: Vec::new(),
        }]);

        // RLP([[zero_address, []]]) = d7 d6 94 <20 zero bytes> c0.
        // Four non-zero framing bytes cost 64 gas; twenty zero address bytes cost
        // 80 gas. The old fixed-overhead approximation returned 192.
        assert_eq!(access_list_rlp_data_gas(&access_list), 144);
    }

    #[tokio::test]
    async fn access_list_profitability_provider_error_returns_err() {
        use alloy_network::Ethereum;
        use alloy_provider::RootProvider;
        use alloy_rpc_client::RpcClient;
        use alloy_transport::mock::Asserter;

        let provider = RootProvider::<Ethereum>::new(RpcClient::mocked(Asserter::new()));
        let access_list = AccessList(vec![AccessListItem {
            address: Address::repeat_byte(0xAA),
            storage_keys: Vec::new(),
        }]);

        let err = access_list_if_profitable(access_list, &provider)
            .await
            .expect_err("provider failures must be distinguishable from unprofitable lists");
        assert!(
            err.to_string().contains("ArbGasInfo") || err.to_string().contains("provider"),
            "unexpected error: {err:#}"
        );
    }

    #[tokio::test]
    async fn access_list_profitability_empty_list_still_returns_none() {
        use alloy_network::Ethereum;
        use alloy_provider::RootProvider;
        use alloy_rpc_client::RpcClient;
        use alloy_transport::mock::Asserter;

        let provider = RootProvider::<Ethereum>::new(RpcClient::mocked(Asserter::new()));
        let result = access_list_if_profitable(AccessList::default(), &provider)
            .await
            .expect("empty list must not query provider");
        assert!(result.is_none());
    }

    #[tokio::test]
    async fn resolve_access_list_l1_returns_non_empty_without_provider_calls() {
        use alloy_network::Ethereum;
        use alloy_provider::RootProvider;
        use alloy_rpc_client::RpcClient;
        use alloy_transport::mock::Asserter;

        let provider = RootProvider::<Ethereum>::new(RpcClient::mocked(Asserter::new()));
        let access_list = AccessList(vec![AccessListItem {
            address: Address::repeat_byte(0xAA),
            storage_keys: Vec::new(),
        }]);

        let result = resolve_access_list(access_list.clone(), &provider, AccessListPricing::L1)
            .await
            .expect("L1 must not query provider");
        assert_eq!(result, Some(access_list));

        let empty = resolve_access_list(AccessList::default(), &provider, AccessListPricing::L1)
            .await
            .expect("empty L1 list must not query provider");
        assert!(empty.is_none());
    }

    #[tokio::test]
    async fn resolve_access_list_op_stack_uses_oracle_incremental_fee() {
        use alloy_network::Ethereum;
        use alloy_provider::RootProvider;
        use alloy_rpc_client::RpcClient;
        use alloy_transport::mock::Asserter;

        let asserter = Asserter::new();
        asserter.push_success(&100u128); // eth_gasPrice
        asserter.push_success(&U256::from(1_000u64)); // getL1Fee(tx_without)
        asserter.push_success(&U256::from(1_010u64)); // getL1Fee(tx_with)
        let provider = RootProvider::<Ethereum>::new(RpcClient::mocked(asserter));
        let access_list = AccessList(vec![AccessListItem {
            address: Address::repeat_byte(0xAA),
            storage_keys: Vec::new(),
        }]);

        let result = resolve_access_list(
            access_list.clone(),
            &provider,
            AccessListPricing::OpStack {
                tx_without_access_list: Bytes::from_static(b"without"),
                tx_with_access_list: Bytes::from_static(b"with"),
            },
        )
        .await
        .expect("OP Stack pricing succeeds");

        assert_eq!(result, Some(access_list));
    }

    #[tokio::test]
    async fn resolve_access_list_op_stack_unprofitable_returns_none() {
        use alloy_network::Ethereum;
        use alloy_provider::RootProvider;
        use alloy_rpc_client::RpcClient;
        use alloy_transport::mock::Asserter;

        let asserter = Asserter::new();
        asserter.push_success(&100u128); // eth_gasPrice
        asserter.push_success(&U256::from(1_000u64)); // getL1Fee(tx_without)
        asserter.push_success(&U256::from(20_000u64)); // getL1Fee(tx_with)
        let provider = RootProvider::<Ethereum>::new(RpcClient::mocked(asserter));
        let access_list = AccessList(vec![AccessListItem {
            address: Address::repeat_byte(0xAA),
            storage_keys: Vec::new(),
        }]);

        let result = resolve_access_list(
            access_list,
            &provider,
            AccessListPricing::OpStack {
                tx_without_access_list: Bytes::from_static(b"without"),
                tx_with_access_list: Bytes::from_static(b"with"),
            },
        )
        .await
        .expect("OP Stack pricing succeeds");

        assert!(result.is_none());
    }

    #[tokio::test]
    async fn resolve_access_list_op_stack_provider_failure_returns_err() {
        use alloy_network::Ethereum;
        use alloy_provider::RootProvider;
        use alloy_rpc_client::RpcClient;
        use alloy_transport::mock::Asserter;

        let asserter = Asserter::new();
        asserter.push_failure_msg("gas oracle unavailable");
        let provider = RootProvider::<Ethereum>::new(RpcClient::mocked(asserter));
        let access_list = AccessList(vec![AccessListItem {
            address: Address::repeat_byte(0xAA),
            storage_keys: Vec::new(),
        }]);

        let err = resolve_access_list(
            access_list,
            &provider,
            AccessListPricing::OpStack {
                tx_without_access_list: Bytes::from_static(b"without"),
                tx_with_access_list: Bytes::from_static(b"with"),
            },
        )
        .await
        .expect_err("provider failures must be distinguishable from unprofitable lists");

        assert!(
            err.to_string().contains("gas")
                || err.to_string().contains("oracle")
                || err.to_string().contains("provider"),
            "unexpected error: {err:#}"
        );
    }
}