alloy-eip7928 0.4.2

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
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
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
//! Contains the [`BlockAccessList`] type, which represents a simple list of account changes.

use crate::account_changes::AccountChanges;
use alloc::vec::Vec;

#[cfg(not(feature = "std"))]
use once_cell::race::OnceBox as OnceLock;
#[cfg(feature = "std")]
use std::sync::OnceLock;

/// This struct is used to store `account_changes` in a block.
pub type BlockAccessList = Vec<AccountChanges>;

/// Computes the hash of the given block access list.
#[cfg(feature = "rlp")]
pub fn compute_block_access_list_hash(bal: &[AccountChanges]) -> alloy_primitives::B256 {
    let mut buf = Vec::new();
    alloy_rlp::encode_list(bal, &mut buf);
    alloy_primitives::keccak256(&buf)
}

/// Computes the total number of items in the block access list, counting each account and unique
/// storage slot.
pub fn total_bal_items(bal: &[AccountChanges]) -> u64 {
    let mut bal_items: u64 = 0;

    for account in bal {
        // Count address
        bal_items += 1;

        // Collect unique storage slots across reads + writes
        let mut unique_slots = alloy_primitives::map::HashSet::new();

        for change in account.storage_changes() {
            unique_slots.insert(change.slot);
        }

        for slot in account.storage_reads() {
            unique_slots.insert(*slot);
        }

        // Count unique storage keys
        bal_items += unique_slots.len() as u64;
    }
    bal_items
}

/// Block-Level Access List wrapper type with helper methods for metrics and validation.
pub mod bal {
    use super::OnceLock;
    use crate::{
        BlockAccessListGasError, BlockAccessListHashMismatch, account_changes::AccountChanges,
        diff::BalDiff,
    };
    use alloc::vec::{IntoIter, Vec};
    use alloy_primitives::{B256, Bytes};
    use core::{
        ops::{Deref, Index},
        slice::Iter,
    };

