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
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
//! Mining and block creation functions from Orange Paper Section 10.1

#[cfg(test)]
use crate::economic::get_block_subsidy;
use crate::economic::get_block_subsidy_for_network;
use crate::error::Result;
use crate::pow::{check_proof_of_work, next_required_bits};
use crate::transaction::check_transaction;
use crate::types::*;
#[cfg(feature = "spec-lock")]
use blvm_spec_lock::spec_locked;

#[cfg(test)]
use crate::transaction::is_coinbase;

/// Compact bits for a block template.
///
/// `prev_headers` is oldest-to-newest and ends at `prev_header`'s height.
/// Height 0 uses the network proof-of-work limit.
fn template_bits(
    network: Network,
    height: Natural,
    block_timestamp: u64,
    parent: &BlockHeader,
    prev_headers: &[BlockHeader],
) -> Result<Natural> {
    if height == 0 {
        return Ok(match network {
            Network::Regtest => 0x207fffff,
            Network::Signet => 0x1e0377ae,
            Network::Mainnet | Network::Testnet | Network::Testnet4 => {
                crate::constants::MAX_TARGET as u64
            }
        });
    }
    let parent_height = height - 1;
    let window_end = parent_height;
    let window_start = if prev_headers.is_empty() {
        window_end
    } else {
        window_end.saturating_sub(prev_headers.len() as u64 - 1)
    };
    next_required_bits(network, height, block_timestamp, &|h| {
        if h == parent_height {
            return Some((parent.bits, parent.timestamp));
        }
        if prev_headers.is_empty() || h < window_start || h > window_end {
            return None;
        }
        prev_headers
            .get((h - window_start) as usize)
            .map(|hdr| (hdr.bits, hdr.timestamp))
    })?
    .ok_or_else(|| {
        crate::error::ConsensusError::InvalidProofOfWork(
            "genesis template has no parent bits".into(),
        )
    })
}

/// Minimum header version `check_bip90` accepts at `height`.
///
/// Height 1 on mainnet is still version 1. After BIP65 the minimum is 4.
pub fn template_block_version(network: Network, height: Natural) -> i64 {
    use crate::activation::{ForkActivationTable, IsForkActive};
    let table = ForkActivationTable::from_network(network);
    if table.is_fork_active(ForkId::Bip65, height) {
        4
    } else if table.is_fork_active(ForkId::Bip66, height) {
        3
    } else if table.is_fork_active(ForkId::Bip34, height) {
        2
    } else {
        1
    }
}

/// CreateNewBlock: 𝒰𝒮 × 𝒯𝒳* → ℬ
///
/// For UTXO set us and mempool transactions txs:
/// 1. Create coinbase transaction with appropriate subsidy
/// 2. Select transactions from mempool based on fee rate
/// 3. Calculate merkle root
/// 4. Create block header with appropriate difficulty
/// 5. Return new block
pub fn create_new_block(
    utxo_set: &UtxoSet,
    mempool_txs: &[Transaction],
    height: Natural,
    prev_header: &BlockHeader,
    prev_headers: &[BlockHeader],
    coinbase_script: &ByteString,
    coinbase_address: &ByteString,
) -> Result<Block> {
    // For backward compatibility, derive block_time from system clock here.
    let block_time = get_current_timestamp();
    create_new_block_with_time(
        utxo_set,
        mempool_txs,
        height,
        prev_header,
        prev_headers,
        coinbase_script,
        coinbase_address,
        block_time,
        Network::Mainnet,
        None,
    )
}

