blvm-consensus 0.1.82

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
use super::*;
use crate::opcodes::*;

#[test]
fn sigop_adjusted_vsize_matches_core_rule() {
    // Weight wins: 400 weight, 1 sigop * 20 = 20. vsize = ceil(400/4) = 100.
    assert_eq!(sigop_adjusted_vsize(400, 1, DEFAULT_BYTES_PER_SIGOP), 100);
    // Sigop term wins: 100 weight, 10 sigops * 20 = 200. vsize = ceil(200/4) = 50.
    assert_eq!(sigop_adjusted_vsize(100, 10, DEFAULT_BYTES_PER_SIGOP), 50);
    // Ceil: 201 weight is not divisible by 4.
    assert_eq!(sigop_adjusted_vsize(201, 0, DEFAULT_BYTES_PER_SIGOP), 51);
    // Default multiplier is 20 weight units per sigop (5 vbytes).
    assert_eq!(DEFAULT_BYTES_PER_SIGOP, 20);
    assert_eq!(MAX_STANDARD_TX_SIGOPS_COST, 16_000);
}

#[test]
fn test_disabled_opcode_spend_is_rejected() {
    let prevout = OutPoint {
        hash: [1; 32],
        index: 0,
    };
    let mut utxo_set = UtxoSet::default();
    utxo_set.insert(
        prevout,
        std::sync::Arc::new(UTXO {
            value: 10_000,
            script_pubkey: vec![OP_MUL].into(),
            height: 0,
            is_coinbase: false,
        }),
    );
    let tx = Transaction {
        version: 1,
        inputs: vec![TransactionInput {
            prevout,
            script_sig: vec![],
            sequence: 0xffffffff,
        }]
        .into(),
        outputs: vec![TransactionOutput {
            value: 0,
            script_pubkey: vec![OP_1],
        }]
        .into(),
        lock_time: 0,
    };
    let result = accept_to_memory_pool(
        &tx,
        None,
        &utxo_set,
        &Mempool::new(),
        200,
        Some(TimeContext {
            network_time: 1_700_000_000,
            median_time_past: 0,
        }),
        Network::Mainnet,
        None,
    )
    .expect("a disabled opcode is a rejection");
    assert!(matches!(result, MempoolResult::Rejected(_)));
}

#[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();

    // Script: sig=OP_1, spk=OP_1 (UTXO). Stack after: [[1],[1]], top truthy → Accepted.
    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,
        Network::Mainnet,
        None,
    )
    .unwrap();
    assert!(matches!(result, MempoolResult::Accepted));
}

#[test]
fn relative_height_lock_is_not_accepted_until_it_is_old_enough() {
    let mut tx = create_valid_transaction();
    tx.version = 2;
    tx.inputs[0].sequence = 10;
    let prevout = tx.inputs[0].prevout;
    let time_context = Some(TimeContext {
        network_time: 1_700_000_000,
        median_time_past: 1_700_000_000,
    });
    let mempool = Mempool::new();
    let young = utxo_confirmed_at(prevout, 499_995);
    let result = accept_to_memory_pool(
        &tx,
        None,
        &young,
        &mempool,
        500_000,
        time_context,
        Network::Mainnet,
        None,
    )
    .unwrap();
    assert!(matches!(result, MempoolResult::Rejected(_)));

    let old = utxo_confirmed_at(prevout, 499_000);
    let result = accept_to_memory_pool(
        &tx,
        None,
        &old,
        &mempool,
        500_000,
        time_context,
        Network::Mainnet,
        None,
    )
    .unwrap();
    assert!(matches!(result, MempoolResult::Accepted));

    tx.version = 1;
    let result = accept_to_memory_pool(
        &tx,
        None,
        &young,
        &mempool,
        500_000,
        time_context,
        Network::Mainnet,
        None,
    )
    .unwrap();
    assert!(matches!(result, MempoolResult::Accepted));
}

#[test]
fn time_relative_lock_uses_prior_median() {
    const SEQUENCE_LOCKTIME_TYPE_FLAG: u64 = 0x0040_0000;
    let mut tx = create_valid_transaction();
    tx.version = 2;
    tx.inputs[0].sequence = SEQUENCE_LOCKTIME_TYPE_FLAG | 2;
    let prevout = tx.inputs[0].prevout;
    let utxo = utxo_confirmed_at(prevout, 499_000);
    let mempool = Mempool::new();
    let prior = [1_000_000i64];
    let inside = Some(TimeContext {
        network_time: 1_001_023,
        median_time_past: 1_001_023,
    });
    let rejected = accept_to_memory_pool(
        &tx,
        None,
        &utxo,
        &mempool,
        500_000,
        inside,
        Network::Mainnet,
        Some(&prior),
    )
    .unwrap();
    assert!(matches!(rejected, MempoolResult::Rejected(_)));

    let old_enough = Some(TimeContext {
        network_time: 1_001_024,
        median_time_past: 1_001_024,
    });
    let accepted = accept_to_memory_pool(
        &tx,
        None,
        &utxo,
        &mempool,
        500_000,
        old_enough,
        Network::Mainnet,
        Some(&prior),
    )
    .unwrap();
    assert!(matches!(accepted, MempoolResult::Accepted));
}