    /// A wrapper around [`Vec<AccountChanges>`] that provides helper methods for
    /// computing metrics and statistics about the block access list.
    ///
    /// This type implements `Deref` to `[AccountChanges]` for easy access to the
    /// underlying data while providing additional utility methods for BAL analysis.
    #[derive(Clone, Debug, Default, PartialEq, Eq)]
    #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
    #[cfg_attr(
        feature = "rlp",
        derive(alloy_rlp::RlpEncodableWrapper, alloy_rlp::RlpDecodableWrapper)
    )]
    #[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
    #[cfg_attr(feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize))]
    pub struct Bal(Vec<AccountChanges>);

    impl From<Bal> for Vec<AccountChanges> {
        fn from(this: Bal) -> Self {
            this.0
        }
    }

    impl From<Vec<AccountChanges>> for Bal {
        fn from(list: Vec<AccountChanges>) -> Self {
            Self(list)
        }
    }

    #[cfg(feature = "rlp")]
    impl alloy_primitives::Sealable for Bal {
        fn hash_slow(&self) -> alloy_primitives::B256 {
            self.compute_hash()
        }
    }

    impl Deref for Bal {
        type Target = [AccountChanges];

        fn deref(&self) -> &Self::Target {
            self.as_slice()
        }
    }

    impl IntoIterator for Bal {
        type Item = AccountChanges;
        type IntoIter = IntoIter<AccountChanges>;

        fn into_iter(self) -> Self::IntoIter {
            self.0.into_iter()
        }
    }

    impl<'a> IntoIterator for &'a Bal {
        type Item = &'a AccountChanges;
        type IntoIter = Iter<'a, AccountChanges>;

        fn into_iter(self) -> Self::IntoIter {
            self.iter()
        }
    }

    impl FromIterator<AccountChanges> for Bal {
        fn from_iter<I: IntoIterator<Item = AccountChanges>>(iter: I) -> Self {
            Self(iter.into_iter().collect())
        }
    }

    impl<I> Index<I> for Bal
    where
        I: core::slice::SliceIndex<[AccountChanges]>,
    {
        type Output = I::Output;

        #[inline]
        fn index(&self, index: I) -> &Self::Output {
            &self.0[index]
        }
    }

    impl Bal {
        /// Creates a new [`Bal`] from the provided account changes.
        pub const fn new(account_changes: Vec<AccountChanges>) -> Self {
            Self(account_changes)
        }

        /// Adds a new [`AccountChanges`] entry to the list.
        pub fn push(&mut self, account_changes: AccountChanges) {
            self.0.push(account_changes)
        }

        /// Returns `true` if the list contains no elements.
        #[inline]
        pub const fn is_empty(&self) -> bool {
            self.0.is_empty()
        }

        /// Returns the number of account change entries contained in the list.
        #[inline]
        pub const fn len(&self) -> usize {
            self.0.len()
        }

        /// Returns an iterator over the [`AccountChanges`] entries.
        #[inline]
        pub fn iter(&self) -> Iter<'_, AccountChanges> {
            self.0.iter()
        }

        /// Returns a slice of the contained [`AccountChanges`].
        #[inline]
        pub const fn as_slice(&self) -> &[AccountChanges] {
            self.0.as_slice()
        }

        /// Returns a compact diff describing where this BAL first diverges from `other`.
        pub fn diff(&self, other: &[AccountChanges]) -> BalDiff {
            BalDiff::between(self.as_slice(), other)
        }

        /// Returns a vector of [`AccountChanges`].
        pub fn into_inner(self) -> Vec<AccountChanges> {
            self.0
        }

        /// Sorts this block access list in-place according to the canonical EIP-7928 ordering
        /// rules.
        ///
        /// This applies the ordering required by the "Ordering, Uniqueness and Determinism"
        /// section of EIP-7928:
        ///
        /// - accounts are sorted lexicographically by address
        /// - `storage_changes` are sorted lexicographically by storage key
        /// - each per-slot `StorageChange` list is sorted by block access index in ascending order
        /// - `storage_reads` are sorted lexicographically by storage key
        /// - `balance_changes`, `nonce_changes`, and `code_changes` are sorted by block access
        ///   index in ascending order
        ///
        /// The account-local ordering is delegated to [`AccountChanges::sort`], so callers may
        /// sort account internals independently when a parallel sort strategy is useful.
        ///
        /// This method only canonicalizes ordering. It does not enforce the EIP-7928 uniqueness
        /// constraints for accounts, storage keys, or block access indexes.
        pub fn sort(&mut self) {
            self.0.sort_unstable_by_key(|account| account.address);

            for account in &mut self.0 {
                account.sort();
            }
        }

        /// Returns the total number of accounts with changes in this BAL.
        #[inline]
        pub const fn account_count(&self) -> usize {
            self.0.len()
        }

        /// Returns the total number of storage changes across all accounts.
        pub fn total_storage_changes(&self) -> usize {
            self.0.iter().map(|a| a.storage_changes.len()).sum()
        }

        /// Returns the total number of storage reads across all accounts.
        pub fn total_storage_reads(&self) -> usize {
            self.0.iter().map(|a| a.storage_reads.len()).sum()
        }

        /// Returns the total number of storage slots (both changes and reads) across all accounts.
        pub fn total_slots(&self) -> usize {
            self.0.iter().map(|a| a.storage_changes.len() + a.storage_reads.len()).sum()
        }

        /// Returns the total number of balance changes across all accounts.
        pub fn total_balance_changes(&self) -> usize {
            self.0.iter().map(|a| a.balance_changes.len()).sum()
        }

        /// Returns the total number of nonce changes across all accounts.
        pub fn total_nonce_changes(&self) -> usize {
            self.0.iter().map(|a| a.nonce_changes.len()).sum()
        }

        /// Returns the total number of code changes across all accounts.
        pub fn total_code_changes(&self) -> usize {
            self.0.iter().map(|a| a.code_changes.len()).sum()
        }

        /// Returns a summary of all change counts for metrics reporting.
        pub fn change_counts(&self) -> BalChangeCounts {
            let mut counts = BalChangeCounts::default();
            for account in &self.0 {
                counts.accounts += 1;
                counts.storage += account.storage_changes.len();
                counts.balance += account.balance_changes.len();
                counts.nonce += account.nonce_changes.len();
                counts.code += account.code_changes.len();
            }
            counts
        }

        /// Computes the total number of items in this block access list, counting each account and
        /// unique storage slot.
        pub fn total_bal_items(&self) -> u64 {
            super::total_bal_items(&self.0)
        }

        /// Validates this block access list against the block gas limit.
        ///
        /// EIP-7928 specifies that the total cost of the block access list items must not exceed
        /// the gas limit. Each item costs [`crate::constants::ITEM_COST`] gas.
        pub fn validate_gas_limit(&self, gas_limit: u64) -> Result<(), BlockAccessListGasError> {
            let items = self.total_bal_items();
            if items > gas_limit / crate::constants::ITEM_COST as u64 {
                return Err(BlockAccessListGasError::new(items, gas_limit));
            }
            Ok(())
        }

        /// Computes the hash of this block access list.
        #[cfg(feature = "rlp")]
        pub fn compute_hash(&self) -> alloy_primitives::B256 {
            if self.0.is_empty() {
                return crate::constants::EMPTY_BLOCK_ACCESS_LIST_HASH;
            }
            super::compute_block_access_list_hash(&self.0)
        }
    }

    /// Summary of change counts in a BAL for metrics reporting.
    #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
    pub struct BalChangeCounts {
        /// Number of accounts with changes.
        pub accounts: usize,
        /// Total number of storage changes.
        pub storage: usize,
        /// Total number of balance changes.
        pub balance: usize,
        /// Total number of nonce changes.
        pub nonce: usize,
        /// Total number of code changes.
        pub code: usize,
    }

    /// Raw RLP bytes for a block access list with lazy hash computation.
    #[derive(Clone, Debug)]
    #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
    #[cfg_attr(feature = "serde", serde(transparent))]
    pub struct RawBal {
        /// The original raw RLP bytes.
        raw: Bytes,
        /// Lazily computed hash of the block access list.
        #[cfg_attr(feature = "serde", serde(skip, default))]
        hash: OnceLock<B256>,
    }

    impl PartialEq for RawBal {
        #[inline]
        fn eq(&self, other: &Self) -> bool {
            self.raw == other.raw
        }
    }

    impl Eq for RawBal {}

    impl From<Bytes> for RawBal {
        #[inline]
        fn from(raw: Bytes) -> Self {
            Self::new(raw)
        }
    }

    impl RawBal {
        /// Creates a new [`RawBal`] from raw RLP bytes.
        #[inline]
        pub const fn new(raw: Bytes) -> Self {
            Self { raw, hash: OnceLock::new() }
        }

        /// Creates a new [`RawBal`] from raw RLP bytes and a precomputed hash.
        ///
        /// The hash is not checked against the raw bytes. Callers must ensure `hash` is the
        /// keccak256 hash of `raw`.
        #[inline]
        pub fn new_unchecked(raw: Bytes, hash: B256) -> Self {
            let this = Self::new(raw);
            #[allow(clippy::useless_conversion)]
            let _ = this.hash.get_or_init(|| hash.into());
            this
        }

        /// Returns the original raw RLP bytes.
        #[inline]
        pub const fn as_raw(&self) -> &Bytes {
            &self.raw
        }

        /// Consumes this value and returns the raw RLP bytes.
        #[inline]
        pub fn into_raw(self) -> Bytes {
            self.raw
        }

        /// Consumes this value and returns the raw RLP bytes and hash.
        #[inline]
        pub fn into_parts(self) -> (Bytes, B256) {
            let hash = self.hash();
            (self.raw, hash)
        }

        /// Ensures the raw RLP hash matches the expected block access list hash.
        #[inline]
        pub fn ensure_hash(&self, expected: B256) -> Result<(), BlockAccessListHashMismatch> {
            let computed = self.hash();
            if computed == expected {
                Ok(())
            } else {
                Err(BlockAccessListHashMismatch::new(computed, expected))
            }
        }

        /// Returns the hash of the raw block access list bytes.
        ///
        /// The hash is computed lazily on first call and cached for subsequent calls.
        #[inline]
        pub fn hash(&self) -> B256 {
            #[allow(clippy::useless_conversion)]
            *self.hash.get_or_init(|| alloy_primitives::keccak256(self.raw.as_ref()).into())
        }
    }

    #[cfg(feature = "rlp")]
    impl alloy_rlp::Encodable for RawBal {
        #[inline]
        fn encode(&self, out: &mut dyn alloy_rlp::BufMut) {
            out.put_slice(&self.raw);
        }

        #[inline]
        fn length(&self) -> usize {
            self.raw.len()
        }
    }

    #[cfg(feature = "rlp")]
    impl alloy_rlp::Decodable for RawBal {
        #[inline]
        fn decode(buf: &mut &[u8]) -> Result<Self, alloy_rlp::Error> {
            let original = *buf;
            let header = alloy_rlp::Header::decode(buf)?;
            let header_len = original.len() - buf.len();
            let raw_len = header_len + header.payload_length;
            let raw = Bytes::copy_from_slice(&original[..raw_len]);
            *buf = &original[raw_len..];
            Ok(Self::new(raw))
        }
    }

    /// A decoded block access list with lazy hash computation.
    ///
    /// This type wraps a decoded block access list along with the original raw RLP bytes,
    /// allowing efficient hash computation on demand without re-encoding.
    #[derive(Clone, Debug)]
    #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
    pub struct DecodedBal<T = Bal> {
        /// The decoded block access list.
        decoded: T,
        /// Raw RLP bytes and lazily computed hash of the block access list.
        raw: RawBal,
    }

    impl<T: PartialEq> PartialEq for DecodedBal<T> {
        #[inline]
        fn eq(&self, other: &Self) -> bool {
            self.decoded == other.decoded && self.raw == other.raw
        }
    }

    impl<T: Eq> Eq for DecodedBal<T> {}

    impl<T> DecodedBal<T> {
        /// Creates a new [`DecodedBal`] from decoded data and raw bytes.
        #[inline]
        pub const fn new(decoded: T, raw: Bytes) -> Self {
            Self { decoded, raw: RawBal::new(raw) }
        }

        /// Creates a new [`DecodedBal`] from decoded data, raw bytes, and a precomputed hash.
        ///
        /// The hash is not checked against the raw bytes. Callers must ensure `hash` is the
        /// keccak256 hash of `raw`.
        #[inline]
        pub fn new_unchecked(decoded: T, raw: Bytes, hash: B256) -> Self {
            Self { decoded, raw: RawBal::new_unchecked(raw, hash) }
        }

        /// Creates a new [`DecodedBal`] from decoded data and a [`RawBal`].
        #[inline]
        pub const fn with_raw_bal(decoded: T, raw: RawBal) -> Self {
            Self { decoded, raw }
        }

        /// Returns a reference to the decoded block access list.
        #[inline]
        pub const fn as_bal(&self) -> &T {
            &self.decoded
        }

        /// Returns the original raw RLP bytes.
        #[inline]
        pub const fn as_raw(&self) -> &Bytes {
            self.raw.as_raw()
        }

        /// Returns the raw BAL.
        #[inline]
        pub const fn as_raw_bal(&self) -> &RawBal {
            &self.raw
        }

        /// Splits this struct into the decoded BAL and raw bytes.
        #[inline]
        pub fn split(self) -> (T, Bytes) {
            (self.decoded, self.raw.into_raw())
        }

        /// Splits this struct into the decoded BAL and raw BAL.
        #[inline]
        pub fn split_raw_bal(self) -> (T, RawBal) {
            (self.decoded, self.raw)
        }

        /// Splits this struct into the decoded BAL, raw bytes, and hash.
        #[inline]
        pub fn into_parts(self) -> (T, Bytes, B256) {
            let hash = self.hash();
            let (decoded, raw) = self.split();
            (decoded, raw, hash)
        }

        /// Ensures the raw RLP hash matches the expected block access list hash.
        ///
        /// This checks `keccak256(raw_rlp_of_received_bal) == expected` using the cached hash of
        /// the original raw RLP bytes captured at decode time.
        #[inline]
        pub fn ensure_hash(&self, expected: B256) -> Result<(), BlockAccessListHashMismatch> {
            let computed = self.hash();
            if computed == expected {
                Ok(())
            } else {
                Err(BlockAccessListHashMismatch::new(computed, expected))
            }
        }

        /// Returns the hash of this block access list.
        ///
        /// The hash is computed lazily on first call and cached for subsequent calls.
        #[inline]
        pub fn hash(&self) -> B256 {
            self.raw.hash()
        }

        /// Converts the decoded BAL to the given alternative that is [`From<T>`].
        #[inline]
        pub fn convert<U>(self) -> DecodedBal<U>
        where
            U: From<T>,
        {
            self.map(U::from)
        }

        /// Converts the decoded BAL to the given alternative that is [`TryFrom<T>`].
        #[inline]
        pub fn try_convert<U>(self) -> Result<DecodedBal<U>, U::Error>
        where
            U: TryFrom<T>,
        {
            self.try_map(U::try_from)
        }

        /// Applies the given closure to the decoded BAL.
        #[inline]
        pub fn map<U>(self, f: impl FnOnce(T) -> U) -> DecodedBal<U> {
            let Self { decoded, raw } = self;
            DecodedBal { decoded: f(decoded), raw }
        }

        /// Applies the given fallible closure to the decoded BAL.
        #[inline]
        pub fn try_map<U, E>(self, f: impl FnOnce(T) -> Result<U, E>) -> Result<DecodedBal<U>, E> {
            let Self { decoded, raw } = self;
            Ok(DecodedBal { decoded: f(decoded)?, raw })
        }
    }

    #[cfg(feature = "rlp")]
    impl DecodedBal {
        /// Creates a new [`DecodedBal`] by decoding from raw RLP bytes.
        #[inline]
        pub fn from_rlp_bytes(raw: Bytes) -> Result<Self, alloy_rlp::Error> {
            Self::from_rlp_bytes_as(raw)
        }

        /// Creates a new [`DecodedBal`] by decoding from raw RLP bytes in a [`RawBal`].
        #[inline]
        pub fn from_raw_bal(raw: RawBal) -> Result<Self, alloy_rlp::Error> {
            Self::from_raw_bal_as(raw)
        }

        /// Creates a new [`DecodedBal`] by decoding from raw RLP bytes into `T`.
        #[inline]
        pub fn from_rlp_bytes_as<T>(raw: Bytes) -> Result<DecodedBal<T>, alloy_rlp::Error>
        where
            T: alloy_rlp::Decodable,
        {
            Self::from_raw_bal_as(RawBal::new(raw))
        }

        /// Creates a new [`DecodedBal`] by decoding from raw RLP bytes in a [`RawBal`] into `T`.
        #[inline]
        pub fn from_raw_bal_as<T>(raw: RawBal) -> Result<DecodedBal<T>, alloy_rlp::Error>
        where
            T: alloy_rlp::Decodable,
        {
            let mut slice = raw.as_raw().as_ref();
            let decoded = T::decode(&mut slice)?;
            if !slice.is_empty() {
                return Err(alloy_rlp::Error::UnexpectedLength);
            }
            Ok(DecodedBal::with_raw_bal(decoded, raw))
        }
    }

    #[cfg(feature = "rlp")]
    impl<T> DecodedBal<T>
    where
        T: alloy_primitives::Sealable,
    {
        /// Returns the decoded BAL as a sealed borrowed value.
        #[inline]
        pub fn as_sealed_bal(&self) -> alloy_primitives::Sealed<&T> {
            alloy_primitives::Sealable::seal_ref_unchecked(&self.decoded, self.hash())
        }

        /// Consumes this struct and returns the decoded BAL together with its hash.
        #[inline]
        pub fn into_sealed(self) -> alloy_primitives::Sealed<T> {
            let seal = self.hash();
            let (decoded, _) = self.split();
            alloy_primitives::Sealable::seal_unchecked(decoded, seal)
        }
    }

    #[cfg(feature = "rlp")]
    impl<T> alloy_rlp::Decodable for DecodedBal<T>
    where
        T: alloy_rlp::Decodable,
    {
        #[inline]
        fn decode(buf: &mut &[u8]) -> Result<Self, alloy_rlp::Error> {
            let original = *buf;
            let decoded = T::decode(buf)?;
            let consumed = original.len() - buf.len();
            let raw = Bytes::copy_from_slice(&original[..consumed]);
            Ok(Self::new(decoded, raw))
        }
    }

    #[cfg(feature = "rlp")]
    impl<T> alloy_rlp::Encodable for DecodedBal<T> {
        #[inline]
        fn encode(&self, out: &mut dyn alloy_rlp::BufMut) {
            alloy_rlp::Encodable::encode(&self.raw, out);
        }

        #[inline]
        fn length(&self) -> usize {
            alloy_rlp::Encodable::length(&self.raw)
        }
    }

    /// Either raw RLP bytes or a decoded block access list.
    ///
    /// This type is useful when callers may receive raw BAL bytes before the BAL needs to be
    /// decoded, while still allowing decoded values to preserve and re-use their original raw
    /// bytes.
    #[derive(Clone, Debug, PartialEq, Eq)]
    #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
    pub enum RawOrDecodedBal<T = Bal> {
        /// Raw RLP bytes for a block access list with lazy hash computation.
        Raw(RawBal),
        /// A decoded block access list with its original raw RLP bytes.
        Decoded(DecodedBal<T>),
    }

    impl<T> From<Bytes> for RawOrDecodedBal<T> {
        #[inline]
        fn from(raw: Bytes) -> Self {
            Self::Raw(RawBal::new(raw))
        }
    }

    impl<T> From<RawBal> for RawOrDecodedBal<T> {
        #[inline]
        fn from(raw: RawBal) -> Self {
            Self::Raw(raw)
        }
    }

    impl<T> From<DecodedBal<T>> for RawOrDecodedBal<T> {
        #[inline]
        fn from(decoded: DecodedBal<T>) -> Self {
            Self::Decoded(decoded)
        }
    }

    impl<T> RawOrDecodedBal<T> {
        /// Creates a new [`RawOrDecodedBal`] from raw RLP bytes.
        #[inline]
        pub const fn raw(raw: Bytes) -> Self {
            Self::Raw(RawBal::new(raw))
        }

        /// Creates a new [`RawOrDecodedBal`] from raw RLP bytes and a precomputed hash.
        ///
        /// The hash is not checked against the raw bytes. Callers must ensure `hash` is the
        /// keccak256 hash of `raw`.
        #[inline]
        pub fn raw_unchecked(raw: Bytes, hash: B256) -> Self {
            Self::Raw(RawBal::new_unchecked(raw, hash))
        }

        /// Creates a new [`RawOrDecodedBal`] from a [`RawBal`].
        #[inline]
        pub const fn raw_bal(raw: RawBal) -> Self {
            Self::Raw(raw)
        }

        /// Creates a new [`RawOrDecodedBal`] from a decoded BAL.
        #[inline]
        pub const fn decoded(decoded: DecodedBal<T>) -> Self {
            Self::Decoded(decoded)
        }

        /// Returns `true` if this contains raw RLP bytes.
        #[inline]
        pub const fn is_raw(&self) -> bool {
            matches!(self, Self::Raw(_))
        }

        /// Returns `true` if this contains a decoded BAL.
        #[inline]
        pub const fn is_decoded(&self) -> bool {
            matches!(self, Self::Decoded(_))
        }

        /// Returns the raw RLP bytes.
        #[inline]
        pub const fn as_raw(&self) -> &Bytes {
            match self {
                Self::Raw(raw) => raw.as_raw(),
                Self::Decoded(decoded) => decoded.as_raw(),
            }
        }

        /// Returns the raw BAL.
        #[inline]
        pub const fn as_raw_bal(&self) -> &RawBal {
            match self {
                Self::Raw(raw) => raw,
                Self::Decoded(decoded) => decoded.as_raw_bal(),
            }
        }

        /// Returns the decoded BAL if available.
        #[inline]
        pub const fn as_decoded(&self) -> Option<&DecodedBal<T>> {
            match self {
                Self::Raw(_) => None,
                Self::Decoded(decoded) => Some(decoded),
            }
        }

        /// Returns the decoded BAL if available.
        #[inline]
        pub fn into_decoded(self) -> Option<DecodedBal<T>> {
            match self {
                Self::Raw(_) => None,
                Self::Decoded(decoded) => Some(decoded),
            }
        }

        /// Returns the decoded block access list if available.
        #[inline]
        pub const fn as_bal(&self) -> Option<&T> {
            match self {
                Self::Raw(_) => None,
                Self::Decoded(decoded) => Some(decoded.as_bal()),
            }
        }

        /// Consumes this value and returns the raw RLP bytes.
        #[inline]
        pub fn into_raw(self) -> Bytes {
            match self {
                Self::Raw(raw) => raw.into_raw(),
                Self::Decoded(decoded) => decoded.split().1,
            }
        }

        /// Consumes this value and returns the raw BAL.
        #[inline]
        pub fn into_raw_bal(self) -> RawBal {
            match self {
                Self::Raw(raw) => raw,
                Self::Decoded(decoded) => decoded.split_raw_bal().1,
            }
        }

        /// Splits this value into its decoded BAL, if available, and raw RLP bytes.
        #[inline]
        pub fn split(self) -> (Option<T>, Bytes) {
            match self {
                Self::Raw(raw) => (None, raw.into_raw()),
                Self::Decoded(decoded) => {
                    let (bal, raw) = decoded.split();
                    (Some(bal), raw)
                }
            }
        }

        /// Splits this value into its decoded BAL, if available, and raw BAL.
        #[inline]
        pub fn split_raw_bal(self) -> (Option<T>, RawBal) {
            match self {
                Self::Raw(raw) => (None, raw),
                Self::Decoded(decoded) => {
                    let (bal, raw) = decoded.split_raw_bal();
                    (Some(bal), raw)
                }
            }
        }

        /// Ensures the raw RLP hash matches the expected block access list hash.
        #[inline]
        pub fn ensure_hash(&self, expected: B256) -> Result<(), BlockAccessListHashMismatch> {
            let computed = self.hash();
            if computed == expected {
                Ok(())
            } else {
                Err(BlockAccessListHashMismatch::new(computed, expected))
            }
        }

        /// Returns the hash of the raw block access list bytes.
        #[inline]
        pub fn hash(&self) -> B256 {
            match self {
                Self::Raw(raw) => raw.hash(),
                Self::Decoded(decoded) => decoded.hash(),
            }
        }

        /// Converts the decoded BAL to the given alternative that is [`From<T>`].
        ///
        /// Raw values stay raw.
        #[inline]
        pub fn convert<U>(self) -> RawOrDecodedBal<U>
        where
            U: From<T>,
        {
            self.map(U::from)
        }

        /// Converts the decoded BAL to the given alternative that is [`TryFrom<T>`].
        ///
        /// Raw values stay raw.
        #[inline]
        pub fn try_convert<U>(self) -> Result<RawOrDecodedBal<U>, U::Error>
        where
            U: TryFrom<T>,
        {
            self.try_map(U::try_from)
        }

        /// Applies the given closure to the decoded BAL if available.
        #[inline]
        pub fn map<U>(self, f: impl FnOnce(T) -> U) -> RawOrDecodedBal<U> {
            match self {
                Self::Raw(raw) => RawOrDecodedBal::Raw(raw),
                Self::Decoded(decoded) => RawOrDecodedBal::Decoded(decoded.map(f)),
            }
        }

        /// Applies the given fallible closure to the decoded BAL if available.
        #[inline]
        pub fn try_map<U, E>(
            self,
            f: impl FnOnce(T) -> Result<U, E>,
        ) -> Result<RawOrDecodedBal<U>, E> {
            match self {
                Self::Raw(raw) => Ok(RawOrDecodedBal::Raw(raw)),
                Self::Decoded(decoded) => decoded.try_map(f).map(RawOrDecodedBal::Decoded),
            }
        }
    }

    #[cfg(feature = "rlp")]
    impl<T> RawOrDecodedBal<T>
    where
        T: alloy_rlp::Decodable,
    {
        /// Up-converts raw RLP bytes into a decoded BAL, or returns the existing decoded BAL.
        #[inline]
        pub fn try_into_decoded(self) -> Result<DecodedBal<T>, alloy_rlp::Error> {
            match self {
                Self::Raw(raw) => DecodedBal::from_raw_bal_as(raw),
                Self::Decoded(decoded) => Ok(decoded),
            }
        }
    }

    #[cfg(feature = "rlp")]
    impl<T> alloy_rlp::Encodable for RawOrDecodedBal<T> {
        #[inline]
        fn encode(&self, out: &mut dyn alloy_rlp::BufMut) {
            out.put_slice(self.as_raw());
        }

        #[inline]
        fn length(&self) -> usize {
            self.as_raw().len()
        }
    }

    #[cfg(feature = "rlp")]
    impl<T> alloy_rlp::Decodable for RawOrDecodedBal<T> {
        #[inline]
        fn decode(buf: &mut &[u8]) -> Result<Self, alloy_rlp::Error> {
            <RawBal as alloy_rlp::Decodable>::decode(buf).map(Self::Raw)
        }
    }
}

