kona-engine 0.1.2

An implementation of the OP Stack engine client
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
//! Contains a utility method to check if attributes match a block.

use alloy_eips::{Decodable2718, eip1559::BaseFeeParams};
use alloy_network::TransactionResponse;
use alloy_primitives::{Address, B256, Bytes};
use alloy_rpc_types_eth::{Block, BlockTransactions, Withdrawals};
use kona_genesis::RollupConfig;
use kona_protocol::OpAttributesWithParent;
use op_alloy_consensus::{EIP1559ParamError, OpTxEnvelope, decode_holocene_extra_data};
use op_alloy_rpc_types::Transaction;

/// Result of validating payload attributes against an execution layer block.
///
/// Used to verify that proposed payload attributes match the actual executed block,
/// ensuring consistency between the rollup derivation process and execution layer.
/// Validation includes withdrawals, transactions, fees, and other block properties.
///
/// # Examples
///
/// ```rust,ignore
/// use kona_engine::AttributesMatch;
/// use kona_genesis::RollupConfig;
/// use kona_protocol::OpAttributesWithParent;
///
/// let config = RollupConfig::default();
/// let match_result = AttributesMatch::check_withdrawals(&config, &attributes, &block);
///
/// if match_result.is_match() {
///     println!("Attributes are valid for this block");
/// }
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AttributesMatch {
    /// The payload attributes are consistent with the block.
    Match,
    /// The attributes do not match the block (contains mismatch details).
    Mismatch(AttributesMismatch),
}

impl AttributesMatch {
    /// Returns true if the attributes match the block.
    pub const fn is_match(&self) -> bool {
        matches!(self, Self::Match)
    }

    /// Returns true if the attributes do not match the block.
    pub const fn is_mismatch(&self) -> bool {
        matches!(self, Self::Mismatch(_))
    }

    /// Checks that withdrawals for a block and attributes match.
    pub fn check_withdrawals(
        config: &RollupConfig,
        attributes: &OpAttributesWithParent,
        block: &Block<Transaction>,
    ) -> Self {
        let attr_withdrawals = attributes.inner().payload_attributes.withdrawals.as_ref();
        let attr_withdrawals = attr_withdrawals.map(|w| Withdrawals::new(w.to_vec()));
        let block_withdrawals = block.withdrawals.as_ref();

        if config.is_canyon_active(block.header.timestamp) {
            // In canyon, the withdrawals list should be some and empty
            if attr_withdrawals.is_none_or(|w| !w.is_empty()) {
                return Self::Mismatch(AttributesMismatch::CanyonWithdrawalsNotEmpty);
            }
            if block_withdrawals.is_none_or(|w| !w.is_empty()) {
                return Self::Mismatch(AttributesMismatch::CanyonWithdrawalsNotEmpty);
            }
            if !config.is_isthmus_active(block.header.timestamp) {
                // In canyon, the withdrawals root should be set to the empty value
                let empty_hash = alloy_consensus::EMPTY_ROOT_HASH;
                if block.header.inner.withdrawals_root != Some(empty_hash) {
                    return Self::Mismatch(AttributesMismatch::CanyonNotEmptyHash);
                }
            }
        } else {
            // In bedrock, the withdrawals list should be None
            if attr_withdrawals.is_some() {
                return Self::Mismatch(AttributesMismatch::BedrockWithdrawals);
            }
        }

        if config.is_isthmus_active(block.header.timestamp) {
            // In isthmus, the withdrawals root must be set
            if block.header.inner.withdrawals_root.is_none() {
                return Self::Mismatch(AttributesMismatch::IsthmusMissingWithdrawalsRoot);
            }
        }

        Self::Match
    }

