blvm-node 0.1.47

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
//! Settlement Monitoring and Verification
//!
//! Monitors payment transactions from mempool through on-chain confirmation.
//! Tracks UTXO state, mempool transactions, and block confirmations to update
//! payment state machine.

use crate::node::mempool::MempoolManager;
use crate::payment::processor::PaymentError;
use crate::payment::state_machine::PaymentStateMachine;
use crate::storage::Storage;
use crate::{Hash, Transaction};
use blvm_protocol::payment::PaymentOutput;
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;
use tokio::time::sleep;
use tracing::{debug, info, warn};

/// Settlement monitor for payment transactions
pub struct SettlementMonitor {
    /// Payment state machine to update
    state_machine: Arc<PaymentStateMachine>,
    /// Mempool manager for transaction monitoring
    mempool_manager: Option<Arc<MempoolManager>>,
    /// Storage for blockchain access
    storage: Option<Arc<Storage>>,
    /// Tx cache for re-broadcast on reorg (optional)
    #[cfg(feature = "ctv")]
    tx_cache: Option<Arc<crate::payment::PaymentTxCache>>,
    /// Track payment outputs we're monitoring (payment_id -> outputs)
    monitored_payments: Arc<tokio::sync::RwLock<HashMap<String, Vec<PaymentOutput>>>>,
    /// Track expected transaction hashes (payment_id -> tx_hash)
    expected_transactions: Arc<tokio::sync::RwLock<HashMap<String, Hash>>>,
}

impl SettlementMonitor {
    /// Create a new settlement monitor
    pub fn new(state_machine: Arc<PaymentStateMachine>) -> Self {
        Self {
            state_machine,
            mempool_manager: None,
            storage: None,
            #[cfg(feature = "ctv")]
            tx_cache: None,
            monitored_payments: Arc::new(tokio::sync::RwLock::new(HashMap::new())),
            expected_transactions: Arc::new(tokio::sync::RwLock::new(HashMap::new())),
        }
    }

    /// Set mempool manager for transaction monitoring
    pub fn with_mempool_manager(mut self, mempool_manager: Arc<MempoolManager>) -> Self {
        self.mempool_manager = Some(mempool_manager);
        self
    }

    /// Set storage for blockchain access
    pub fn with_storage(mut self, storage: Arc<Storage>) -> Self {
        self.storage = Some(storage);
        self
    }

    /// Set tx cache for re-broadcast on reorg
    #[cfg(feature = "ctv")]
    pub fn with_tx_cache(mut self, tx_cache: Arc<crate::payment::PaymentTxCache>) -> Self {
        self.tx_cache = Some(tx_cache);
        self
    }

    /// Start monitoring a payment for settlement
    ///
    /// # Arguments
    ///
    /// * `payment_id` - Payment request ID to monitor
    /// * `expected_outputs` - Payment outputs to match against transactions
    /// * `expected_tx_hash` - Optional expected transaction hash (if known)
    pub async fn start_monitoring(
        &self,
        payment_id: &str,
        expected_outputs: Vec<PaymentOutput>,
        expected_tx_hash: Option<Hash>,
    ) -> Result<(), PaymentError> {
        debug!("Starting settlement monitoring for payment: {}", payment_id);

        // Store monitored outputs
        {
            let mut monitored = self.monitored_payments.write().await;
            monitored.insert(payment_id.to_string(), expected_outputs);
        }

        // Store expected transaction hash if provided
        if let Some(tx_hash) = expected_tx_hash {
            let mut expected = self.expected_transactions.write().await;
            expected.insert(payment_id.to_string(), tx_hash);
        }

        // Start background monitoring task
        let monitor = self.clone_for_monitoring();
        let payment_id_clone = payment_id.to_string();
        tokio::spawn(async move {
            monitor.monitor_payment_settlement(&payment_id_clone).await;
        });

        Ok(())
    }

    /// Stop monitoring a payment
    pub async fn stop_monitoring(&self, payment_id: &str) {
        let mut monitored = self.monitored_payments.write().await;
        monitored.remove(payment_id);
        let mut expected = self.expected_transactions.write().await;
        expected.remove(payment_id);
    }