/// CreateNewBlock variant that accepts an explicit block_time.
///
/// This allows callers (e.g., node layer) to provide a median time-past or
/// adjusted network time instead of relying on `SystemTime::now()` inside
/// consensus code.
#[allow(clippy::too_many_arguments)]
pub fn create_new_block_with_time(
    utxo_set: &UtxoSet,
    mempool_txs: &[Transaction],
    height: Natural,
    prev_header: &BlockHeader,
    prev_headers: &[BlockHeader],
    coinbase_script: &ByteString,
    coinbase_address: &ByteString,
    block_time: u64,
    network: Network,
    mempool_witnesses: Option<&[Option<Vec<crate::segwit::Witness>>]>,
) -> Result<Block> {
    use crate::bip113::get_median_time_past;
    use crate::mempool::{Mempool, MempoolResult, accept_to_memory_pool};

    let median_time_past = get_median_time_past(prev_headers);
    let block_time = block_time.max(median_time_past.saturating_add(1));

    // 1. Create coinbase transaction
    let coinbase_tx = create_coinbase_transaction(
        height,
        get_block_subsidy_for_network(height, network),
        coinbase_script,
        coinbase_address,
    )?;

    // Header (80 bytes plus a 3-byte transaction count) and the coinbase are in the
    // block before any mempool transaction. The witness commitment is appended later.
    const WITNESS_COMMITMENT_RESERVE: u64 = 4_000;
    let header_weight = 83u64.saturating_mul(4);
    let coinbase_weight =
        crate::segwit::transaction_weight_from_stacks(&coinbase_tx, None).unwrap_or(0);
    let mut running_weight = header_weight
        .saturating_add(coinbase_weight)
        .saturating_add(WITNESS_COMMITMENT_RESERVE);
    // Block sigop cost counts witness programs. P2SH is 0x01. Witness is 0x800.
    const BLOCK_SIGOP_FLAGS: u32 = 0x01 | 0x800;
    let mut running_sigops = crate::sigop::get_transaction_sigop_cost_with_witness_slices(
        &coinbase_tx,
        utxo_set,
        None,
        BLOCK_SIGOP_FLAGS,
    )
    .unwrap_or(0);
    let max_weight = crate::constants::MAX_BLOCK_WEIGHT as u64;
    let max_sigops = crate::constants::MAX_BLOCK_SIGOPS_COST;

    // 2. Select transactions from mempool with proper validation.
    // Outputs of a selected transaction are spendable by a later one in this template.
    // A child that sorts ahead of its parent is tried again after that parent is selected.
    let mut selected_txs = Vec::new();
    let mut temp_mempool = Mempool::new();
    let mut available = utxo_set.clone();
    let mut waiting: Vec<usize> = (0..mempool_txs.len()).collect();

    while !waiting.is_empty() {
        let mut still_waiting = Vec::new();
        let selected_before = selected_txs.len();
        for idx in waiting {
            let tx = &mempool_txs[idx];
            if check_transaction(tx)? != ValidationResult::Valid {
                continue;
            }

            let time_context = Some(TimeContext {
                network_time: block_time,
                median_time_past,
            });
            let tx_witnesses = mempool_witnesses
                .and_then(|all| all.get(idx))
                .and_then(|w| w.as_deref());
            match accept_to_memory_pool(
                tx,
                tx_witnesses,
                &available,
                &temp_mempool,
                height,
                time_context,
                network,
                None,
            )? {
                MempoolResult::Accepted => {
                    let tx_weight =
                        match crate::segwit::transaction_weight_from_stacks(tx, tx_witnesses) {
                            Ok(weight) => weight,
                            Err(_) => continue,
                        };
                    let tx_sigops =
                        match crate::sigop::get_transaction_sigop_cost_with_witness_slices(
                            tx,
                            &available,
                            tx_witnesses,
                            BLOCK_SIGOP_FLAGS,
                        ) {
                            Ok(cost) => cost,
                            Err(_) => continue,
                        };
                    if running_weight.saturating_add(tx_weight) > max_weight
                        || running_sigops.saturating_add(tx_sigops) > max_sigops
                    {
                        continue;
                    }
                    running_weight = running_weight.saturating_add(tx_weight);
                    running_sigops = running_sigops.saturating_add(tx_sigops);
                    temp_mempool.insert_transaction_with_witness(tx, tx_witnesses);
                    let txid = crate::block::calculate_tx_id(tx);
                    for (index, output) in tx.outputs.iter().enumerate() {
                        available.insert(
                            OutPoint {
                                hash: txid,
                                index: index as u32,
                            },
                            std::sync::Arc::new(UTXO {
                                value: output.value,
                                script_pubkey: output.script_pubkey.clone().into(),
                                height,
                                is_coinbase: false,
                            }),
                        );
                    }
                    selected_txs.push(tx.clone());
                }
                MempoolResult::Rejected(_reason) => {
                    // The parent may appear later in this fee order.
                    #[cfg(test)]
                    eprintln!("Transaction rejected: {_reason}");
                    still_waiting.push(idx);
                }
            }
        }
        if selected_txs.len() == selected_before {
            break;
        }
        waiting = still_waiting;
    }

    // 3. Build transaction list (coinbase first)
    let mut transactions = vec![coinbase_tx];
    transactions.extend(selected_txs);

    // 4. Calculate merkle root
    let merkle_root = calculate_merkle_root(&transactions)?;

    // 5. Required compact bits for this height. Off a period boundary this is the
    // parent's bits (or the minimum-difficulty exception). The retarget formula
    // runs only when this height starts a new difficulty period.
    let next_work = template_bits(network, height, block_time, prev_header, prev_headers)?;

    // 6. Create block header
    let header = BlockHeader {
        version: template_block_version(network, height),
        prev_block_hash: calculate_block_hash(prev_header),
        merkle_root,
        timestamp: block_time,
        bits: next_work,
        nonce: 0, // Will be set during mining
    };

    Ok(Block {
        header,
        transactions: transactions.into_boxed_slice(),
    })
}

/// MineBlock: ℬ × ℕ → ℬ × {success, failure}
///
/// Attempt to mine a block by finding a valid nonce:
/// 1. Try different nonce values
/// 2. Check if resulting hash meets difficulty target
/// 3. Return mined block or failure
#[track_caller] // Better error messages showing caller location
pub fn mine_block(mut block: Block, max_attempts: Natural) -> Result<(Block, MiningResult)> {
    for nonce in 0..max_attempts {
        block.header.nonce = nonce;

        if check_proof_of_work(&block.header).unwrap_or(false) {
            return Ok((block, MiningResult::Success));
        }
    }

    Ok((block, MiningResult::Failure))
}