#[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,
        Network::Mainnet,
        None,
    )
    .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();
    // BIP125 opts in strictly below SEQUENCE_RBF (0xfffffffe).
    existing_tx.inputs[0].sequence = (SEQUENCE_RBF as u64) - 1;
    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 existing_tx = create_valid_transaction();
    let mut new_tx = existing_tx.clone();
    new_tx.outputs[0].value = 500;

    assert!(!signals_rbf(&existing_tx));
    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. Sequence is one below SEQUENCE_RBF, so it signals.
    let mut existing_tx = create_valid_transaction();
    existing_tx.inputs[0].sequence = (SEQUENCE_RBF as u64) - 1;
    existing_tx.outputs[0].value = 9000; // Fee = 1000 sats

    // One sat short of the 100 sat/kvB relay bump. The fee rate is higher.
    let bump = incremental_relay_bump(calculate_transaction_size_vbytes(&existing_tx, None) as u64);
    let mut new_tx = existing_tx.clone();
    new_tx.outputs[0].value = 10_000 - (1_000 + bump - 1);

    assert!(!replacement_checks(&new_tx, &existing_tx, &utxo_set, &mempool).unwrap());
}

// ============================================================================
// 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,
        Network::Mainnet,
        None,
    )
    .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; // 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) - 1;
    existing_tx.outputs[0].value = 9500; // Fee = 500 sats

    let bump = incremental_relay_bump(calculate_transaction_size_vbytes(&existing_tx, None) as u64);
    let mut new_tx = existing_tx.clone();
    new_tx.outputs[0].value = 10_000 - (500 + bump - 1);
    assert!(!replacement_checks(&new_tx, &existing_tx, &utxo_set, &mempool).unwrap());
    new_tx.outputs[0].value = 10_000 - (500 + bump);
    assert!(replacement_checks(&new_tx, &existing_tx, &utxo_set, &mempool).unwrap());
}

#[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, None, 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, None, fee, &mempool).unwrap();
    assert!(result);
}

#[test]
fn test_mempool_total_vbytes_tracking() {
    let mut mempool = Mempool::new();
    let tx = create_valid_transaction();
    let vsize = calculate_transaction_size_vbytes(&tx, None);
    let txid = crate::block::calculate_tx_id(&tx);

    mempool.insert_transaction(&tx);
    assert_eq!(mempool.total_vbytes(), vsize);

    mempool.remove(&txid);
    assert_eq!(mempool.total_vbytes(), 0);
}

#[test]
fn mempool_vsize_includes_witness_stacks() {
    let tx = create_valid_transaction();
    let bare = calculate_transaction_size_vbytes(&tx, None);
    let empty_stacks: Vec<Witness> = vec![vec![]];
    assert_eq!(
        calculate_transaction_size_vbytes(&tx, Some(&empty_stacks)),
        bare
    );

    let heavy_stacks: Vec<Witness> = vec![vec![vec![0u8; 4_000]]];
    let heavy = calculate_transaction_size_vbytes(&tx, Some(&heavy_stacks));
    assert!(heavy > bare);

    let fee = (heavy as i64) - 1;
    assert!(fee >= 1_000);
    let mempool = Mempool::new();
    assert!(check_mempool_rules(&tx, None, fee, &mempool).unwrap());
    assert!(!check_mempool_rules(&tx, Some(&heavy_stacks), fee, &mempool).unwrap());

    let mut pool = Mempool::new();
    assert!(pool.insert_transaction_with_witness(&tx, Some(&heavy_stacks)));
    assert_eq!(pool.total_vbytes(), heavy);
}

#[test]
fn test_mempool_byte_limit_exceeded() {
    let mut mempool = Mempool::new();
    let tx = create_valid_transaction();
    let vsize = calculate_transaction_size_vbytes(&tx, None);
    mempool.insert_transaction(&tx);

    let at_limit = mempool.total_vbytes();
    assert!(!mempool_size_limits_exceeded(
        &mempool, 0, 1_000_000, at_limit
    ));
    assert!(mempool_size_limits_exceeded(
        &mempool, 1, 1_000_000, at_limit
    ));
    assert!(!mempool_size_limits_exceeded(
        &mempool,
        vsize,
        1_000_000,
        at_limit.saturating_add(vsize)
    ));
}

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

    // Fill mempool to the tx-count limit (default max_mempool_txs is 100,000).
    for i in 0..100_000 {
        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);
    }

    assert_eq!(mempool.len(), 100_000);

    let result = check_mempool_rules(&tx, None, 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 (same outpoint, different txid)
    let mut pool_tx = tx.clone();
    pool_tx.version = 2;
    mempool.insert_transaction(&pool_tx);

    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 = 0xfffffffd; // below 0xfffffffe

    assert!(signals_rbf(&tx));
}

#[test]
fn test_signals_rbf_locktime_sequence_opts_in() {
    let mut tx = create_valid_transaction();
    tx.inputs[0].sequence = 0xfffffffe;

    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], // OP_1 for valid script
    }
}

fn utxo_confirmed_at(prevout: OutPoint, height: u64) -> UtxoSet {
    let mut utxo_set = UtxoSet::default();
    utxo_set.insert(
        prevout,
        std::sync::Arc::new(UTXO {
            value: 10000,
            script_pubkey: vec![OP_1].into(),
            height,
            is_coinbase: false,
        }),
    );
    utxo_set
}

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],
                index: 0xffffffff,
            },
            script_sig: vec![],
            sequence: 0xffffffff,
        }]
        .into(),
        outputs: vec![TransactionOutput {
            value: 5000000000,
            script_pubkey: vec![],
        }]
        .into(),
        lock_time: 0,
    }
}