blvm-consensus 0.1.10

Bitcoin Commons BLVM: Direct mathematical implementation of Bitcoin consensus rules from the Orange Paper
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
//! Mempool validation functions from Orange Paper Section 9

use crate::constants::*;
use crate::economic::calculate_fee;
use crate::error::{ConsensusError, Result};
use crate::script::verify_script;
use crate::segwit::Witness;
use crate::transaction::{check_transaction, check_tx_inputs};
use crate::types::*;
use blvm_spec_lock::spec_locked;
use std::collections::HashSet;

/// AcceptToMemoryPool: 𝒯𝒳 × 𝒰𝒮 → {accepted, rejected}
///
/// For transaction tx and UTXO set us:
/// 1. Check if tx is already in mempool
/// 2. Validate transaction structure
/// 3. Check inputs against UTXO set
/// 4. Verify scripts
/// 5. Check mempool-specific rules (size, fee rate, etc.)
/// 6. Check for conflicts with existing mempool transactions
/// 7. Return acceptance result
///
/// # Arguments
///
/// * `tx` - Transaction to validate
/// * `witnesses` - Optional witness data for each input (Vec<Witness> where Witness = Vec<ByteString>)
/// * `utxo_set` - Current UTXO set
/// * `mempool` - Current mempool state
/// * `height` - Current block height
/// * `time_context` - Time context with median time-past of chain tip (BIP113) for transaction finality check
#[spec_locked("9.1")]
pub fn accept_to_memory_pool(
    tx: &Transaction,
    witnesses: Option<&[Witness]>,
    utxo_set: &UtxoSet,
    mempool: &Mempool,
    height: Natural,
    time_context: Option<TimeContext>,
) -> Result<MempoolResult> {
    // Precondition assertions: Validate function inputs
    // Note: We check coinbase and empty transactions and return Rejected rather than asserting,
    // to allow tests to verify the validation logic properly
    if tx.inputs.is_empty() && tx.outputs.is_empty() {
        return Ok(MempoolResult::Rejected(
            "Transaction must have at least one input or output".to_string(),
        ));
    }
    if is_coinbase(tx) {
        return Ok(MempoolResult::Rejected(
            "Coinbase transactions cannot be added to mempool".to_string(),
        ));
    }
    assert!(
        height <= i64::MAX as u64,
        "Block height {height} must fit in i64"
    );
    assert!(
        utxo_set.len() <= u32::MAX as usize,
        "UTXO set size {} exceeds maximum",
        utxo_set.len()
    );
    if let Some(wits) = witnesses {
        assert!(
            wits.len() == tx.inputs.len(),
            "Witness count {} must match input count {}",
            wits.len(),
            tx.inputs.len()
        );
    }

    // 1. Check if transaction is already in mempool
    let tx_id = crate::block::calculate_tx_id(tx);
    // Invariant assertion: Transaction ID must be valid
    assert!(tx_id != [0u8; 32], "Transaction ID must be non-zero");
    if mempool.contains(&tx_id) {
        return Ok(MempoolResult::Rejected(
            "Transaction already in mempool".to_string(),
        ));
    }

    // 2. Validate transaction structure
    if !matches!(check_transaction(tx)?, ValidationResult::Valid) {
        return Ok(MempoolResult::Rejected(
            "Invalid transaction structure".to_string(),
        ));
    }

    // 2.5. Check transaction finality
    // Use median time-past of chain tip (BIP113) for proper locktime/sequence validation
    let block_time = time_context.map(|ctx| ctx.median_time_past).unwrap_or(0);
    if !is_final_tx(tx, height, block_time) {
        return Ok(MempoolResult::Rejected(
            "Transaction not final (locktime not satisfied)".to_string(),
        ));
    }

    // 3. Check inputs against UTXO set
    let (input_valid, fee) = check_tx_inputs(tx, utxo_set, height)?;
    // Invariant assertion: Fee must be non-negative
    assert!(fee >= 0, "Fee {fee} must be non-negative");
    use crate::constants::MAX_MONEY;
    assert!(fee <= MAX_MONEY, "Fee {fee} must not exceed MAX_MONEY");
    if !matches!(input_valid, ValidationResult::Valid) {
        return Ok(MempoolResult::Rejected(
            "Invalid transaction inputs".to_string(),
        ));
    }

    // 4. Verify scripts for non-coinbase transactions
    if !is_coinbase(tx) {
        // Calculate script verification flags
        // Enable SegWit flag if transaction has witness data
        let flags = calculate_script_flags(tx, witnesses);

        #[cfg(all(feature = "production", feature = "rayon"))]
        {
            use rayon::prelude::*;

            // Optimization: Batch UTXO lookups and parallelize script verification
            // Pre-lookup all UTXOs to avoid concurrent HashMap access
            // Pre-allocate with known size
            let input_utxos: Vec<(usize, Option<&UTXO>)> = {
                let mut result = Vec::with_capacity(tx.inputs.len());
                for (i, input) in tx.inputs.iter().enumerate() {
                    result.push((i, utxo_set.get(&input.prevout).map(|a| a.as_ref())));
                }
                result
            };

            // Parallelize script verification (read-only operations) ✅ Thread-safe
            let script_results: Result<Vec<bool>> = input_utxos
                .par_iter()
                .map(|(i, opt_utxo)| {
                    if let Some(utxo) = opt_utxo {
                        let input = &tx.inputs[*i];
                        let witness: Option<&ByteString> = witnesses
                            .and_then(|wits| wits.get(*i))
                            .and_then(|wit| wit.first());

                        verify_script(&input.script_sig, &utxo.script_pubkey, witness, flags)
                    } else {
                        Ok(false)
                    }
                })
                .collect();

            // Check results sequentially
            let script_results = script_results?;
            // Invariant assertion: Script results count must match input count
            assert!(
                script_results.len() == tx.inputs.len(),
                "Script results count {} must match input count {}",
                script_results.len(),
                tx.inputs.len()
            );
            for (i, &is_valid) in script_results.iter().enumerate() {
                // Bounds checking assertion: Input index must be valid
                assert!(
                    i < tx.inputs.len(),
                    "Input index {i} out of bounds in script validation loop",
                );
                // Invariant: is_valid is bool from verify_script
                if !is_valid {
                    return Ok(MempoolResult::Rejected(format!(
                        "Invalid script at input {i}"
                    )));
                }
            }
        }

        #[cfg(not(all(feature = "production", feature = "rayon")))]
        {
            // Sequential fallback
            for (i, input) in tx.inputs.iter().enumerate() {
                if let Some(utxo) = utxo_set.get(&input.prevout) {
                    // Get witness for this input if available
                    // Witness is Vec<ByteString> per input, for verify_script we need Option<&ByteString>
                    // For SegWit P2WPKH/P2WSH, we typically use the witness stack elements
                    // For now, we'll use the first element if available (simplified)
                    let witness: Option<&ByteString> =
                        witnesses.and_then(|wits| wits.get(i)).and_then(|wit| {
                            // Witness is Vec<ByteString> - for verify_script we can pass the first element
                            // or construct a combined witness script. For now, use first element.
                            wit.first()
                        });

                    if !verify_script(&input.script_sig, &utxo.script_pubkey, witness, flags)? {
                        return Ok(MempoolResult::Rejected(format!(
                            "Invalid script at input {i}"
                        )));
                    }
                }
            }
        }
    }

    // 5. Check mempool-specific rules
    if !check_mempool_rules(tx, fee, mempool)? {
        return Ok(MempoolResult::Rejected("Failed mempool rules".to_string()));
    }

    // 6. Check for conflicts with existing mempool transactions
    if has_conflicts(tx, mempool)? {
        return Ok(MempoolResult::Rejected(
            "Transaction conflicts with mempool".to_string(),
        ));
    }

    Ok(MempoolResult::Accepted)
}