    /// Checks the attributes and block transaction list for consolidation.
    /// We start by checking that there are the same number of transactions in both the attribute
    /// payload and the block. Then we compare their contents
    fn check_transactions(attributes_txs: &[Bytes], block: &Block<Transaction>) -> Self {
        // Before checking the number of transactions, we have to make sure that the block
        // has the right transactions format. We need to have access to the
        // full transactions to be able to compare their contents.
        let block_txs = match block.transactions {
            BlockTransactions::Hashes(_) | BlockTransactions::Full(_)
                if attributes_txs.is_empty() && block.transactions.is_empty() =>
            {
                // We early return when both attributes and blocks are empty. This is for ergonomics
                // because the default [`BlockTransactions`] format is
                // [`BlockTransactions::Hash`], which may cause
                // the [`BlockTransactions`] format check to fail right below. We may want to be a
                // bit more flexible and not reject the hash format if both the
                // attributes and the block are empty.
                return Self::Match;
            }
            BlockTransactions::Uncle => {
                // This can never be uncle transactions
                error!(
                    "Invalid format for the block transactions. The `Uncle` transaction format is not relevant in that context and should not get used here. This is a bug"
                );

                return AttributesMismatch::MalformedBlockTransactions.into();
            }
            BlockTransactions::Hashes(_) => {
                // We can't have hash transactions with non empty blocks
                error!(
                    "Invalid format for the block transactions. The `Hash` transaction format is not relevant in that context and should not get used here. This is a bug."
                );

                return AttributesMismatch::MalformedBlockTransactions.into();
            }
            BlockTransactions::Full(ref block_txs) => block_txs,
        };

        let attributes_txs_len = attributes_txs.len();
        let block_txs_len = block_txs.len();

        if attributes_txs_len != block_txs_len {
            return AttributesMismatch::TransactionLen(attributes_txs_len, block_txs_len).into();
        }

        // Then we need to check that the content of the encoded transactions match
        // Note that it is safe to zip both iterators because we checked their length
        // beforehand.
        for (attr_tx_bytes, block_tx) in attributes_txs.iter().zip(block_txs) {
            trace!(
                target: "engine",
                ?attr_tx_bytes,
                block_tx_hash = %block_tx.tx_hash(),
                "Checking attributes transaction against block transaction",
            );
            // Let's try to deserialize the attributes transaction
            let Ok(attr_tx) = OpTxEnvelope::decode_2718(&mut &attr_tx_bytes[..]) else {
                error!(
                    "Impossible to deserialize transaction from attributes. If we have stored these attributes it means the transactions where well formatted. This is a bug"
                );

                return AttributesMismatch::MalformedAttributesTransaction.into();
            };

            if &attr_tx != block_tx.inner.inner.inner() {
                warn!(target: "engine", ?attr_tx, ?block_tx, "Transaction mismatch in derived attributes");
                return AttributesMismatch::TransactionContent(attr_tx.tx_hash(), block_tx.tx_hash())
                    .into()
            }
        }

        Self::Match
    }

    /// Validates and compares EIP1559 parameters for consolidation.
    fn check_eip1559(
        config: &RollupConfig,
        attributes: &OpAttributesWithParent,
        block: &Block<Transaction>,
    ) -> Self {
        // We can assume that the EIP-1559 params are set iff holocene is active.
        // Note here that we don't need to check for the attributes length because of type-safety.
        let (ae, ad): (u128, u128) = match attributes.inner().decode_eip_1559_params() {
            None => {
                // Holocene is active but the eip1559 are not set. This is a bug!
                // Note: we checked the timestamp match above, so we can assume that both the
                // attributes and the block have the same stamps
                if config.is_holocene_active(block.header.timestamp) {
                    error!(
                        "EIP1559 parameters for attributes not set while holocene is active. This is a bug"
                    );
                    return AttributesMismatch::MissingAttributesEIP1559.into();
                }

                // If the attributes are not specified, that means we can just early return.
                return Self::Match;
            }
            Some((0, e)) if e != 0 => {
                error!(
                    "Holocene EIP1559 params cannot have a 0 denominator unless elasticity is also 0. This is a bug"
                );
                return AttributesMismatch::InvalidEIP1559ParamsCombination.into();
            }
            // We need to translate (0, 0) parameters to pre-holocene protocol constants.
            // Since holocene is supposed to be active, canyon should be as well. We take the canyon
            // base fee params.
            Some((0, 0)) => {
                let BaseFeeParams { max_change_denominator, elasticity_multiplier } =
                    config.chain_op_config.as_canyon_base_fee_params();

                (elasticity_multiplier, max_change_denominator)
            }
            Some((ae, ad)) => (ae.into(), ad.into()),
        };

        // We decode the extra data stemming from the block header.
        let (be, bd): (u128, u128) = match decode_holocene_extra_data(&block.header.extra_data) {
            Ok((be, bd)) => (be.into(), bd.into()),
            Err(EIP1559ParamError::NoEIP1559Params) => {
                error!(
                    "EIP1559 parameters for the block not set while holocene is active. This is a bug"
                );
                return AttributesMismatch::MissingBlockEIP1559.into();
            }
            Err(EIP1559ParamError::InvalidVersion(v)) => {
                error!(
                    version = v,
                    "The version in the extra data EIP1559 payload is incorrect. Should be 0. This is a bug",
                );
                return AttributesMismatch::InvalidExtraDataVersion.into();
            }
            Err(e) => {
                error!(err = ?e, "An unknown extra data decoding error occurred. This is a bug",);

                return AttributesMismatch::UnknownExtraDataDecodingError(e).into();
            }
        };

        // We now have to check that both parameters match
        if ae != be || ad != bd {
            return AttributesMismatch::EIP1559Parameters(
                BaseFeeParams { max_change_denominator: ad, elasticity_multiplier: ae },
                BaseFeeParams { max_change_denominator: bd, elasticity_multiplier: be },
            )
            .into()
        }

        Self::Match
    }