/// BlockTemplate: Interface for mining software
///
/// Provides a template for mining software to work with:
/// 1. Block header with current difficulty
/// 2. Coinbase transaction template
/// 3. Selected transactions
/// 4. Mining parameters
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct BlockTemplate {
    pub header: BlockHeader,
    pub coinbase_tx: Transaction,
    pub transactions: Vec<Transaction>,
    pub target: crate::pow::U256,
    pub height: Natural,
    pub timestamp: Natural,
}

/// Create a block template for mining
pub fn create_block_template(
    utxo_set: &UtxoSet,
    mempool_txs: &[Transaction],
    height: Natural,
    prev_header: &BlockHeader,
    prev_headers: &[BlockHeader],
    coinbase_script: &ByteString,
    coinbase_address: &ByteString,
    network: Network,
    mempool_witnesses: Option<&[Option<Vec<crate::segwit::Witness>>]>,
) -> Result<BlockTemplate> {
    let block_time = get_current_timestamp();
    let block = create_new_block_with_time(
        utxo_set,
        mempool_txs,
        height,
        prev_header,
        prev_headers,
        coinbase_script,
        coinbase_address,
        block_time,
        network,
        mempool_witnesses,
    )?;

    let target = crate::pow::expand_target(block.header.bits)?;

    let header = block.header.clone();

    // Use proven bounds for coinbase access (block has been validated)
    #[cfg(feature = "production")]
    let coinbase_tx = {
        use crate::optimizations::_optimized_access::get_proven_by_;
        get_proven_by_(&block.transactions, 0)
            .ok_or_else(|| {
                crate::error::ConsensusError::BlockValidation("Block has no transactions".into())
            })?
            .clone()
    };

    #[cfg(not(feature = "production"))]
    let coinbase_tx = block.transactions[0].clone();

    Ok(BlockTemplate {
        header: block.header.clone(),
        coinbase_tx,
        transactions: block.transactions[1..].to_vec(),
        target,
        height,
        timestamp: header.timestamp,
    })
}

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

/// Result of mining attempt
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MiningResult {
    Success,
    Failure,
}

/// Create coinbase transaction
/// Orange Paper 12.2: Coinbase transaction structure.
/// BIP54: When BIP54 is active, coinbase must have nLockTime = height - 1 and nSequence != 0xffff_ffff.
/// This implementation sets those so that blocks are valid under BIP54 when activated.
fn create_coinbase_transaction(
    height: Natural,
    subsidy: Integer,
    script: &ByteString,
    address: &ByteString,
) -> Result<Transaction> {
    create_coinbase_with_outputs(height, script, &[(subsidy, address.clone())])
}

/// Multi-output coinbase. Callers set values (subsidy + fees). Empty list is an error.
/// Witness commitment is appended by `create_block_template_with_outputs` / Stratum.
pub fn create_coinbase_with_outputs(
    height: Natural,
    script: &ByteString,
    outputs: &[(Integer, ByteString)],
) -> Result<Transaction> {
    if outputs.is_empty() {
        return Err(crate::error::ConsensusError::InvalidProofOfWork(
            "coinbase requires at least one output".into(),
        ));
    }
    let lock_time = height.saturating_sub(1);
    let coinbase_input = TransactionInput {
        prevout: OutPoint {
            hash: [0u8; 32],
            index: 0xffffffff,
        },
        script_sig: script.clone(),
        sequence: 0xfffffffe, // BIP54: not 0xffffffff so coinbase is unique
    };
    let outs: Vec<TransactionOutput> = outputs
        .iter()
        .map(|(value, script_pubkey)| TransactionOutput {
            value: *value,
            script_pubkey: script_pubkey.clone(),
        })
        .collect();

    Ok(Transaction {
        version: 1,
        inputs: crate::tx_inputs![coinbase_input],
        outputs: outs.into(),
        lock_time,
    })
}

/// Same as `create_block_template`, then replace the coinbase and merkle root.
#[allow(clippy::too_many_arguments)]
pub fn create_block_template_with_outputs(
    utxo_set: &UtxoSet,
    mempool_txs: &[Transaction],
    height: Natural,
    prev_header: &BlockHeader,
    prev_headers: &[BlockHeader],
    coinbase_script: &ByteString,
    coinbase_outputs: &[(Integer, ByteString)],
    network: Network,
    mempool_witnesses: Option<&[Option<Vec<crate::segwit::Witness>>]>,
) -> Result<BlockTemplate> {
    let address = coinbase_outputs
        .first()
        .map(|(_, s)| s.clone())
        .unwrap_or_default();
    let mut tmpl = create_block_template(
        utxo_set,
        mempool_txs,
        height,
        prev_header,
        prev_headers,
        coinbase_script,
        &address,
        network,
        mempool_witnesses,
    )?;
    let subsidy = get_block_subsidy_for_network(height, network);
    let fees = sum_selected_fees(utxo_set, &tmpl.transactions);
    let fitted = fit_payouts_to_reward(coinbase_outputs, subsidy, fees)?;
    tmpl.coinbase_tx = create_coinbase_with_outputs(height, coinbase_script, &fitted)?;
    let mut txs = Vec::with_capacity(1 + tmpl.transactions.len());
    txs.push(tmpl.coinbase_tx.clone());
    txs.extend(tmpl.transactions.iter().cloned());
    let nested = nested_witnesses_for_selected(&txs, mempool_txs, mempool_witnesses)?;
    append_witness_commitment_from_nested(&mut tmpl.coinbase_tx, &mut txs, Some(&nested))?;
    tmpl.header.merkle_root = calculate_merkle_root(&txs)?;
    Ok(tmpl)
}

