blvm-protocol 0.1.4

Bitcoin Commons BLVM: Bitcoin protocol abstraction layer for multiple variants and evolution
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
#![cfg(feature = "utxo-commitments")]

//! Integration tests for UTXO Commitments module
//!
//! Tests end-to-end workflows:
//! - Initial sync with peer consensus
//! - Spam filtering and filtered block processing
//! - UTXO set updates and commitment generation
//! - Configuration loading and validation

mod tests {
    use blvm_consensus::test_utils::create_test_header;
    use blvm_consensus::types::{
        BlockHeader, ByteString, Hash, Natural, OutPoint, Transaction, TransactionInput,
        TransactionOutput, UTXO,
    };
    use blvm_protocol::utxo_commitments::*;

    /// Create a test UTXO
    fn create_test_utxo(value: i64, height: Natural) -> UTXO {
        UTXO {
            value,
            script_pubkey: vec![0x76, 0xa9, 0x14].into(), // P2PKH pattern
            height,
            is_coinbase: false,
        }
    }

    /// Create a test transaction
    fn create_test_transaction(
        inputs: Vec<OutPoint>,
        outputs: Vec<(i64, ByteString)>,
    ) -> Transaction {
        Transaction {
            version: 1,
            inputs: inputs
                .into_iter()
                .map(|prevout| TransactionInput {
                    prevout,
                    script_sig: vec![],
                    sequence: 0xffffffff,
                })
                .collect(),
            outputs: outputs
                .into_iter()
                .map(|(value, script_pubkey)| TransactionOutput {
                    value,
                    script_pubkey,
                })
                .collect(),
            lock_time: 0,
        }
    }

    #[test]
    fn test_utxo_commitment_full_workflow() {
        // 1. Create UTXO Merkle tree
        let mut tree = UtxoMerkleTree::new().unwrap();

        // 2. Insert some UTXOs
        let outpoint1 = OutPoint {
            hash: [1; 32],
            index: 0,
        };
        let utxo1 = create_test_utxo(10000, 0);
        tree.insert(outpoint1.clone(), utxo1.clone()).unwrap();

        let outpoint2 = OutPoint {
            hash: [2; 32],
            index: 0,
        };
        let utxo2 = create_test_utxo(5000, 0);
        tree.insert(outpoint2.clone(), utxo2.clone()).unwrap();

        // 3. Generate commitment
        let block_hash = [3; 32];
        let commitment = tree.generate_commitment(block_hash, 0);

        assert_eq!(commitment.utxo_count, 2);
        assert_eq!(commitment.total_supply, 15000);
        assert_eq!(commitment.block_height, 0);
        assert_eq!(commitment.block_hash, block_hash);

        // 4. Verify supply
        let verify_result = verify_supply(&commitment);
        // Note: At height 0, expected supply is 50 BTC (genesis block)
        // Our test UTXOs are just test data, so this might fail
        // In real usage, UTXO set would match expected supply
    }

