alloy-eip7928 0.4.11

Implementation of EIP-7928 type definitions
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
//! Validation for decoded EIP-7928 block access lists.

use crate::{AccountChanges, BlockAccessIndex};
use alloy_primitives::{Address, U256};

/// A change list within an EIP-7928 account entry.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum BlockAccessListChangeKind {
    /// Storage changes for one slot.
    Storage,
    /// Account balance changes.
    Balance,
    /// Account nonce changes.
    Nonce,
    /// Account code changes.
    Code,
}

impl core::fmt::Display for BlockAccessListChangeKind {
    #[inline]
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.write_str(match self {
            Self::Storage => "storage",
            Self::Balance => "balance",
            Self::Nonce => "nonce",
            Self::Code => "code",
        })
    }
}

/// Error returned when a decoded EIP-7928 block access list is invalid.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, thiserror::Error)]
pub enum BlockAccessListValidationError {
    /// The same account occurs more than once.
    #[error("account {address} occurs more than once in the block access list")]
    DuplicateAccount {
        /// Duplicated account address.
        address: Address,
    },
    /// Account entries are not ordered lexicographically by address.
    #[error(
        "block access list account {address} appears after {previous}, violating canonical order"
    )]
    AccountsOutOfOrder {
        /// Previous account address.
        previous: Address,
        /// Out-of-order account address.
        address: Address,
    },
    /// The same storage key occurs more than once or in both storage lists.
    #[error("storage slot {slot:#x} occurs more than once for account {address}")]
    DuplicateStorageKey {
        /// Account containing the duplicated key.
        address: Address,
        /// Duplicated storage key.
        slot: U256,
    },
    /// Storage keys in one of the account's lists are not in canonical order.
    #[error(
        "storage slot {slot:#x} appears after {previous:#x} for account {address}, violating canonical order"
    )]
    StorageKeysOutOfOrder {
        /// Account containing the out-of-order key.
        address: Address,
        /// Previous storage key.
        previous: U256,
        /// Out-of-order storage key.
        slot: U256,
    },
    /// A storage change entry has no changes.
    #[error("storage slot {slot:#x} has an empty change list for account {address}")]
    EmptyStorageChanges {
        /// Account containing the empty entry.
        address: Address,
        /// Storage key with no changes.
        slot: U256,
    },
    /// The same block access index occurs more than once in one change list.
    #[error(
        "block access index {index} occurs more than once in a {kind} change list for account {address}"
    )]
    DuplicateBlockAccessIndex {
        /// Account containing the change list.
        address: Address,
        /// Kind of change list.
        kind: BlockAccessListChangeKind,
        /// Duplicated block access index.
        index: BlockAccessIndex,
    },
    /// A change list is not ordered by block access index.
    #[error(
        "block access index {index} appears after {previous} in a {kind} change list for account {address}"
    )]
    ChangeIndicesOutOfOrder {
        /// Account containing the change list.
        address: Address,
        /// Kind of change list.
        kind: BlockAccessListChangeKind,
        /// Previous block access index.
        previous: BlockAccessIndex,
        /// Out-of-order block access index.
        index: BlockAccessIndex,
    },
    /// A block access index does not fit the EIP-7928 `uint32` representation.
    #[error(
        "block access index {index} in a {kind} change list for account {address} exceeds uint32"
    )]
    BlockAccessIndexOutOfRange {
        /// Account containing the change list.
        address: Address,
        /// Kind of change list.
        kind: BlockAccessListChangeKind,
        /// Invalid block access index.
        index: BlockAccessIndex,
    },
    /// A block access index is greater than the block's post-execution index.
    #[error(
        "block access index {index} in a {kind} change list for account {address} exceeds the block maximum {max}"
    )]
    BlockAccessIndexExceedsBlock {
        /// Account containing the change list.
        address: Address,
        /// Kind of change list.
        kind: BlockAccessListChangeKind,
        /// Invalid block access index.
        index: BlockAccessIndex,
        /// Post-execution index for the block.
        max: BlockAccessIndex,
    },
}

/// Validates a decoded EIP-7928 block access list.
///
/// This enforces the ordering and uniqueness rules on accounts, storage keys, and change indices,
/// requires every storage change entry to be non-empty, and checks that every block access index
/// fits its `uint32` wire type and is at most the block's post-execution index
/// (`transaction_count + 1`).
pub fn validate_block_access_list(
    block_access_list: &[AccountChanges],
    transaction_count: usize,
) -> Result<(), BlockAccessListValidationError> {
    let max_block_access_index = BlockAccessIndex::new(
        u64::try_from(transaction_count).unwrap_or(u64::MAX).saturating_add(1),
    );
    let mut previous_address = None;

    for account in block_access_list {
        validate_account_order(previous_address, account.address)?;
        previous_address = Some(account.address);
        validate_account(account, max_block_access_index)?;
    }

    Ok(())
}