    /// Monitor payment settlement (background task)
    async fn monitor_payment_settlement(&self, payment_id: &str) {
        info!("Monitoring settlement for payment: {}", payment_id);

        // Get expected outputs
        let expected_outputs = {
            let monitored = self.monitored_payments.read().await;
            monitored.get(payment_id).cloned()
        };

        if expected_outputs.is_none() {
            warn!("No expected outputs found for payment: {}", payment_id);
            return;
        }

        let expected_outputs = expected_outputs.unwrap();
        let expected_tx_hash = {
            let expected = self.expected_transactions.read().await;
            expected.get(payment_id).copied()
        };

        // Monitor for up to 24 hours (86400 seconds)
        let max_duration = Duration::from_secs(86400);
        let start_time = std::time::Instant::now();
        let check_interval = Duration::from_secs(10); // Check every 10 seconds

        loop {
            // Check timeout
            if start_time.elapsed() > max_duration {
                warn!("Settlement monitoring timeout for payment: {}", payment_id);
                let _ = self
                    .state_machine
                    .mark_failed(
                        payment_id,
                        "Settlement monitoring timeout (24 hours)".to_string(),
                    )
                    .await;
                break;
            }

            // Check mempool first
            if let Some(mempool_manager) = &self.mempool_manager {
                if let Ok(tx_hash) = self
                    .check_mempool(
                        mempool_manager,
                        payment_id,
                        &expected_outputs,
                        expected_tx_hash,
                    )
                    .await
                {
                    // Transaction found in mempool
                    let _ = self
                        .state_machine
                        .mark_in_mempool(payment_id, tx_hash)
                        .await;
                }
            }

            // Check blockchain for confirmation
            if let Some(storage) = &self.storage {
                if let Ok(Some((tx_hash, block_hash, confirmations))) = self
                    .check_blockchain(storage, payment_id, &expected_outputs, expected_tx_hash)
                    .await
                {
                    // Transaction confirmed
                    let _ = self
                        .state_machine
                        .mark_settled(
                            payment_id,
                            tx_hash,
                            block_hash,
                            confirmations,
                            Some(expected_outputs.to_vec()),
                        )
                        .await;
                    break; // Stop monitoring once settled
                }
            }

            // Wait before next check
            sleep(check_interval).await;
        }

        // Clean up monitoring
        self.stop_monitoring(payment_id).await;
    }

    /// Check mempool for matching transaction
    async fn check_mempool(
        &self,
        mempool_manager: &MempoolManager,
        payment_id: &str,
        expected_outputs: &[PaymentOutput],
        expected_tx_hash: Option<Hash>,
    ) -> Result<Hash, PaymentError> {
        // If we have an expected transaction hash, check for it directly
        if let Some(expected_hash) = expected_tx_hash {
            if let Some(tx) = mempool_manager.get_transaction(&expected_hash) {
                #[cfg(feature = "ctv")]
                if let Some(ref cache) = self.tx_cache {
                    cache.store(expected_hash, tx.clone());
                }
                return Ok(expected_hash);
            }
        }

        // Otherwise, search for transactions matching expected outputs
        let transactions = mempool_manager.get_transactions();
        for tx in transactions {
            if self.transaction_matches_outputs(&tx, expected_outputs) {
                let tx_hash = crate::network::txhash::calculate_txid(&tx);
                #[cfg(feature = "ctv")]
                if let Some(ref cache) = self.tx_cache {
                    cache.store(tx_hash, tx.clone());
                }
                debug!(
                    "Found matching transaction in mempool for payment {}: {}",
                    payment_id,
                    hex::encode(tx_hash)
                );
                return Ok(tx_hash);
            }
        }

        Err(PaymentError::ProcessingError(
            "Transaction not found in mempool".to_string(),
        ))
    }

    /// Check blockchain for confirmed transaction
    async fn check_blockchain(
        &self,
        storage: &Storage,
        payment_id: &str,
        expected_outputs: &[PaymentOutput],
        expected_tx_hash: Option<Hash>,
    ) -> Result<Option<(Hash, Hash, u32)>, PaymentError> {
        // If we have an expected transaction hash, check for it directly
        if let Some(expected_hash) = expected_tx_hash {
            if let Some(found) = self.lookup_confirmed_transaction(
                storage,
                payment_id,
                expected_hash,
                expected_outputs,
            )? {
                return Ok(Some(found));
            }
        }

        // Search the tx index by expected output scripts when hash is unknown or lookup failed.
        self.find_confirmed_by_outputs(storage, payment_id, expected_outputs)
    }