/// Resolve named txids from a mempool list, in declaration order.
/// Unknown or duplicate txids are errors. An empty list is allowed (coinbase-only).
pub fn resolve_declared_txs(
    mempool_txs: &[Transaction],
    txids: &[Hash],
) -> Result<Vec<Transaction>> {
    use crate::block::calculate_tx_id;
    use std::collections::{HashMap, HashSet};

    let mut by_id: HashMap<Hash, &Transaction> = HashMap::new();
    for tx in mempool_txs {
        by_id.entry(calculate_tx_id(tx)).or_insert(tx);
    }
    let mut seen = HashSet::new();
    let mut out = Vec::with_capacity(txids.len());
    for id in txids {
        if !seen.insert(*id) {
            return Err(crate::error::ConsensusError::BlockValidation(
                format!("duplicate declared txid {}", hex::encode(id)).into(),
            ));
        }
        let tx = by_id.get(id).ok_or_else(|| {
            crate::error::ConsensusError::BlockValidation(
                format!("unknown declared txid {}", hex::encode(id)).into(),
            )
        })?;
        out.push((*tx).clone());
    }
    Ok(out)
}

fn declared_tx_spends_witness_utxo(tx: &Transaction, utxo_set: &UtxoSet) -> bool {
    tx.inputs.iter().any(|input| {
        utxo_set.get(&input.prevout).is_some_and(|utxo| {
            crate::witness::is_standard_witness_program(utxo.script_pubkey.as_ref())
        })
    })
}

/// Align mempool witness slots to the declared subset by txid.
/// A witness spend without stored stacks is an error (no empty-stack BIP141).
fn align_declared_witnesses(
    declared: &[Transaction],
    mempool_txs: &[Transaction],
    mempool_witnesses: Option<&[Option<Vec<crate::segwit::Witness>>]>,
    utxo_set: &UtxoSet,
) -> Result<Vec<Option<Vec<crate::segwit::Witness>>>> {
    use crate::block::calculate_tx_id;
    use std::collections::HashMap;

    let mut by_txid = HashMap::new();
    if let Some(wits) = mempool_witnesses {
        for (tx, w) in mempool_txs.iter().zip(wits.iter()) {
            by_txid.insert(calculate_tx_id(tx), w.clone());
        }
    }
    let mut out = Vec::with_capacity(declared.len());
    for tx in declared {
        let id = calculate_tx_id(tx);
        let slot = by_txid.get(&id).cloned().flatten();
        if let Some(ref stacks) = slot {
            if stacks.len() != tx.inputs.len() {
                return Err(crate::error::ConsensusError::BlockValidation(
                    format!(
                        "witness count {} != input count {} for declared tx {}",
                        stacks.len(),
                        tx.inputs.len(),
                        hex::encode(id)
                    )
                    .into(),
                ));
            }
        } else if declared_tx_spends_witness_utxo(tx, utxo_set) {
            return Err(crate::error::ConsensusError::BlockValidation(
                format!(
                    "missing mempool witnesses for declared witness spend {}",
                    hex::encode(id)
                )
                .into(),
            ));
        }
        // A legacy spend has no witness. Store empty stacks so the commitment
        // lookup finds this txid. A None slot stays an error for a witness spend.
        let slot = slot.or_else(|| Some(vec![Vec::new(); tx.inputs.len()]));
        out.push(slot);
    }
    Ok(out)
}