/// Error returned when a block access list item cost exceeds the block gas limit.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, thiserror::Error)]
#[error(
    "block access list item cost exceeds gas limit: items={items}, max_items={max_items}, gas_limit={gas_limit}"
)]
pub struct BlockAccessListGasError {
    /// Number of block access list items.
    pub items: u64,
    /// Maximum number of block access list items allowed by the gas limit.
    pub max_items: u64,
    /// Block gas limit used for validation.
    pub gas_limit: u64,
}

impl BlockAccessListGasError {
    /// Creates a new gas limit validation error for the provided item count and gas limit.
    pub const fn new(items: u64, gas_limit: u64) -> Self {
        Self { items, max_items: gas_limit / crate::constants::ITEM_COST as u64, gas_limit }
    }
}

/// Error returned when a decoded block access list hash does not match the expected hash.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, thiserror::Error)]
#[error("block access list hash mismatch: computed={computed}, expected={expected}")]
pub struct BlockAccessListHashMismatch {
    /// Hash computed from the received BAL bytes.
    pub computed: alloy_primitives::B256,
    /// Hash expected by the caller, typically `header.block_access_list_hash`.
    pub expected: alloy_primitives::B256,
}

impl BlockAccessListHashMismatch {
    /// Creates a new block access list hash validation error.
    pub const fn new(computed: alloy_primitives::B256, expected: alloy_primitives::B256) -> Self {
        Self { computed, expected }
    }
}

