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
//! 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 Ok(Some(tx)) = storage.transactions().get_transaction(&expected_hash) {
#[cfg(feature = "ctv")]
if let Some(ref cache) = self.tx_cache {
cache.store(expected_hash, tx.clone());
}
// Get transaction metadata
if let Ok(Some(metadata)) = storage.transactions().get_metadata(&expected_hash) {
let block_hash = metadata.block_hash;
let block_height = storage
.blocks()
.get_height_by_hash(&block_hash)
.map_err(|e| {
PaymentError::ProcessingError(format!(
"Failed to get block height: {e}"
))
})?
.ok_or_else(|| {
PaymentError::ProcessingError("Block height not found".to_string())
})?;
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(block_height) + 1) as u32;
debug!(
"Found confirmed transaction for payment {}: {} (block: {}, confirmations: {})",
payment_id,
hex::encode(expected_hash),
hex::encode(block_hash),
confirmations
);
return Ok(Some((expected_hash, block_hash, confirmations)));
}
}
}
// Otherwise, search for transactions matching expected outputs
// This is more expensive, so we only do it if we don't have an expected hash
// In practice, we'd maintain an index of payment outputs -> transactions
// For now, we'll rely on the expected transaction hash
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),
}
}
}