    /// Checks if the specified [`OpAttributesWithParent`] matches the specified [`Block`].
    /// Returns [`AttributesMatch::Match`] if they match, otherwise returns
    /// [`AttributesMatch::Mismatch`].
    pub fn check(
        config: &RollupConfig,
        attributes: &OpAttributesWithParent,
        block: &Block<Transaction>,
    ) -> Self {
        if attributes.parent.block_info.hash != block.header.inner.parent_hash {
            return AttributesMismatch::ParentHash(
                attributes.parent.block_info.hash,
                block.header.inner.parent_hash,
            )
            .into();
        }

        if attributes.inner().payload_attributes.timestamp != block.header.inner.timestamp {
            return AttributesMismatch::Timestamp(
                attributes.inner().payload_attributes.timestamp,
                block.header.inner.timestamp,
            )
            .into();
        }

        let mix_hash = block.header.inner.mix_hash;
        if attributes.inner().payload_attributes.prev_randao != mix_hash {
            return AttributesMismatch::PrevRandao(
                attributes.inner().payload_attributes.prev_randao,
                mix_hash,
            )
            .into();
        }

        // Let's extract the list of attribute transactions
        let default_vec = vec![];
        let attributes_txs =
            attributes.inner().transactions.as_ref().map_or_else(|| &default_vec, |attrs| attrs);

        // Check transactions
        if let mismatch @ Self::Mismatch(_) = Self::check_transactions(attributes_txs, block) {
            return mismatch
        }

        let Some(gas_limit) = attributes.inner().gas_limit else {
            return AttributesMismatch::MissingAttributesGasLimit.into();
        };

        if gas_limit != block.header.inner.gas_limit {
            return AttributesMismatch::GasLimit(gas_limit, block.header.inner.gas_limit).into();
        }

        if let Self::Mismatch(m) = Self::check_withdrawals(config, attributes, block) {
            return m.into();
        }

        if attributes.inner().payload_attributes.parent_beacon_block_root !=
            block.header.inner.parent_beacon_block_root
        {
            return AttributesMismatch::ParentBeaconBlockRoot(
                attributes.inner().payload_attributes.parent_beacon_block_root,
                block.header.inner.parent_beacon_block_root,
            )
            .into();
        }

        if attributes.inner().payload_attributes.suggested_fee_recipient !=
            block.header.inner.beneficiary
        {
            return AttributesMismatch::FeeRecipient(
                attributes.inner().payload_attributes.suggested_fee_recipient,
                block.header.inner.beneficiary,
            )
            .into();
        }

        // Check the EIP-1559 parameters in a separate helper method
        if let m @ Self::Mismatch(_) = Self::check_eip1559(config, attributes, block) {
            return m;
        }

        Self::Match
    }
}

/// An enum over the type of mismatch between [`OpAttributesWithParent`]
/// and a [`Block`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AttributesMismatch {
    /// The parent hash of the block does not match the parent hash of the attributes.
    ParentHash(B256, B256),
    /// The timestamp of the block does not match the timestamp of the attributes.
    Timestamp(u64, u64),
    /// The prev randao of the block does not match the prev randao of the attributes.
    PrevRandao(B256, B256),
    /// The block contains malformed transactions. This is a bug - the transaction format
    /// should be checked before the consolidation step.
    MalformedBlockTransactions,
    /// There is a malformed transaction inside the attributes. This is a bug - the transaction
    /// format should be checked before the consolidation step.
    MalformedAttributesTransaction,
    /// A mismatch in the number of transactions contained in the attributes and the block.
    TransactionLen(usize, usize),
    /// A mismatch in the content of some transactions contained in the attributes and the block.
    TransactionContent(B256, B256),
    /// The EIP1559 payload for the [`OpAttributesWithParent`] is missing when holocene is active.
    MissingAttributesEIP1559,
    /// The EIP1559 payload for the block is missing when holocene is active.
    MissingBlockEIP1559,
    /// The version in the extra data EIP1559 payload is incorrect. Should be 0.
    InvalidExtraDataVersion,
    /// An unknown extra data decoding error occurred.
    UnknownExtraDataDecodingError(EIP1559ParamError),
    /// Holocene EIP1559 params cannot have a 0 denominator unless elasticity is also 0
    InvalidEIP1559ParamsCombination,
    /// The EIP1559 base fee parameters of the attributes and the block don't match
    EIP1559Parameters(BaseFeeParams, BaseFeeParams),
    /// Transactions mismatch.
    Transactions(u64, u64),
    /// The gas limit of the block does not match the gas limit of the attributes.
    GasLimit(u64, u64),
    /// The gas limit for the [`OpAttributesWithParent`] is missing.
    MissingAttributesGasLimit,
    /// The fee recipient of the block does not match the fee recipient of the attributes.
    FeeRecipient(Address, Address),
    /// A mismatch in the parent beacon block root.
    ParentBeaconBlockRoot(Option<B256>, Option<B256>),
    /// After the canyon hardfork, withdrawals cannot be empty.
    CanyonWithdrawalsNotEmpty,
    /// After the canyon hardfork, the withdrawals root must be the empty hash.
    CanyonNotEmptyHash,
    /// In the bedrock hardfork, the attributes must has empty withdrawals.
    BedrockWithdrawals,
    /// In the isthmus hardfork, the withdrawals root must be set.
    IsthmusMissingWithdrawalsRoot,
}

