mega-evm 1.5.1

The evm tailored for the MegaETH
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
//! Tests for block-level limit enforcement in `MegaBlockExecutor`.
//!
//! These tests verify that the block executor properly enforces block-level data
//! and KV-update limits across multiple transactions within a block.

use std::convert::Infallible;

use alloy_consensus::{Signed, Transaction, TxLegacy};
use alloy_eips::eip2718::Encodable2718;
use alloy_evm::{block::BlockExecutor, Evm, EvmEnv, EvmFactory};
use alloy_op_evm::block::receipt_builder::OpAlloyReceiptBuilder;
use alloy_primitives::{address, Bytes, Signature, TxKind, B256, U256};
use mega_evm::{
    test_utils::{BytecodeBuilder, MemoryDatabase},
    BlockLimits, MegaBlockExecutionCtx, MegaBlockExecutor, MegaEvmFactory, MegaHardforkConfig,
    MegaSpecId, MegaTxEnvelope, TestExternalEnvs,
};
use revm::{
    bytecode::opcode::{ADD, DUP1, LOG0, PUSH0, SLOAD, SSTORE},
    context::BlockEnv,
    database::{Database, State},
};

const CALLER: alloy_primitives::Address = address!("2000000000000000000000000000000000000002");
const CONTRACT: alloy_primitives::Address = address!("1000000000000000000000000000000000000001");

/// Helper function to create a recovered transaction.
fn create_transaction(
    nonce: u64,
    gas_limit: u64,
) -> alloy_consensus::transaction::Recovered<MegaTxEnvelope> {
    let tx_legacy = TxLegacy {
        chain_id: Some(8453), // Base mainnet
        nonce,
        gas_price: 1_000_000,
        gas_limit,
        to: TxKind::Call(CONTRACT),
        value: U256::ZERO,
        input: Bytes::new(),
    };
    let signed = Signed::new_unchecked(tx_legacy, Signature::test_signature(), Default::default());
    let tx = MegaTxEnvelope::Legacy(signed);
    alloy_consensus::transaction::Recovered::new_unchecked(tx, CALLER)
}

/// Creates a contract that generates a log with specified data size.
///
/// The contract will emit LOG0 with the specified number of zero bytes.
fn create_log_generating_contract(data_size: usize) -> Bytes {
    let mut builder = BytecodeBuilder::default();

    // Push data size
    if data_size <= 0xFF {
        builder = builder.push_number(data_size as u8);
    } else if data_size <= 0xFFFF {
        builder = builder.push_number(data_size as u16);
    } else {
        builder = builder.push_number(data_size as u32);
    }

    // Push memory offset (0)
    builder = builder.append(PUSH0);

    // LOG0(offset, size)
    builder = builder.append(LOG0);

    // Stop
    builder.stop().build()
}

/// Creates a contract that performs N SSTORE operations.
///
/// Each SSTORE loads from storage slot i, increments it, and stores it back.
fn create_sstore_contract(num_writes: usize) -> Bytes {
    let mut builder = BytecodeBuilder::default();

    for i in 1..=num_writes {
        // Push key (slot number) for SLOAD
        if i <= 0xFF {
            builder = builder.push_number(i as u8);
        } else if i <= 0xFFFF {
            builder = builder.push_number(i as u16);
        } else {
            builder = builder.push_number(i as u32);
        }

        // Duplicate key on stack for SSTORE later
        // DUP1 duplicates the top stack item
        builder = builder.append(DUP1);

        // SLOAD - load current value from slot
        // Stack: [key, value]
        builder = builder.append(SLOAD);

        // Push 1 to increment
        // Stack: [key, value, 1]
        builder = builder.push_number(1u8);

        // ADD - increment the loaded value
        // Stack: [key, incremented_value]
        builder = builder.append(ADD);

        // SSTORE - store incremented value back to slot
        // Stack: []
        builder = builder.append(SSTORE);
    }

    builder.stop().build()
}