    #[test]
    fn test_spam_transaction_removes_spent_inputs() {
        use blvm_consensus::types::{
            OutPoint, Transaction, TransactionInput, TransactionOutput, UTXO,
        };
        use blvm_protocol::utxo_commitments::initial_sync::InitialSync;
        use blvm_protocol::utxo_commitments::merkle_tree::UtxoMerkleTree;
        use blvm_protocol::utxo_commitments::peer_consensus::ConsensusConfig;

        // Create initial sync manager
        let config = ConsensusConfig {
            min_peers: 2,
            consensus_threshold: 0.8,
            safety_margin: 6,
            ..Default::default()
        };
        let initial_sync = InitialSync::new(config);

        // Create UTXO tree with a non-spam UTXO
        let mut utxo_tree = UtxoMerkleTree::new().unwrap();
        let non_spam_outpoint = OutPoint {
            hash: [1u8; 32],
            index: 0,
        };
        let non_spam_utxo = UTXO {
            value: 100000, // 0.001 BTC
            script_pubkey: vec![
                0x76, 0xa9, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x88, 0xac,
            ]
            .into(), // P2PKH
            height: 100,
            is_coinbase: false,
        };
        utxo_tree
            .insert(non_spam_outpoint.clone(), non_spam_utxo.clone())
            .unwrap();

        // Verify UTXO exists
        assert!(utxo_tree.get(&non_spam_outpoint).unwrap().is_some());
        let initial_supply = utxo_tree.total_supply();
        assert_eq!(initial_supply, 100000);

        // Create a spam transaction that spends the non-spam UTXO
        let spam_tx = Transaction {
            version: 1,
            inputs: vec![TransactionInput {
                prevout: non_spam_outpoint.clone(),
                script_sig: vec![0x51].into(), // OP_1 minimal script_sig for test
                sequence: 0xffffffff,
            }]
            .into(),
            outputs: vec![TransactionOutput {
                value: 50000,
                script_pubkey: {
                    // OP_RETURN with large data (spam pattern)
                    let mut script = vec![0x6a].into(); // OP_RETURN
                    script.extend(vec![0x00; 100]); // Large data
                    script
                },
            }]
            .into(),
            lock_time: 0,
        };

        // Process the spam transaction
        let (spam_summary, _root) = initial_sync
            .process_filtered_block(&mut utxo_tree, 101, &[spam_tx])
            .unwrap();

        // Verify spam was detected
        assert_eq!(spam_summary.filtered_count, 1);
        assert!(spam_summary.by_type.ordinals > 0 || spam_summary.by_type.dust > 0);

        // CRITICAL: Verify the spent input was removed (even though transaction is spam)
        assert!(
            utxo_tree.get(&non_spam_outpoint).unwrap().is_none(),
            "Spam transaction must remove spent inputs from UTXO tree"
        );

        // Verify supply was reduced (input was spent)
        let final_supply = utxo_tree.total_supply();
        assert!(
            final_supply < initial_supply,
            "Supply must decrease when UTXO is spent, even if transaction is spam"
        );

        // Verify spam output was NOT added (bandwidth savings)
        // Compute tx_id using the same method as process_filtered_block
        use blvm_consensus::serialization::transaction::serialize_transaction;
        use sha2::{Digest, Sha256};
        let serialized = serialize_transaction(&spam_tx);
        let first_hash = Sha256::digest(&serialized);
        let second_hash = Sha256::digest(first_hash);
        let mut spam_tx_id = [0u8; 32];
        spam_tx_id.copy_from_slice(&second_hash);
        let spam_output_outpoint = OutPoint {
            hash: spam_tx_id,
            index: 0,
        };
        assert!(
            utxo_tree.get(&spam_output_outpoint).unwrap().is_none(),
            "Spam transaction outputs should not be added to UTXO tree"
        );
    }

    #[test]
    fn test_spam_filtering_integration() {
        let filter = SpamFilter::new();

        // Create mix of transactions
        let transactions = vec![
            // Non-spam transaction
            create_test_transaction(
                vec![OutPoint {
                    hash: [1; 32],
                    index: 0,
                }],
                vec![(10000, vec![0x76, 0xa9])],
            ),
            // Dust transaction (all outputs < 546)
            create_test_transaction(
                vec![OutPoint {
                    hash: [2; 32],
                    index: 0,
                }],
                vec![(100, vec![])], // Below dust threshold
            ),
            // Ordinals transaction (OP_RETURN with large data)
            create_test_transaction(
                vec![OutPoint {
                    hash: [3; 32],
                    index: 0,
                }],
                vec![(1000, {
                    let mut script = vec![0x6a]; // OP_RETURN
                    script.extend(vec![0x00; 100]); // Large data
                    script
                })],
            ),
            // Another non-spam transaction
            create_test_transaction(
                vec![OutPoint {
                    hash: [4; 32],
                    index: 0,
                }],
                vec![(5000, vec![0x76, 0xa9])],
            ),
        ];

        // Filter block
        let (filtered_txs, summary) = filter.filter_block(&transactions);

        // Should filter out 2 spam transactions
        assert_eq!(filtered_txs.len(), 2);
        assert_eq!(summary.filtered_count, 2);
        assert!(summary.filtered_size > 0);
    }