#[cfg(test)]
mod hash_tests {
    use super::bal::{Bal, DecodedBal, RawBal, RawOrDecodedBal};
    use crate::{
        AccountChanges, BalanceChange, BlockAccessIndex, CodeChange, NonceChange, SlotChanges,
        StorageChange, constants::ITEM_COST,
    };
    use alloy_primitives::{Address, B256, Bytes, U256};

    #[test]
    fn decoded_bal_hash_uses_raw_bytes_without_rlp_feature() {
        let raw = Bytes::from_static(&[0xc0]);
        let decoded = DecodedBal::new(Bal::default(), raw.clone());

        assert_eq!(decoded.hash(), alloy_primitives::keccak256(raw.as_ref()));

        let (bal, split_raw, split_hash) = decoded.into_parts();
        assert!(bal.is_empty());
        assert_eq!(split_raw, raw);
        assert_eq!(split_hash, alloy_primitives::keccak256(raw.as_ref()));
    }

    #[test]
    fn decoded_bal_map_preserves_raw_and_hash() {
        let raw = Bytes::from_static(&[0xc0]);
        let decoded = DecodedBal::new(Bal::default(), raw.clone());
        let hash = decoded.hash();

        let mapped = decoded.map(|bal| bal.len());

        assert_eq!(mapped.as_bal(), &0);
        assert_eq!(mapped.as_raw(), &raw);
        assert_eq!(mapped.hash(), hash);
    }