/// Stage 3b slice 1: miner-declared mempool subset.
/// Resolves `declared_txids`, builds the Stage 3a template, and errors if the
/// selected non-coinbase txids are not exactly that list (no silent skip).
/// Empty `declared_txids` is coinbase-only. Spec-locked builders are unchanged.
/// Overweight uses Bitcoin `MAX_BLOCK_WEIGHT` (not a pool-invented cap).
#[allow(clippy::too_many_arguments)]
pub fn create_block_template_declared(
    utxo_set: &UtxoSet,
    mempool_txs: &[Transaction],
    declared_txids: &[Hash],
    height: Natural,
    prev_header: &BlockHeader,
    prev_headers: &[BlockHeader],
    coinbase_script: &ByteString,
    coinbase_outputs: &[(Integer, ByteString)],
    network: Network,
    mempool_witnesses: Option<&[Option<Vec<crate::segwit::Witness>>]>,
) -> Result<BlockTemplate> {
    use crate::block::calculate_tx_id;

    let declared = resolve_declared_txs(mempool_txs, declared_txids)?;
    let aligned = align_declared_witnesses(&declared, mempool_txs, mempool_witnesses, utxo_set)?;
    let tmpl = create_block_template_with_outputs(
        utxo_set,
        &declared,
        height,
        prev_header,
        prev_headers,
        coinbase_script,
        coinbase_outputs,
        network,
        Some(aligned.as_slice()),
    )?;
    let selected: Vec<Hash> = tmpl.transactions.iter().map(calculate_tx_id).collect();
    if selected.as_slice() != declared_txids {
        return Err(crate::error::ConsensusError::BlockValidation(
            "declared transaction was not selected (rejected or skipped)".into(),
        ));
    }
    check_template_weight(&tmpl, crate::constants::MAX_BLOCK_WEIGHT as u64)?;
    Ok(tmpl)
}

/// Weight of coinbase + selected txs (empty stacks if none stored).
/// Uses Bitcoin `MAX_BLOCK_WEIGHT` (4_000_000). Not an invented pool cap.
pub fn check_template_weight(tmpl: &BlockTemplate, max_weight: u64) -> Result<()> {
    let mut txs = Vec::with_capacity(1 + tmpl.transactions.len());
    txs.push(tmpl.coinbase_tx.clone());
    txs.extend(tmpl.transactions.iter().cloned());
    let block = Block {
        header: tmpl.header.clone(),
        transactions: txs.clone().into_boxed_slice(),
    };
    let nested: Vec<Vec<crate::segwit::Witness>> = txs
        .iter()
        .map(|tx| vec![Vec::new(); tx.inputs.len()])
        .collect();
    let weight = crate::segwit::calculate_block_weight_from_nested(&block, &nested)?;
    if weight > max_weight {
        return Err(crate::error::ConsensusError::BlockValidation(
            format!("declared template weight {weight} exceeds max {max_weight}").into(),
        ));
    }
    Ok(())
}

/// Coinbase empty stacks, then mempool witnesses aligned to selected txs by txid.
fn nested_witnesses_for_selected(
    txs: &[Transaction],
    mempool_txs: &[Transaction],
    mempool_witnesses: Option<&[Option<Vec<crate::segwit::Witness>>]>,
) -> Result<Vec<Vec<crate::segwit::Witness>>> {
    use crate::block::calculate_tx_id;
    use std::collections::HashMap;

    let mut by_txid = HashMap::new();
    if let Some(wits) = mempool_witnesses {
        for (tx, w) in mempool_txs.iter().zip(wits.iter()) {
            if let Some(stacks) = w {
                by_txid.insert(calculate_tx_id(tx), stacks.clone());
            }
        }
    }
    let mut nested = Vec::with_capacity(txs.len());
    for (i, tx) in txs.iter().enumerate() {
        if i == 0 || mempool_witnesses.is_none() {
            nested.push(vec![Vec::new(); tx.inputs.len()]);
            continue;
        }
        let stacks = by_txid.get(&calculate_tx_id(tx)).cloned().ok_or_else(|| {
            crate::error::ConsensusError::BlockValidation(
                "missing witness for selected transaction".into(),
            )
        })?;
        nested.push(stacks);
    }
    Ok(nested)
}

/// Fees of selected (non-coinbase) txs.
/// An input missing from the chain set is priced from an earlier selected transaction.
/// Any other missing input contributes nothing.
pub fn sum_selected_fees(utxo_set: &UtxoSet, txs: &[Transaction]) -> Integer {
    use crate::block::calculate_tx_id;

    let mut created: std::collections::HashMap<Hash, usize> = std::collections::HashMap::new();
    let mut total = 0i64;
    for (i, tx) in txs.iter().enumerate() {
        if !crate::transaction::is_coinbase(tx) {
            total = total.saturating_add(selected_tx_fee(tx, utxo_set, txs, &created));
        }
        created.insert(calculate_tx_id(tx), i);
    }
    total
}

fn selected_tx_fee(
    tx: &Transaction,
    utxo_set: &UtxoSet,
    txs: &[Transaction],
    created: &std::collections::HashMap<Hash, usize>,
) -> Integer {
    let mut total_input = 0i64;
    for input in &tx.inputs {
        let value = if let Some(utxo) = utxo_set.get(&input.prevout) {
            utxo.value
        } else if let Some(&parent_i) = created.get(&input.prevout.hash) {
            let Some(output) = txs[parent_i].outputs.get(input.prevout.index as usize) else {
                return 0;
            };
            output.value
        } else {
            return 0;
        };
        total_input = match total_input.checked_add(value) {
            Some(sum) => sum,
            None => return 0,
        };
    }
    let total_output = tx
        .outputs
        .iter()
        .try_fold(0i64, |acc, output| acc.checked_add(output.value))
        .unwrap_or(0);
    total_input.saturating_sub(total_output).max(0)
}