    #[test]
    fn test_peer_consensus_workflow() {
        // Create peer consensus manager (shuffle_peers: false for deterministic test)
        let config = ConsensusConfig {
            shuffle_peers: false,
            ..Default::default()
        };
        let peer_consensus = PeerConsensus::new(config);

        // Create diverse peers
        let all_peers = vec![
            PeerInfo {
                address: std::net::IpAddr::V4(std::net::Ipv4Addr::new(1, 1, 1, 1)),
                asn: Some(1),
                country: Some("US".to_string()),
                implementation: Some("reference".to_string()),
                subnet: 0x01010000,
            },
            PeerInfo {
                address: std::net::IpAddr::V4(std::net::Ipv4Addr::new(2, 2, 2, 2)),
                asn: Some(2),
                country: Some("DE".to_string()),
                implementation: Some("reference".to_string()),
                subnet: 0x02020000,
            },
            PeerInfo {
                address: std::net::IpAddr::V4(std::net::Ipv4Addr::new(3, 3, 3, 3)),
                asn: Some(3),
                country: Some("JP".to_string()),
                implementation: Some("reference".to_string()),
                subnet: 0x03030000,
            },
        ];

        // Discover diverse peers
        let diverse_peers = peer_consensus.discover_diverse_peers(all_peers);
        assert_eq!(diverse_peers.len(), 3);

        // Test checkpoint height determination
        let peer_tips = vec![100000, 100050, 100100];
        let checkpoint = peer_consensus.determine_checkpoint_height(peer_tips);
        assert!(checkpoint > 0);
        assert!(checkpoint < 100000); // Should be below tips minus safety margin
    }

    #[test]
    fn test_configuration_loading() {
        // Create temporary config file
        let config_dir = std::env::temp_dir();
        let config_path = config_dir.join("utxo_commitments_test_config.json");

        // Create default config
        let default_config = UtxoCommitmentsConfig::default();
        default_config.to_json_file(&config_path).unwrap();

        // Load config
        let loaded_config = UtxoCommitmentsConfig::from_json_file(&config_path).unwrap();

        assert_eq!(loaded_config.sync_mode, default_config.sync_mode);
        assert_eq!(
            loaded_config.verification_level,
            default_config.verification_level
        );
        assert_eq!(
            loaded_config.consensus.min_peers,
            default_config.consensus.min_peers
        );

        // Validate config
        assert!(loaded_config.validate().is_ok());

        // Cleanup
        let _ = std::fs::remove_file(&config_path);
    }

    #[test]
    fn test_configuration_validation() {
        let mut config = UtxoCommitmentsConfig::default();

        // Valid config should pass
        assert!(config.validate().is_ok());

        // Invalid: min_peers = 0
        config.consensus.min_peers = 0;
        assert!(config.validate().is_err());

        // Reset and test invalid threshold
        config = UtxoCommitmentsConfig::default();
        config.consensus.consensus_threshold = 1.5; // > 1.0
        assert!(config.validate().is_err());

        // Reset and test invalid target_peers
        config = UtxoCommitmentsConfig::default();
        config.consensus.target_peers = 1; // < min_peers (5)
        assert!(config.validate().is_err());
    }

    #[test]
    fn test_initial_sync_with_config() {
        // Create config
        let consensus_config = ConsensusConfig::default();
        let spam_filter_config = SpamFilterConfig::default();

        // Create initial sync manager
        let initial_sync = InitialSync::with_spam_filter(consensus_config, spam_filter_config);

        // Create test header chain
        let mut headers = Vec::new();
        let mut prev_hash = [0; 32];
        for i in 0..10 {
            let header = create_test_header(1234567890 + (i * 600), prev_hash);
            prev_hash = compute_block_hash(&header);
            headers.push(header);
        }

        // Test would normally execute initial sync here
        // For now, just verify manager is created correctly
        // (actual sync requires network integration)
    }

    #[test]
    fn test_merkle_tree_incremental_updates() {
        let mut tree = UtxoMerkleTree::new().unwrap();
        let initial_root = tree.root();

        // Insert UTXO
        let outpoint1 = OutPoint {
            hash: [1; 32],
            index: 0,
        };
        let utxo1 = create_test_utxo(1000, 0);
        let root1 = tree.insert(outpoint1.clone(), utxo1.clone()).unwrap();
        assert_ne!(root1, initial_root); // Root should change

        // Insert another UTXO
        let outpoint2 = OutPoint {
            hash: [2; 32],
            index: 0,
        };
        let utxo2 = create_test_utxo(2000, 0);
        let root2 = tree.insert(outpoint2.clone(), utxo2.clone()).unwrap();
        assert_ne!(root2, root1); // Root should change again

        // Remove first UTXO
        let root3 = tree.remove(&outpoint1, &utxo1).unwrap();
        assert_ne!(root3, root2); // Root should change

        // Verify supply tracking
        assert_eq!(tree.total_supply(), 2000);
        assert_eq!(tree.utxo_count(), 1);
    }