#[test]
fn test_block_custom_data_limit() {
    // Create database and deploy contract
    let mut db = MemoryDatabase::default();
    let bytecode = create_log_generating_contract(2000); // 2 KB log
    db.set_account_code(CONTRACT, bytecode);
    // Fund the caller account
    db.set_account_balance(CALLER, U256::from(1_000_000_000_000_000u64));

    // Create state
    let mut state = State::builder().with_database(&mut db).build();

    // Create EVM factory
    let external_envs = TestExternalEnvs::<Infallible>::new();
    let evm_factory = MegaEvmFactory::new().with_external_env_factory(external_envs);

    // Create EVM environment
    let mut cfg_env = revm::context::CfgEnv::default();
    cfg_env.spec = MegaSpecId::MINI_REX;
    let block_env = BlockEnv {
        number: U256::from(1000),
        timestamp: U256::from(1_800_000_000),
        gas_limit: 30_000_000,
        ..Default::default()
    };
    let evm_env = EvmEnv::new(cfg_env, block_env);

    // Create EVM
    let evm = evm_factory.create_evm(&mut state, evm_env);

    // Create block context with VERY LOW custom data limit
    // New behavior: The first transaction that exceeds the limit is allowed,
    // but subsequent transactions are rejected in pre-execution
    let block_ctx = MegaBlockExecutionCtx::new(
        B256::ZERO,
        None,
        Bytes::new(),
        BlockLimits::no_limits().with_block_txs_data_limit(2_500),
    ); // 2.5 KB data limit

    // Create block executor with MiniRex hardfork activated
    use alloy_hardforks::ForkCondition;
    use mega_evm::MegaHardfork;
    let chain_spec =
        MegaHardforkConfig::default().with(MegaHardfork::MiniRex, ForkCondition::Timestamp(0));
    let receipt_builder = OpAlloyReceiptBuilder::default();
    let mut executor = MegaBlockExecutor::new(evm, block_ctx, chain_spec, receipt_builder);

    // Execute first transaction (should succeed and likely exceed the limit)
    let tx1 = create_transaction(0, 1_000_000);
    let result1 = executor.execute_transaction(&tx1);
    assert!(result1.is_ok(), "First transaction should succeed");
    assert!(result1.unwrap() < tx1.gas_limit(), "Gas used should be less than gas limit");

    // Execute second transaction (should succeed, causing block to exceed limit)
    let tx2 = create_transaction(1, 1_000_000);
    let result2 = executor.execute_transaction(&tx2);
    assert!(result2.is_ok(), "Second transaction should succeed (last tx can exceed limit)");
    assert!(result2.unwrap() < tx2.gas_limit(), "Gas used should be less than gas limit");

    // Execute third transaction (should fail due to block data limit already exceeded)
    let tx3 = create_transaction(2, 1_000_000);
    let result3 = executor.execute_transaction(&tx3);
    assert!(result3.is_err(), "Third transaction should fail due to block data limit");
    let err_msg = format!("{:?}", result3.unwrap_err());
    assert!(
        err_msg.contains("TransactionDataLimit"),
        "Error should mention TransactionDataLimit, got: {}",
        err_msg
    );
}

#[test]
fn test_block_custom_kv_update_limit() {
    // Create database and deploy contract
    let mut db = MemoryDatabase::default();
    let bytecode = create_sstore_contract(50); // 50 storage writes
    db.set_account_code(CONTRACT, bytecode);
    // Fund the caller account
    db.set_account_balance(CALLER, U256::from(1_000_000_000_000_000u64));

    // Create state
    let mut state = State::builder().with_database(&mut db).build();

    // Create EVM factory
    let external_envs = TestExternalEnvs::<Infallible>::new();
    let evm_factory = MegaEvmFactory::new().with_external_env_factory(external_envs);

    // Create EVM environment
    let mut cfg_env = revm::context::CfgEnv::default();
    cfg_env.spec = MegaSpecId::MINI_REX;
    let block_env = BlockEnv {
        number: U256::from(1000),
        timestamp: U256::from(1_800_000_000),
        gas_limit: 30_000_000,
        ..Default::default()
    };
    let evm_env = EvmEnv::new(cfg_env, block_env);

    // Create EVM
    let evm = evm_factory.create_evm(&mut state, evm_env);

    // Create block context with custom KV update limit
    // Set limit to 1 to test the new behavior where the first transaction that
    // exceeds the limit is allowed, but subsequent transactions are rejected
    let block_ctx = MegaBlockExecutionCtx::new(
        B256::ZERO,
        None,
        Bytes::new(),
        BlockLimits::no_limits().with_block_kv_update_limit(1),
    ); // Very low limit - first tx will exceed it

    // Create block executor with MiniRex hardfork activated
    use alloy_hardforks::ForkCondition;
    use mega_evm::MegaHardfork;
    let chain_spec =
        MegaHardforkConfig::default().with(MegaHardfork::MiniRex, ForkCondition::Timestamp(0));
    let receipt_builder = OpAlloyReceiptBuilder::default();
    let mut executor = MegaBlockExecutor::new(evm, block_ctx, chain_spec, receipt_builder);

    // Execute first transaction (should succeed even though it will exceed the limit)
    let result1 = executor.execute_transaction(&create_transaction(0, 10_000_000));
    assert!(result1.is_ok(), "First transaction should succeed (last tx can exceed limit)");

    // Execute second transaction (should fail due to KV limit already exceeded)
    let result2 = executor.execute_transaction(&create_transaction(1, 10_000_000));
    assert!(result2.is_err(), "Second transaction should fail due to block KV update limit");
    let err_msg = format!("{:?}", result2.unwrap_err());
    assert!(
        err_msg.contains("KVUpdateLimit"),
        "Error should mention KVUpdateLimit, got: {}",
        err_msg
    );
}