/// Calculate script verification flags based on transaction type
///
/// Returns appropriate flags for script validation:
/// - Base flags: Standard validation flags (P2SH, STRICTENC, DERSIG, LOW_S, etc.)
/// - SegWit flag (SCRIPT_VERIFY_WITNESS = 0x800): Enabled if transaction uses SegWit
/// - Taproot flag (SCRIPT_VERIFY_TAPROOT = 0x4000): Enabled if transaction uses Taproot
fn calculate_script_flags(tx: &Transaction, witnesses: Option<&[Witness]>) -> u32 {
    // Delegate to the canonical script flag calculation used by block validation.
    //
    // Note: For mempool policy we only care about which flags are enabled, not the
    // actual witness contents here, so we rely on the transaction structure itself
    // (including SegWit/Taproot outputs) in `calculate_script_flags_for_block`.
    // Witness data is still threaded through to `verify_script` separately.
    //
    // For mempool policy, we use a height that activates all soft forks (well past all activations).
    // This ensures we validate using the most strict rules.
    // Check if witness data is present (optimization: just check bool, no witness needed)
    let has_witness = witnesses.map(|w| !w.is_empty()).unwrap_or(false);
    const MEMPOOL_POLICY_HEIGHT: u64 = 1_000_000; // All soft forks active at this height
    crate::block::calculate_script_flags_for_block_network(
        tx,
        has_witness,
        MEMPOOL_POLICY_HEIGHT,
        crate::types::Network::Mainnet,
    )
}

/// IsStandardTx: 𝒯𝒳 → {true, false}
///
/// Check if transaction follows standard rules for mempool acceptance:
/// 1. Transaction size limits
/// 2. Script size limits
/// 3. Standard script types
/// 4. Fee rate requirements
#[spec_locked("9.2")]
pub fn is_standard_tx(tx: &Transaction) -> Result<bool> {
    // 1. Check transaction size
    let tx_size = calculate_transaction_size(tx);
    if tx_size > MAX_TX_SIZE {
        return Ok(false);
    }

    // 2. Check script sizes
    for (i, input) in tx.inputs.iter().enumerate() {
        // Bounds checking assertion: Input index must be valid
        assert!(i < tx.inputs.len(), "Input index {i} out of bounds");
        // Invariant assertion: Script size must be reasonable
        assert!(
            input.script_sig.len() <= MAX_SCRIPT_SIZE * 2,
            "Script size {} must be reasonable for input {}",
            input.script_sig.len(),
            i
        );
        if input.script_sig.len() > MAX_SCRIPT_SIZE {
            return Ok(false);
        }
    }

    for (i, output) in tx.outputs.iter().enumerate() {
        // Bounds checking assertion: Output index must be valid
        assert!(i < tx.outputs.len(), "Output index {i} out of bounds");
        // Invariant assertion: Script size must be reasonable
        assert!(
            output.script_pubkey.len() <= MAX_SCRIPT_SIZE * 2,
            "Script size {} must be reasonable for output {}",
            output.script_pubkey.len(),
            i
        );
        if output.script_pubkey.len() > MAX_SCRIPT_SIZE {
            return Ok(false);
        }
    }

    // 3. Check for standard script types (simplified)
    for (i, output) in tx.outputs.iter().enumerate() {
        // Bounds checking assertion: Output index must be valid
        assert!(
            i < tx.outputs.len(),
            "Output index {i} out of bounds in standard check"
        );
        if !is_standard_script(&output.script_pubkey)? {
            return Ok(false);
        }
    }

    // Postcondition assertion: Result must be boolean
    let result = true;
    // Note: Result is boolean (tautology for formal verification)
    Ok(result)
}