fn validate_account(
    account: &AccountChanges,
    max_block_access_index: BlockAccessIndex,
) -> Result<(), BlockAccessListValidationError> {
    let address = account.address;
    let mut previous_slot = None;
    for slot_changes in &account.storage_changes {
        validate_storage_key_order(address, &mut previous_slot, slot_changes.slot)?;
        if slot_changes.changes.is_empty() {
            return Err(BlockAccessListValidationError::EmptyStorageChanges {
                address,
                slot: slot_changes.slot,
            });
        }
        validate_change_indices(
            address,
            BlockAccessListChangeKind::Storage,
            slot_changes.changes.iter().map(|change| change.block_access_index),
            max_block_access_index,
        )?;
    }

    previous_slot = None;
    for &slot in &account.storage_reads {
        validate_storage_key_order(address, &mut previous_slot, slot)?;
    }
    validate_storage_disjointness(account)?;

    validate_change_indices(
        address,
        BlockAccessListChangeKind::Balance,
        account.balance_changes.iter().map(|change| change.block_access_index),
        max_block_access_index,
    )?;
    validate_change_indices(
        address,
        BlockAccessListChangeKind::Nonce,
        account.nonce_changes.iter().map(|change| change.block_access_index),
        max_block_access_index,
    )?;
    validate_change_indices(
        address,
        BlockAccessListChangeKind::Code,
        account.code_changes.iter().map(|change| change.block_access_index),
        max_block_access_index,
    )
}

fn validate_account_order(
    previous: Option<Address>,
    address: Address,
) -> Result<(), BlockAccessListValidationError> {
    if let Some(previous) = previous {
        if previous == address {
            return Err(BlockAccessListValidationError::DuplicateAccount { address });
        }
        if previous > address {
            return Err(BlockAccessListValidationError::AccountsOutOfOrder { previous, address });
        }
    }
    Ok(())
}

fn validate_storage_key_order(
    address: Address,
    previous: &mut Option<U256>,
    slot: U256,
) -> Result<(), BlockAccessListValidationError> {
    if let Some(previous) = *previous {
        if previous == slot {
            return Err(BlockAccessListValidationError::DuplicateStorageKey { address, slot });
        }
        if previous > slot {
            return Err(BlockAccessListValidationError::StorageKeysOutOfOrder {
                address,
                previous,
                slot,
            });
        }
    }
    *previous = Some(slot);
    Ok(())
}

fn validate_storage_disjointness(
    account: &AccountChanges,
) -> Result<(), BlockAccessListValidationError> {
    let mut changes = account.storage_changes.iter().peekable();
    let mut reads = account.storage_reads.iter().peekable();

    while let (Some(change), Some(read)) = (changes.peek(), reads.peek()) {
        match change.slot.cmp(read) {
            core::cmp::Ordering::Less => {
                changes.next();
            }
            core::cmp::Ordering::Greater => {
                reads.next();
            }
            core::cmp::Ordering::Equal => {
                return Err(BlockAccessListValidationError::DuplicateStorageKey {
                    address: account.address,
                    slot: **read,
                });
            }
        }
    }

    Ok(())
}