#[test]
fn test_block_multiple_transactions_within_limits() {
    // Create database and deploy contract
    let mut db = MemoryDatabase::default();
    let bytecode = create_log_generating_contract(100); // 100 bytes per tx
    db.set_account_code(CONTRACT, bytecode);
    // Fund the caller account
    db.set_account_balance(CALLER, U256::from(1_000_000_000_000_000u64));

    // Create state
    let mut state = State::builder().with_database(&mut db).build();

    // Create EVM factory
    let external_envs = TestExternalEnvs::<Infallible>::new();
    let evm_factory = MegaEvmFactory::new().with_external_env_factory(external_envs);

    // Create EVM environment
    let mut cfg_env = revm::context::CfgEnv::default();
    cfg_env.spec = MegaSpecId::MINI_REX;
    let block_env = BlockEnv {
        number: U256::from(1000),
        timestamp: U256::from(1_800_000_000),
        gas_limit: 30_000_000,
        ..Default::default()
    };
    let evm_env = EvmEnv::new(cfg_env, block_env);

    // Create EVM
    let evm = evm_factory.create_evm(&mut state, evm_env);

    // Create block context with reasonable limits
    let block_ctx = MegaBlockExecutionCtx::new(
        B256::ZERO,
        None,
        Bytes::new(),
        BlockLimits::no_limits()
            .with_block_txs_data_limit(10_000)
            .with_block_kv_update_limit(1_000),
    ); // 10 KB data limit and 1000 KV update limit

    // Create block executor with MiniRex hardfork activated
    use alloy_hardforks::ForkCondition;
    use mega_evm::MegaHardfork;
    let chain_spec =
        MegaHardforkConfig::default().with(MegaHardfork::MiniRex, ForkCondition::Timestamp(0));
    let receipt_builder = OpAlloyReceiptBuilder::default();
    let mut executor = MegaBlockExecutor::new(evm, block_ctx, chain_spec, receipt_builder);

    // Execute 5 transactions, all should succeed
    for nonce in 0..5 {
        let tx = create_transaction(nonce, 1_000_000);
        let result = executor.execute_transaction(&tx);
        assert!(result.is_ok(), "Transaction {} should succeed", nonce);
        assert!(result.unwrap() < tx.gas_limit(), "Gas used should be less than gas limit");
    }

    // Finish the block and get receipts
    let block_result = executor.finish();
    assert!(block_result.is_ok(), "Block should finish successfully");

    let (_, receipts) = block_result.unwrap();
    assert_eq!(receipts.receipts.len(), 5, "Should have 5 receipts");
}

#[test]
fn test_block_data_limit_exceeded_mid_block() {
    // Create database and deploy contract
    let mut db = MemoryDatabase::default();
    let bytecode = create_log_generating_contract(2000);
    db.set_account_code(CONTRACT, bytecode);
    // Fund the caller account
    db.set_account_balance(CALLER, U256::from(1_000_000_000_000_000u64));

    // Create state
    let mut state = State::builder().with_database(&mut db).build();

    // Create EVM factory
    let external_envs = TestExternalEnvs::<Infallible>::new();
    let evm_factory = MegaEvmFactory::new().with_external_env_factory(external_envs);

    // Create EVM environment
    let mut cfg_env = revm::context::CfgEnv::default();
    cfg_env.spec = MegaSpecId::MINI_REX;
    let block_env = BlockEnv {
        number: U256::from(1000),
        timestamp: U256::from(1_800_000_000),
        gas_limit: 30_000_000,
        ..Default::default()
    };
    let evm_env = EvmEnv::new(cfg_env, block_env);

    // Create EVM
    let evm = evm_factory.create_evm(&mut state, evm_env);

    // Create block context with ~6 KB limit
    // The transaction that causes the block to exceed the limit is allowed,
    // but the next transaction is rejected
    let block_ctx = MegaBlockExecutionCtx::new(
        B256::ZERO,
        None,
        Bytes::new(),
        BlockLimits::no_limits().with_block_txs_data_limit(6_000),
    ); // 6 KB data limit

    // Create block executor with MiniRex hardfork activated
    use alloy_hardforks::ForkCondition;
    use mega_evm::MegaHardfork;
    let chain_spec =
        MegaHardforkConfig::default().with(MegaHardfork::MiniRex, ForkCondition::Timestamp(0));
    let receipt_builder = OpAlloyReceiptBuilder::default();
    let mut executor = MegaBlockExecutor::new(evm, block_ctx, chain_spec, receipt_builder);

    // Execute first transaction (should succeed)
    let result1 = executor.execute_transaction(&create_transaction(0, 1_000_000));
    assert!(result1.is_ok(), "First transaction should succeed");

    // Execute second transaction (should succeed)
    let result2 = executor.execute_transaction(&create_transaction(1, 1_000_000));
    assert!(result2.is_ok(), "Second transaction should succeed");

    // Execute third transaction (should succeed, likely causing block to exceed limit)
    let result3 = executor.execute_transaction(&create_transaction(2, 1_000_000));
    assert!(result3.is_ok(), "Third transaction should succeed (last tx can exceed limit)");

    // Execute fourth transaction (should fail due to block data limit already exceeded)
    let result4 = executor.execute_transaction(&create_transaction(3, 1_000_000));
    assert!(result4.is_err(), "Fourth transaction should fail due to block data limit");

    // Finish the block - should have 3 receipts
    let block_result = executor.finish();
    assert!(block_result.is_ok(), "Block should finish successfully");

    let (_, receipts) = block_result.unwrap();
    assert_eq!(receipts.receipts.len(), 3, "Should have 3 receipts (4th tx failed)");
}