/// ReplacementChecks: 𝒯𝒳 × 𝒯𝒳 × 𝒰𝒮 × Mempool → {true, false}
///
/// Check if new transaction can replace existing one (BIP125 RBF rules).
///
/// According to BIP125 and Orange Paper Section 9.3, replacement is allowed if:
/// 1. Existing transaction signals RBF (nSequence < SEQUENCE_FINAL)
/// 2. New transaction has higher fee rate: FeeRate(tx_2) > FeeRate(tx_1)
/// 3. New transaction pays absolute fee bump: Fee(tx_2) > Fee(tx_1) + MIN_RELAY_FEE
/// 4. New transaction conflicts with existing: tx_2 spends at least one input from tx_1
/// 5. No new unconfirmed dependencies: All inputs of tx_2 are confirmed or from tx_1
#[spec_locked("9.3")]
pub fn replacement_checks(
    new_tx: &Transaction,
    existing_tx: &Transaction,
    utxo_set: &UtxoSet,
    mempool: &Mempool,
) -> Result<bool> {
    // Precondition checks: Validate function inputs
    // Note: We check these conditions and return an error rather than asserting,
    // to allow tests to verify the validation logic properly
    // Bitcoin requires transactions to have both inputs and outputs (except coinbase)
    if new_tx.inputs.is_empty() && new_tx.outputs.is_empty() {
        return Err(crate::error::ConsensusError::ConsensusRuleViolation(
            "New transaction must have at least one input or output"
                .to_string()
                .into(),
        ));
    }
    if existing_tx.inputs.is_empty() && existing_tx.outputs.is_empty() {
        return Err(crate::error::ConsensusError::ConsensusRuleViolation(
            "Existing transaction must have at least one input or output"
                .to_string()
                .into(),
        ));
    }
    if is_coinbase(new_tx) {
        return Err(crate::error::ConsensusError::ConsensusRuleViolation(
            "New transaction cannot be coinbase".to_string().into(),
        ));
    }
    if is_coinbase(existing_tx) {
        return Err(crate::error::ConsensusError::ConsensusRuleViolation(
            "Existing transaction cannot be coinbase".to_string().into(),
        ));
    }
    assert!(
        utxo_set.len() <= u32::MAX as usize,
        "UTXO set size {} exceeds maximum",
        utxo_set.len()
    );

    // 1. Check RBF signaling - existing transaction must signal RBF
    // Note: new_tx doesn't need to signal RBF per BIP125, only existing_tx does
    if !signals_rbf(existing_tx) {
        return Ok(false);
    }

    // 2. Check fee rate: FeeRate(tx_2) > FeeRate(tx_1)
    let new_fee = calculate_fee(new_tx, utxo_set)?;
    let existing_fee = calculate_fee(existing_tx, utxo_set)?;
    // Invariant assertion: Fees must be non-negative
    assert!(new_fee >= 0, "New fee {new_fee} must be non-negative");
    assert!(
        existing_fee >= 0,
        "Existing fee {existing_fee} must be non-negative"
    );
    use crate::constants::MAX_MONEY;
    assert!(
        new_fee <= MAX_MONEY,
        "New fee {new_fee} must not exceed MAX_MONEY"
    );
    assert!(
        existing_fee <= MAX_MONEY,
        "Existing fee {existing_fee} must not exceed MAX_MONEY"
    );

    let new_tx_size = calculate_transaction_size_vbytes(new_tx);
    let existing_tx_size = calculate_transaction_size_vbytes(existing_tx);
    // Invariant assertion: Transaction sizes must be positive
    assert!(
        new_tx_size > 0,
        "New transaction size {new_tx_size} must be positive"
    );
    assert!(
        existing_tx_size > 0,
        "Existing transaction size {existing_tx_size} must be positive"
    );
    assert!(
        new_tx_size <= MAX_TX_SIZE * 2,
        "New transaction size {new_tx_size} must be reasonable"
    );
    assert!(
        existing_tx_size <= MAX_TX_SIZE * 2,
        "Existing transaction size {existing_tx_size} must be reasonable"
    );

    if new_tx_size == 0 || existing_tx_size == 0 {
        return Ok(false);
    }

    // Use integer-based comparison to avoid floating-point precision issues
    // Compare: new_fee / new_tx_size > existing_fee / existing_tx_size
    // Equivalent to: new_fee * existing_tx_size > existing_fee * new_tx_size
    // This avoids floating-point division and precision errors

    // Runtime assertion: Transaction sizes must be positive
    debug_assert!(
        new_tx_size > 0,
        "New transaction size ({new_tx_size}) must be positive"
    );
    debug_assert!(
        existing_tx_size > 0,
        "Existing transaction size ({existing_tx_size}) must be positive"
    );

    // Use integer multiplication to avoid floating-point precision issues
    // Check: new_fee * existing_tx_size > existing_fee * new_tx_size
    let new_fee_scaled = (new_fee as u128)
        .checked_mul(existing_tx_size as u128)
        .ok_or_else(|| {
            ConsensusError::TransactionValidation("Fee rate calculation overflow".into())
        })?;
    let existing_fee_scaled = (existing_fee as u128)
        .checked_mul(new_tx_size as u128)
        .ok_or_else(|| {
            ConsensusError::TransactionValidation("Fee rate calculation overflow".into())
        })?;

    if new_fee_scaled <= existing_fee_scaled {
        return Ok(false);
    }

    // 3. Check absolute fee bump: Fee(tx_2) > Fee(tx_1) + MIN_RELAY_FEE
    if new_fee <= existing_fee + MIN_RELAY_FEE {
        return Ok(false);
    }

    // 4. Check conflict: tx_2 must spend at least one input from tx_1
    if !has_conflict_with_tx(new_tx, existing_tx) {
        return Ok(false);
    }

    // 5. Check for new unconfirmed dependencies
    // All inputs of tx_2 must be confirmed (in UTXO set) or from tx_1
    if creates_new_dependencies(new_tx, existing_tx, utxo_set, mempool)? {
        return Ok(false);
    }

    Ok(true)
}

// ============================================================================
// HELPER FUNCTIONS
// ============================================================================

/// Mempool data structure
pub type Mempool = HashSet<Hash>;

/// Result of mempool acceptance
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MempoolResult {
    Accepted,
    Rejected(String),
}

/// Update mempool after block connection
///
/// Removes transactions that were included in the block and transactions
/// that became invalid due to spent inputs.
///
/// This function should be called after successfully connecting a block
/// to keep the mempool synchronized with the blockchain state.
///
/// # Arguments
///
/// * `mempool` - Mutable reference to the mempool
/// * `block` - The block that was just connected
/// * `utxo_set` - The updated UTXO set after block connection
///
/// # Returns
///
/// Returns a vector of transaction IDs that were removed from the mempool.
///
/// # Example
///
/// ```rust
/// use blvm_consensus::mempool::{Mempool, update_mempool_after_block};
/// use blvm_consensus::block::{connect_block, BlockValidationContext};
/// use blvm_consensus::ValidationResult;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # use blvm_consensus::types::*;
/// # use blvm_consensus::mining::calculate_merkle_root;
/// # let coinbase_tx = Transaction {
/// #     version: 1,
/// #     inputs: vec![TransactionInput {
/// #         prevout: OutPoint { hash: [0; 32].into(), index: 0xffffffff },
/// #         script_sig: vec![],
/// #         sequence: 0xffffffff,
/// #     }].into(),
/// #     outputs: vec![TransactionOutput { value: 5000000000, script_pubkey: vec![].into() }].into(),
/// #     lock_time: 0,
/// # };
/// # let merkle_root = calculate_merkle_root(&[coinbase_tx.clone()]).unwrap();
/// # let block = Block {
/// #     header: BlockHeader {
/// #         version: 1, prev_block_hash: [0; 32], merkle_root,
/// #         timestamp: 1234567890, bits: 0x1d00ffff, nonce: 0,
/// #     },
/// #     transactions: vec![coinbase_tx].into(),
/// # };
/// // One `Vec<Witness>` per tx; coinbase has one input → one empty witness stack.
/// # let witnesses: Vec<Vec<blvm_consensus::segwit::Witness>> = vec![vec![vec![]]];
/// # let mut utxo_set = UtxoSet::default();
/// # let height = 0;
/// # let mut mempool = Mempool::new();
/// let ctx = BlockValidationContext::for_network(Network::Regtest);
/// let (result, new_utxo_set, _) = connect_block(&block, &witnesses, utxo_set, height, &ctx)?;
/// if matches!(result, ValidationResult::Valid) {
///     let removed = update_mempool_after_block(&mut mempool, &block, &new_utxo_set)?;
///     println!("Removed {} transactions from mempool", removed.len());
/// }
/// # Ok(())
/// # }
/// ```
#[spec_locked("9.1")]
pub fn update_mempool_after_block(
    mempool: &mut Mempool,
    block: &crate::types::Block,
    _utxo_set: &crate::types::UtxoSet,
) -> Result<Vec<Hash>> {
    let mut removed = Vec::new();

    // 1. Remove transactions that were included in the block
    for tx in &block.transactions {
        let tx_id = crate::block::calculate_tx_id(tx);
        if mempool.remove(&tx_id) {
            removed.push(tx_id);
        }
    }

    // 2. Remove transactions that became invalid (inputs were spent by block)
    // Note: We don't have the transaction here, just the ID
    // In a full implementation, we'd need a transaction store or lookup
    // For now, we'll skip this check and rely on the caller to handle it
    // Use `update_mempool_after_block_with_lookup` for full validation
    // This is a limitation that should be addressed with a transaction index

    Ok(removed)
}

