blvm-node 0.1.88

Bitcoin Commons BLVM: Minimal Bitcoin node implementation using blvm-protocol and blvm-consensus
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
//! Compact Block Relay (BIP152) Implementation
//!
//! Reduces bandwidth during block propagation by sending only block headers
//! and short transaction IDs, allowing peers to reconstruct blocks using
//! transactions from their mempool.
//!
//! Specification: https://github.com/bitcoin/bips/blob/master/bip-0152.mediawiki
//!
//! ## Iroh Integration
//!
//! Compact blocks work seamlessly over Iroh QUIC transport. When using Iroh,
//! compact blocks provide additional benefits:
//! - Lower latency due to QUIC's multiplexing and stream prioritization
//! - Better NAT traversal support (Iroh's magic endpoint)
//! - Encryption by default (QUIC/TLS)
//!
//! Both features are optional and work independently:
//! - Compact blocks can be used with TCP, Quinn, or Iroh
//! - Iroh can be used with or without compact blocks
//! - The combination provides optimal bandwidth and latency for mobile nodes and NAT-traversed connections

use crate::network::transport::TransportType;
use anyhow::Result;
use blvm_protocol::segwit::Witness;
use blvm_protocol::{Block, BlockHeader, Hash, Transaction};
use sha2::{Digest, Sha256};
use std::collections::{HashMap, HashSet};
use std::hash::Hasher;
use std::time::{Duration, Instant};

pub use blvm_protocol::bip152::{CompactBlock, ShortTxId};

/// A compact block with more transactions than this is fetched whole.
pub const MAX_COMPACT_BLOCK_TXS: usize = 100_000;
/// More holes than this, and the block is fetched whole.
pub const MAX_COMPACT_MISSING: usize = 128;
/// Assemblies retained while `getblocktxn` is in flight.
pub const MAX_PENDING_COMPACT: usize = 4;
/// Bytes retained across those assemblies.
pub const MAX_PENDING_COMPACT_BYTES: usize = 8 * 1024 * 1024;
/// An assembly older than this is dropped and the block is fetched whole.
pub const COMPACT_ASSEMBLY_DEADLINE: Duration = Duration::from_secs(60);

/// Calculate Bitcoin transaction hash (double SHA256 of serialized transaction)
///
/// Properly serializes a transaction according to Bitcoin protocol and computes
/// the transaction ID (txid) using double SHA256.
///
/// # Arguments
/// * `tx` - Transaction to hash
///
/// # Returns
/// Transaction hash (32 bytes)
pub fn calculate_tx_hash(tx: &Transaction) -> Hash {
    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());

    // Double SHA256
    let hash1 = Sha256::digest(&data);
    let hash2 = Sha256::digest(hash1);

    let mut result = [0u8; 32];
    result.copy_from_slice(&hash2);
    result
}

/// 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
    }
}

/// SipHash-2-4 keys for a compact-block short id.
///
/// The keys are the first two little-endian 64-bit integers of
/// SHA256(80-byte header || compact-block nonce).
fn short_id_keys(header: &BlockHeader, nonce: u64) -> (u64, u64) {
    let mut data = blvm_protocol::serialization::serialize_block_header(header);
    data.extend_from_slice(&nonce.to_le_bytes());
    let hash = Sha256::digest(&data);
    let k0 = u64::from_le_bytes(hash[0..8].try_into().expect("sha256 prefix"));
    let k1 = u64::from_le_bytes(hash[8..16].try_into().expect("sha256 prefix"));
    (k0, k1)
}

/// Calculate a compact-block short transaction id.
///
/// `wtxid` is the witness transaction id. A transaction with no witness uses
/// the transaction id. The SipHash keys come from `header` and `nonce`.
pub fn calculate_short_tx_id(header: &BlockHeader, wtxid: &Hash, nonce: u64) -> ShortTxId {
    let (k0, k1) = short_id_keys(header, nonce);
    use siphasher::sip::SipHasher24;
    let mut hasher = SipHasher24::new_with_keys(k0, k1);
    hasher.write(wtxid);
    let hash_result = hasher.finish();
    let mut short_id = [0u8; 6];
    short_id.copy_from_slice(&hash_result.to_le_bytes()[..6]);
    short_id
}

