blvm-node 0.1.2

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
//! CTV + BIP70 Payment Integration Tests
//!
//! Tests the complete payment flow including:
//! - Payment request creation
//! - CTV covenant proof creation
//! - Payment state machine transitions
//! - Settlement monitoring
//! - Instant proof (not instant settlement) functionality

use blvm_node::config::PaymentConfig;
#[cfg(feature = "ctv")]
use blvm_node::payment::covenant::{CovenantEngine, CovenantProof};
use blvm_node::payment::processor::PaymentProcessor;
use blvm_node::payment::settlement::SettlementMonitor;
use blvm_node::payment::state_machine::{PaymentState, PaymentStateMachine};
use blvm_protocol::payment::PaymentOutput;
use std::sync::Arc;
use tempfile::TempDir;

/// Helper to create test payment outputs
fn create_test_outputs() -> Vec<PaymentOutput> {
    create_test_outputs_unique(0)
}

/// Payment IDs are derived from the serialized request; vary `salt` so multiple requests stay distinct.
fn create_test_outputs_unique(salt: u64) -> Vec<PaymentOutput> {
    vec![
        PaymentOutput {
            script: vec![0x51, 0x87],    // OP_1, OP_EQUAL (test script)
            amount: Some(100000 + salt), // 0.001 BTC + salt
        },
        PaymentOutput {
            script: vec![0x52, 0x87],   // OP_2, OP_EQUAL
            amount: Some(50000 + salt), // 0.0005 BTC + salt
        },
    ]
}

/// Test payment request creation
#[tokio::test]
async fn test_payment_request_creation() {
    let temp_dir = TempDir::new().unwrap();
    let config = PaymentConfig::default();
    let processor =
        Arc::new(PaymentProcessor::new(config).expect("Failed to create payment processor"));
    let state_machine = Arc::new(PaymentStateMachine::new(processor));

    let outputs = create_test_outputs();
    let merchant_data = Some(b"test_merchant_data".to_vec());

    // Create payment request without covenant
    let (payment_id, covenant_proof) = state_machine
        .create_payment_request(outputs.clone(), merchant_data.clone(), false)
        .await
        .expect("Failed to create payment request");

    assert!(!payment_id.is_empty(), "Payment ID should not be empty");
    assert_eq!(
        covenant_proof, None,
        "Covenant proof should be None when create_covenant=false"
    );

    // Verify state
    let state = state_machine
        .get_payment_state(&payment_id)
        .await
        .expect("Failed to get payment state");

    match state {
        PaymentState::RequestCreated { request_id } => {
            assert_eq!(request_id, payment_id);
        }
        _ => panic!("Expected RequestCreated state, got {:?}", state),
    }
}

/// Test CTV covenant proof creation
#[tokio::test]
#[cfg(feature = "ctv")]
async fn test_covenant_proof_creation() {
    let temp_dir = TempDir::new().unwrap();
    let config = PaymentConfig::default();
    let processor =
        Arc::new(PaymentProcessor::new(config).expect("Failed to create payment processor"));
    let state_machine = Arc::new(PaymentStateMachine::new(processor));

    let outputs = create_test_outputs();

    // Create payment request with covenant
    let (payment_id, covenant_proof) = state_machine
        .create_payment_request(outputs.clone(), None, true)
        .await
        .expect("Failed to create payment request with covenant");

    assert!(!payment_id.is_empty(), "Payment ID should not be empty");
    assert!(
        covenant_proof.is_some(),
        "Covenant proof should be created when create_covenant=true"
    );

    let proof = covenant_proof.unwrap();
    assert_eq!(
        proof.payment_request_id, payment_id,
        "Covenant proof should reference correct payment ID"
    );
    assert!(
        !proof.template_hash.is_empty(),
        "Template hash should not be empty"
    );

    // Verify state
    let state = state_machine
        .get_payment_state(&payment_id)
        .await
        .expect("Failed to get payment state");

    match state {
        PaymentState::ProofCreated { request_id, .. } => {
            assert_eq!(request_id, payment_id);
        }
        _ => panic!("Expected ProofCreated state, got {:?}", state),
    }
}