impl From<AttributesMismatch> for AttributesMatch {
    fn from(mismatch: AttributesMismatch) -> Self {
        Self::Mismatch(mismatch)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::AttributesMismatch::EIP1559Parameters;
    use alloy_consensus::EMPTY_ROOT_HASH;
    use alloy_primitives::{Bytes, FixedBytes, address, b256};
    use alloy_rpc_types_eth::BlockTransactions;
    use arbitrary::{Arbitrary, Unstructured};
    use kona_protocol::{BlockInfo, L2BlockInfo};
    use kona_registry::ROLLUP_CONFIGS;
    use op_alloy_consensus::encode_holocene_extra_data;
    use op_alloy_rpc_types_engine::OpPayloadAttributes;

    fn default_attributes() -> OpAttributesWithParent {
        OpAttributesWithParent {
            inner: OpPayloadAttributes::default(),
            parent: L2BlockInfo::default(),
            derived_from: Some(BlockInfo::default()),
            is_last_in_span: true,
        }
    }

    fn default_rollup_config() -> &'static RollupConfig {
        let opm = 10;
        ROLLUP_CONFIGS.get(&opm).expect("default rollup config should exist")
    }

    #[test]
    fn test_attributes_match_parent_hash_mismatch() {
        let cfg = default_rollup_config();
        let attributes = default_attributes();
        let mut block = Block::<Transaction>::default();
        block.header.inner.parent_hash =
            b256!("1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef");
        let check = AttributesMatch::check(cfg, &attributes, &block);
        let expected: AttributesMatch = AttributesMismatch::ParentHash(
            attributes.parent.block_info.hash,
            block.header.inner.parent_hash,
        )
        .into();
        assert_eq!(check, expected);
        assert!(check.is_mismatch());
    }

    #[test]
    fn test_attributes_match_check_timestamp() {
        let cfg = default_rollup_config();
        let attributes = default_attributes();
        let mut block = Block::<Transaction>::default();
        block.header.inner.timestamp = 1234567890;
        let check = AttributesMatch::check(cfg, &attributes, &block);
        let expected: AttributesMatch = AttributesMismatch::Timestamp(
            attributes.inner().payload_attributes.timestamp,
            block.header.inner.timestamp,
        )
        .into();
        assert_eq!(check, expected);
        assert!(check.is_mismatch());
    }

    #[test]
    fn test_attributes_match_check_prev_randao() {
        let cfg = default_rollup_config();
        let attributes = default_attributes();
        let mut block = Block::<Transaction>::default();
        block.header.inner.mix_hash =
            b256!("1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef");
        let check = AttributesMatch::check(cfg, &attributes, &block);
        let expected: AttributesMatch = AttributesMismatch::PrevRandao(
            attributes.inner().payload_attributes.prev_randao,
            block.header.inner.mix_hash,
        )
        .into();
        assert_eq!(check, expected);
        assert!(check.is_mismatch());
    }

    #[test]
    fn test_attributes_match_missing_gas_limit() {
        let cfg = default_rollup_config();
        let attributes = default_attributes();
        let mut block = Block::<Transaction>::default();
        block.header.inner.gas_limit = 123456;
        let check = AttributesMatch::check(cfg, &attributes, &block);
        let expected: AttributesMatch = AttributesMismatch::MissingAttributesGasLimit.into();
        assert_eq!(check, expected);
        assert!(check.is_mismatch());
    }