    #[test]
    fn decoded_bal_try_map_converts_or_returns_error() {
        let raw = Bytes::from_static(&[0xc0]);
        let decoded = DecodedBal::new(Bal::default(), raw.clone());

        let mapped = decoded.try_map(|bal| Ok::<_, core::convert::Infallible>(bal.len())).unwrap();

        assert_eq!(mapped.as_bal(), &0);
        assert_eq!(mapped.as_raw(), &raw);

        let decoded = DecodedBal::new(Bal::default(), raw);
        let err = decoded.try_map(|_| Err::<usize, _>("expected error")).unwrap_err();

        assert_eq!(err, "expected error");
    }

    #[derive(Debug, PartialEq, Eq)]
    struct BalLen(usize);

    impl From<Bal> for BalLen {
        fn from(value: Bal) -> Self {
            Self(value.len())
        }
    }

    #[derive(Debug, PartialEq, Eq)]
    struct NonEmptyBal(Bal);

    #[derive(Clone, Copy, Debug, PartialEq, Eq)]
    struct EmptyBal;

    impl TryFrom<Bal> for NonEmptyBal {
        type Error = EmptyBal;

        fn try_from(value: Bal) -> Result<Self, Self::Error> {
            if value.is_empty() { Err(EmptyBal) } else { Ok(Self(value)) }
        }
    }

    #[test]
    fn decoded_bal_convert_and_try_convert_use_inner_conversions() {
        let raw = Bytes::from_static(&[0xc0]);
        let converted: DecodedBal<BalLen> = DecodedBal::new(Bal::default(), raw.clone()).convert();

        assert_eq!(converted.as_bal(), &BalLen(0));
        assert_eq!(converted.as_raw(), &raw);

        let err = DecodedBal::new(Bal::default(), raw.clone()).try_convert::<NonEmptyBal>();
        assert_eq!(err.unwrap_err(), EmptyBal);

        let bal = Bal::new(vec![AccountChanges::new(Address::from([0x11; 20]))]);
        let converted = DecodedBal::new(bal, raw).try_convert::<NonEmptyBal>().unwrap();

        assert_eq!(converted.as_bal().0.len(), 1);
    }