/// Reconstruct full block from compact block
///
/// Attempts to match short IDs with transactions from mempool,
/// then requests missing transactions from peer.
///
/// **BIP125 + BIP152 Integration**: When matching transactions from mempool,
/// this function uses RBF conflict detection to ensure that matched transactions
/// don't conflict with each other or with already-matched transactions.
///
/// # Arguments
/// * `compact_block` - The compact block to reconstruct
/// * `mempool_txs` - Map of transaction hash to transaction from mempool
///
/// # Returns
/// Vector of indices for missing transactions (to request via getblocktxn)
pub fn reconstruct_block(
    compact_block: &CompactBlock,
    mempool_txs: &HashMap<Hash, Transaction>,
) -> Result<Vec<usize>> {
    let mut missing_indices = Vec::new();
    let mut reconstructed_txs = Vec::new();
    let prefilled: HashSet<usize> = compact_block
        .prefilled_txs
        .iter()
        .map(|(index, _, _)| *index)
        .collect();
    let mut next_block_index = 0usize;
    let mut by_short: HashMap<ShortTxId, Vec<&Transaction>> = HashMap::new();
    for (tx_hash, tx) in mempool_txs {
        let short_id = calculate_short_tx_id(&compact_block.header, tx_hash, compact_block.nonce);
        by_short.entry(short_id).or_default().push(tx);
    }

    // Short ids are only the transactions that were not prefilled, in block order.
    for &short_id in &compact_block.short_ids {
        while prefilled.contains(&next_block_index) {
            next_block_index = next_block_index.saturating_add(1);
        }
        let block_index = next_block_index;
        next_block_index = next_block_index.saturating_add(1);

        // More than one pool transaction with this short id is ambiguous.
        // The block transaction has to be requested rather than guessed.
        let matches = by_short.get(&short_id).map(Vec::as_slice).unwrap_or(&[]);

        if let [tx] = matches {
            let tx = *tx;
            let conflicts_match = reconstructed_txs
                .iter()
                .any(|(_, existing_tx)| has_conflict_with_tx(tx, existing_tx));
            let conflicts_prefilled = compact_block
                .prefilled_txs
                .iter()
                .any(|(_, existing_tx, _)| has_conflict_with_tx(tx, existing_tx));
            if conflicts_match || conflicts_prefilled {
                missing_indices.push(block_index);
            } else {
                reconstructed_txs.push((block_index, (*tx).clone()));
            }
        } else {
            missing_indices.push(block_index);
        }
    }

    Ok(missing_indices)
}

/// One slot of a compact block while `getblocktxn` / `blocktxn` are in flight.
#[derive(Debug, Clone)]
pub struct CompactAssembly {
    pub header: BlockHeader,
    pub nonce: u64,
    /// `None` until a short-id match or a `blocktxn` fills the position.
    pub slots: Vec<Option<(Transaction, Option<Vec<Witness>>)>>,
    pub missing: Vec<usize>,
    /// When this assembly was created. Older than [`COMPACT_ASSEMBLY_DEADLINE`] is dropped.
    pub started: Instant,
    /// `6 * short_ids.len()`, counted in [`assembly_retained_bytes`].
    pub short_id_bytes: usize,
}

impl CompactAssembly {
    pub fn expired_at(&self, now: Instant) -> bool {
        now.saturating_duration_since(self.started) >= COMPACT_ASSEMBLY_DEADLINE
    }
}

/// Serialized transaction bytes in `slots`, plus one slot's size times capacity,
/// plus the short-id bytes recorded on the assembly.
pub fn assembly_retained_bytes(assembly: &CompactAssembly) -> usize {
    let tx_bytes = assembly
        .slots
        .iter()
        .flatten()
        .map(|(tx, witness)| serialized_slot_bytes(tx, witness.as_deref()))
        .sum::<usize>();
    let slot_bytes = assembly
        .slots
        .capacity()
        .saturating_mul(std::mem::size_of::<
            Option<(Transaction, Option<Vec<Witness>>)>,
        >());
    tx_bytes
        .saturating_add(slot_bytes)
        .saturating_add(assembly.short_id_bytes)
}

fn serialized_slot_bytes(tx: &Transaction, witness: Option<&[Witness]>) -> usize {
    let body = blvm_protocol::serialization::transaction::serialize_transaction(tx).len();
    let witness_bytes = witness
        .map(|stacks| {
            stacks
                .iter()
                .map(|stack| stack.iter().map(|item| item.len()).sum::<usize>())
                .sum::<usize>()
        })
        .unwrap_or(0);
    body.saturating_add(witness_bytes)
}

/// What the pending table should do with one compact block.
pub struct CompactBudget {
    /// Assemblies already held, not counting this block's hash.
    pub assemblies: usize,
    /// Retained bytes of those assemblies.
    pub retained_bytes: usize,
    /// This block hash is already in the table.
    pub same_hash: bool,
}

pub enum CompactDecision {
    Hold(CompactAssembly),
    /// Fetch this block whole. Do not insert an assembly.
    FullBlock,
    /// A reconstruction of this hash is already held and the replacement
    /// does not fit. Leave that assembly in place.
    KeepCurrent,
}