    #[test]
    fn test_attributes_match_check_gas_limit() {
        let cfg = default_rollup_config();
        let mut attributes = default_attributes();
        attributes.inner.gas_limit = Some(123457);
        let mut block = Block::<Transaction>::default();
        block.header.inner.gas_limit = 123456;
        let check = AttributesMatch::check(cfg, &attributes, &block);
        let expected: AttributesMatch = AttributesMismatch::GasLimit(
            attributes.inner().gas_limit.unwrap_or_default(),
            block.header.inner.gas_limit,
        )
        .into();
        assert_eq!(check, expected);
        assert!(check.is_mismatch());
    }

    #[test]
    fn test_attributes_match_check_parent_beacon_block_root() {
        let cfg = default_rollup_config();
        let mut attributes = default_attributes();
        attributes.inner.gas_limit = Some(0);
        attributes.inner.payload_attributes.parent_beacon_block_root =
            Some(b256!("1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef"));
        let block = Block::<Transaction>::default();
        let check = AttributesMatch::check(cfg, &attributes, &block);
        let expected: AttributesMatch = AttributesMismatch::ParentBeaconBlockRoot(
            attributes.inner().payload_attributes.parent_beacon_block_root,
            block.header.inner.parent_beacon_block_root,
        )
        .into();
        assert_eq!(check, expected);
        assert!(check.is_mismatch());
    }

    #[test]
    fn test_attributes_match_check_fee_recipient() {
        let cfg = default_rollup_config();
        let mut attributes = default_attributes();
        attributes.inner.gas_limit = Some(0);
        let mut block = Block::<Transaction>::default();
        block.header.inner.beneficiary = address!("1234567890abcdef1234567890abcdef12345678");
        let check = AttributesMatch::check(cfg, &attributes, &block);
        let expected: AttributesMatch = AttributesMismatch::FeeRecipient(
            attributes.inner().payload_attributes.suggested_fee_recipient,
            block.header.inner.beneficiary,
        )
        .into();
        assert_eq!(check, expected);
        assert!(check.is_mismatch());
    }

    fn generate_txs(num_txs: usize) -> Vec<Transaction> {
        // Simulate some random data
        let mut data = vec![0; 1024];
        let mut rng = rand::rng();

        (0..num_txs)
            .map(|_| {
                rand::Rng::fill(&mut rng, &mut data[..]);

                // Create unstructured data with the random bytes
                let u = Unstructured::new(&data);

                // Generate a random instance of MyStruct
                Transaction::arbitrary_take_rest(u).expect("Impossible to generate arbitrary tx")
            })
            .collect()
    }

    fn test_transactions_match_helper() -> (OpAttributesWithParent, Block<Transaction>) {
        const NUM_TXS: usize = 10;

        let transactions = generate_txs(NUM_TXS);
        let mut attributes = default_attributes();
        attributes.inner.gas_limit = Some(0);
        attributes.inner.transactions = Some(
            transactions
                .iter()
                .map(|tx| {
                    use alloy_eips::Encodable2718;
                    let mut buf = vec![];
                    tx.inner.inner.inner().encode_2718(&mut buf);
                    Bytes::from(buf)
                })
                .collect::<Vec<_>>(),
        );

        let block = Block::<Transaction> {
            transactions: BlockTransactions::Full(transactions),
            ..Default::default()
        };

        (attributes, block)
    }

    #[test]
    fn test_attributes_match_check_transactions() {
        let cfg = default_rollup_config();
        let (attributes, block) = test_transactions_match_helper();
        let check = AttributesMatch::check(cfg, &attributes, &block);
        assert_eq!(check, AttributesMatch::Match);
    }

    #[test]
    fn test_attributes_mismatch_check_transactions_len() {
        let cfg = default_rollup_config();
        let (mut attributes, block) = test_transactions_match_helper();
        attributes.inner = OpPayloadAttributes {
            transactions: attributes.inner.transactions.map(|mut txs| {
                txs.pop();
                txs
            }),
            ..attributes.inner
        };

        let block_txs_len = block.transactions.len();

        let expected: AttributesMatch =
            AttributesMismatch::TransactionLen(block_txs_len - 1, block_txs_len).into();

        let check = AttributesMatch::check(cfg, &attributes, &block);
        assert_eq!(check, expected);
        assert!(check.is_mismatch());
    }