fn validate_change_indices(
    address: Address,
    kind: BlockAccessListChangeKind,
    indices: impl IntoIterator<Item = BlockAccessIndex>,
    max: BlockAccessIndex,
) -> Result<(), BlockAccessListValidationError> {
    let mut previous = None;
    for index in indices {
        if index.get() > u32::MAX as u64 {
            return Err(BlockAccessListValidationError::BlockAccessIndexOutOfRange {
                address,
                kind,
                index,
            });
        }
        if index > max {
            return Err(BlockAccessListValidationError::BlockAccessIndexExceedsBlock {
                address,
                kind,
                index,
                max,
            });
        }
        if let Some(previous) = previous {
            if previous == index {
                return Err(BlockAccessListValidationError::DuplicateBlockAccessIndex {
                    address,
                    kind,
                    index,
                });
            }
            if previous > index {
                return Err(BlockAccessListValidationError::ChangeIndicesOutOfOrder {
                    address,
                    kind,
                    previous,
                    index,
                });
            }
        }
        previous = Some(index);
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{BalanceChange, CodeChange, NonceChange, SlotChanges, StorageChange};
    use alloc::vec;
    use alloy_primitives::{Bytes, U256};

    const fn index(value: u64) -> BlockAccessIndex {
        BlockAccessIndex::new(value)
    }

    fn account(address: u8) -> AccountChanges {
        AccountChanges {
            address: Address::with_last_byte(address),
            storage_changes: vec![SlotChanges::new(
                U256::from(1),
                vec![StorageChange::new(index(0), U256::from(10))],
            )],
            storage_reads: vec![U256::from(2)],
            balance_changes: vec![BalanceChange::new(index(1), U256::from(20))],
            nonce_changes: vec![NonceChange::new(index(2), 1)],
            code_changes: vec![CodeChange::new(index(3), Bytes::new())],
        }
    }

    #[test]
    fn accepts_canonical_block_access_list() {
        let bal = crate::bal::Bal::new(vec![account(1), account(2)]);

        assert_eq!(validate_block_access_list(bal.as_slice(), 2), Ok(()));
        assert_eq!(bal.validate_structure(2), Ok(()));
    }

    #[test]
    fn rejects_duplicate_and_out_of_order_accounts() {
        let address = Address::with_last_byte(2);
        assert_eq!(
            validate_block_access_list(&[account(2), account(2)], 2),
            Err(BlockAccessListValidationError::DuplicateAccount { address })
        );

        let previous = Address::with_last_byte(2);
        let address = Address::with_last_byte(1);
        assert_eq!(
            validate_block_access_list(&[account(2), account(1)], 2),
            Err(BlockAccessListValidationError::AccountsOutOfOrder { previous, address })
        );
    }

    #[test]
    fn rejects_invalid_storage_entries() {
        let address = Address::with_last_byte(1);
        let slot = U256::from(1);
        let mut empty = account(1);
        empty.storage_changes[0].changes.clear();
        assert_eq!(
            validate_block_access_list(&[empty], 2),
            Err(BlockAccessListValidationError::EmptyStorageChanges { address, slot })
        );

        let mut duplicate = account(1);
        duplicate.storage_reads.insert(0, slot);
        assert_eq!(
            validate_block_access_list(&[duplicate], 2),
            Err(BlockAccessListValidationError::DuplicateStorageKey { address, slot })
        );

        let mut out_of_order = account(1);
        out_of_order.storage_reads = vec![U256::from(3), U256::from(2)];
        assert_eq!(
            validate_block_access_list(&[out_of_order], 2),
            Err(BlockAccessListValidationError::StorageKeysOutOfOrder {
                address,
                previous: U256::from(3),
                slot: U256::from(2),
            })
        );
    }

    #[test]
    fn rejects_invalid_change_indices() {
        let address = Address::with_last_byte(1);
        let mut duplicate = account(1);
        duplicate.balance_changes.push(BalanceChange::new(index(1), U256::from(30)));
        assert_eq!(
            validate_block_access_list(&[duplicate], 2),
            Err(BlockAccessListValidationError::DuplicateBlockAccessIndex {
                address,
                kind: BlockAccessListChangeKind::Balance,
                index: index(1),
            })
        );

        let mut out_of_order = account(1);
        out_of_order.nonce_changes =
            vec![NonceChange::new(index(2), 1), NonceChange::new(index(1), 2)];
        assert_eq!(
            validate_block_access_list(&[out_of_order], 2),
            Err(BlockAccessListValidationError::ChangeIndicesOutOfOrder {
                address,
                kind: BlockAccessListChangeKind::Nonce,
                previous: index(2),
                index: index(1),
            })
        );

        let mut exceeds_u32 = account(1);
        exceeds_u32.code_changes[0].block_access_index = index(u32::MAX as u64 + 1);
        assert_eq!(
            validate_block_access_list(&[exceeds_u32], usize::MAX),
            Err(BlockAccessListValidationError::BlockAccessIndexOutOfRange {
                address,
                kind: BlockAccessListChangeKind::Code,
                index: index(u32::MAX as u64 + 1),
            })
        );

        let mut exceeds_block = account(1);
        exceeds_block.storage_changes[0].changes[0].block_access_index = index(4);
        assert_eq!(
            validate_block_access_list(&[exceeds_block], 2),
            Err(BlockAccessListValidationError::BlockAccessIndexExceedsBlock {
                address,
                kind: BlockAccessListChangeKind::Storage,
                index: index(4),
                max: index(3),
            })
        );
    }
}