/// Caps from the compact-block budget. A `FullBlock` result must be fetched with `getdata`.
pub fn decide_compact_block(
    compact: &CompactBlock,
    matches: &HashMap<Hash, (Transaction, Option<Vec<Witness>>)>,
    budget: CompactBudget,
) -> Result<CompactDecision> {
    let total = compact
        .short_ids
        .len()
        .saturating_add(compact.prefilled_txs.len());
    if total > MAX_COMPACT_BLOCK_TXS {
        return Ok(CompactDecision::FullBlock);
    }
    let assembly = begin_compact_assembly(compact, matches)?;
    if assembly.missing.len() > MAX_COMPACT_MISSING
        || assembly
            .missing
            .iter()
            .any(|index| u16::try_from(*index).is_err())
    {
        return Ok(CompactDecision::FullBlock);
    }
    let adding = if budget.same_hash { 0 } else { 1 };
    if budget.assemblies.saturating_add(adding) > MAX_PENDING_COMPACT {
        return Ok(CompactDecision::FullBlock);
    }
    let next_bytes = budget
        .retained_bytes
        .saturating_add(assembly_retained_bytes(&assembly));
    if next_bytes > MAX_PENDING_COMPACT_BYTES {
        if budget.same_hash {
            return Ok(CompactDecision::KeepCurrent);
        }
        return Ok(CompactDecision::FullBlock);
    }
    Ok(CompactDecision::Hold(assembly))
}

/// Place prefilled transactions and unique, non-conflicting pool matches.
///
/// `mempool_txs` is keyed by witness transaction id. Missing positions stay empty.
pub fn begin_compact_assembly(
    compact_block: &CompactBlock,
    mempool_txs: &HashMap<Hash, (Transaction, Option<Vec<blvm_protocol::segwit::Witness>>)>,
) -> Result<CompactAssembly> {
    let tx_only: HashMap<Hash, Transaction> = mempool_txs
        .iter()
        .map(|(hash, (tx, _))| (*hash, tx.clone()))
        .collect();
    let missing = reconstruct_block(compact_block, &tx_only)?;
    let n = compact_block.short_ids.len() + compact_block.prefilled_txs.len();
    let mut slots = vec![None; n];
    for (index, tx, witness) in &compact_block.prefilled_txs {
        if *index >= n {
            anyhow::bail!("prefilled compact-block index {index} is past {n}");
        }
        slots[*index] = Some((tx.clone(), witness.clone()));
    }
    let prefilled: HashSet<usize> = compact_block
        .prefilled_txs
        .iter()
        .map(|(index, _, _)| *index)
        .collect();
    // First pool row for a short id wins, matching the previous scan. Ambiguous
    // ids are already in `missing` from `reconstruct_block` and are not filled.
    let mut by_short: HashMap<ShortTxId, &(Transaction, Option<Vec<Witness>>)> = HashMap::new();
    for (tx_hash, pair) in mempool_txs {
        let short_id = calculate_short_tx_id(&compact_block.header, tx_hash, compact_block.nonce);
        by_short.entry(short_id).or_insert(pair);
    }
    let missing_set: HashSet<usize> = missing.iter().copied().collect();
    let mut next_block_index = 0usize;
    for &short_id in &compact_block.short_ids {
        while prefilled.contains(&next_block_index) {
            next_block_index = next_block_index.saturating_add(1);
        }
        let block_index = next_block_index;
        next_block_index = next_block_index.saturating_add(1);
        if missing_set.contains(&block_index) {
            continue;
        }
        let found = by_short.get(&short_id).map(|pair| (*pair).clone());
        if let Some(filled) = found {
            if block_index < slots.len() {
                slots[block_index] = Some(filled);
            }
        }
    }
    Ok(CompactAssembly {
        header: compact_block.header.clone(),
        nonce: compact_block.nonce,
        slots,
        missing,
        started: Instant::now(),
        short_id_bytes: 6usize.saturating_mul(compact_block.short_ids.len()),
    })
}

/// Fill `missing` positions from a `blocktxn`, keeping each witness stack.
pub fn apply_blocktxn(
    assembly: &mut CompactAssembly,
    transactions: &[Transaction],
    witnesses: Option<&[Vec<blvm_protocol::segwit::Witness>]>,
) -> Result<()> {
    if transactions.len() != assembly.missing.len() {
        anyhow::bail!(
            "blocktxn count {} does not match {} missing positions",
            transactions.len(),
            assembly.missing.len()
        );
    }
    for (i, &index) in assembly.missing.iter().enumerate() {
        if index >= assembly.slots.len() {
            anyhow::bail!("blocktxn index {index} is past the compact block");
        }
        let witness = witnesses.and_then(|all| all.get(i).cloned());
        assembly.slots[index] = Some((transactions[i].clone(), witness));
    }
    assembly.missing.clear();
    Ok(())
}

/// Transactions and witness stacks in block order, after every slot is filled.
pub fn completed_compact_block(
    assembly: &CompactAssembly,
) -> Result<(Block, Vec<Vec<blvm_protocol::segwit::Witness>>)> {
    let mut transactions = Vec::with_capacity(assembly.slots.len());
    let mut witnesses = Vec::with_capacity(assembly.slots.len());
    for (index, slot) in assembly.slots.iter().enumerate() {
        let Some((tx, witness)) = slot else {
            anyhow::bail!("compact block position {index} is still missing");
        };
        let stacks = witness
            .clone()
            .unwrap_or_else(|| tx.inputs.iter().map(|_| Vec::new()).collect());
        witnesses.push(stacks);
        transactions.push(tx.clone());
    }
    Ok((
        Block {
            header: assembly.header.clone(),
            transactions: transactions.into_boxed_slice(),
        },
        witnesses,
    ))
}