#[test]
fn test_block_kv_limit_exceeded_mid_block() {
    // Create database and deploy contract with minimal SSTORE operations
    let mut db = MemoryDatabase::default();
    let bytecode = create_sstore_contract(1); // Just 1 SSTORE
    db.set_account_code(CONTRACT, bytecode);
    // Fund the caller account
    db.set_account_balance(CALLER, U256::from(1_000_000_000_000_000u64));

    // Create state
    let mut state = State::builder().with_database(&mut db).build();

    // Create EVM factory
    let external_envs = TestExternalEnvs::<Infallible>::new();
    let evm_factory = MegaEvmFactory::new().with_external_env_factory(external_envs);

    // Create EVM environment
    let mut cfg_env = revm::context::CfgEnv::default();
    cfg_env.spec = MegaSpecId::MINI_REX;
    let block_env = BlockEnv {
        number: U256::from(1000),
        timestamp: U256::from(1_800_000_000),
        gas_limit: 30_000_000,
        ..Default::default()
    };
    let evm_env = EvmEnv::new(cfg_env, block_env);

    // Create EVM
    let evm = evm_factory.create_evm(&mut state, evm_env);

    // Create block context with limit of 1 KV update
    // New behavior: The transaction that causes the block to exceed the limit is allowed,
    // but the next transaction is rejected
    // Each transaction induces 2 KV updates (sender account info and storage slot)
    let block_ctx = MegaBlockExecutionCtx::new(
        B256::ZERO,
        None,
        Bytes::new(),
        BlockLimits::no_limits().with_block_kv_update_limit(1),
    ); // 1 KV update limit - first tx will exceed it

    // Create block executor with MiniRex hardfork activated
    use alloy_hardforks::ForkCondition;
    use mega_evm::MegaHardfork;
    let chain_spec =
        MegaHardforkConfig::default().with(MegaHardfork::MiniRex, ForkCondition::Timestamp(0));
    let receipt_builder = OpAlloyReceiptBuilder::default();
    let mut executor = MegaBlockExecutor::new(evm, block_ctx, chain_spec, receipt_builder);

    // Execute first transaction (should succeed with 2 KV updates, exceeding the limit)
    let tx1 = create_transaction(0, 10_000_000);
    let result1 = executor.execute_transaction(&tx1);
    assert!(result1.is_ok(), "First transaction should succeed (last tx can exceed limit)");
    assert!(result1.unwrap() < tx1.gas_limit(), "Gas used should be less than gas limit");

    // Execute second transaction (should fail due to KV limit already exceeded)
    let tx2 = create_transaction(1, 10_000_000);
    let result2 = executor.execute_transaction(&tx2);
    assert!(result2.is_err(), "Second transaction should fail due to block KV update limit");

    // Finish the block - should have 1 receipt
    let block_result = executor.finish();
    assert!(block_result.is_ok(), "Block should finish successfully");

    let (_, receipts) = block_result.unwrap();
    assert_eq!(receipts.receipts.len(), 1, "Should have 1 receipt (2nd tx failed)");
}

#[test]
fn test_block_no_state_commit_on_limit_exceeded() {
    // Create database and deploy contract
    let mut db = MemoryDatabase::default();
    let bytecode = BytecodeBuilder::default()
        .push_number(42u8) // value
        .append(PUSH0) // key (slot 0)
        .append(SSTORE) // SSTORE
        .stop()
        .build();
    db.set_account_code(CONTRACT, bytecode);
    // Fund the caller account
    db.set_account_balance(CALLER, U256::from(1_000_000_000_000_000u64));

    // Create state
    let mut state = State::builder().with_database(&mut db).build();

    // Create EVM factory
    let external_envs = TestExternalEnvs::<Infallible>::new();
    let evm_factory = MegaEvmFactory::new().with_external_env_factory(external_envs);

    // Create EVM environment
    let mut cfg_env = revm::context::CfgEnv::default();
    cfg_env.spec = MegaSpecId::MINI_REX;
    let block_env = BlockEnv {
        number: U256::from(1000),
        timestamp: U256::from(1_800_000_000),
        gas_limit: 30_000_000,
        ..Default::default()
    };
    let evm_env = EvmEnv::new(cfg_env, block_env);

    // Create EVM
    let evm = evm_factory.create_evm(&mut state, evm_env);

    // Create block context with VERY low KV update limit
    // With new behavior, we need to set limit to 0 to ensure no transactions can be added
    // Setting to 0 means block_kv_updates_used (0) >= limit (0) is true from the start
    let block_ctx = MegaBlockExecutionCtx::new(
        B256::ZERO,
        None,
        Bytes::new(),
        BlockLimits::no_limits().with_block_kv_update_limit(0),
    ); // Zero limit

    // Create block executor with MiniRex hardfork activated
    use alloy_hardforks::ForkCondition;
    use mega_evm::MegaHardfork;
    let chain_spec =
        MegaHardforkConfig::default().with(MegaHardfork::MiniRex, ForkCondition::Timestamp(0));
    let receipt_builder = OpAlloyReceiptBuilder::default();
    let mut executor = MegaBlockExecutor::new(evm, block_ctx, chain_spec, receipt_builder);

    // Execute transaction (should fail due to KV limit of 0)
    let result = executor.execute_transaction(&create_transaction(0, 10_000_000));
    assert!(result.is_err(), "Transaction should fail due to block KV update limit of 0");

    // Verify storage slot 0 is NOT set in the database
    let db_state = executor.evm_mut().db_mut();
    let _ = db_state.load_cache_account(CONTRACT);
    let value = db_state.storage(CONTRACT, U256::ZERO).expect("Storage access should not fail");

    // The storage should be zero (not 42) since the transaction was not committed
    assert_eq!(value, U256::ZERO, "Storage should not be committed");

    // Finish the block - should have 0 receipts
    let block_result = executor.finish();
    assert!(block_result.is_ok(), "Block should finish successfully");

    let (_, receipts) = block_result.unwrap();
    assert_eq!(receipts.receipts.len(), 0, "Should have 0 receipts (tx rejected in pre-execution)");
}