    #[test]
    fn decoded_bal_ensure_hash_reports_both_hashes() {
        let raw = Bytes::from_static(&[0xc0]);
        let decoded = DecodedBal::new(Bal::default(), raw.clone());
        let computed = alloy_primitives::keccak256(raw.as_ref());
        let expected = B256::from([0x11; 32]);

        assert_eq!(decoded.ensure_hash(computed), Ok(()));
        assert_eq!(
            decoded.ensure_hash(expected),
            Err(super::BlockAccessListHashMismatch::new(computed, expected))
        );
    }

    #[test]
    fn raw_bal_hash_uses_raw_bytes() {
        let raw = Bytes::from_static(&[0xc0]);
        let raw_bal = RawBal::new(raw.clone());
        let computed = alloy_primitives::keccak256(raw.as_ref());
        let expected = B256::from([0x11; 32]);

        assert_eq!(raw_bal.as_raw(), &raw);
        assert_eq!(raw_bal.hash(), computed);
        assert_eq!(raw_bal.ensure_hash(computed), Ok(()));
        assert_eq!(
            raw_bal.ensure_hash(expected),
            Err(super::BlockAccessListHashMismatch::new(computed, expected))
        );

        let (split_raw, split_hash) = raw_bal.into_parts();
        assert_eq!(split_raw, raw);
        assert_eq!(split_hash, computed);
    }

    #[test]
    fn raw_bal_new_unchecked_uses_supplied_hash() {
        let raw = Bytes::from_static(&[0xc0]);
        let hash = B256::from([0x11; 32]);
        let raw_bal = RawBal::new_unchecked(raw.clone(), hash);

        assert_eq!(raw_bal.as_raw(), &raw);
        assert_eq!(raw_bal.hash(), hash);
        assert_eq!(raw_bal.ensure_hash(hash), Ok(()));

        let (split_raw, split_hash) = raw_bal.into_parts();
        assert_eq!(split_raw, raw);
        assert_eq!(split_hash, hash);
    }

    #[test]
    fn decoded_bal_exposes_raw_bal() {
        let raw = Bytes::from_static(&[0xc0]);
        let raw_bal = RawBal::new(raw.clone());
        let decoded = DecodedBal::with_raw_bal(Bal::default(), raw_bal.clone());

        assert_eq!(decoded.as_raw_bal(), &raw_bal);
        assert_eq!(decoded.as_raw(), &raw);

        let (bal, split_raw_bal) = decoded.split_raw_bal();
        assert!(bal.is_empty());
        assert_eq!(split_raw_bal, raw_bal);
    }

    #[test]
    fn decoded_bal_new_unchecked_uses_supplied_hash() {
        let raw = Bytes::from_static(&[0xc0]);
        let hash = B256::from([0x11; 32]);
        let decoded = DecodedBal::new_unchecked(Bal::default(), raw.clone(), hash);

        assert_eq!(decoded.as_raw(), &raw);
        assert_eq!(decoded.hash(), hash);
        assert_eq!(decoded.ensure_hash(hash), Ok(()));
    }

    #[cfg(feature = "serde")]
    #[test]
    fn decoded_bal_serde_keeps_raw_bytes_field() {
        let raw = Bytes::from_static(&[0xc0]);
        let decoded = DecodedBal::new(Bal::default(), raw.clone());
        let value = serde_json::to_value(&decoded).unwrap();

        assert!(value.get("decoded").is_some());
        assert_eq!(value.get("raw"), Some(&serde_json::to_value(&raw).unwrap()));
        assert!(value.get("hash").is_none());

        let decoded = serde_json::from_value::<DecodedBal>(value).unwrap();
        assert_eq!(decoded.as_bal(), &Bal::default());
        assert_eq!(decoded.as_raw(), &raw);
    }

    #[test]
    fn raw_or_decoded_bal_raw_helpers_use_raw_bytes() {
        let raw = Bytes::from_static(&[0xc0]);
        let bal = RawOrDecodedBal::<Bal>::raw(raw.clone());
        let hash = alloy_primitives::keccak256(raw.as_ref());

        assert!(bal.is_raw());
        assert!(!bal.is_decoded());
        assert_eq!(bal.as_raw(), &raw);
        assert_eq!(bal.as_raw_bal().as_raw(), &raw);
        assert_eq!(bal.as_decoded(), None);
        assert_eq!(bal.as_bal(), None);
        assert_eq!(bal.hash(), hash);
        assert_eq!(bal.ensure_hash(hash), Ok(()));

        let (decoded, split_raw) = bal.clone().split();
        assert_eq!(decoded, None);
        assert_eq!(split_raw, raw);
        let (decoded, split_raw_bal) = bal.clone().split_raw_bal();
        assert_eq!(decoded, None);
        assert_eq!(split_raw_bal.as_raw(), &raw);
        assert_eq!(bal.clone().into_raw_bal().as_raw(), &raw);
        assert_eq!(bal.into_raw(), raw);
    }

    #[test]
    fn raw_or_decoded_bal_raw_unchecked_uses_supplied_hash() {
        let raw = Bytes::from_static(&[0xc0]);
        let hash = B256::from([0x11; 32]);
        let bal = RawOrDecodedBal::<Bal>::raw_unchecked(raw.clone(), hash);

        assert!(bal.is_raw());
        assert_eq!(bal.as_raw(), &raw);
        assert_eq!(bal.hash(), hash);
        assert_eq!(bal.ensure_hash(hash), Ok(()));
    }

    #[test]
    fn raw_or_decoded_bal_decoded_helpers_use_decoded_bal() {
        let raw = Bytes::from_static(&[0xc0]);
        let decoded = DecodedBal::new(Bal::default(), raw.clone());
        let hash = decoded.hash();
        let bal = RawOrDecodedBal::decoded(decoded.clone());

        assert!(!bal.is_raw());
        assert!(bal.is_decoded());
        assert_eq!(bal.as_raw(), &raw);
        assert_eq!(bal.as_raw_bal(), decoded.as_raw_bal());
        assert_eq!(bal.as_decoded(), Some(&decoded));
        assert_eq!(bal.as_bal(), Some(decoded.as_bal()));
        assert_eq!(bal.hash(), hash);

        let (split_bal, split_raw) = bal.clone().split();
        assert_eq!(split_bal, Some(Bal::default()));
        assert_eq!(split_raw, raw);
        let (split_bal, split_raw_bal) = bal.clone().split_raw_bal();
        assert_eq!(split_bal, Some(Bal::default()));
        assert_eq!(split_raw_bal.as_raw(), &raw);
        assert_eq!(bal.into_decoded(), Some(decoded));
    }