/// Check if two transactions conflict (BIP125 requirement #4)
///
/// A conflict exists if tx1 and tx2 spend at least one common input.
/// Used during compact block reconstruction to detect RBF conflicts.
fn has_conflict_with_tx(tx1: &Transaction, tx2: &Transaction) -> bool {
    for input1 in &tx1.inputs {
        for input2 in &tx2.inputs {
            if input1.prevout == input2.prevout {
                return true;
            }
        }
    }
    false
}

/// Create compact block from full block
///
/// # Arguments
/// * `block` - Full block to convert
/// * `nonce` - Nonce for short ID calculation (typically from block header)
/// * `prefilled_indices` - Indices of transactions to include in full (not as short IDs)
///
/// # Returns
/// Compact block representation
pub fn create_compact_block(
    block: &Block,
    nonce: u64,
    prefilled_indices: &HashSet<usize>,
) -> CompactBlock {
    create_compact_block_with_witnesses(block, nonce, prefilled_indices, &[])
}

/// Create a compact block. `witnesses` is one optional stack list per transaction.
/// A missing or empty stack list uses the transaction id, which equals the witness id.
pub fn create_compact_block_with_witnesses(
    block: &Block,
    nonce: u64,
    prefilled_indices: &HashSet<usize>,
    witnesses: &[Option<Vec<blvm_protocol::segwit::Witness>>],
) -> CompactBlock {
    let mut short_ids = Vec::new();
    let mut prefilled_txs = Vec::new();

    for (index, tx) in block.transactions.iter().enumerate() {
        if prefilled_indices.contains(&index) {
            let witness = witnesses
                .get(index)
                .and_then(|stacks| stacks.clone())
                .filter(|stacks| stacks.iter().any(|stack| !stack.is_empty()));
            prefilled_txs.push((index, tx.clone(), witness));
        } else {
            let witness = witnesses.get(index).and_then(|stacks| stacks.as_deref());
            let wtxid = crate::network::txhash::calculate_wtxid(tx, witness);
            let short_id = calculate_short_tx_id(&block.header, &wtxid, nonce);
            short_ids.push(short_id);
        }
    }

    CompactBlock {
        header: block.header.clone(),
        nonce,
        short_ids,
        prefilled_txs,
    }
}

/// Determine if compact blocks should be preferred for a given transport type
///
/// Compact blocks are especially beneficial for QUIC transports (Iroh/Quinn)
/// due to QUIC's lower latency and better handling of multiple streams.
///
/// # Arguments
/// * `transport_type` - The transport type being used
///
/// # Returns
/// `true` if compact blocks should be preferred for this transport
pub fn should_prefer_compact_blocks(transport_type: TransportType) -> bool {
    match transport_type {
        #[cfg(feature = "quinn")]
        TransportType::Quinn => true, // QUIC: prefer compact blocks for lower latency
        #[cfg(feature = "iroh")]
        TransportType::Iroh => true, // Iroh QUIC: definitely prefer compact blocks
        #[cfg(any(feature = "quinn", feature = "iroh"))]
        TransportType::Tcp => false, // TCP: standard blocks are fine, compact blocks optional
        #[cfg(not(any(feature = "quinn", feature = "iroh")))]
        TransportType::Tcp => false, // TCP: standard blocks are fine, compact blocks optional
    }
}

/// Negotiate both compact blocks and block filters with a peer
///
/// When a peer supports both BIP152 (compact blocks) and BIP157 (filters),
/// coordinate the negotiation to use both optimizations together.
///
/// # Arguments
/// * `transport_type` - The transport being used
/// * `peer_services` - Service flags from peer's version message
///
/// # Returns
/// Tuple of (compact_block_version, prefer_compact, supports_filters)
pub fn negotiate_optimizations(
    transport_type: TransportType,
    peer_services: u64,
) -> (u64, bool, bool) {
    use blvm_protocol::bip157::NODE_COMPACT_FILTERS;

    let compact_version = recommended_compact_block_version(transport_type);
    let prefer_compact = should_prefer_compact_blocks(transport_type);
    let supports_filters = (peer_services & NODE_COMPACT_FILTERS) != 0;

    (compact_version, prefer_compact, supports_filters)
}

/// Create optimized SendCmpct message considering both compact blocks and filters
///
/// When both features are available, recommends using compact blocks
/// with filter support for maximum bandwidth efficiency.
///
/// This coordinates BIP152 (compact blocks) and BIP157 (filters) negotiation,
/// ensuring peers can use both optimizations together when available.
pub fn create_optimized_sendcmpct(
    transport_type: TransportType,
    peer_services: u64,
) -> crate::network::protocol::SendCmpctMessage {
    use crate::network::protocol::SendCmpctMessage;

    let (version, prefer_cmpct, _supports_filters) =
        negotiate_optimizations(transport_type, peer_services);

    // When both compact blocks and filters are available, prefer compact blocks
    // This reduces bandwidth for both features working together
    SendCmpctMessage {
        version,
        prefer_cmpct: if prefer_cmpct { 1 } else { 0 },
    }
}