/// Test payment state transitions
#[tokio::test]
async fn test_payment_state_transitions() {
    let temp_dir = TempDir::new().unwrap();
    let config = PaymentConfig::default();
    let processor =
        Arc::new(PaymentProcessor::new(config).expect("Failed to create payment processor"));
    let state_machine = Arc::new(PaymentStateMachine::new(processor));

    let outputs = create_test_outputs();

    // Create payment request
    let (payment_id, _) = state_machine
        .create_payment_request(outputs, None, false)
        .await
        .expect("Failed to create payment request");

    // Verify initial state
    let state = state_machine
        .get_payment_state(&payment_id)
        .await
        .expect("Failed to get payment state");
    assert!(matches!(state, PaymentState::RequestCreated { .. }));

    // Mark as in mempool
    let tx_hash = [0x42u8; 32];
    state_machine
        .mark_in_mempool(&payment_id, tx_hash)
        .await
        .expect("Failed to mark in mempool");

    let state = state_machine
        .get_payment_state(&payment_id)
        .await
        .expect("Failed to get payment state");
    match state {
        PaymentState::InMempool {
            request_id,
            tx_hash: tx,
        } => {
            assert_eq!(request_id, payment_id);
            assert_eq!(tx, tx_hash);
        }
        _ => panic!("Expected InMempool state, got {:?}", state),
    }

    // Mark as settled
    let block_hash = [0x43u8; 32];
    state_machine
        .mark_settled(&payment_id, tx_hash, block_hash, 1, None)
        .await
        .expect("Failed to mark settled");

    let state = state_machine
        .get_payment_state(&payment_id)
        .await
        .expect("Failed to get payment state");
    match state {
        PaymentState::Settled {
            request_id,
            tx_hash: tx,
            block_hash: block,
            confirmation_count,
            ..
        } => {
            assert_eq!(request_id, payment_id);
            assert_eq!(tx, tx_hash);
            assert_eq!(block, block_hash);
            assert_eq!(confirmation_count, 1);
        }
        _ => panic!("Expected Settled state, got {:?}", state),
    }
}

/// Test payment failure state
#[tokio::test]
async fn test_payment_failure() {
    let temp_dir = TempDir::new().unwrap();
    let config = PaymentConfig::default();
    let processor =
        Arc::new(PaymentProcessor::new(config).expect("Failed to create payment processor"));
    let state_machine = Arc::new(PaymentStateMachine::new(processor));

    let outputs = create_test_outputs();

    // Create payment request
    let (payment_id, _) = state_machine
        .create_payment_request(outputs, None, false)
        .await
        .expect("Failed to create payment request");

    // Mark as failed
    let reason = "Transaction rejected".to_string();
    state_machine
        .mark_failed(&payment_id, reason.clone())
        .await
        .expect("Failed to mark failed");

    let state = state_machine
        .get_payment_state(&payment_id)
        .await
        .expect("Failed to get payment state");
    match state {
        PaymentState::Failed {
            request_id,
            reason: r,
        } => {
            assert_eq!(request_id, payment_id);
            assert_eq!(r, reason);
        }
        _ => panic!("Expected Failed state, got {:?}", state),
    }
}

/// Test listing all payments
#[tokio::test]
async fn test_list_payments() {
    let temp_dir = TempDir::new().unwrap();
    let config = PaymentConfig::default();
    let processor =
        Arc::new(PaymentProcessor::new(config).expect("Failed to create payment processor"));
    let state_machine = Arc::new(PaymentStateMachine::new(processor));

    // Create multiple payment requests
    let mut payment_ids = Vec::new();
    for i in 0..3 {
        let outputs = create_test_outputs_unique(i);
        let (payment_id, _) = state_machine
            .create_payment_request(outputs, None, false)
            .await
            .expect("Failed to create payment request");
        payment_ids.push(payment_id);
    }

    // List all payments
    let states = state_machine.list_payment_states();
    assert!(
        states.len() >= 3,
        "Should have at least 3 payments, got {}",
        states.len()
    );

    // Verify all created payments are in the list
    for payment_id in &payment_ids {
        assert!(
            states.contains_key(payment_id),
            "Payment {} should be in list",
            payment_id
        );
    }
}