    #[test]
    fn test_attributes_mismatch_check_transaction_content() {
        let cfg = default_rollup_config();
        let (attributes, mut block) = test_transactions_match_helper();
        let BlockTransactions::Full(block_txs) = &mut block.transactions else {
            unreachable!("The helper should build a full list of transactions")
        };

        let first_tx = block_txs.last().unwrap().clone();
        let first_tx_hash = first_tx.tx_hash();

        // We set the last tx to be the same as the first transaction.
        // Since the transactions are generated randomly and there are more than one transaction,
        // there is a very high likelihood that any pair of transactions is distinct.
        let last_tx = block_txs.first_mut().unwrap();
        let last_tx_hash = last_tx.tx_hash();
        *last_tx = first_tx;

        let expected: AttributesMatch =
            AttributesMismatch::TransactionContent(last_tx_hash, first_tx_hash).into();

        let check = AttributesMatch::check(cfg, &attributes, &block);
        assert_eq!(check, expected);
        assert!(check.is_mismatch());
    }

    /// Checks the edge case where the attributes array is empty.
    #[test]
    fn test_attributes_mismatch_empty_tx_attributes() {
        let cfg = default_rollup_config();
        let (mut attributes, block) = test_transactions_match_helper();
        attributes.inner = OpPayloadAttributes { transactions: None, ..attributes.inner };

        let block_txs_len = block.transactions.len();

        let expected: AttributesMatch = AttributesMismatch::TransactionLen(0, block_txs_len).into();

        let check = AttributesMatch::check(cfg, &attributes, &block);
        assert_eq!(check, expected);
        assert!(check.is_mismatch());
    }

    /// Checks the edge case where the transactions contained in the block have the wrong
    /// format.
    #[test]
    fn test_block_transactions_wrong_format() {
        let cfg = default_rollup_config();
        let (attributes, mut block) = test_transactions_match_helper();
        block.transactions = BlockTransactions::Uncle;

        let expected: AttributesMatch = AttributesMismatch::MalformedBlockTransactions.into();

        let check = AttributesMatch::check(cfg, &attributes, &block);
        assert_eq!(check, expected);
        assert!(check.is_mismatch());
    }

    /// Checks the edge case where the transactions contained in the attributes have the wrong
    /// format.
    #[test]
    fn test_attributes_transactions_wrong_format() {
        let cfg = default_rollup_config();
        let (mut attributes, block) = test_transactions_match_helper();
        let txs = attributes.inner.transactions.as_mut().unwrap();
        let first_tx_bytes = txs.first_mut().unwrap();
        *first_tx_bytes = Bytes::copy_from_slice(&[0, 1, 2]);

        let expected: AttributesMatch = AttributesMismatch::MalformedAttributesTransaction.into();

        let check = AttributesMatch::check(cfg, &attributes, &block);
        assert_eq!(check, expected);
        assert!(check.is_mismatch());
    }

    // Test that the check pass if the transactions obtained from the attributes have the format
    // `Some(vec![])`, ie an empty vector inside a `Some` option.
    #[test]
    fn test_attributes_and_block_transactions_empty() {
        let cfg = default_rollup_config();
        let (mut attributes, mut block) = test_transactions_match_helper();

        attributes.inner = OpPayloadAttributes { transactions: Some(vec![]), ..attributes.inner };

        block.transactions = BlockTransactions::Full(vec![]);

        let check = AttributesMatch::check(cfg, &attributes, &block);
        assert_eq!(check, AttributesMatch::Match);

        // Edge case: if the block transactions and the payload attributes are empty, we can also
        // use the hash format (this is the default value of `BlockTransactions`).
        attributes.inner = OpPayloadAttributes { transactions: None, ..attributes.inner };
        block.transactions = BlockTransactions::Hashes(vec![]);

        let check = AttributesMatch::check(cfg, &attributes, &block);
        assert_eq!(check, AttributesMatch::Match);
    }

    // Edge case: if the payload attributes has the format `Some(vec![])`, we can still
    // use the hash format.
    #[test]
    fn test_attributes_and_block_transactions_empty_hash_format() {
        let cfg = default_rollup_config();
        let (mut attributes, mut block) = test_transactions_match_helper();

        attributes.inner = OpPayloadAttributes { transactions: Some(vec![]), ..attributes.inner };

        block.transactions = BlockTransactions::Hashes(vec![]);

        let check = AttributesMatch::check(cfg, &attributes, &block);
        assert_eq!(check, AttributesMatch::Match);
    }