/// Update mempool after block connection (with transaction lookup)
///
/// This is a more complete version that can check if mempool transactions
/// became invalid. Requires a way to look up transactions by ID.
///
/// # Arguments
///
/// * `mempool` - Mutable reference to the mempool
/// * `block` - The block that was just connected
/// * `get_tx_by_id` - Function to look up transactions by ID
///
/// # Returns
///
/// Returns a vector of transaction IDs that were removed from the mempool.
#[spec_locked("9.1")]
pub fn update_mempool_after_block_with_lookup<F>(
    mempool: &mut Mempool,
    block: &crate::types::Block,
    get_tx_by_id: F,
) -> Result<Vec<Hash>>
where
    F: Fn(&Hash) -> Option<crate::types::Transaction>,
{
    let mut removed = Vec::new();

    // 1. Remove transactions that were included in the block
    for tx in &block.transactions {
        let tx_id = crate::block::calculate_tx_id(tx);
        if mempool.remove(&tx_id) {
            removed.push(tx_id);
        }
    }

    // 2. Remove transactions that became invalid (inputs were spent by block)
    // Collect spent outpoints from the block
    let mut spent_outpoints = std::collections::HashSet::new();
    for tx in &block.transactions {
        if !crate::transaction::is_coinbase(tx) {
            for input in &tx.inputs {
                spent_outpoints.insert(input.prevout);
            }
        }
    }

    // Check each mempool transaction to see if it spends any of the spent outpoints
    let mut invalid_tx_ids = Vec::new();
    for &tx_id in mempool.iter() {
        if let Some(tx) = get_tx_by_id(&tx_id) {
            // Check if any input of this transaction was spent by the block
            for input in &tx.inputs {
                if spent_outpoints.contains(&input.prevout) {
                    invalid_tx_ids.push(tx_id);
                    break;
                }
            }
        }
    }

    // Remove invalid transactions
    for tx_id in invalid_tx_ids {
        if mempool.remove(&tx_id) {
            removed.push(tx_id);
        }
    }

    Ok(removed)
}

/// Check mempool-specific rules
fn check_mempool_rules(tx: &Transaction, fee: Integer, mempool: &Mempool) -> Result<bool> {
    // Check minimum fee rate (simplified)
    let tx_size = calculate_transaction_size(tx);
    // Use integer-based fee rate calculation to avoid floating-point precision issues
    // For display purposes, we still use f64, but for comparisons we use integer math
    // Runtime assertion: Transaction size must be positive
    debug_assert!(
        tx_size > 0,
        "Transaction size ({tx_size}) must be positive for fee rate calculation"
    );

    let fee_rate = (fee as f64) / (tx_size as f64);

    // Runtime assertion: Fee rate must be non-negative
    debug_assert!(
        fee_rate >= 0.0,
        "Fee rate ({fee_rate:.6}) must be non-negative (fee: {fee}, size: {tx_size})"
    );

    // Get minimum fee rate from configuration
    let config = crate::config::get_consensus_config_ref();
    let min_fee_rate = config.mempool.min_relay_fee_rate as f64; // sat/vB
    let min_tx_fee = config.mempool.min_tx_fee; // absolute minimum fee

    // Check absolute minimum fee
    if fee < min_tx_fee {
        return Ok(false);
    }

    // Check fee rate (sat/vB)
    if fee_rate < min_fee_rate {
        return Ok(false);
    }

    // Check mempool size limits using configuration
    // Use transaction count limit (simpler than size-based for now)
    if mempool.len() > config.mempool.max_mempool_txs {
        return Ok(false);
    }

    Ok(true)
}

/// Check for transaction conflicts
fn has_conflicts(tx: &Transaction, mempool: &Mempool) -> Result<bool> {
    // Check if any input is already spent by mempool transaction
    for input in &tx.inputs {
        // In a real implementation, we'd check if this input is already spent
        // by another transaction in the mempool
        // For now, we'll do a simplified check
        if mempool.contains(&input.prevout.hash) {
            return Ok(true);
        }
    }

    Ok(false)
}