/// Test instant proof functionality (CTV covenant)
#[tokio::test]
#[cfg(feature = "ctv")]
async fn test_instant_proof_functionality() {
    let temp_dir = TempDir::new().unwrap();
    let config = PaymentConfig::default();
    let processor =
        Arc::new(PaymentProcessor::new(config).expect("Failed to create payment processor"));
    let state_machine = Arc::new(PaymentStateMachine::new(processor));

    let outputs = create_test_outputs();

    // Create payment request with instant proof (CTV covenant)
    let (payment_id, covenant_proof) = state_machine
        .create_payment_request(outputs.clone(), None, true)
        .await
        .expect("Failed to create payment request with instant proof");

    // Verify instant proof was created
    assert!(
        covenant_proof.is_some(),
        "Instant proof (covenant) should be created immediately"
    );

    let proof = covenant_proof.unwrap();

    // Verify proof structure
    assert_eq!(
        proof.payment_request_id, payment_id,
        "Proof should reference correct payment ID"
    );
    assert!(
        !proof.template_hash.is_empty(),
        "Template hash should be present"
    );
    assert!(
        proof.transaction_template.outputs.len() == outputs.len(),
        "Template should have same number of outputs"
    );

    // Verify state is ProofCreated (instant proof ready)
    let state = state_machine
        .get_payment_state(&payment_id)
        .await
        .expect("Failed to get payment state");

    match &state {
        PaymentState::ProofCreated { request_id, .. } => {
            assert_eq!(request_id, &payment_id);
            // This is the key: instant proof is available, but settlement hasn't happened yet
            // Merchant can verify payment commitment immediately via CTV proof
        }
        _ => panic!(
            "Expected ProofCreated state for instant proof, got {:?}",
            state
        ),
    }

    // Verify that settlement hasn't happened yet (this is the point of instant proof)
    // The payment is NOT in mempool or settled - proof is instant, settlement is delayed
    assert!(
        !matches!(
            &state,
            PaymentState::InMempool { .. } | PaymentState::Settled { .. }
        ),
        "Settlement should not have happened yet - instant proof is separate from settlement"
    );
}

/// Test covenant proof creation for existing payment
#[tokio::test]
#[cfg(feature = "ctv")]
async fn test_create_covenant_proof_for_existing_payment() {
    let temp_dir = TempDir::new().unwrap();
    let config = PaymentConfig::default();
    let processor =
        Arc::new(PaymentProcessor::new(config).expect("Failed to create payment processor"));
    let state_machine = Arc::new(PaymentStateMachine::new(processor));

    let outputs = create_test_outputs();

    // Create payment request without covenant
    let (payment_id, _) = state_machine
        .create_payment_request(outputs, None, false)
        .await
        .expect("Failed to create payment request");

    // Create covenant proof for existing payment
    let covenant_proof = state_machine
        .create_covenant_proof(&payment_id)
        .await
        .expect("Failed to create covenant proof");

    assert_eq!(
        covenant_proof.payment_request_id, payment_id,
        "Covenant proof should reference correct payment ID"
    );

    // Verify state transitioned to ProofCreated
    let state = state_machine
        .get_payment_state(&payment_id)
        .await
        .expect("Failed to get payment state");

    match state {
        PaymentState::ProofCreated { request_id, .. } => {
            assert_eq!(request_id, payment_id);
        }
        _ => panic!("Expected ProofCreated state, got {:?}", state),
    }
}

/// Test settlement monitor initialization
#[tokio::test]
async fn test_settlement_monitor_initialization() {
    let temp_dir = TempDir::new().unwrap();
    let config = PaymentConfig::default();
    let processor =
        Arc::new(PaymentProcessor::new(config).expect("Failed to create payment processor"));
    let state_machine = Arc::new(PaymentStateMachine::new(processor));

    // Create settlement monitor
    let monitor = SettlementMonitor::new(state_machine);

    // Monitor should be created successfully
    // (No assertions needed - just verify it compiles and initializes)
    assert!(true, "Settlement monitor created successfully");
}

/// Test payment state machine with multiple concurrent payments
#[tokio::test]
async fn test_concurrent_payment_requests() {
    let temp_dir = TempDir::new().unwrap();
    let config = PaymentConfig::default();
    let processor =
        Arc::new(PaymentProcessor::new(config).expect("Failed to create payment processor"));
    let state_machine = Arc::new(PaymentStateMachine::new(processor));

    // Create multiple payment requests concurrently
    let handles: Vec<_> = (0..5)
        .map(|i| {
            let state_machine = Arc::clone(&state_machine);
            let outputs = create_test_outputs_unique(i);
            tokio::spawn(async move {
                state_machine
                    .create_payment_request(outputs, None, false)
                    .await
            })
        })
        .collect();

    // Wait for all requests to complete
    let mut payment_ids = Vec::new();
    for handle in handles {
        let result = handle.await.expect("Task should complete successfully");
        let (payment_id, _) = result.expect("Payment request should succeed");
        payment_ids.push(payment_id);
    }

    // Verify all payments were created
    assert_eq!(payment_ids.len(), 5, "Should have 5 payment requests");

    // Verify all payment IDs are unique
    let unique_ids: std::collections::HashSet<String> = payment_ids.iter().cloned().collect();
    assert_eq!(unique_ids.len(), 5, "All payment IDs should be unique");

    // Verify all payments are in the state machine
    let states = state_machine.list_payment_states();
    for payment_id in &payment_ids {
        assert!(
            states.contains_key(payment_id),
            "Payment {} should be in state machine",
            payment_id
        );
    }
}