    fn lookup_confirmed_transaction(
        &self,
        storage: &Storage,
        payment_id: &str,
        tx_hash: Hash,
        expected_outputs: &[PaymentOutput],
    ) -> Result<Option<(Hash, Hash, u32)>, PaymentError> {
        let tx_index = storage.transactions();
        let tx = match tx_index.get_transaction(&tx_hash) {
            Ok(Some(tx)) => tx,
            Ok(None) => return Ok(None),
            Err(e) => {
                return Err(PaymentError::ProcessingError(format!(
                    "Failed to load transaction {}: {e}",
                    hex::encode(tx_hash)
                )));
            }
        };

        if !self.transaction_matches_outputs(&tx, expected_outputs) {
            return Ok(None);
        }

        #[cfg(feature = "ctv")]
        if let Some(ref cache) = self.tx_cache {
            cache.store(tx_hash, tx);
        }

        let Some(metadata) = tx_index.get_metadata(&tx_hash).map_err(|e| {
            PaymentError::ProcessingError(format!(
                "Failed to load metadata for {}: {e}",
                hex::encode(tx_hash)
            ))
        })?
        else {
            return Ok(None);
        };

        let tip_height = storage
            .chain()
            .get_height()
            .map_err(|e| PaymentError::ProcessingError(format!("Failed to get tip height: {e}")))?
            .unwrap_or(0);
        let confirmations = (tip_height.saturating_sub(metadata.block_height) + 1) as u32;

        debug!(
            "Found confirmed transaction for payment {}: {} (block: {}, confirmations: {})",
            payment_id,
            hex::encode(tx_hash),
            hex::encode(metadata.block_hash),
            confirmations
        );

        Ok(Some((tx_hash, metadata.block_hash, confirmations)))
    }

    fn find_confirmed_by_outputs(
        &self,
        storage: &Storage,
        payment_id: &str,
        expected_outputs: &[PaymentOutput],
    ) -> Result<Option<(Hash, Hash, u32)>, PaymentError> {
        if expected_outputs.is_empty() {
            return Ok(None);
        }

        use std::collections::HashSet;

        let tx_index = storage.transactions();
        let mut seen: HashSet<Hash> = HashSet::new();

        for output in expected_outputs {
            if output.script.is_empty() {
                continue;
            }
            let candidates = tx_index
                .get_transactions_by_address(&output.script)
                .map_err(|e| {
                    PaymentError::ProcessingError(format!(
                        "Failed to query transactions for payment output script: {e}"
                    ))
                })?;
            for tx in candidates {
                let tx_hash = blvm_protocol::block::calculate_tx_id(&tx);
                if !seen.insert(tx_hash) {
                    continue;
                }
                if let Some(found) = self.lookup_confirmed_transaction(
                    storage,
                    payment_id,
                    tx_hash,
                    expected_outputs,
                )? {
                    return Ok(Some(found));
                }
            }
        }

        Ok(None)
    }

    /// Check if transaction matches expected payment outputs
    fn transaction_matches_outputs(
        &self,
        tx: &Transaction,
        expected_outputs: &[PaymentOutput],
    ) -> bool {
        // Convert expected outputs to transaction outputs for comparison
        use blvm_protocol::types::{ByteString, Integer, TransactionOutput};
        let expected_tx_outputs: Vec<TransactionOutput> = expected_outputs
            .iter()
            .filter_map(|po| {
                // PaymentOutput.amount might be Option<u64> or u64, handle both
                let amount = po.amount?;
                Some(TransactionOutput {
                    value: Integer::from(amount as i64),
                    script_pubkey: ByteString::from(po.script.clone()),
                })
            })
            .collect();

        // Check if transaction has matching outputs
        if tx.outputs.len() < expected_tx_outputs.len() {
            return false;
        }

        // Check if all expected outputs are present (order-independent)
        for expected_output in &expected_tx_outputs {
            let mut found = false;
            for tx_output in &tx.outputs {
                if tx_output.value == expected_output.value
                    && tx_output.script_pubkey == expected_output.script_pubkey
                {
                    found = true;
                    break;
                }
            }
            if !found {
                return false;
            }
        }

        true
    }