    /// Compute block header hash (double SHA256)
    fn compute_block_hash(header: &BlockHeader) -> Hash {
        use sha2::{Digest, Sha256};

        let mut bytes = Vec::with_capacity(80);
        bytes.extend_from_slice(&header.version.to_le_bytes());
        bytes.extend_from_slice(&header.prev_block_hash);
        bytes.extend_from_slice(&header.merkle_root);
        bytes.extend_from_slice(&header.timestamp.to_le_bytes());
        bytes.extend_from_slice(&header.bits.to_le_bytes());
        bytes.extend_from_slice(&header.nonce.to_le_bytes());

        let first_hash = Sha256::digest(&bytes);
        let second_hash = Sha256::digest(&first_hash);

        let mut hash = [0u8; 32];
        hash.copy_from_slice(&second_hash);
        hash
    }

    #[test]
    fn test_utxo_set_to_merkle_tree_conversion() {
        use blvm_consensus::types::UtxoSet;
        use blvm_protocol::utxo_commitments::merkle_tree::UtxoMerkleTree;

        // Create UtxoSet
        let mut utxo_set = UtxoSet::default();
        let outpoint1 = OutPoint {
            hash: [1; 32],
            index: 0,
        };
        let utxo1 = create_test_utxo(10000, 100);
        utxo_set.insert(outpoint1.clone(), std::sync::Arc::new(utxo1.clone()));

        let outpoint2 = OutPoint {
            hash: [2; 32],
            index: 0,
        };
        let utxo2 = create_test_utxo(5000, 100);
        utxo_set.insert(outpoint2.clone(), std::sync::Arc::new(utxo2.clone()));

        // Convert to UtxoMerkleTree
        let tree = UtxoMerkleTree::from_utxo_set(&utxo_set).unwrap();

        // Verify tree matches UtxoSet
        assert_eq!(tree.total_supply(), 15000);
        assert_eq!(tree.utxo_count(), 2);
        assert!(tree.get(&outpoint1).unwrap().is_some());
        assert!(tree.get(&outpoint2).unwrap().is_some());
    }

    #[test]
    fn test_utxo_tree_incremental_update_from_utxo_set() {
        use blvm_consensus::types::UtxoSet;
        use blvm_protocol::utxo_commitments::merkle_tree::UtxoMerkleTree;

        // Create initial tree
        let mut tree = UtxoMerkleTree::new().unwrap();
        let outpoint1 = OutPoint {
            hash: [1; 32],
            index: 0,
        };
        let utxo1 = create_test_utxo(10000, 100);
        tree.insert(outpoint1.clone(), utxo1.clone()).unwrap();

        let outpoint2 = OutPoint {
            hash: [2; 32],
            index: 0,
        };
        let utxo2 = create_test_utxo(5000, 100);
        tree.insert(outpoint2.clone(), utxo2.clone()).unwrap();

        let initial_root = tree.root();
        let initial_supply = tree.total_supply();

        // Create new UtxoSet (removed outpoint1, added outpoint3)
        let mut new_utxo_set = UtxoSet::default();
        let outpoint3 = OutPoint {
            hash: [3; 32],
            index: 0,
        };
        let utxo3 = create_test_utxo(8000, 101);
        new_utxo_set.insert(outpoint2.clone(), std::sync::Arc::new(utxo2.clone())); // Kept
        new_utxo_set.insert(outpoint3.clone(), std::sync::Arc::new(utxo3.clone())); // Added

        // Create old UtxoSet for comparison
        let mut old_utxo_set = UtxoSet::default();
        old_utxo_set.insert(outpoint1.clone(), std::sync::Arc::new(utxo1.clone()));
        old_utxo_set.insert(outpoint2.clone(), std::sync::Arc::new(utxo2.clone()));

        // Update tree incrementally
        let new_root = tree
            .update_from_utxo_set(&new_utxo_set, &old_utxo_set)
            .unwrap();

        // Verify changes
        assert_ne!(new_root, initial_root);
        assert_eq!(tree.total_supply(), 13000); // 5000 + 8000
        assert_eq!(tree.utxo_count(), 2);
        assert!(tree.get(&outpoint1).unwrap().is_none()); // Removed
        assert!(tree.get(&outpoint2).unwrap().is_some()); // Kept
        assert!(tree.get(&outpoint3).unwrap().is_some()); // Added
    }
}