#[test]
fn test_block_tx_size_limit_default_unlimited() {
    // Create database and deploy contract
    let mut db = MemoryDatabase::default();
    let bytecode = create_log_generating_contract(100);
    db.set_account_code(CONTRACT, bytecode);
    // Fund the caller account
    db.set_account_balance(CALLER, U256::from(1_000_000_000_000_000u64));

    // Create state
    let mut state = State::builder().with_database(&mut db).build();

    // Create EVM factory
    let external_envs = TestExternalEnvs::<Infallible>::new();
    let evm_factory = MegaEvmFactory::new().with_external_env_factory(external_envs);

    // Create EVM environment
    let mut cfg_env = revm::context::CfgEnv::default();
    cfg_env.spec = MegaSpecId::MINI_REX;
    let block_env = BlockEnv {
        number: U256::from(1000),
        timestamp: U256::from(1_800_000_000),
        gas_limit: 30_000_000,
        ..Default::default()
    };
    let evm_env = EvmEnv::new(cfg_env, block_env);

    // Create EVM
    let evm = evm_factory.create_evm(&mut state, evm_env);

    // Create block context with default limits (tx size should be u64::MAX)
    let block_ctx =
        MegaBlockExecutionCtx::new(B256::ZERO, None, Bytes::new(), BlockLimits::no_limits());
    assert_eq!(
        block_ctx.block_limits.block_txs_encode_size_limit,
        u64::MAX,
        "Default tx size limit should be u64::MAX"
    );

    // Create block executor with MiniRex hardfork activated
    use alloy_hardforks::ForkCondition;
    use mega_evm::MegaHardfork;
    let chain_spec =
        MegaHardforkConfig::default().with(MegaHardfork::MiniRex, ForkCondition::Timestamp(0));
    let receipt_builder = OpAlloyReceiptBuilder::default();
    let mut executor = MegaBlockExecutor::new(evm, block_ctx, chain_spec, receipt_builder);

    // Execute multiple transactions - should all succeed with unlimited size
    for nonce in 0..10 {
        let tx = create_transaction(nonce, 1_000_000);
        let result = executor.execute_transaction(&tx);
        assert!(
            result.is_ok(),
            "Transaction {} should succeed with unlimited tx size limit",
            nonce
        );
    }

    // Finish the block
    let block_result = executor.finish();
    assert!(block_result.is_ok(), "Block should finish successfully");

    let (_, receipts) = block_result.unwrap();
    assert_eq!(receipts.receipts.len(), 10, "Should have 10 receipts");
}

#[test]
fn test_block_tx_size_limit_allows_multiple_transactions() {
    // Create database and deploy contract
    let mut db = MemoryDatabase::default();
    let bytecode = create_log_generating_contract(100);
    db.set_account_code(CONTRACT, bytecode);
    // Fund the caller account
    db.set_account_balance(CALLER, U256::from(1_000_000_000_000_000u64));

    // Create state
    let mut state = State::builder().with_database(&mut db).build();

    // Create EVM factory
    let external_envs = TestExternalEnvs::<Infallible>::new();
    let evm_factory = MegaEvmFactory::new().with_external_env_factory(external_envs);

    // Create EVM environment
    let mut cfg_env = revm::context::CfgEnv::default();
    cfg_env.spec = MegaSpecId::MINI_REX;
    let block_env = BlockEnv {
        number: U256::from(1000),
        timestamp: U256::from(1_800_000_000),
        gas_limit: 30_000_000,
        ..Default::default()
    };
    let evm_env = EvmEnv::new(cfg_env, block_env);

    // Create EVM
    let evm = evm_factory.create_evm(&mut state, evm_env);

    // Create a transaction to measure its size
    let sample_tx = create_transaction(0, 1_000_000);
    let tx_size = sample_tx.encode_2718_len() as u64;

    // Set limit to allow exactly 5 transactions
    let block_ctx = MegaBlockExecutionCtx::new(
        B256::ZERO,
        None,
        Bytes::new(),
        BlockLimits::no_limits().with_block_txs_encode_size_limit(tx_size * 5),
    );

    // Create block executor with MiniRex hardfork activated
    use alloy_hardforks::ForkCondition;
    use mega_evm::MegaHardfork;
    let chain_spec =
        MegaHardforkConfig::default().with(MegaHardfork::MiniRex, ForkCondition::Timestamp(0));
    let receipt_builder = OpAlloyReceiptBuilder::default();
    let mut executor = MegaBlockExecutor::new(evm, block_ctx, chain_spec, receipt_builder);

    // Execute 5 transactions - should all succeed
    for nonce in 0..5 {
        let tx = create_transaction(nonce, 1_000_000);
        let result = executor.execute_transaction(&tx);
        assert!(result.is_ok(), "Transaction {} should succeed", nonce);
    }

    // Finish the block
    let block_result = executor.finish();
    assert!(block_result.is_ok(), "Block should finish successfully");

    let (_, receipts) = block_result.unwrap();
    assert_eq!(receipts.receipts.len(), 5, "Should have 5 receipts");
}