/// Check if transaction is final (Orange Paper Section 9.1 - Transaction Finality)
///
/// IsFinalTx — locktime/sequence validation (BIP65/68).
///
/// A transaction is final if:
/// 1. tx.lock_time == 0 (no locktime restriction), OR
/// 2. If locktime < LOCKTIME_THRESHOLD (block height): height > tx.lock_time
/// 3. If locktime >= LOCKTIME_THRESHOLD (timestamp): block_time > tx.lock_time
/// 4. OR if all inputs have SEQUENCE_FINAL (0xffffffff), locktime is ignored
///
/// Mathematical specification:
/// ∀ tx ∈ Transaction, height ∈ ℕ, block_time ∈ ℕ:
/// - is_final_tx(tx, height, block_time) = true ⟹
///   (tx.lock_time = 0 ∨
///   (tx.lock_time < LOCKTIME_THRESHOLD ∧ height > tx.lock_time) ∨
///   (tx.lock_time >= LOCKTIME_THRESHOLD ∧ block_time > tx.lock_time) ∨
///   (∀ input ∈ tx.inputs: input.sequence == SEQUENCE_FINAL))
///
/// Check if transaction is final (Orange Paper Section 9.1 - Transaction Finality)
///
/// IsFinalTx — locktime/sequence validation (BIP65/68).
///
/// A transaction is final if:
/// 1. tx.lock_time == 0 (no locktime restriction), OR
/// 2. If locktime < LOCKTIME_THRESHOLD (block height): height > tx.lock_time
/// 3. If locktime >= LOCKTIME_THRESHOLD (timestamp): block_time > tx.lock_time
/// 4. OR if all inputs have SEQUENCE_FINAL (0xffffffff), locktime is ignored
///
/// Mathematical specification:
/// ∀ tx ∈ Transaction, height ∈ ℕ, block_time ∈ ℕ:
/// - is_final_tx(tx, height, block_time) = true ⟹
///   (tx.lock_time = 0 ∨
///   (tx.lock_time < LOCKTIME_THRESHOLD ∧ height > tx.lock_time) ∨
///   (tx.lock_time >= LOCKTIME_THRESHOLD ∧ block_time > tx.lock_time) ∨
///   (∀ input ∈ tx.inputs: input.sequence == SEQUENCE_FINAL))
///
/// # Arguments
/// * `tx` - Transaction to check
/// * `height` - Current block height
/// * `block_time` - Median time-past of chain tip (BIP113) for timestamp locktime validation
#[spec_locked("9.1")]
pub fn is_final_tx(tx: &Transaction, height: Natural, block_time: Natural) -> bool {
    use crate::constants::SEQUENCE_FINAL;

    // If locktime is 0, transaction is always final
    if tx.lock_time == 0 {
        return true;
    }

    // Check if locktime is satisfied based on type
    // If locktime < threshold, compare to block height; else compare to block time
    // This means: locktime < (condition ? height : block_time)
    // So: if locktime < threshold, check locktime < height
    //     if locktime >= threshold, check locktime < block_time
    let locktime_satisfied = if (tx.lock_time as u32) < LOCKTIME_THRESHOLD {
        // Block height locktime: check if locktime < height
        (tx.lock_time as Natural) < height
    } else {
        // Timestamp locktime: check if locktime < block_time
        (tx.lock_time as Natural) < block_time
    };

    if locktime_satisfied {
        return true;
    }

    // Even if locktime isn't satisfied, transaction is final if all inputs have SEQUENCE_FINAL
    // This allows transactions to bypass locktime by setting all sequences to 0xffffffff
    // If all inputs have SEQUENCE_FINAL, locktime is ignored
    for input in &tx.inputs {
        if (input.sequence as u32) != SEQUENCE_FINAL {
            return false;
        }
    }

    // All inputs have SEQUENCE_FINAL - transaction is final regardless of locktime
    true
}

/// Check if transaction signals RBF
///
/// Returns true if any input has nSequence < SEQUENCE_FINAL (0xffffffff)
#[spec_locked("9.3")]
pub fn signals_rbf(tx: &Transaction) -> bool {
    for input in &tx.inputs {
        if (input.sequence as u32) < SEQUENCE_FINAL {
            return true;
        }
    }
    false
}

/// Calculate transaction size in virtual bytes (vbytes)
///
/// For SegWit transactions, uses weight/4 (virtual bytes).
/// For non-SegWit transactions, uses byte size.
/// This is a simplified version - proper implementation would use segwit::calculate_weight()
fn calculate_transaction_size_vbytes(tx: &Transaction) -> usize {
    // Simplified: use byte size as approximation
    // In production, should use proper weight calculation for SegWit
    calculate_transaction_size(tx)
}

/// Check if new transaction conflicts with existing transaction
///
/// A conflict exists if new_tx spends at least one input from existing_tx.
/// This is requirement #4 of BIP125.
#[spec_locked("9.3")]
pub fn has_conflict_with_tx(new_tx: &Transaction, existing_tx: &Transaction) -> bool {
    for new_input in &new_tx.inputs {
        for existing_input in &existing_tx.inputs {
            if new_input.prevout == existing_input.prevout {
                return true;
            }
        }
    }
    false
}

/// Check if new transaction creates new unconfirmed dependencies
///
/// BIP125 requirement #5: All inputs of tx_2 must be:
/// - Confirmed (in UTXO set), OR
/// - From tx_1 (spending the same inputs)
fn creates_new_dependencies(
    new_tx: &Transaction,
    existing_tx: &Transaction,
    utxo_set: &UtxoSet,
    mempool: &Mempool,
) -> Result<bool> {
    for input in &new_tx.inputs {
        // Check if input is confirmed (in UTXO set)
        if utxo_set.contains_key(&input.prevout) {
            continue;
        }

        // Check if input was spent by existing transaction
        let mut found_in_existing = false;
        for existing_input in &existing_tx.inputs {
            if existing_input.prevout == input.prevout {
                found_in_existing = true;
                break;
            }
        }

        if found_in_existing {
            continue;
        }

        // If not confirmed and not from existing tx, it's a new unconfirmed dependency
        // Check if it's at least in mempool (but still unconfirmed)
        if !mempool.contains(&input.prevout.hash) {
            return Ok(true); // New unconfirmed dependency
        }
    }

    Ok(false)
}

/// Check if script is standard
fn is_standard_script(script: &ByteString) -> Result<bool> {
    // Simplified standard script check
    // In reality, this would check for P2PKH, P2SH, P2WPKH, P2WSH, etc.
    if script.is_empty() {
        return Ok(false);
    }

    // Basic checks
    if script.len() > MAX_SCRIPT_SIZE {
        return Ok(false);
    }

    // Check for non-standard opcodes (simplified)
    for &byte in script {
        if byte > 0x60 && byte < 0x7f {
            // Some non-standard opcodes
            return Ok(false);
        }
    }

    Ok(true)
}

/// Calculate transaction ID (deprecated - use crate::block::calculate_tx_id instead)
///
/// This function is kept for backward compatibility but delegates to the
/// standard implementation in block.rs.
#[deprecated(note = "Use crate::block::calculate_tx_id instead")]
#[spec_locked("5.1")]
pub fn calculate_tx_id(tx: &Transaction) -> Hash {
    crate::block::calculate_tx_id(tx)
}

/// Calculate transaction size (simplified)
// Use the actual serialization-based size calculation from transaction module
// Ensures consistency with base serialization size (no witness)
fn calculate_transaction_size(tx: &Transaction) -> usize {
    use crate::transaction::calculate_transaction_size as tx_size;
    tx_size(tx)
}