    // Test that the check fails if the block format is incorrect and the attributes are empty
    #[test]
    fn test_attributes_empty_and_block_uncle() {
        let cfg = default_rollup_config();
        let (mut attributes, mut block) = test_transactions_match_helper();

        attributes.inner = OpPayloadAttributes { transactions: Some(vec![]), ..attributes.inner };

        block.transactions = BlockTransactions::Uncle;

        let expected: AttributesMatch = AttributesMismatch::MalformedBlockTransactions.into();

        let check = AttributesMatch::check(cfg, &attributes, &block);
        assert_eq!(check, expected);
    }

    fn eip1559_test_setup() -> (RollupConfig, OpAttributesWithParent, Block<Transaction>) {
        let mut cfg = default_rollup_config().clone();

        // We need to activate holocene to make sure it works! We set the activation time to zero to
        // make sure that it is activated by default.
        cfg.hardforks.holocene_time = Some(0);

        let mut attributes = default_attributes();
        attributes.inner.gas_limit = Some(0);
        // For canyon and above we need to specify the withdrawals
        attributes.inner.payload_attributes.withdrawals = Some(vec![]);

        // For canyon and above we also need to specify the withdrawal headers
        let block = Block {
            withdrawals: Some(Withdrawals(vec![])),
            header: alloy_rpc_types_eth::Header {
                inner: alloy_consensus::Header {
                    withdrawals_root: Some(EMPTY_ROOT_HASH),
                    ..Default::default()
                },
                ..Default::default()
            },
            ..Default::default()
        };

        (cfg, attributes, block)
    }

    /// Ensures that we have to set the EIP1559 parameters for holocene and above.
    #[test]
    fn test_eip1559_parameters_not_specified_holocene() {
        let (cfg, attributes, block) = eip1559_test_setup();

        let check = AttributesMatch::check(&cfg, &attributes, &block);
        assert_eq!(check, AttributesMatch::Mismatch(AttributesMismatch::MissingAttributesEIP1559));
        assert!(check.is_mismatch());
    }

    /// Ensures that we have to set the EIP1559 parameters for holocene and above.
    #[test]
    fn test_eip1559_parameters_specified_attributes_but_not_block() {
        let (cfg, mut attributes, block) = eip1559_test_setup();

        attributes.inner.eip_1559_params = Some(Default::default());

        let check = AttributesMatch::check(&cfg, &attributes, &block);
        assert_eq!(check, AttributesMatch::Mismatch(AttributesMismatch::MissingBlockEIP1559));
        assert!(check.is_mismatch());
    }

    /// Check that, when the eip1559 params are specified and empty, the check fails because we
    /// fallback on canyon params for the attributes but not for the block (edge case).
    #[test]
    fn test_eip1559_parameters_specified_both_and_empty() {
        let (cfg, mut attributes, mut block) = eip1559_test_setup();

        attributes.inner.eip_1559_params = Some(Default::default());
        block.header.extra_data = vec![0; 9].into();

        let check = AttributesMatch::check(&cfg, &attributes, &block);
        assert_eq!(
            check,
            AttributesMatch::Mismatch(EIP1559Parameters(
                BaseFeeParams { max_change_denominator: 250, elasticity_multiplier: 6 },
                BaseFeeParams { max_change_denominator: 0, elasticity_multiplier: 0 }
            ))
        );
        assert!(check.is_mismatch());
    }

    #[test]
    fn test_eip1559_parameters_empty_for_attr_only() {
        let (cfg, mut attributes, mut block) = eip1559_test_setup();

        attributes.inner.eip_1559_params = Some(Default::default());
        block.header.extra_data = encode_holocene_extra_data(
            Default::default(),
            BaseFeeParams { max_change_denominator: 250, elasticity_multiplier: 6 },
        )
        .unwrap();

        let check = AttributesMatch::check(&cfg, &attributes, &block);
        assert_eq!(check, AttributesMatch::Match);
        assert!(check.is_match());
    }

    #[test]
    fn test_eip1559_parameters_custom_values_match() {
        let (cfg, mut attributes, mut block) = eip1559_test_setup();

        let eip1559_extra_params = encode_holocene_extra_data(
            Default::default(),
            BaseFeeParams { max_change_denominator: 100, elasticity_multiplier: 2 },
        )
        .unwrap();
        let eip1559_params: FixedBytes<8> =
            eip1559_extra_params.clone().split_off(1).as_ref().try_into().unwrap();

        attributes.inner.eip_1559_params = Some(eip1559_params);
        block.header.extra_data = eip1559_extra_params;

        let check = AttributesMatch::check(&cfg, &attributes, &block);
        assert_eq!(check, AttributesMatch::Match);
        assert!(check.is_match());
    }