    /// Clone for background monitoring task
    fn clone_for_monitoring(&self) -> Self {
        Self {
            state_machine: Arc::clone(&self.state_machine),
            mempool_manager: self.mempool_manager.as_ref().map(Arc::clone),
            storage: self.storage.as_ref().map(Arc::clone),
            #[cfg(feature = "ctv")]
            tx_cache: self.tx_cache.as_ref().map(Arc::clone),
            monitored_payments: Arc::clone(&self.monitored_payments),
            expected_transactions: Arc::clone(&self.expected_transactions),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::PaymentConfig;
    use crate::config::{IndexingConfig, IndexingStrategy};
    use crate::payment::processor::PaymentProcessor;
    use crate::storage::database::DatabaseBackend;
    use blvm_protocol::{OutPoint, TransactionInput, TransactionOutput};
    use std::sync::Arc;
    use tempfile::TempDir;

    fn sample_payment_tx(script: Vec<u8>, amount: u64) -> Transaction {
        Transaction {
            version: 2,
            inputs: vec![TransactionInput {
                prevout: OutPoint {
                    hash: [1u8; 32],
                    index: 0,
                },
                script_sig: vec![],
                sequence: 0xffffffff,
            }]
            .into(),
            outputs: vec![TransactionOutput {
                value: amount as i64,
                script_pubkey: script,
            }]
            .into(),
            lock_time: 0,
        }
    }

    #[test]
    fn transaction_matches_outputs_requires_all_scripts_and_amounts() {
        let processor =
            Arc::new(PaymentProcessor::new(PaymentConfig::default()).expect("payment processor"));
        let monitor = SettlementMonitor::new(Arc::new(PaymentStateMachine::new(processor)));

        let script_a = vec![0x51, 0x87];
        let script_b = vec![0x52, 0x88];
        let tx = Transaction {
            version: 2,
            inputs: blvm_protocol::tx_inputs![],
            outputs: blvm_protocol::tx_outputs![
                TransactionOutput {
                    value: 10_000,
                    script_pubkey: script_a.clone(),
                },
                TransactionOutput {
                    value: 20_000,
                    script_pubkey: script_b.clone(),
                },
            ],
            lock_time: 0,
        };

        let expected = vec![
            PaymentOutput {
                script: script_a,
                amount: Some(10_000),
            },
            PaymentOutput {
                script: script_b,
                amount: Some(20_000),
            },
        ];

        assert!(monitor.transaction_matches_outputs(&tx, &expected));
        assert!(!monitor.transaction_matches_outputs(
            &tx,
            &[PaymentOutput {
                script: vec![0x53],
                amount: Some(10_000),
            }]
        ));
    }

    #[test]
    fn find_confirmed_by_outputs_matches_indexed_transaction() {
        let dir = TempDir::new().unwrap();
        let storage = Arc::new(
            Storage::with_backend_pruning_and_indexing(
                dir.path(),
                DatabaseBackend::Heed3,
                None,
                Some(IndexingConfig {
                    enable_address_index: true,
                    strategy: IndexingStrategy::Eager,
                    ..Default::default()
                }),
                None,
                None,
                None,
            )
            .expect("storage"),
        );

        let script = vec![0x51, 0x87];
        let tx = sample_payment_tx(script.clone(), 50_000);
        let block_hash = [0x42u8; 32];
        storage
            .transactions()
            .index_transaction(&tx, &block_hash, 5, 0)
            .expect("index tx");

        let processor =
            Arc::new(PaymentProcessor::new(PaymentConfig::default()).expect("payment processor"));
        let monitor = SettlementMonitor::new(Arc::new(PaymentStateMachine::new(processor)))
            .with_storage(storage.clone());

        let outputs = vec![PaymentOutput {
            script,
            amount: Some(50_000),
        }];
        let found = monitor
            .find_confirmed_by_outputs(&storage, "payment-index", &outputs)
            .expect("lookup")
            .expect("confirmed tx");

        let expected_hash = blvm_protocol::block::calculate_tx_id(&tx);
        assert_eq!(found.0, expected_hash);
        assert_eq!(found.1, block_hash);
        assert!(found.2 >= 1);
    }
}