/// Check if transaction is coinbase
fn is_coinbase(tx: &Transaction) -> bool {
    // Optimization: Use constant folding for zero hash check
    #[cfg(feature = "production")]
    {
        use crate::optimizations::constant_folding::is_zero_hash;
        tx.inputs.len() == 1
            && is_zero_hash(&tx.inputs[0].prevout.hash)
            && tx.inputs[0].prevout.index == 0xffffffff
    }

    #[cfg(not(feature = "production"))]
    {
        tx.inputs.len() == 1
            && tx.inputs[0].prevout.hash == [0u8; 32]
            && tx.inputs[0].prevout.index == 0xffffffff
    }
}

// ============================================================================
// FORMAL VERIFICATION
// ============================================================================

/// Mathematical Specification for Mempool:
/// ∀ tx ∈ 𝒯𝒳, utxo_set ∈ 𝒰𝒮, mempool ∈ Mempool:
/// - accept_to_memory_pool(tx, utxo_set, mempool) = Accepted ⟹
///   (tx ∉ mempool ∧
///    CheckTransaction(tx) = valid ∧
///    CheckTxInputs(tx, utxo_set) = valid ∧
///    VerifyScripts(tx) = valid ∧
///    ¬has_conflicts(tx, mempool))
///
/// Invariants:
/// - Mempool never contains duplicate transactions
/// - Mempool never contains conflicting transactions
/// - Accepted transactions are valid
/// - RBF rules are enforced

#[cfg(test)]
mod tests {
    use super::*;
    use crate::opcodes::*;

    #[test]
    fn test_accept_to_memory_pool_valid() {
        // Skip script validation for now - focus on mempool logic
        let tx = create_valid_transaction();
        let utxo_set = create_test_utxo_set();
        let mempool = Mempool::new();

        // This will fail on script validation, but that's expected
        let time_context = Some(TimeContext {
            network_time: 1234567890,
            median_time_past: 1234567890,
        });
        let result =
            accept_to_memory_pool(&tx, None, &utxo_set, &mempool, 100, time_context).unwrap();
        assert!(matches!(result, MempoolResult::Rejected(_)));
    }

    #[test]
    fn test_accept_to_memory_pool_duplicate() {
        let tx = create_valid_transaction();
        let utxo_set = create_test_utxo_set();
        let mut mempool = Mempool::new();
        mempool.insert(crate::block::calculate_tx_id(&tx));

        let time_context = Some(TimeContext {
            network_time: 1234567890,
            median_time_past: 1234567890,
        });
        let result =
            accept_to_memory_pool(&tx, None, &utxo_set, &mempool, 100, time_context).unwrap();
        assert!(matches!(result, MempoolResult::Rejected(_)));
    }

    #[test]
    fn test_is_standard_tx_valid() {
        let tx = create_valid_transaction();
        assert!(is_standard_tx(&tx).unwrap());
    }

    #[test]
    fn test_is_standard_tx_too_large() {
        let mut tx = create_valid_transaction();
        // Make transaction too large by adding many inputs
        // MAX_INPUTS (100,000) * ~42 bytes/input >> MAX_TX_SIZE (1,000,000)
        for _ in 0..MAX_INPUTS {
            tx.inputs.push(create_dummy_input());
        }
        // Transaction exceeds MAX_TX_SIZE so it should NOT be standard
        assert!(!is_standard_tx(&tx).unwrap());
    }

    #[test]
    fn test_replacement_checks_all_requirements() {
        let utxo_set = create_test_utxo_set();
        let mempool = Mempool::new();

        // Create existing transaction with RBF signaling and lower fee
        let mut existing_tx = create_valid_transaction();
        existing_tx.inputs[0].sequence = SEQUENCE_RBF as u64;
        existing_tx.outputs[0].value = 9000; // Fee = 10000 - 9000 = 1000 sats

        // Create new transaction that:
        // 1. Signals RBF (or doesn't - per BIP125 only existing needs to signal)
        // 2. Conflicts with existing (same input)
        // 3. Has higher fee rate and absolute fee
        let mut new_tx = existing_tx.clone();
        new_tx.outputs[0].value = 8000; // Fee = 10000 - 8000 = 2000 sats
                                        // Higher fee rate and absolute fee bump (2000 > 1000 + 1000 = 2000, needs >)
        new_tx.outputs[0].value = 7999; // Fee = 10000 - 7999 = 2001 sats

        // Should pass all BIP125 checks
        let result = replacement_checks(&new_tx, &existing_tx, &utxo_set, &mempool).unwrap();
        assert!(result, "Valid RBF replacement should be accepted");
    }

    #[test]
    fn test_replacement_checks_no_rbf_signal() {
        let utxo_set = create_test_utxo_set();
        let mempool = Mempool::new();

        let new_tx = create_valid_transaction();
        let existing_tx = create_valid_transaction(); // No RBF signal

        // Should fail: existing transaction doesn't signal RBF
        assert!(!replacement_checks(&new_tx, &existing_tx, &utxo_set, &mempool).unwrap());
    }

    #[test]
    fn test_replacement_checks_no_conflict() {
        let mut utxo_set = create_test_utxo_set();
        // Add UTXO for the new transaction's input
        let new_outpoint = OutPoint {
            hash: [2; 32],
            index: 0,
        };
        let new_utxo = UTXO {
            value: 10000,
            script_pubkey: vec![OP_1].into(),
            height: 0,
            is_coinbase: false,
        };
        utxo_set.insert(new_outpoint, std::sync::Arc::new(new_utxo));

        let mempool = Mempool::new();

        let mut existing_tx = create_valid_transaction();
        existing_tx.inputs[0].sequence = SEQUENCE_RBF as u64;

        // New transaction with different input (no conflict)
        let mut new_tx = create_valid_transaction();
        new_tx.inputs[0].prevout.hash = [2; 32]; // Different input
        new_tx.inputs[0].sequence = SEQUENCE_RBF as u64;
        // Ensure output value doesn't exceed input value to avoid negative fee
        new_tx.outputs[0].value = 5000; // Less than input value of 10000

        // Should fail: no conflict (requirement #4)
        assert!(!replacement_checks(&new_tx, &existing_tx, &utxo_set, &mempool).unwrap());
    }