    #[test]
    fn test_eip1559_parameters_custom_values_mismatch() {
        let (cfg, mut attributes, mut block) = eip1559_test_setup();

        let eip1559_extra_params = encode_holocene_extra_data(
            Default::default(),
            BaseFeeParams { max_change_denominator: 100, elasticity_multiplier: 2 },
        )
        .unwrap();

        let eip1559_params: FixedBytes<8> = encode_holocene_extra_data(
            Default::default(),
            BaseFeeParams { max_change_denominator: 99, elasticity_multiplier: 2 },
        )
        .unwrap()
        .split_off(1)
        .as_ref()
        .try_into()
        .unwrap();

        attributes.inner.eip_1559_params = Some(eip1559_params);
        block.header.extra_data = eip1559_extra_params;

        let check = AttributesMatch::check(&cfg, &attributes, &block);
        assert_eq!(
            check,
            AttributesMatch::Mismatch(AttributesMismatch::EIP1559Parameters(
                BaseFeeParams { max_change_denominator: 99, elasticity_multiplier: 2 },
                BaseFeeParams { max_change_denominator: 100, elasticity_multiplier: 2 }
            ))
        );
        assert!(check.is_mismatch());
    }

    /// Edge case: if the elasticity multiplier is 0, the max change denominator cannot be 0 as well
    #[test]
    fn test_eip1559_parameters_combination_mismatch() {
        let (cfg, mut attributes, mut block) = eip1559_test_setup();

        let eip1559_extra_params = encode_holocene_extra_data(
            Default::default(),
            BaseFeeParams { max_change_denominator: 5, elasticity_multiplier: 0 },
        )
        .unwrap();
        let eip1559_params: FixedBytes<8> =
            eip1559_extra_params.clone().split_off(1).as_ref().try_into().unwrap();

        attributes.inner.eip_1559_params = Some(eip1559_params);
        block.header.extra_data = eip1559_extra_params;

        let check = AttributesMatch::check(&cfg, &attributes, &block);
        assert_eq!(
            check,
            AttributesMatch::Mismatch(AttributesMismatch::InvalidEIP1559ParamsCombination)
        );
        assert!(check.is_mismatch());
    }

    /// Check that the version of the extra block data must be zero.
    #[test]
    fn test_eip1559_parameters_invalid_version() {
        let (cfg, mut attributes, mut block) = eip1559_test_setup();

        let eip1559_extra_params = encode_holocene_extra_data(
            Default::default(),
            BaseFeeParams { max_change_denominator: 100, elasticity_multiplier: 2 },
        )
        .unwrap();
        let eip1559_params: FixedBytes<8> =
            eip1559_extra_params.clone().split_off(1).as_ref().try_into().unwrap();

        let mut raw_extra_params_bytes = eip1559_extra_params.to_vec();
        raw_extra_params_bytes[0] = 10;

        attributes.inner.eip_1559_params = Some(eip1559_params);
        block.header.extra_data = raw_extra_params_bytes.into();

        let check = AttributesMatch::check(&cfg, &attributes, &block);
        assert_eq!(check, AttributesMatch::Mismatch(AttributesMismatch::InvalidExtraDataVersion));
        assert!(check.is_mismatch());
    }

    /// The default parameters can't overflow the u32 byte representation of the base fee params!
    #[test]
    fn test_eip1559_default_param_cant_overflow() {
        let (mut cfg, mut attributes, mut block) = eip1559_test_setup();
        cfg.chain_op_config.eip1559_denominator_canyon = u64::MAX;
        cfg.chain_op_config.eip1559_elasticity = u64::MAX;

        attributes.inner.eip_1559_params = Some(Default::default());
        block.header.extra_data = vec![0; 9].into();

        let check = AttributesMatch::check(&cfg, &attributes, &block);

        // Note that in this case we *always* have a mismatch because there isn't enough bytes in
        // the default representation of the extra params to represent a u128
        assert_eq!(
            check,
            AttributesMatch::Mismatch(EIP1559Parameters(
                BaseFeeParams {
                    max_change_denominator: u64::MAX as u128,
                    elasticity_multiplier: u64::MAX as u128
                },
                BaseFeeParams { max_change_denominator: 0, elasticity_multiplier: 0 }
            ))
        );
        assert!(check.is_mismatch());
    }

    #[test]
    fn test_attributes_match() {
        let cfg = default_rollup_config();
        let mut attributes = default_attributes();
        attributes.inner.gas_limit = Some(0);
        let block = Block::<Transaction>::default();
        let check = AttributesMatch::check(cfg, &attributes, &block);
        assert_eq!(check, AttributesMatch::Match);
        assert!(check.is_match());
    }
}