/// If payouts exceed subsidy+fees, error. Shortfall is added to the first output.
pub fn fit_payouts_to_reward(
    payouts: &[(Integer, ByteString)],
    subsidy: Integer,
    fees: Integer,
) -> Result<Vec<(Integer, ByteString)>> {
    if payouts.is_empty() {
        return Err(crate::error::ConsensusError::InvalidProofOfWork(
            "coinbase requires at least one output".into(),
        ));
    }
    let max = subsidy.saturating_add(fees);
    let sum: Integer = payouts
        .iter()
        .map(|(v, _)| *v)
        .fold(0, |a, b| a.saturating_add(b));
    if sum > max {
        return Err(crate::error::ConsensusError::EconomicValidation(
            format!("payouts {sum} exceed subsidy+fees {max}").into(),
        ));
    }
    let mut out = payouts.to_vec();
    if sum < max {
        out[0].0 = out[0].0.saturating_add(max - sum);
    }
    Ok(out)
}

/// BIP141: coinbase wtxid is 0, so appending the OP_RETURN does not change the witness root.
/// Empty stacks when `nested` is `None` (legacy). Prefer
/// [`append_witness_commitment_from_nested`] when mempool witnesses are known.
pub fn append_witness_commitment(
    coinbase: &mut Transaction,
    txs: &mut [Transaction],
) -> Result<()> {
    append_witness_commitment_from_nested(coinbase, txs, None)
}

/// Same as [`append_witness_commitment`], using per-tx per-input stacks.
/// `nested[i]` is the witness for `txs[i]`. Coinbase wtxid stays 0.
pub fn append_witness_commitment_from_nested(
    coinbase: &mut Transaction,
    txs: &mut [Transaction],
    nested: Option<&[Vec<crate::segwit::Witness>]>,
) -> Result<()> {
    if txs.is_empty() {
        return Ok(());
    }
    let owned: Vec<Vec<crate::segwit::Witness>>;
    let nested = if let Some(n) = nested {
        if n.len() < txs.len() {
            return Err(crate::error::ConsensusError::BlockValidation(
                "missing witness for selected transaction".into(),
            ));
        }
        n
    } else {
        owned = txs
            .iter()
            .map(|tx| vec![Vec::new(); tx.inputs.len()])
            .collect();
        &owned
    };
    let header = BlockHeader {
        version: 1,
        prev_block_hash: [0u8; 32],
        merkle_root: [0u8; 32],
        timestamp: 0,
        bits: 0,
        nonce: 0,
    };
    let block = Block {
        header,
        transactions: txs.to_vec().into_boxed_slice(),
    };
    let root = crate::segwit::compute_witness_merkle_root_from_nested(&block, nested, None)?;
    let script = crate::segwit::witness_commitment_script(&root, &[0u8; 32]);
    let mut outs = coinbase.outputs.to_vec();
    outs.push(TransactionOutput {
        value: 0,
        script_pubkey: script,
    });
    coinbase.outputs = outs.into();
    txs[0] = coinbase.clone();
    Ok(())
}