#[test]
fn test_block_tx_size_limit_exceeded_first_transaction() {
    // Create database and deploy contract
    let mut db = MemoryDatabase::default();
    let bytecode = create_log_generating_contract(100);
    db.set_account_code(CONTRACT, bytecode);
    // Fund the caller account
    db.set_account_balance(CALLER, U256::from(1_000_000_000_000_000u64));

    // Create state
    let mut state = State::builder().with_database(&mut db).build();

    // Create EVM factory
    let external_envs = TestExternalEnvs::<Infallible>::new();
    let evm_factory = MegaEvmFactory::new().with_external_env_factory(external_envs);

    // Create EVM environment
    let mut cfg_env = revm::context::CfgEnv::default();
    cfg_env.spec = MegaSpecId::MINI_REX;
    let block_env = BlockEnv {
        number: U256::from(1000),
        timestamp: U256::from(1_800_000_000),
        gas_limit: 30_000_000,
        ..Default::default()
    };
    let evm_env = EvmEnv::new(cfg_env, block_env);

    // Create EVM
    let evm = evm_factory.create_evm(&mut state, evm_env);

    // Set a very small tx size limit that won't fit even one transaction
    let block_ctx = MegaBlockExecutionCtx::new(
        B256::ZERO,
        None,
        Bytes::new(),
        BlockLimits::no_limits().with_block_txs_encode_size_limit(10),
    ); // Very small limit

    // Create block executor with MiniRex hardfork activated
    use alloy_hardforks::ForkCondition;
    use mega_evm::MegaHardfork;
    let chain_spec =
        MegaHardforkConfig::default().with(MegaHardfork::MiniRex, ForkCondition::Timestamp(0));
    let receipt_builder = OpAlloyReceiptBuilder::default();
    let mut executor = MegaBlockExecutor::new(evm, block_ctx, chain_spec, receipt_builder);

    // Execute transaction (should fail immediately due to tx size limit)
    let result = executor.execute_transaction(&create_transaction(0, 1_000_000));
    assert!(result.is_err(), "Transaction should fail due to tx size limit");

    let err_msg = format!("{:?}", result.unwrap_err());
    assert!(
        err_msg.contains("TransactionEncodeSizeLimit"),
        "Error should mention TransactionEncodeSizeLimit, got: {}",
        err_msg
    );

    // Finish the block - should have 0 receipts
    let block_result = executor.finish();
    assert!(block_result.is_ok(), "Block should finish successfully");

    let (_, receipts) = block_result.unwrap();
    assert_eq!(receipts.receipts.len(), 0, "Should have 0 receipts (tx failed)");
}

#[test]
fn test_block_tx_size_limit_exceeded_mid_block() {
    // Create database and deploy contract
    let mut db = MemoryDatabase::default();
    let bytecode = create_log_generating_contract(100);
    db.set_account_code(CONTRACT, bytecode);
    // Fund the caller account
    db.set_account_balance(CALLER, U256::from(1_000_000_000_000_000u64));

    // Create state
    let mut state = State::builder().with_database(&mut db).build();

    // Create EVM factory
    let external_envs = TestExternalEnvs::<Infallible>::new();
    let evm_factory = MegaEvmFactory::new().with_external_env_factory(external_envs);

    // Create EVM environment
    let mut cfg_env = revm::context::CfgEnv::default();
    cfg_env.spec = MegaSpecId::MINI_REX;
    let block_env = BlockEnv {
        number: U256::from(1000),
        timestamp: U256::from(1_800_000_000),
        gas_limit: 30_000_000,
        ..Default::default()
    };
    let evm_env = EvmEnv::new(cfg_env, block_env);

    // Create EVM
    let evm = evm_factory.create_evm(&mut state, evm_env);

    // Create a transaction to measure its size
    let sample_tx = create_transaction(0, 1_000_000);
    let tx_size = sample_tx.encode_2718_len() as u64;

    // Set limit to allow exactly 3 transactions
    let block_ctx = MegaBlockExecutionCtx::new(
        B256::ZERO,
        None,
        Bytes::new(),
        BlockLimits::no_limits().with_block_txs_encode_size_limit(tx_size * 3),
    );

    // Create block executor with MiniRex hardfork activated
    use alloy_hardforks::ForkCondition;
    use mega_evm::MegaHardfork;
    let chain_spec =
        MegaHardforkConfig::default().with(MegaHardfork::MiniRex, ForkCondition::Timestamp(0));
    let receipt_builder = OpAlloyReceiptBuilder::default();
    let mut executor = MegaBlockExecutor::new(evm, block_ctx, chain_spec, receipt_builder);

    // Execute first 3 transactions (should all succeed)
    for nonce in 0..3 {
        let tx = create_transaction(nonce, 1_000_000);
        let result = executor.execute_transaction(&tx);
        assert!(result.is_ok(), "Transaction {} should succeed", nonce);
    }

    // Execute 4th transaction (should fail due to tx size limit)
    let result = executor.execute_transaction(&create_transaction(3, 1_000_000));
    assert!(result.is_err(), "Fourth transaction should fail due to tx size limit");

    let err_msg = format!("{:?}", result.unwrap_err());
    assert!(
        err_msg.contains("TransactionEncodeSizeLimit"),
        "Error should mention TransactionEncodeSizeLimit, got: {}",
        err_msg
    );

    // Finish the block - should have 3 receipts
    let block_result = executor.finish();
    assert!(block_result.is_ok(), "Block should finish successfully");

    let (_, receipts) = block_result.unwrap();
    assert_eq!(receipts.receipts.len(), 3, "Should have 3 receipts (4th tx failed)");
}