    #[test]
    fn test_replacement_checks_fee_rate_too_low() {
        let utxo_set = create_test_utxo_set();
        let mempool = Mempool::new();

        // Existing transaction with higher fee rate
        let mut existing_tx = create_valid_transaction();
        existing_tx.inputs[0].sequence = SEQUENCE_RBF as u64;
        existing_tx.outputs[0].value = 5000; // Fee = 5000 sats, size = small

        // New transaction with same or lower fee rate (but higher absolute fee)
        let mut new_tx = existing_tx.clone();
        new_tx.outputs[0].value = 4999; // Fee = 5001 sats, but same size so same fee rate

        // Should fail: fee rate not higher (requirement #2)
        assert!(!replacement_checks(&new_tx, &existing_tx, &utxo_set, &mempool).unwrap());
    }

    #[test]
    fn test_replacement_checks_absolute_fee_insufficient() {
        let utxo_set = create_test_utxo_set();
        let mempool = Mempool::new();

        // Existing transaction
        let mut existing_tx = create_valid_transaction();
        existing_tx.inputs[0].sequence = SEQUENCE_RBF as u64;
        existing_tx.outputs[0].value = 9000; // Fee = 1000 sats

        // New transaction with higher fee rate but insufficient absolute fee bump
        // Fee must be > 1000 + 1000 = 2000, so need > 2000
        let mut new_tx = existing_tx.clone();
        new_tx.outputs[0].value = 8001; // Fee = 1999 sats (insufficient)

        // Should fail: absolute fee not high enough (requirement #3)
        assert!(!replacement_checks(&new_tx, &existing_tx, &utxo_set, &mempool).unwrap());

        // Now with sufficient fee
        new_tx.outputs[0].value = 7999; // Fee = 2001 sats (sufficient)
                                        // Should still fail on other checks (conflict, etc.), but fee check passes
                                        // For full test, need to ensure conflict exists
    }

    // ============================================================================
    // COMPREHENSIVE MEMPOOL TESTS
    // ============================================================================

    #[test]
    fn test_accept_to_memory_pool_coinbase() {
        let coinbase_tx = create_coinbase_transaction();
        let utxo_set = UtxoSet::default();
        let mempool = Mempool::new();
        // Coinbase transactions should be rejected from mempool
        let time_context = Some(TimeContext {
            network_time: 0,
            median_time_past: 0,
        });
        let result =
            accept_to_memory_pool(&coinbase_tx, None, &utxo_set, &mempool, 100, time_context)
                .unwrap();
        assert!(matches!(result, MempoolResult::Rejected(_)));
    }

    #[test]
    fn test_is_standard_tx_large_script() {
        let mut tx = create_valid_transaction();
        // Create a script that's too large
        tx.inputs[0].script_sig = vec![OP_1; MAX_SCRIPT_SIZE + 1];

        let result = is_standard_tx(&tx).unwrap();
        assert!(!result);
    }

    #[test]
    fn test_is_standard_tx_large_output_script() {
        let mut tx = create_valid_transaction();
        // Create an output script that's too large
        tx.outputs[0].script_pubkey = vec![OP_1; MAX_SCRIPT_SIZE + 1];

        let result = is_standard_tx(&tx).unwrap();
        assert!(!result);
    }

    #[test]
    fn test_replacement_checks_new_unconfirmed_dependency() {
        let utxo_set = create_test_utxo_set();
        let mempool = Mempool::new();

        // Existing transaction
        let mut existing_tx = create_valid_transaction();
        existing_tx.inputs[0].sequence = SEQUENCE_RBF as u64;

        // New transaction that adds a new unconfirmed input
        let mut new_tx = existing_tx.clone();
        new_tx.inputs.push(TransactionInput {
            prevout: OutPoint {
                hash: [99; 32],
                index: 0,
            }, // Not in UTXO set
            script_sig: vec![],
            sequence: SEQUENCE_RBF as u64,
        });
        new_tx.outputs[0].value = 7000; // Higher fee

        // Should fail: creates new unconfirmed dependency (requirement #5)
        assert!(!replacement_checks(&new_tx, &existing_tx, &utxo_set, &mempool).unwrap());
    }

    #[test]
    fn test_has_conflict_with_tx_true() {
        let tx1 = create_valid_transaction();
        let mut tx2 = create_valid_transaction();
        tx2.inputs[0].prevout = tx1.inputs[0].prevout.clone(); // Same input = conflict

        assert!(has_conflict_with_tx(&tx2, &tx1));
    }

    #[test]
    fn test_has_conflict_with_tx_false() {
        let tx1 = create_valid_transaction();
        let mut tx2 = create_valid_transaction();
        tx2.inputs[0].prevout.hash = [2; 32]; // Different input = no conflict

        assert!(!has_conflict_with_tx(&tx2, &tx1));
    }

    #[test]
    fn test_replacement_checks_minimum_relay_fee() {
        let utxo_set = create_test_utxo_set();
        let mempool = Mempool::new();

        // Existing transaction
        let mut existing_tx = create_valid_transaction();
        existing_tx.inputs[0].sequence = SEQUENCE_RBF as u64;
        existing_tx.outputs[0].value = 9500; // Fee = 500 sats

        // New transaction with exactly MIN_RELAY_FEE bump (not enough, need >)
        let mut new_tx = existing_tx.clone();
        new_tx.outputs[0].value = 8500; // Fee = 1500 sats (1500 > 500 + 1000 = 1500? No, need >)
        assert!(!replacement_checks(&new_tx, &existing_tx, &utxo_set, &mempool).unwrap());

        // New transaction with sufficient bump
        // Fee = 1501 sats (1501 > 500 + 1000 = 1500)
        // Conflict detection and fee rate validation are handled by accept_to_memory_pool
        new_tx.outputs[0].value = 8499;
    }

    #[test]
    fn test_check_mempool_rules_low_fee() {
        let tx = create_valid_transaction();
        let fee = 1; // Very low fee
        let mempool = Mempool::new();

        let result = check_mempool_rules(&tx, fee, &mempool).unwrap();
        assert!(!result);
    }

    #[test]
    fn test_check_mempool_rules_high_fee() {
        let tx = create_valid_transaction();
        let fee = 10000; // High fee
        let mempool = Mempool::new();

        let result = check_mempool_rules(&tx, fee, &mempool).unwrap();
        assert!(result);
    }

    #[test]
    fn test_check_mempool_rules_full_mempool() {
        let tx = create_valid_transaction();
        let fee = 10000;
        let mut mempool = Mempool::new();

        // Fill mempool beyond limit with unique hashes
        // Default max_mempool_txs is 100,000, so we need to exceed that
        for i in 0..100_001 {
            let mut hash = [0u8; 32];
            hash[0] = (i & 0xff) as u8;
            hash[1] = ((i >> 8) & 0xff) as u8;
            hash[2] = ((i >> 16) & 0xff) as u8;
            hash[3] = ((i >> 24) & 0xff) as u8;
            mempool.insert(hash);
        }

        // Verify mempool is actually full (exceeds max_mempool_txs limit of 100,000)
        assert!(mempool.len() > 100_000);

        let result = check_mempool_rules(&tx, fee, &mempool).unwrap();
        assert!(!result);
    }