/// Calculate merkle root using proper Bitcoin Merkle tree construction
#[track_caller] // Better error messages showing caller location
#[cfg_attr(feature = "production", inline(always))]
#[cfg_attr(not(feature = "production"), inline)]
#[cfg_attr(feature = "spec-lock", spec_locked("8.4.1", "ComputeMerkleRoot"))]
pub fn calculate_merkle_root(transactions: &[Transaction]) -> Result<Hash> {
    if transactions.is_empty() {
        return Err(crate::error::ConsensusError::InvalidProofOfWork(
            "Cannot calculate merkle root for empty transaction list".into(),
        ));
    }

    // Calculate transaction hashes with batch optimization (if available)
    // Uses SIMD vectorization + proven bounds for optimal performance
    // Use cache-aligned structures throughout merkle tree building
    #[cfg(feature = "production")]
    let mut hashes: Vec<crate::optimizations::CacheAlignedHash> = {
        use crate::optimizations::simd_vectorization;

        // Serialize all transactions in parallel (if rayon available)
        // Then batch hash all serialized forms using double SHA256
        // Pre-allocate serialization buffers
        let serialized_txs: Vec<Vec<u8>> = {
            #[cfg(feature = "rayon")]
            {
                use rayon::prelude::*;
                transactions
                    .par_iter()
                    .map(serialize_tx_for_hash) // Uses prealloc_tx_buffer internally
                    .collect()
            }
            #[cfg(not(feature = "rayon"))]
            {
                transactions
                    .iter()
                    .map(serialize_tx_for_hash) // Uses prealloc_tx_buffer internally
                    .collect()
            }
        };

        // Batch hash all serialized transactions using double SHA256
        // Keep cache-aligned structures for better cache locality
        let tx_data_refs: Vec<&[u8]> = serialized_txs.iter().map(|v| v.as_slice()).collect();
        simd_vectorization::batch_double_sha256_aligned(&tx_data_refs)
    };

    #[cfg(not(feature = "production"))]
    let mut hashes: Vec<Hash> = {
        // Sequential fallback for non-production builds
        let mut hashes = Vec::with_capacity(transactions.len());
        for tx in transactions {
            hashes.push(calculate_tx_hash(tx));
        }
        hashes
    };

    // Build Merkle tree bottom-up
    // Pre-allocate next level and combined buffers
    // Optimization: Process multiple tree levels in parallel where safe
    // Use cache-aligned structures in production mode for better cache locality
    #[cfg(feature = "production")]
    {
        use crate::optimizations::CacheAlignedHash;

        let mut mutated = false;

        while hashes.len() > 1 {
            // CVE-2012-2459: Detect mutations (duplicate hashes at same level)
            let mut level_mutated = false;
            let mut pos = 0;
            while pos + 1 < hashes.len() {
                if hashes[pos].as_bytes() == hashes[pos + 1].as_bytes() {
                    level_mutated = true;
                }
                pos += 2;
            }
            if level_mutated {
                mutated = true;
            }

            // Duplicate last hash if odd number of hashes (Bitcoin's special rule)
            if hashes.len() & 1 != 0 {
                let last = hashes[hashes.len() - 1].clone();
                hashes.push(last);
            }

            // Stack-allocated 64-byte buffer, double SHA256 at each level
            let next_level: Vec<CacheAlignedHash> = hashes
                .chunks(2)
                .map(|chunk| {
                    let mut combined = [0u8; 64];
                    combined[..32].copy_from_slice(chunk[0].as_bytes());
                    combined[32..].copy_from_slice(if chunk.len() == 2 {
                        chunk[1].as_bytes()
                    } else {
                        chunk[0].as_bytes()
                    });
                    CacheAlignedHash::new(double_sha256_hash(&combined))
                })
                .collect();

            hashes = next_level;
        }

        if mutated {
            return Err(crate::error::ConsensusError::InvalidProofOfWork(
                "Merkle root mutation detected (CVE-2012-2459)".into(),
            ));
        }

        Ok(*hashes[0].as_bytes())
    }

    #[cfg(not(feature = "production"))]
    {
        let (root, mutated) = merkle_tree_from_hashes(&mut hashes)?;
        if mutated {
            return Err(crate::error::ConsensusError::InvalidProofOfWork(
                "Merkle root mutation detected (CVE-2012-2459)".into(),
            ));
        }
        Ok(root)
    }
}

/// Build merkle tree from pre-computed leaf hashes.
///
/// Also usable directly when tx_ids are already computed, avoiding
/// redundant serialization and hashing.
/// Implements ComputeMerkleRoot (Orange Paper 8.4.1).
///
/// Returns `Err` if `tx_ids` is empty **or** if a CVE-2012-2459 mutation is detected
/// (duplicate adjacent hashes at any merkle level). Use [`compute_merkle_root_and_mutated`]
/// when you need the root regardless of mutation (Orange Paper §8.4.1 merkle root).
#[cfg_attr(feature = "spec-lock", spec_locked("8.4.1", "ComputeMerkleRoot"))]
pub fn calculate_merkle_root_from_tx_ids(tx_ids: &[Hash]) -> Result<Hash> {
    let (root, mutated) = compute_merkle_root_and_mutated(tx_ids)?;
    if mutated {
        return Err(crate::error::ConsensusError::InvalidProofOfWork(
            "Merkle root mutation detected (CVE-2012-2459)".into(),
        ));
    }
    Ok(root)
}

/// Compute merkle root **and** CVE-2012-2459 mutation flag without aborting on mutation.
/// Computes merkle root from leaves with optional mutation detection — always returns a root.
/// Callers decide policy: `CheckBlock` rejects mutated blocks; `ConnectBlock` may skip
/// the check for already-accepted blocks.
#[cfg_attr(feature = "spec-lock", spec_locked("8.4.1", "ComputeMerkleRoot"))]
pub fn compute_merkle_root_and_mutated(tx_ids: &[Hash]) -> Result<(Hash, bool)> {
    if tx_ids.is_empty() {
        return Err(crate::error::ConsensusError::InvalidProofOfWork(
            "Cannot calculate merkle root for empty transaction list".into(),
        ));
    }
    let mut hashes = tx_ids.to_vec();
    merkle_tree_from_hashes(&mut hashes)
}

/// Merkle tree building logic. Uses double SHA256 at each level (Bitcoin standard).
/// Stack-allocates the 64-byte pair buffer to avoid heap allocation per node.
/// Orange Paper 8.4.1: ComputeMerkleRoot pair-and-hash construction.
/// Returns `(root, mutated)` — caller decides policy on mutation.
#[cfg_attr(feature = "spec-lock", spec_locked("8.4.1", "ComputeMerkleRoot"))]
fn merkle_tree_from_hashes(hashes: &mut Vec<Hash>) -> Result<(Hash, bool)> {
    let mut mutated = false;

    while hashes.len() > 1 {
        // CVE-2012-2459: Detect mutations (duplicate hashes at same level)
        for pos in (0..hashes.len().saturating_sub(1)).step_by(2) {
            if hashes[pos] == hashes[pos + 1] {
                mutated = true;
            }
        }

        // Duplicate last hash if odd number of hashes (Bitcoin's special rule)
        if hashes.len() & 1 != 0 {
            hashes.push(hashes[hashes.len() - 1]);
        }

        let mut next_level = Vec::with_capacity(hashes.len() / 2);

        // Stack-allocated 64-byte buffer for combining hash pairs
        for chunk in hashes.chunks(2) {
            let mut combined = [0u8; 64];
            combined[..32].copy_from_slice(&chunk[0]);
            combined[32..].copy_from_slice(if chunk.len() == 2 {
                &chunk[1]
            } else {
                &chunk[0]
            });
            next_level.push(double_sha256_hash(&combined));
        }

        *hashes = next_level;
    }

    Ok((hashes[0], mutated))
}