const CALLER2: alloy_primitives::Address = address!("3000000000000000000000000000000000000003");
const CALLER3: alloy_primitives::Address = address!("4000000000000000000000000000000000000004");

/// Helper function to create a recovered transaction with a specific caller.
fn create_transaction_with_caller(
    caller: alloy_primitives::Address,
    nonce: u64,
    gas_limit: u64,
) -> alloy_consensus::transaction::Recovered<MegaTxEnvelope> {
    let tx_legacy = TxLegacy {
        chain_id: Some(8453), // Base mainnet
        nonce,
        gas_price: 1_000_000,
        gas_limit,
        to: TxKind::Call(CONTRACT),
        value: U256::ZERO,
        input: Bytes::new(),
    };
    let signed = Signed::new_unchecked(tx_legacy, Signature::test_signature(), Default::default());
    let tx = MegaTxEnvelope::Legacy(signed);
    alloy_consensus::transaction::Recovered::new_unchecked(tx, caller)
}

/// Tests that the commit-time `pre_execution_check` catches parallel execution race conditions.
///
/// This test simulates the following scenario:
/// 1. TX A: `run_transaction()` → passes (block has capacity for one more tx)
/// 2. TX B: `run_transaction()` → passes (block still has capacity - checked before A committed)
/// 3. TX B: `commit_transaction_outcome()` → succeeds, updates block counters (now at limit)
/// 4. TX A: `commit_transaction_outcome()` → should FAIL due to commit-time `pre_execution_check`
#[test]
fn test_commit_time_pre_execution_check_parallel_simulation() {
    // Create database and deploy a simple contract
    let mut db = MemoryDatabase::default();
    let bytecode = create_log_generating_contract(100);
    db.set_account_code(CONTRACT, bytecode);
    // Fund multiple caller accounts (simulating parallel execution from different senders)
    db.set_account_balance(CALLER, U256::from(1_000_000_000_000_000u64));
    db.set_account_balance(CALLER2, U256::from(1_000_000_000_000_000u64));
    db.set_account_balance(CALLER3, U256::from(1_000_000_000_000_000u64));

    // Create state
    let mut state = State::builder().with_database(&mut db).build();

    // Create EVM factory
    let external_envs = TestExternalEnvs::<Infallible>::new();
    let evm_factory = MegaEvmFactory::new().with_external_env_factory(external_envs);

    // Determine gas needed per transaction
    // We'll use gas_limit of 100_000 per transaction
    let tx_gas_limit = 100_000u64;

    // Create EVM environment with block gas limit that allows exactly 2 transactions
    // The pre_execution_check uses: block_gas_used + gas_limit > block_gas_limit
    // Actual gas used per tx is ~30_000 (simple contract call)
    // After 2 txs commit: block_gas_used ≈ 60_000
    // For 3rd tx to fail: 60_000 + 100_000 = 160_000 > block_gas_limit
    // So we set block_gas_limit = 150_000 (allows 2 txs, rejects 3rd)
    let block_gas_limit = 150_000u64;
    let mut cfg_env = revm::context::CfgEnv::default();
    cfg_env.spec = MegaSpecId::MINI_REX;
    let block_env = BlockEnv {
        number: U256::from(1000),
        timestamp: U256::from(1_800_000_000),
        gas_limit: block_gas_limit,
        ..Default::default()
    };
    let evm_env = EvmEnv::new(cfg_env, block_env);

    // Create EVM
    let evm = evm_factory.create_evm(&mut state, evm_env);

    // Create block context with block_gas_limit set (this is what pre_execution_check uses)
    let block_ctx = MegaBlockExecutionCtx::new(
        B256::ZERO,
        None,
        Bytes::new(),
        BlockLimits::no_limits().with_block_gas_limit(block_gas_limit),
    );

    // Create block executor with MiniRex hardfork activated
    use alloy_hardforks::ForkCondition;
    use mega_evm::MegaHardfork;
    let chain_spec =
        MegaHardforkConfig::default().with(MegaHardfork::MiniRex, ForkCondition::Timestamp(0));
    let receipt_builder = OpAlloyReceiptBuilder::default();
    let mut executor = MegaBlockExecutor::new(evm, block_ctx, chain_spec, receipt_builder);

    // Create 3 transactions from different callers (nonce 0 for each)
    // This simulates parallel execution where each transaction is independent
    let tx1 = create_transaction_with_caller(CALLER, 0, tx_gas_limit);
    let tx2 = create_transaction_with_caller(CALLER2, 0, tx_gas_limit);
    let tx3 = create_transaction_with_caller(CALLER3, 0, tx_gas_limit);

    // Step 1: Run tx1 (should pass - block has capacity)
    let outcome1 = executor.run_transaction(&tx1);
    assert!(outcome1.is_ok(), "run_transaction(tx1) should succeed");
    let outcome1 = outcome1.unwrap();

    // Step 2: Run tx2 (should pass - block still has capacity, tx1 not committed yet)
    let outcome2 = executor.run_transaction(&tx2);
    assert!(outcome2.is_ok(), "run_transaction(tx2) should succeed (tx1 not committed yet)");
    let outcome2 = outcome2.unwrap();

    // Step 3: Run tx3 (should pass - block still has capacity, neither tx1 nor tx2 committed)
    let outcome3 = executor.run_transaction(&tx3);
    assert!(outcome3.is_ok(), "run_transaction(tx3) should succeed (no tx committed yet)");
    let outcome3 = outcome3.unwrap();

    // Step 4: Commit tx2 first (out of order - simulating parallel execution)
    let commit2 = executor.commit_transaction_outcome(outcome2);
    assert!(commit2.is_ok(), "commit_transaction_outcome(tx2) should succeed");

    // Step 5: Commit tx3 (should succeed, filling up the block)
    let commit3 = executor.commit_transaction_outcome(outcome3);
    assert!(commit3.is_ok(), "commit_transaction_outcome(tx3) should succeed");

    // Step 6: Commit tx1 (should FAIL - block is now at capacity)
    // This tests the commit-time pre_execution_check that catches the race condition.
    // Even though tx1 passed run_transaction() initially (before tx2/tx3 committed),
    // the commit-time check re-validates and catches that block_gas_used + gas_limit
    // now exceeds block_gas_limit.
    let commit1 = executor.commit_transaction_outcome(outcome1);
    assert!(commit1.is_err(), "commit_transaction_outcome(tx1) should fail - block at capacity");

    let err_msg = format!("{:?}", commit1.unwrap_err());
    assert!(
        err_msg.contains("TransactionGasLimitMoreThanAvailableBlockGas"),
        "Error should mention TransactionGasLimitMoreThanAvailableBlockGas, got: {}",
        err_msg
    );

    // Finish the block - should have 2 receipts (tx2 and tx3, tx1 was rejected at commit time)
    let block_result = executor.finish();
    assert!(block_result.is_ok(), "Block should finish successfully");

    let (_, receipts) = block_result.unwrap();
    assert_eq!(
        receipts.receipts.len(),
        2,
        "Should have 2 receipts (tx1 failed at commit time due to race condition)"
    );
}