/// Get recommended compact block version based on transport
///
/// Returns the compact block version to negotiate based on transport capabilities.
/// Version 2 adds prefilled transaction index optimization which works well with QUIC.
///
/// # Arguments
/// * `transport_type` - The transport type being used
///
/// # Returns
/// Recommended compact block version (1 or 2)
pub fn recommended_compact_block_version(transport_type: TransportType) -> u64 {
    match transport_type {
        TransportType::Tcp => 1, // Version 1 is sufficient for TCP
        #[cfg(feature = "quinn")]
        TransportType::Quinn => 2, // Version 2 for QUIC (better prefilled optimization)
        #[cfg(feature = "iroh")]
        TransportType::Iroh => 2, // Version 2 for Iroh (NAT traversal benefits from optimization)
    }
}

/// Check if transport supports QUIC (Iroh or Quinn)
///
/// QUIC transports benefit more from compact blocks due to:
/// - Lower latency on connection establishment
/// - Better multiplexing for multiple block requests
/// - Stream prioritization for compact block data
///
/// # Arguments
/// * `transport_type` - The transport type to check
///
/// # Returns
/// `true` if transport is QUIC-based
pub fn is_quic_transport(transport_type: TransportType) -> bool {
    match transport_type {
        #[cfg(feature = "quinn")]
        TransportType::Quinn => true,
        #[cfg(feature = "iroh")]
        TransportType::Iroh => true,
        #[cfg(any(feature = "quinn", feature = "iroh"))]
        TransportType::Tcp => false,
        #[cfg(not(any(feature = "quinn", feature = "iroh")))]
        TransportType::Tcp => false,
    }
}

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

    #[test]
    fn test_calculate_short_tx_id() {
        let tx_hash = [0u8; 32];
        let nonce = 12345u64;
        let header = BlockHeader {
            version: 1,
            prev_block_hash: [0; 32],
            merkle_root: [0; 32],
            timestamp: 0,
            bits: 0,
            nonce: 0,
        };
        let short_id = calculate_short_tx_id(&header, &tx_hash, nonce);

        // Short ID should be 6 bytes
        assert_eq!(short_id.len(), 6);
    }

    #[test]
    fn short_id_uses_header_keys_and_witness_hash() {
        use blvm_protocol::opcodes::OP_1;

        let wtxid = [1u8; 32];
        let nonce = 7u64;
        let header = BlockHeader {
            version: 1,
            prev_block_hash: [2u8; 32],
            merkle_root: [3u8; 32],
            timestamp: 0,
            bits: 0,
            nonce: 0,
        };
        let other_header = BlockHeader {
            merkle_root: [4u8; 32],
            ..header.clone()
        };
        assert_ne!(
            calculate_short_tx_id(&header, &wtxid, nonce),
            calculate_short_tx_id(&other_header, &wtxid, nonce)
        );

        let tx = Transaction {
            version: 1,
            inputs: vec![blvm_protocol::TransactionInput {
                prevout: blvm_protocol::OutPoint {
                    hash: [5u8; 32],
                    index: 0,
                },
                script_sig: Vec::new(),
                sequence: blvm_protocol::constants::SEQUENCE_FINAL as u64,
            }]
            .into(),
            outputs: vec![blvm_protocol::TransactionOutput {
                value: 50_000,
                script_pubkey: vec![OP_1],
            }]
            .into(),
            lock_time: 0,
        };
        let block = Block {
            header: header.clone(),
            transactions: vec![tx.clone()].into_boxed_slice(),
        };
        let bare = create_compact_block_with_witnesses(&block, nonce, &HashSet::new(), &[]);
        let stacks = vec![vec![vec![OP_1]]];
        let with_witness = create_compact_block_with_witnesses(
            &block,
            nonce,
            &HashSet::new(),
            &[Some(stacks.clone())],
        );
        assert_ne!(bare.short_ids, with_witness.short_ids);
        let wtxid = crate::network::txhash::calculate_wtxid(&tx, Some(&stacks));
        assert_eq!(
            with_witness.short_ids,
            vec![calculate_short_tx_id(&header, &wtxid, nonce)]
        );
    }

    #[test]
    fn test_reconstruct_block_empty_mempool() {
        let compact_block = CompactBlock {
            header: BlockHeader {
                version: 1,
                prev_block_hash: [0; 32],
                merkle_root: [0; 32],
                timestamp: 0,
                bits: 0,
                nonce: 0,
            },
            nonce: 0,
            short_ids: vec![[0u8; 6]],
            prefilled_txs: vec![],
        };

        let mempool_txs = HashMap::new();
        let missing = reconstruct_block(&compact_block, &mempool_txs).unwrap();

        // All transactions should be missing
        assert_eq!(missing.len(), 1);
    }

    #[test]
    fn missing_short_id_keeps_the_block_index_after_a_prefilled_coinbase() {
        use blvm_protocol::constants::SEQUENCE_FINAL;
        use blvm_protocol::opcodes::OP_1;
        use blvm_protocol::{OutPoint, TransactionInput, TransactionOutput};

        let tx = |prev: u8, value: i64| Transaction {
            version: 1,
            inputs: vec![TransactionInput {
                prevout: OutPoint {
                    hash: [prev; 32],
                    index: 0,
                },
                script_sig: Vec::new(),
                sequence: SEQUENCE_FINAL as u64,
            }]
            .into(),
            outputs: vec![TransactionOutput {
                value,
                script_pubkey: vec![OP_1],
            }]
            .into(),
            lock_time: 0,
        };
        let header = BlockHeader {
            version: 1,
            prev_block_hash: [0; 32],
            merkle_root: [0; 32],
            timestamp: 0,
            bits: 0,
            nonce: 0,
        };
        let block = Block {
            header,
            transactions: vec![tx(1, 50_000), tx(2, 40_000)].into_boxed_slice(),
        };
        let compact = create_compact_block(&block, 7, &HashSet::from([0]));
        assert_eq!(compact.prefilled_txs[0].0, 0);
        assert_eq!(compact.short_ids.len(), 1);

        let missing = reconstruct_block(&compact, &HashMap::new()).unwrap();
        assert_eq!(missing, vec![1]);
    }

    #[test]
    fn prefilled_witness_survives_wire_conversion() {
        use blvm_protocol::constants::SEQUENCE_FINAL;
        use blvm_protocol::opcodes::OP_1;
        use blvm_protocol::{OutPoint, TransactionInput, TransactionOutput};

        let tx = Transaction {
            version: 1,
            inputs: vec![TransactionInput {
                prevout: OutPoint {
                    hash: [1u8; 32],
                    index: 0,
                },
                script_sig: Vec::new(),
                sequence: SEQUENCE_FINAL as u64,
            }]
            .into(),
            outputs: vec![TransactionOutput {
                value: 50_000,
                script_pubkey: vec![OP_1],
            }]
            .into(),
            lock_time: 0,
        };
        let block = Block {
            header: BlockHeader {
                version: 1,
                prev_block_hash: [0; 32],
                merkle_root: [0; 32],
                timestamp: 0,
                bits: 0,
                nonce: 0,
            },
            transactions: vec![tx].into_boxed_slice(),
        };
        let witness = vec![vec![vec![OP_1]]];
        let compact = create_compact_block_with_witnesses(
            &block,
            7,
            &HashSet::from([0]),
            &[Some(witness.clone())],
        );
        assert_eq!(compact.prefilled_txs[0].2, Some(witness.clone()));

        let message = blvm_protocol::network::CmpctBlockMessage::try_from(compact).unwrap();
        assert_eq!(message.prefilled_txs[0].witness, Some(witness.clone()));

        let back = blvm_protocol::bip152::CompactBlock::from(&message);
        assert_eq!(back.prefilled_txs[0].2, Some(witness));
    }

    #[test]
    fn ambiguous_short_id_is_requested() {
        use blvm_protocol::constants::SEQUENCE_FINAL;
        use blvm_protocol::opcodes::OP_1;
        use blvm_protocol::{OutPoint, TransactionInput, TransactionOutput};

        let header = BlockHeader {
            version: 0,
            prev_block_hash: [0; 32],
            merkle_root: [0; 32],
            timestamp: 0,
            bits: 0,
            nonce: 0,
        };
        let nonce = 0u64;
        let mut first = [0u8; 32];
        first[24] = 0x99;
        first[25] = 0x13;
        first[26] = 0x4b;
        let mut second = [0u8; 32];
        second[24] = 0xd7;
        second[25] = 0x08;
        second[26] = 0x79;
        let shared = calculate_short_tx_id(&header, &first, nonce);
        assert_eq!(shared, calculate_short_tx_id(&header, &second, nonce));

        let unique = [9u8; 32];
        let unique_id = calculate_short_tx_id(&header, &unique, nonce);
        assert_ne!(unique_id, shared);

        let tx = |version: u64| Transaction {
            version,
            inputs: vec![TransactionInput {
                prevout: OutPoint {
                    hash: [version as u8; 32],
                    index: 0,
                },
                script_sig: Vec::new(),
                sequence: SEQUENCE_FINAL as u64,
            }]
            .into(),
            outputs: vec![TransactionOutput {
                value: 50_000,
                script_pubkey: vec![OP_1],
            }]
            .into(),
            lock_time: 0,
        };
        let mut pool = HashMap::new();
        pool.insert(first, tx(1));
        pool.insert(second, tx(2));
        pool.insert(unique, tx(3));
        let compact = CompactBlock {
            header,
            nonce,
            short_ids: vec![shared, unique_id],
            prefilled_txs: vec![],
        };
        let missing = reconstruct_block(&compact, &pool).unwrap();
        assert_eq!(missing, vec![0]);
    }

    #[test]
    fn test_should_prefer_compact_blocks_tcp() {
        // TCP: compact blocks optional, not preferred by default
        assert!(!should_prefer_compact_blocks(TransportType::Tcp));
    }

    #[cfg(feature = "quinn")]
    #[test]
    fn test_should_prefer_compact_blocks_quinn() {
        // Quinn QUIC: prefer compact blocks
        assert_eq!(should_prefer_compact_blocks(TransportType::Quinn), true);
    }

    #[cfg(feature = "iroh")]
    #[test]
    fn test_should_prefer_compact_blocks_iroh() {
        // Iroh QUIC: definitely prefer compact blocks
        assert_eq!(should_prefer_compact_blocks(TransportType::Iroh), true);
    }

    #[test]
    fn test_recommended_compact_block_version_tcp() {
        // TCP: version 1 is sufficient
        assert_eq!(recommended_compact_block_version(TransportType::Tcp), 1);
    }

    #[cfg(feature = "quinn")]
    #[test]
    fn test_recommended_compact_block_version_quinn() {
        // Quinn: version 2 for better optimization
        assert_eq!(recommended_compact_block_version(TransportType::Quinn), 2);
    }

    #[cfg(feature = "iroh")]
    #[test]
    fn test_recommended_compact_block_version_iroh() {
        // Iroh: version 2 for NAT traversal benefits
        assert_eq!(recommended_compact_block_version(TransportType::Iroh), 2);
    }

    #[test]
    fn test_is_quic_transport_tcp() {
        // TCP is not QUIC
        assert!(!is_quic_transport(TransportType::Tcp));
    }

    #[cfg(feature = "quinn")]
    #[test]
    fn test_is_quic_transport_quinn() {
        // Quinn is QUIC
        assert_eq!(is_quic_transport(TransportType::Quinn), true);
    }

    #[cfg(feature = "iroh")]
    #[test]
    fn test_is_quic_transport_iroh() {
        // Iroh is QUIC
        assert_eq!(is_quic_transport(TransportType::Iroh), true);
    }

    #[test]
    fn blocktxn_witness_fills_the_short_id_after_a_prefilled_coinbase() {
        use blvm_protocol::constants::SEQUENCE_FINAL;
        use blvm_protocol::opcodes::OP_1;
        use blvm_protocol::{OutPoint, TransactionInput, TransactionOutput};

        let tx = |prev: u8| Transaction {
            version: 1,
            inputs: vec![TransactionInput {
                prevout: OutPoint {
                    hash: [prev; 32],
                    index: 0,
                },
                script_sig: Vec::new(),
                sequence: SEQUENCE_FINAL as u64,
            }]
            .into(),
            outputs: vec![TransactionOutput {
                value: 50_000,
                script_pubkey: vec![OP_1],
            }]
            .into(),
            lock_time: 0,
        };
        let header = BlockHeader {
            version: 1,
            prev_block_hash: [0; 32],
            merkle_root: [0; 32],
            timestamp: 0,
            bits: 0,
            nonce: 0,
        };
        let block = Block {
            header,
            transactions: vec![tx(1), tx(2)].into_boxed_slice(),
        };
        let compact = create_compact_block(&block, 7, &HashSet::from([0]));
        let mut assembly = begin_compact_assembly(&compact, &HashMap::new()).unwrap();
        assert_eq!(assembly.missing, vec![1]);
        let witness = vec![vec![vec![OP_1]]];
        apply_blocktxn(&mut assembly, &[tx(2)], Some(&[witness.clone()])).unwrap();
        let (reconstructed, witnesses) = completed_compact_block(&assembly).unwrap();
        assert_eq!(witnesses[1], witness);
        assert_eq!(
            reconstructed.transactions[1].outputs[0].script_pubkey,
            vec![OP_1]
        );
    }

    fn bare_compact(short_ids: Vec<ShortTxId>) -> CompactBlock {
        CompactBlock {
            header: BlockHeader {
                version: 1,
                prev_block_hash: [0; 32],
                merkle_root: [0; 32],
                timestamp: 0,
                bits: 0,
                nonce: 0,
            },
            nonce: 1,
            short_ids,
            prefilled_txs: vec![],
        }
    }

    fn open_budget() -> CompactBudget {
        CompactBudget {
            assemblies: 0,
            retained_bytes: 0,
            same_hash: false,
        }
    }

    #[test]
    fn a_fifth_assembly_is_fetched_whole() {
        let compact = bare_compact(vec![[1u8; 6]]);
        let held = decide_compact_block(
            &compact,
            &HashMap::new(),
            CompactBudget {
                assemblies: MAX_PENDING_COMPACT,
                retained_bytes: 0,
                same_hash: false,
            },
        )
        .unwrap();
        assert!(matches!(held, CompactDecision::FullBlock));
        let replaced = decide_compact_block(
            &compact,
            &HashMap::new(),
            CompactBudget {
                assemblies: MAX_PENDING_COMPACT - 1,
                retained_bytes: 0,
                same_hash: true,
            },
        )
        .unwrap();
        assert!(matches!(replaced, CompactDecision::Hold(_)));
    }

    #[test]
    fn retained_bytes_past_eight_mib_are_fetched_whole() {
        let compact = bare_compact(vec![[1u8; 6]]);
        let held = decide_compact_block(
            &compact,
            &HashMap::new(),
            CompactBudget {
                assemblies: 0,
                retained_bytes: MAX_PENDING_COMPACT_BYTES,
                same_hash: false,
            },
        )
        .unwrap();
        assert!(matches!(held, CompactDecision::FullBlock));
    }

    #[test]
    fn a_replacement_past_the_byte_cap_keeps_the_current_assembly() {
        let compact = bare_compact(vec![[1u8; 6]]);
        let kept = decide_compact_block(
            &compact,
            &HashMap::new(),
            CompactBudget {
                assemblies: 0,
                retained_bytes: MAX_PENDING_COMPACT_BYTES,
                same_hash: true,
            },
        )
        .unwrap();
        assert!(matches!(kept, CompactDecision::KeepCurrent));
    }

    #[test]
    fn more_than_128_holes_are_fetched_whole() {
        let compact = bare_compact(vec![[2u8; 6]; MAX_COMPACT_MISSING + 1]);
        let held = decide_compact_block(&compact, &HashMap::new(), open_budget()).unwrap();
        assert!(matches!(held, CompactDecision::FullBlock));
        let within = bare_compact(vec![[2u8; 6]; MAX_COMPACT_MISSING]);
        let held = decide_compact_block(&within, &HashMap::new(), open_budget()).unwrap();
        assert!(matches!(held, CompactDecision::Hold(_)));
    }

    #[test]
    fn more_than_100_000_transactions_are_fetched_whole() {
        let compact = bare_compact(vec![[3u8; 6]; MAX_COMPACT_BLOCK_TXS + 1]);
        let held = decide_compact_block(&compact, &HashMap::new(), open_budget()).unwrap();
        assert!(matches!(held, CompactDecision::FullBlock));
    }

    #[test]
    fn an_index_past_u16_is_fetched_whole() {
        let compact = bare_compact(vec![[4u8; 6]; (u16::MAX as usize) + 2]);
        let held = decide_compact_block(&compact, &HashMap::new(), open_budget()).unwrap();
        assert!(matches!(held, CompactDecision::FullBlock));
    }

    #[test]
    fn an_assembly_older_than_sixty_seconds_is_expired() {
        let compact = bare_compact(vec![[5u8; 6]]);
        let CompactDecision::Hold(mut assembly) =
            decide_compact_block(&compact, &HashMap::new(), open_budget()).unwrap()
        else {
            panic!("one hole fits the budget");
        };
        let now = Instant::now();
        assert!(!assembly.expired_at(now));
        assembly.started = now - COMPACT_ASSEMBLY_DEADLINE;
        assert!(assembly.expired_at(now));
    }

    #[test]
    fn short_id_lookup_clones_only_the_match() {
        use crate::node::mempool::MempoolManager;
        use blvm_protocol::constants::SEQUENCE_FINAL;
        use blvm_protocol::{OutPoint, TransactionInput, TransactionOutput};
        use std::sync::Arc;

        let tx = |prev: u8| Transaction {
            version: 1,
            inputs: vec![TransactionInput {
                prevout: OutPoint {
                    hash: [prev; 32],
                    index: 0,
                },
                script_sig: Vec::new(),
                sequence: SEQUENCE_FINAL as u64,
            }]
            .into(),
            outputs: vec![TransactionOutput {
                value: 10_000,
                script_pubkey: vec![0x6a],
            }]
            .into(),
            lock_time: 0,
        };
        let mempool = MempoolManager::new();
        let mut policy = crate::config::MempoolPolicyConfig::default();
        policy.require_standard = false;
        policy.min_tx_fee = 0;
        policy.min_relay_fee_rate = 0;
        mempool.set_policy_config(Some(policy));
        for prev in [1u8, 2u8] {
            let tx = tx(prev);
            let mut utxo = blvm_protocol::UtxoSet::default();
            utxo.insert(
                tx.inputs[0].prevout,
                Arc::new(blvm_protocol::UTXO {
                    value: 20_000,
                    script_pubkey: vec![0x51].into(),
                    height: 1,
                    is_coinbase: false,
                }),
            );
            mempool.set_utxo_set_arc(Arc::new(tokio::sync::Mutex::new(utxo)));
            assert!(
                mempool.add_transaction(tx).unwrap(),
                "prev {prev} was not admitted"
            );
        }
        assert_eq!(mempool.get_transactions().len(), 2);
        let header = BlockHeader {
            version: 1,
            prev_block_hash: [0; 32],
            merkle_root: [0; 32],
            timestamp: 0,
            bits: 0,
            nonce: 0,
        };
        let nonce = 11u64;
        let wanted = {
            let txs = mempool.get_transactions();
            let wtxid = crate::network::txhash::calculate_wtxid(&txs[0], None);
            calculate_short_tx_id(&header, &wtxid, nonce)
        };
        let matched = mempool.transactions_for_short_ids(&header, nonce, &[wanted]);
        assert_eq!(matched.len(), 1);
        let _ = wanted;
    }
}