    #[test]
    fn raw_or_decoded_bal_convert_maps_only_decoded_values() {
        let raw = Bytes::from_static(&[0xc0]);
        let raw_bal: RawOrDecodedBal<Bal> = RawOrDecodedBal::raw(raw.clone());
        let converted_raw: RawOrDecodedBal<BalLen> = raw_bal.convert();

        assert!(converted_raw.is_raw());
        assert_eq!(converted_raw.as_raw(), &raw);
        assert_eq!(converted_raw.as_bal(), None);

        let decoded = DecodedBal::new(Bal::default(), raw.clone());
        let converted_decoded: RawOrDecodedBal<BalLen> =
            RawOrDecodedBal::decoded(decoded).convert();

        assert!(converted_decoded.is_decoded());
        assert_eq!(converted_decoded.as_bal(), Some(&BalLen(0)));
        assert_eq!(converted_decoded.as_raw(), &raw);

        let err = RawOrDecodedBal::decoded(DecodedBal::new(Bal::default(), raw.clone()))
            .try_convert::<NonEmptyBal>();
        assert_eq!(err.unwrap_err(), EmptyBal);

        let raw_result: Result<RawOrDecodedBal<NonEmptyBal>, EmptyBal> =
            RawOrDecodedBal::<Bal>::raw(raw.clone()).try_convert();
        let raw_result = raw_result.unwrap();
        assert!(raw_result.is_raw());
        assert_eq!(raw_result.as_raw(), &raw);
    }

    #[test]
    fn bal_sort_orders_all_eip7928_lists() {
        let address_1 = Address::from([0x11; 20]);
        let address_2 = Address::from([0x22; 20]);
        let mut bal = Bal::new(vec![
            AccountChanges {
                address: address_2,
                storage_changes: vec![
                    SlotChanges::new(
                        U256::from(3),
                        vec![
                            StorageChange::new(BlockAccessIndex::new(8), U256::from(0x80)),
                            StorageChange::new(BlockAccessIndex::new(2), U256::from(0x20)),
                        ],
                    ),
                    SlotChanges::new(
                        U256::from(1),
                        vec![
                            StorageChange::new(BlockAccessIndex::new(5), U256::from(0x50)),
                            StorageChange::new(BlockAccessIndex::new(1), U256::from(0x10)),
                        ],
                    ),
                ],
                storage_reads: vec![U256::from(4), U256::from(2)],
                balance_changes: vec![
                    BalanceChange::new(BlockAccessIndex::new(6), U256::from(600)),
                    BalanceChange::new(BlockAccessIndex::new(3), U256::from(300)),
                ],
                nonce_changes: vec![
                    NonceChange::new(BlockAccessIndex::new(7), 70),
                    NonceChange::new(BlockAccessIndex::new(4), 40),
                ],
                code_changes: vec![
                    CodeChange::new(BlockAccessIndex::new(9), Bytes::from_static(&[0x60, 0x09])),
                    CodeChange::new(BlockAccessIndex::new(5), Bytes::from_static(&[0x60, 0x05])),
                ],
            },
            AccountChanges {
                address: address_1,
                storage_changes: vec![
                    SlotChanges::new(
                        U256::from(2),
                        vec![
                            StorageChange::new(BlockAccessIndex::new(4), U256::from(0x40)),
                            StorageChange::new(BlockAccessIndex::new(0), U256::from(0x00)),
                        ],
                    ),
                    SlotChanges::new(
                        U256::from(1),
                        vec![
                            StorageChange::new(BlockAccessIndex::new(3), U256::from(0x30)),
                            StorageChange::new(BlockAccessIndex::new(1), U256::from(0x10)),
                        ],
                    ),
                ],
                storage_reads: vec![U256::from(5), U256::from(3)],
                balance_changes: vec![
                    BalanceChange::new(BlockAccessIndex::new(5), U256::from(500)),
                    BalanceChange::new(BlockAccessIndex::new(2), U256::from(200)),
                ],
                nonce_changes: vec![
                    NonceChange::new(BlockAccessIndex::new(8), 80),
                    NonceChange::new(BlockAccessIndex::new(1), 10),
                ],
                code_changes: vec![
                    CodeChange::new(BlockAccessIndex::new(4), Bytes::from_static(&[0x60, 0x04])),
                    CodeChange::new(BlockAccessIndex::new(2), Bytes::from_static(&[0x60, 0x02])),
                ],
            },
        ]);

        bal.sort();

        assert_eq!(bal[0].address, address_1);
        assert_eq!(bal[1].address, address_2);

        for account in bal.iter() {
            assert!(account.storage_changes.windows(2).all(|slots| slots[0].slot <= slots[1].slot));
            for slot_changes in &account.storage_changes {
                assert!(
                    slot_changes
                        .changes
                        .windows(2)
                        .all(|changes| changes[0].block_access_index
                            <= changes[1].block_access_index)
                );
            }
            assert!(account.storage_reads.windows(2).all(|slots| slots[0] <= slots[1]));
            assert!(
                account
                    .balance_changes
                    .windows(2)
                    .all(|changes| changes[0].block_access_index <= changes[1].block_access_index)
            );
            assert!(
                account
                    .nonce_changes
                    .windows(2)
                    .all(|changes| changes[0].block_access_index <= changes[1].block_access_index)
            );
            assert!(
                account
                    .code_changes
                    .windows(2)
                    .all(|changes| changes[0].block_access_index <= changes[1].block_access_index)
            );
        }
    }

    #[test]
    fn bal_validate_gas_limit_accepts_exact_item_cost() {
        let bal = Bal::new(vec![
            AccountChanges::new(Address::from([0x11; 20]))
                .with_storage_read(U256::from(1))
                .with_storage_change(SlotChanges::new(
                    U256::from(1),
                    vec![StorageChange::new(BlockAccessIndex::new(0), U256::from(0xaa))],
                )),
        ]);

        assert_eq!(bal.total_bal_items(), 2);
        assert_eq!(bal.validate_gas_limit(2 * ITEM_COST as u64), Ok(()));
    }

    #[test]
    fn bal_validate_gas_limit_rejects_item_cost_above_limit() {
        let bal = Bal::new(vec![
            AccountChanges::new(Address::from([0x11; 20]))
                .with_storage_read(U256::from(1))
                .with_storage_read(U256::from(2)),
        ]);
        let gas_limit = 3 * ITEM_COST as u64 - 1;

        assert_eq!(bal.total_bal_items(), 3);
        assert_eq!(
            bal.validate_gas_limit(gas_limit),
            Err(super::BlockAccessListGasError::new(3, gas_limit))
        );
    }
}

#[cfg(all(test, feature = "rlp"))]
mod tests {
    use super::bal::{Bal, DecodedBal, RawBal, RawOrDecodedBal};
    use crate::{
        AccountChanges, BalanceChange, BlockAccessIndex, CodeChange, NonceChange, SlotChanges,
        StorageChange, constants::EMPTY_BLOCK_ACCESS_LIST_HASH,
    };
    use alloy_primitives::{Address, Bytes, U256};