#[test]
fn test_block_tx_size_limit_with_varying_sizes() {
    // Create database and deploy contract
    let mut db = MemoryDatabase::default();
    let bytecode = create_log_generating_contract(100);
    db.set_account_code(CONTRACT, bytecode);
    // Fund the caller account
    db.set_account_balance(CALLER, U256::from(1_000_000_000_000_000u64));

    // Create state
    let mut state = State::builder().with_database(&mut db).build();

    // Create EVM factory
    let external_envs = TestExternalEnvs::<Infallible>::new();
    let evm_factory = MegaEvmFactory::new().with_external_env_factory(external_envs);

    // Create EVM environment
    let mut cfg_env = revm::context::CfgEnv::default();
    cfg_env.spec = MegaSpecId::MINI_REX;
    let block_env = BlockEnv {
        number: U256::from(1000),
        timestamp: U256::from(1_800_000_000),
        gas_limit: 30_000_000,
        ..Default::default()
    };
    let evm_env = EvmEnv::new(cfg_env, block_env);

    // Create EVM
    let evm = evm_factory.create_evm(&mut state, evm_env);

    // Measure transaction sizes with different gas limits
    let small_tx = create_transaction(0, 100_000);
    let large_tx = create_transaction(0, 10_000_000);
    let small_size = small_tx.encode_2718_len() as u64;
    let large_size = large_tx.encode_2718_len() as u64;

    // Set limit to allow 2 small + 1 large transaction
    let block_ctx = MegaBlockExecutionCtx::new(
        B256::ZERO,
        None,
        Bytes::new(),
        BlockLimits::no_limits().with_block_txs_encode_size_limit(small_size * 2 + large_size),
    );

    // Create block executor with MiniRex hardfork activated
    use alloy_hardforks::ForkCondition;
    use mega_evm::MegaHardfork;
    let chain_spec =
        MegaHardforkConfig::default().with(MegaHardfork::MiniRex, ForkCondition::Timestamp(0));
    let receipt_builder = OpAlloyReceiptBuilder::default();
    let mut executor = MegaBlockExecutor::new(evm, block_ctx, chain_spec, receipt_builder);

    // Execute 2 small transactions
    let result1 = executor.execute_transaction(&create_transaction(0, 100_000));
    assert!(result1.is_ok(), "First small transaction should succeed");

    let result2 = executor.execute_transaction(&create_transaction(1, 100_000));
    assert!(result2.is_ok(), "Second small transaction should succeed");

    // Execute 1 large transaction
    let result3 = executor.execute_transaction(&create_transaction(2, 10_000_000));
    assert!(result3.is_ok(), "Large transaction should succeed");

    // Try to execute one more small transaction (should fail)
    let result4 = executor.execute_transaction(&create_transaction(3, 100_000));
    assert!(result4.is_err(), "Fourth transaction should fail due to tx size limit");

    // Finish the block - should have 3 receipts
    let block_result = executor.finish();
    assert!(block_result.is_ok(), "Block should finish successfully");

    let (_, receipts) = block_result.unwrap();
    assert_eq!(receipts.receipts.len(), 3, "Should have 3 receipts (4th tx failed)");
}