/// Serialize transaction for hashing (used for batch hashing optimization)
///
/// This is the same serialization as calculate_tx_hash but returns the serialized bytes
/// instead of hashing them, allowing batch hashing to be applied.
fn serialize_tx_for_hash(tx: &Transaction) -> Vec<u8> {
    // Pre-allocate buffer using proven maximum size
    #[cfg(feature = "production")]
    let mut data = {
        use crate::optimizations::prealloc_tx_buffer;
        prealloc_tx_buffer()
    };

    #[cfg(not(feature = "production"))]
    let mut data = Vec::new();

    // Version (4 bytes, little-endian)
    data.extend_from_slice(&(tx.version as u32).to_le_bytes());

    // Input count (varint)
    data.extend_from_slice(&encode_varint(tx.inputs.len() as u64));

    // Inputs
    for input in &tx.inputs {
        // Previous output hash (32 bytes)
        data.extend_from_slice(&input.prevout.hash);
        // Previous output index (4 bytes, little-endian)
        data.extend_from_slice(&input.prevout.index.to_le_bytes());
        // Script length (varint)
        data.extend_from_slice(&encode_varint(input.script_sig.len() as u64));
        // Script
        data.extend_from_slice(&input.script_sig);
        // Sequence (4 bytes, little-endian)
        data.extend_from_slice(&(input.sequence as u32).to_le_bytes());
    }

    // Output count (varint)
    data.extend_from_slice(&encode_varint(tx.outputs.len() as u64));

    // Outputs
    for output in &tx.outputs {
        // Value (8 bytes, little-endian)
        data.extend_from_slice(&(output.value as u64).to_le_bytes());
        // Script length (varint)
        data.extend_from_slice(&encode_varint(output.script_pubkey.len() as u64));
        // Script
        data.extend_from_slice(&output.script_pubkey);
    }

    // Lock time (4 bytes, little-endian)
    data.extend_from_slice(&(tx.lock_time as u32).to_le_bytes());

    data
}

/// Calculate transaction hash using proper Bitcoin serialization
///
/// This function computes the double SHA256 hash of the serialized transaction.
/// For batch operations, use serialize_tx_for_hash + batch_double_sha256 instead.
#[allow(dead_code)] // Used in tests
fn calculate_tx_hash(tx: &Transaction) -> Hash {
    let data = serialize_tx_for_hash(tx);
    // Double SHA256 (Bitcoin standard)
    let hash1 = sha256_hash(&data);
    sha256_hash(&hash1)
}

/// Encode a number as a Bitcoin varint
fn encode_varint(value: u64) -> Vec<u8> {
    if value < 0xfd {
        vec![value as u8]
    } else if value <= 0xffff {
        let mut result = vec![0xfd];
        result.extend_from_slice(&(value as u16).to_le_bytes());
        result
    } else if value <= 0xffffffff {
        let mut result = vec![0xfe];
        result.extend_from_slice(&(value as u32).to_le_bytes());
        result
    } else {
        let mut result = vec![0xff];
        result.extend_from_slice(&value.to_le_bytes());
        result
    }
}

/// Orange Paper 7.2: Block hash = SHA256d(serialize(header)) — same as blockstore / PoW.
fn calculate_block_hash(header: &BlockHeader) -> Hash {
    let wire = crate::serialization::block::serialize_block_header(header);
    double_sha256_hash(&wire)
}

/// Simple SHA256 hash function
///
/// Performance optimization: Uses OptimizedSha256 (SHA-NI or AVX2) instead of sha2 crate
/// for faster hashing in Merkle tree construction.
#[inline(always)]
fn sha256_hash(data: &[u8]) -> Hash {
    use crate::crypto::OptimizedSha256;
    OptimizedSha256::new().hash(data)
}

/// Double SHA256 hash (Bitcoin standard for merkle tree nodes and txids)
#[inline(always)]
fn double_sha256_hash(data: &[u8]) -> Hash {
    use crate::crypto::OptimizedSha256;
    OptimizedSha256::new().hash256(data)
}

/// Wall-clock Unix timestamp for block template creation.
fn get_current_timestamp() -> Natural {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap_or(std::time::Duration::ZERO)
        .as_secs()
}

#[cfg(test)]
#[path = "mining_tests.rs"]
mod tests;