    fn sample_bal() -> Bal {
        Bal::new(vec![
            AccountChanges::new(Address::from([0x11; 20]))
                .with_storage_read(U256::from(0x10))
                .with_storage_change(SlotChanges::new(
                    U256::from(0x01),
                    vec![StorageChange::new(BlockAccessIndex::new(0), U256::from(0xaa))],
                ))
                .with_balance_change(BalanceChange::new(
                    BlockAccessIndex::new(1),
                    U256::from(1_000),
                ))
                .with_nonce_change(NonceChange::new(BlockAccessIndex::new(2), 7))
                .with_code_change(CodeChange::new(
                    BlockAccessIndex::new(3),
                    Bytes::from(vec![0x60, 0x00]),
                )),
            AccountChanges::new(Address::from([0x22; 20]))
                .with_storage_read(U256::from(0x20))
                .with_storage_change(SlotChanges::new(
                    U256::from(0x02),
                    vec![StorageChange::new(BlockAccessIndex::new(4), U256::from(0xbb))],
                )),
        ])
    }

    #[test]
    fn bal_compute_hash_returns_empty_hash_for_empty_bal() {
        let bal = Bal::default();

        assert_eq!(bal.compute_hash(), EMPTY_BLOCK_ACCESS_LIST_HASH);
    }

    #[test]
    fn bal_compute_hash_matches_free_function_for_non_empty_bal() {
        let bal = sample_bal();

        assert_eq!(bal.compute_hash(), super::compute_block_access_list_hash(bal.as_slice()));
        assert_ne!(bal.compute_hash(), EMPTY_BLOCK_ACCESS_LIST_HASH);
    }

    #[test]
    fn decoded_bal_from_rlp_bytes_preserves_raw_and_hash() {
        let bal = sample_bal();
        let raw = Bytes::from(alloy_rlp::encode(&bal));
        let decoded = DecodedBal::from_rlp_bytes(raw.clone()).unwrap();

        assert_eq!(decoded.as_bal(), &bal);
        assert_eq!(decoded.as_raw(), &raw);
        assert_eq!(decoded.hash(), bal.compute_hash());
        assert_eq!(decoded.hash(), alloy_primitives::keccak256(raw.as_ref()));
        assert_eq!(decoded.as_sealed_bal().hash(), bal.compute_hash());
        assert_eq!(decoded.as_sealed_bal().inner(), &decoded.as_bal());

        let (split_bal, split_raw) = decoded.clone().split();
        assert_eq!(split_bal, bal);
        assert_eq!(split_raw, raw);

        let (split_bal, split_raw, split_hash) = decoded.clone().into_parts();
        assert_eq!(split_bal, bal);
        assert_eq!(split_raw, raw);
        assert_eq!(split_hash, bal.compute_hash());

        let sealed = decoded.into_sealed();
        assert_eq!(sealed.hash(), bal.compute_hash());
        assert_eq!(sealed.inner(), &bal);
    }

    #[test]
    fn decoded_bal_from_rlp_bytes_decodes_generic_inner_type() {
        let bal = sample_bal();
        let raw = Bytes::from(alloy_rlp::encode(&bal));
        let decoded = DecodedBal::from_rlp_bytes_as::<Vec<AccountChanges>>(raw.clone()).unwrap();

        assert_eq!(decoded.as_bal().as_slice(), bal.as_slice());
        assert_eq!(decoded.as_raw(), &raw);
        assert_eq!(decoded.hash(), bal.compute_hash());
    }

    #[test]
    fn decoded_bal_decode_consumes_exact_raw_rlp_item() {
        let bal = sample_bal();
        let raw = alloy_rlp::encode(&bal);
        let mut buf = raw.as_ref();
        let decoded = <DecodedBal as alloy_rlp::Decodable>::decode(&mut buf).unwrap();

        assert!(buf.is_empty());
        assert_eq!(decoded.as_bal(), &bal);
        assert_eq!(decoded.as_raw().as_ref(), raw.as_slice());
        assert_eq!(alloy_rlp::encode(&decoded), raw);
    }

    #[test]
    fn raw_bal_rlp_roundtrip_preserves_raw_item() {
        let bal = sample_bal();
        let raw = alloy_rlp::encode(&bal);
        let mut buf = raw.as_ref();
        let raw_bal = <RawBal as alloy_rlp::Decodable>::decode(&mut buf).unwrap();

        assert!(buf.is_empty());
        assert_eq!(raw_bal.as_raw().as_ref(), raw.as_slice());
        assert_eq!(alloy_rlp::encode(&raw_bal), raw);
        assert_eq!(raw_bal.hash(), bal.compute_hash());
    }

    #[test]
    fn raw_or_decoded_bal_try_into_decoded_decodes_raw() {
        let bal = sample_bal();
        let raw = Bytes::from(alloy_rlp::encode(&bal));
        let decoded = RawOrDecodedBal::<Bal>::raw(raw.clone()).try_into_decoded().unwrap();

        assert_eq!(decoded.as_bal(), &bal);
        assert_eq!(decoded.as_raw(), &raw);
        assert_eq!(decoded.hash(), bal.compute_hash());
    }

    #[test]
    fn raw_or_decoded_bal_try_into_decoded_reuses_decoded() {
        let bal = sample_bal();
        let raw = Bytes::from(alloy_rlp::encode(&bal));
        let decoded = DecodedBal::new(bal.clone(), raw.clone());
        let decoded = RawOrDecodedBal::decoded(decoded).try_into_decoded().unwrap();

        assert_eq!(decoded.as_bal(), &bal);
        assert_eq!(decoded.as_raw(), &raw);
        assert_eq!(decoded.hash(), bal.compute_hash());
    }

    #[test]
    fn raw_or_decoded_bal_rlp_encodes_raw_bytes() {
        let bal = sample_bal();
        let raw = alloy_rlp::encode(&bal);
        let raw_bal = RawOrDecodedBal::<Bal>::raw(Bytes::from(raw.clone()));
        let decoded_bal = RawOrDecodedBal::decoded(DecodedBal::new(bal, Bytes::from(raw.clone())));

        assert_eq!(alloy_rlp::encode(&raw_bal), raw);
        assert_eq!(alloy_rlp::encode(&decoded_bal), raw);
    }

    #[test]
    fn raw_or_decoded_bal_decode_preserves_raw_rlp_item() {
        let bal = sample_bal();
        let raw = alloy_rlp::encode(&bal);
        let mut buf = raw.as_ref();
        let decoded = <RawOrDecodedBal as alloy_rlp::Decodable>::decode(&mut buf).unwrap();

        assert!(buf.is_empty());
        assert!(decoded.is_raw());
        assert_eq!(decoded.as_raw().as_ref(), raw.as_slice());
        assert_eq!(alloy_rlp::encode(&decoded), raw);

        let decoded = decoded.try_into_decoded().unwrap();
        assert_eq!(decoded.as_bal(), &bal);
    }
}