    #[test]
    fn test_has_conflicts_no_conflicts() {
        let tx = create_valid_transaction();
        let mempool = Mempool::new();

        let result = has_conflicts(&tx, &mempool).unwrap();
        assert!(!result);
    }

    #[test]
    fn test_has_conflicts_with_conflicts() {
        let tx = create_valid_transaction();
        let mut mempool = Mempool::new();

        // Add a conflicting transaction to mempool
        mempool.insert(tx.inputs[0].prevout.hash);

        let result = has_conflicts(&tx, &mempool).unwrap();
        assert!(result);
    }

    #[test]
    fn test_signals_rbf_true() {
        let mut tx = create_valid_transaction();
        tx.inputs[0].sequence = 0xfffffffe; // RBF signal

        assert!(signals_rbf(&tx));
    }

    #[test]
    fn test_signals_rbf_false() {
        let tx = create_valid_transaction(); // sequence = 0xffffffff (final)

        assert!(!signals_rbf(&tx));
    }

    #[test]
    fn test_calculate_fee_rate() {
        let tx = create_valid_transaction();
        let utxo_set = create_test_utxo_set();
        let fee = calculate_fee(&tx, &utxo_set);

        // Fee should be calculable (may be 0 for valid transactions)
        assert!(fee.is_ok());
    }

    #[test]
    fn test_creates_new_dependencies_no_new() {
        let new_tx = create_valid_transaction();
        let existing_tx = create_valid_transaction();
        let mempool = Mempool::new();

        let utxo_set = create_test_utxo_set();
        let result = creates_new_dependencies(&new_tx, &existing_tx, &utxo_set, &mempool).unwrap();
        assert!(!result);
    }

    #[test]
    fn test_creates_new_dependencies_with_new() {
        let mut new_tx = create_valid_transaction();
        let existing_tx = create_valid_transaction();
        let mempool = Mempool::new();

        // Make new_tx spend a different input
        new_tx.inputs[0].prevout.hash = [2; 32];

        let utxo_set = create_test_utxo_set();
        let result = creates_new_dependencies(&new_tx, &existing_tx, &utxo_set, &mempool).unwrap();
        assert!(result);
    }

    #[test]
    fn test_is_standard_script_empty() {
        let script = vec![];
        let result = is_standard_script(&script).unwrap();
        assert!(!result);
    }

    #[test]
    fn test_is_standard_script_too_large() {
        let script = vec![OP_1; MAX_SCRIPT_SIZE + 1];
        let result = is_standard_script(&script).unwrap();
        assert!(!result);
    }

    #[test]
    fn test_is_standard_script_non_standard_opcode() {
        let script = vec![OP_VERIF]; // Non-standard opcode (disabled)
        let result = is_standard_script(&script).unwrap();
        assert!(!result);
    }

    #[test]
    fn test_is_standard_script_valid() {
        let script = vec![OP_1];
        let result = is_standard_script(&script).unwrap();
        assert!(result);
    }

    #[test]
    fn test_calculate_tx_id() {
        let tx = create_valid_transaction();
        let tx_id = crate::block::calculate_tx_id(&tx);

        // Should be a 32-byte hash
        assert_eq!(tx_id.len(), 32);

        // Same transaction should produce same ID
        let tx_id2 = crate::block::calculate_tx_id(&tx);
        assert_eq!(tx_id, tx_id2);
    }

    #[test]
    fn test_calculate_tx_id_different_txs() {
        let tx1 = create_valid_transaction();
        let mut tx2 = tx1.clone();
        tx2.version = 2; // Different version

        let id1 = crate::block::calculate_tx_id(&tx1);
        let id2 = crate::block::calculate_tx_id(&tx2);

        assert_ne!(id1, id2);
    }

    #[test]
    fn test_calculate_transaction_size() {
        let tx = create_valid_transaction();
        let size = calculate_transaction_size(&tx);

        assert!(size > 0);

        // Size should be deterministic
        let size2 = calculate_transaction_size(&tx);
        assert_eq!(size, size2);
    }

    #[test]
    fn test_calculate_transaction_size_multiple_inputs_outputs() {
        let mut tx = create_valid_transaction();
        tx.inputs.push(create_dummy_input());
        tx.outputs.push(create_dummy_output());

        let size = calculate_transaction_size(&tx);
        assert!(size > 0);
    }

    #[test]
    fn test_is_coinbase_true() {
        let coinbase_tx = create_coinbase_transaction();
        assert!(is_coinbase(&coinbase_tx));
    }

    #[test]
    fn test_is_coinbase_false() {
        let regular_tx = create_valid_transaction();
        assert!(!is_coinbase(&regular_tx));
    }

    // Helper functions for tests
    fn create_valid_transaction() -> Transaction {
        Transaction {
            version: 1,
            inputs: vec![create_dummy_input()].into(),
            outputs: vec![create_dummy_output()].into(),
            lock_time: 0,
        }
    }

    fn create_dummy_input() -> TransactionInput {
        TransactionInput {
            prevout: OutPoint {
                hash: [1; 32],
                index: 0,
            },
            script_sig: vec![OP_1],
            sequence: 0xffffffff,
        }
    }

    fn create_dummy_output() -> TransactionOutput {
        TransactionOutput {
            value: 1000,
            script_pubkey: vec![OP_1].into(), // OP_1 for valid script
        }
    }

    fn create_test_utxo_set() -> UtxoSet {
        let mut utxo_set = UtxoSet::default();
        let outpoint = OutPoint {
            hash: [1; 32],
            index: 0,
        };
        let utxo = UTXO {
            value: 10000,
            script_pubkey: vec![OP_1].into(), // OP_1 for valid script
            height: 0,
            is_coinbase: false,
        };
        utxo_set.insert(outpoint, std::sync::Arc::new(utxo));
        utxo_set
    }

    fn create_coinbase_transaction() -> Transaction {
        Transaction {
            version: 1,
            inputs: vec![TransactionInput {
                prevout: OutPoint {
                    hash: [0; 32].into(),
                    index: 0xffffffff,
                },
                script_sig: vec![],
                sequence: 0xffffffff,
            }]
            .into(),
            outputs: vec![TransactionOutput {
                value: 5000000000,
                script_pubkey: vec![].into(),
            }]
            .into(),
            lock_time: 0,
        }
    }
}