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
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
//! Mempool RPC Methods
//!
//! Implements mempool-related JSON-RPC methods:
//! - getmempoolinfo
//! - getrawmempool
//! - savemempool

use crate::node::mempool::MempoolManager;
use crate::rpc::errors::RpcResult;
use crate::rpc::params::{param_bool_default, param_str, param_str_required};
use crate::storage::Storage;
use crate::utils::current_timestamp;
use blvm_protocol::Hash;
use serde_json::{json, Value};
use std::sync::Arc;
use tracing::debug;

/// Mempool RPC methods
#[derive(Clone)]
pub struct MempoolRpc {
    mempool: Option<Arc<MempoolManager>>,
    storage: Option<Arc<Storage>>,
}

impl MempoolRpc {
    /// Create a new mempool RPC handler
    pub fn new() -> Self {
        Self {
            mempool: None,
            storage: None,
        }
    }

    /// Create with dependencies
    pub fn with_dependencies(mempool: Arc<MempoolManager>, storage: Arc<Storage>) -> Self {
        Self {
            mempool: Some(mempool),
            storage: Some(storage),
        }
    }

    /// Get mempool information
    ///
    /// Params: []
    pub async fn getmempoolinfo(&self, _params: &Value) -> RpcResult<Value> {
        #[cfg(debug_assertions)]
        debug!("RPC: getmempoolinfo");

        if let Some(ref mempool) = self.mempool {
            let size = mempool.size();

            // This is much faster for large mempools (approximate: avg tx size ~250 bytes)
            let bytes = if size == 0 {
                0
            } else {
                // Fast path: estimate from size (good enough for RPC)
                // For exact calculation, would need to serialize all, but that's expensive
                size * 250 // Approximate average transaction size
            };

            Ok(json!({
                "loaded": true,
                "size": size,
                "bytes": bytes,
                "usage": bytes,
                "maxmempool": 300000000,
                "mempoolminfee": 0.00001000,
                "minrelaytxfee": 0.00001000
            }))
        } else {
            // Graceful degradation: return empty mempool info when mempool unavailable
            tracing::debug!(
                "getmempoolinfo called but mempool not available, returning empty mempool"
            );
            Ok(json!({
                "loaded": false,
                "size": 0,
                "bytes": 0,
                "usage": 0,
                "maxmempool": 300000000,
                "mempoolminfee": 0.00001000,
                "minrelaytxfee": 0.00001000,
                "note": "Mempool not available - returning empty mempool"
            }))
        }
    }

    /// Get all transaction IDs in mempool
    ///
    /// Params: [verbose (optional, default: false)]
    pub async fn getrawmempool(&self, params: &Value) -> RpcResult<Value> {
        #[cfg(debug_assertions)]
        debug!("RPC: getrawmempool");

        let verbose = param_bool_default(params, 0, false);

        if let Some(ref mempool) = self.mempool {
            let transactions = mempool.get_transactions();
            use blvm_protocol::block::calculate_tx_id;
            use blvm_protocol::serialization::transaction::serialize_transaction;

            if verbose {
                let mut result = serde_json::Map::new();

                let utxo_set = if let (Some(_mempool), Some(storage)) =
                    (self.mempool.as_ref(), self.storage.as_ref())
                {
                    Some(storage.utxos().get_all_utxos().unwrap_or_default())
                } else {
                    None
                };

                for tx in transactions {
                    let txid = calculate_tx_id(&tx);
                    let txid_hex = hex::encode(txid);
                    let txid_hex_clone = txid_hex.clone();
                    let size = serialize_transaction(&tx).len();

                    result.insert(txid_hex, json!({
                        "size": size,
                        "fee": if let (Some(mempool), Some(utxo_set)) = (self.mempool.as_ref(), utxo_set.as_ref()) {
                            let fee_satoshis = mempool.calculate_transaction_fee(&tx, utxo_set);
                            fee_satoshis as f64 / 100_000_000.0
                        } else {
                            0.00001000
                        },
                        "modifiedfee": 0.00001000,
                        "time": current_timestamp(),
                        "height": -1,
                        "descendantcount": 1,
                        "descendantsize": size,
                        "descendantfees": 0.00001000,
                        "ancestorcount": 1,
                        "ancestorsize": size,
                        "ancestorfees": 0.00001000,
                        "wtxid": txid_hex_clone,
                        "fees": {
                            "base": 0.00001000,
                            "modified": 0.00001000,
                            "ancestor": 0.00001000,
                            "descendant": 0.00001000
                        },
                        "depends": [],
                        "spentby": [],
                        "bip125-replaceable": false
                    }));
                }
                Ok(json!(result))
            } else {
                let txids: Vec<String> = transactions
                    .iter()
                    .map(|tx| {
                        let txid = calculate_tx_id(tx);
                        hex::encode(txid)
                    })
                    .collect();
                Ok(json!(txids))
            }
        } else if verbose {
            Ok(json!({
                "0000000000000000000000000000000000000000000000000000000000000000": {
                    "size": 250,
                    "fee": 0.00001000,
                    "modifiedfee": 0.00001000,
                    "time": 1231006505,
                    "height": 0,
                    "descendantcount": 1,
                    "descendantsize": 250,
                    "descendantfees": 0.00001000,
                    "ancestorcount": 1,
                    "ancestorsize": 250,
                    "ancestorfees": 0.00001000,
                    "wtxid": "0000000000000000000000000000000000000000000000000000000000000000",
                    "fees": {
                        "base": 0.00001000,
                        "modified": 0.00001000,
                        "ancestor": 0.00001000,
                        "descendant": 0.00001000
                    },
                    "depends": [],
                    "spentby": [],
                    "bip125-replaceable": false
                }
            }))
        } else {
            Ok(json!([]))
        }
    }

    /// Save mempool to disk (for node restart persistence)
    ///
    /// Params: []
    pub async fn savemempool(&self, _params: &Value) -> RpcResult<Value> {
        debug!("RPC: savemempool");

        if let Some(mempool) = &self.mempool {
            use crate::utils::env_or_default;
            let data_dir = env_or_default("DATA_DIR", "data");
            let mempool_path = std::path::Path::new(&data_dir).join("mempool.dat");

            // Arc implements Deref, so we can call methods directly
            if let Err(e) = mempool.save_to_disk(&mempool_path) {
                return Err(crate::rpc::errors::RpcError::internal_error(format!(
                    "Failed to save mempool: {e}"
                )));
            }

            Ok(Value::Null)
        } else {
            Err(crate::rpc::errors::RpcError::internal_error(
                "Mempool not initialized".to_string(),
            ))
        }
    }

    /// Get mempool ancestors for a transaction
    ///
    /// Params: ["txid", verbose (optional, default: false)]
    pub async fn getmempoolancestors(&self, params: &Value) -> RpcResult<Value> {
        debug!("RPC: getmempoolancestors");

        let txid = param_str_required(params, 0, "getmempoolancestors")?;

        let verbose = param_bool_default(params, 1, false);

        let hash_bytes = hex::decode(&txid).map_err(|e| {
            crate::rpc::errors::RpcError::invalid_hash_format(
                &txid,
                Some(32),
                Some(&format!("Invalid hex encoding: {e}")),
            )
        })?;
        if hash_bytes.len() != 32 {
            return Err(crate::rpc::errors::RpcError::invalid_params(
                "Transaction ID must be 32 bytes".to_string(),
            ));
        }
        let mut hash = [0u8; 32];
        hash.copy_from_slice(&hash_bytes);

        if let Some(ref mempool) = self.mempool {
            // Find ancestors: transactions that this transaction depends on (spends their outputs)
            let ancestors = self.get_ancestors(mempool, &hash);

            if verbose {
                // Return detailed ancestor information
                let mut result = serde_json::Map::new();
                for ancestor_hash in ancestors {
                    if let Some(ancestor_tx) = mempool.get_transaction(&ancestor_hash) {
                        let ancestor_txid = hex::encode(ancestor_hash);
                        let ancestor_txid_clone = ancestor_txid.clone();
                        use blvm_protocol::serialization::transaction::serialize_transaction;
                        let size = serialize_transaction(&ancestor_tx).len();

                        result.insert(ancestor_txid, json!({
                            "size": size,
                            "fee": if let Some(ref storage) = self.storage {
                                let utxo_set = storage.utxos().get_all_utxos().unwrap_or_default();
                                let fee_satoshis = mempool.calculate_transaction_fee(&ancestor_tx, &utxo_set);
                                fee_satoshis as f64 / 100_000_000.0
                            } else {
                                0.0
                            },
                            "modifiedfee": 0.0,
                            "time": current_timestamp(),
                            "height": -1,
                            "descendantcount": 1,
                            "descendantsize": size,
                            "descendantfees": 0.0,
                            "ancestorcount": 1,
                            "ancestorsize": size,
                            "ancestorfees": 0.0,
                            "wtxid": ancestor_txid_clone,
                            "fees": {
                                "base": 0.0,
                                "modified": 0.0,
                                "ancestor": 0.0,
                                "descendant": 0.0
                            },
                            "depends": [],
                            "spentby": [],
                            "bip125-replaceable": false
                        }));
                    }
                }
                Ok(json!(result))
            } else {
                // Return just transaction IDs
                let txids: Vec<String> = ancestors.iter().map(hex::encode).collect();
                Ok(json!(txids))
            }
        } else if verbose {
            Ok(json!({}))
        } else {
            Ok(json!([]))
        }
    }

    /// Get mempool descendants for a transaction
    ///
    /// Params: ["txid", verbose (optional, default: false)]
    pub async fn getmempooldescendants(&self, params: &Value) -> RpcResult<Value> {
        debug!("RPC: getmempooldescendants");

        let txid = param_str_required(params, 0, "getmempooldescendants")?;

        let verbose = param_bool_default(params, 1, false);

        let hash_bytes = hex::decode(&txid).map_err(|e| {
            crate::rpc::errors::RpcError::invalid_hash_format(
                &txid,
                Some(32),
                Some(&format!("Invalid hex encoding: {e}")),
            )
        })?;
        if hash_bytes.len() != 32 {
            return Err(crate::rpc::errors::RpcError::invalid_params(
                "Transaction ID must be 32 bytes".to_string(),
            ));
        }
        let mut hash = [0u8; 32];
        hash.copy_from_slice(&hash_bytes);

        if let Some(ref mempool) = self.mempool {
            // Find descendants by checking which transactions spend outputs created by this transaction which transactions spend outputs created by this transaction which transactions spend outputs created by this transaction
            let mut descendants = Vec::new();

            if let Some(tx) = mempool.get_transaction(&hash) {
                // Get all output outpoints from this transaction
                let mut output_outpoints = Vec::new();
                for (idx, _output) in tx.outputs.iter().enumerate() {
                    output_outpoints.push(blvm_protocol::OutPoint {
                        hash,
                        index: idx as u32,
                    });
                }

                // Find transactions that spend these outputs
                use blvm_protocol::block::calculate_tx_id;
                let transactions = mempool.get_transactions();
                for descendant_tx in transactions {
                    let descendant_hash = calculate_tx_id(&descendant_tx);
                    for input in &descendant_tx.inputs {
                        if output_outpoints.contains(&input.prevout) {
                            descendants.push(descendant_hash);
                            break;
                        }
                    }
                }
            }

            if verbose {
                // Return detailed descendant information
                let mut result = serde_json::Map::new();
                for descendant_hash in descendants {
                    if let Some(descendant_tx) = mempool.get_transaction(&descendant_hash) {
                        let descendant_txid = hex::encode(descendant_hash);
                        let descendant_txid_clone = descendant_txid.clone();
                        use blvm_protocol::serialization::transaction::serialize_transaction;
                        let size = serialize_transaction(&descendant_tx).len();

                        result.insert(descendant_txid, json!({
                            "size": size,
                            "fee": if let Some(ref storage) = self.storage {
                                let utxo_set = storage.utxos().get_all_utxos().unwrap_or_default();
                                let fee_satoshis = mempool.calculate_transaction_fee(&descendant_tx, &utxo_set);
                                fee_satoshis as f64 / 100_000_000.0
                            } else {
                                0.0
                            },
                            "modifiedfee": 0.0,
                            "time": current_timestamp(),
                            "height": -1,
                            "descendantcount": 1,
                            "descendantsize": size,
                            "descendantfees": 0.0,
                            "ancestorcount": 1,
                            "ancestorsize": size,
                            "ancestorfees": 0.0,
                            "wtxid": descendant_txid_clone,
                            "fees": {
                                "base": 0.0,
                                "modified": 0.0,
                                "ancestor": 0.0,
                                "descendant": 0.0
                            },
                            "depends": [],
                            "spentby": [],
                            "bip125-replaceable": false
                        }));
                    }
                }
                Ok(json!(result))
            } else {
                // Return just transaction IDs
                let txids: Vec<String> = descendants.iter().map(hex::encode).collect();
                Ok(json!(txids))
            }
        } else if verbose {
            Ok(json!({}))
        } else {
            Ok(json!([]))
        }
    }

    /// Get specific mempool entry
    ///
    /// Params: ["txid"]
    pub async fn getmempoolentry(&self, params: &Value) -> RpcResult<Value> {
        debug!("RPC: getmempoolentry");

        let txid = param_str_required(params, 0, "getmempoolentry")?;

        let hash_bytes = hex::decode(&txid).map_err(|e| {
            crate::rpc::errors::RpcError::invalid_hash_format(
                &txid,
                Some(32),
                Some(&format!("Invalid hex encoding: {e}")),
            )
        })?;
        if hash_bytes.len() != 32 {
            return Err(crate::rpc::errors::RpcError::invalid_params(
                "Transaction ID must be 32 bytes".to_string(),
            ));
        }
        let mut hash = [0u8; 32];
        hash.copy_from_slice(&hash_bytes);

        if let Some(ref mempool) = self.mempool {
            if let Some(tx) = mempool.get_transaction(&hash) {
                use blvm_protocol::serialization::transaction::serialize_transaction;
                let size = serialize_transaction(&tx).len();

                // Get ancestors and descendants
                let ancestors = self.get_ancestors(mempool, &hash);
                let descendants = self.get_descendants(mempool, &hash);

                let ancestor_count = ancestors.len();
                let descendant_count = descendants.len();
                let ancestor_size: usize = ancestors
                    .iter()
                    .filter_map(|h| mempool.get_transaction(h))
                    .map(|tx| serialize_transaction(&tx).len())
                    .sum();
                let descendant_size: usize = descendants
                    .iter()
                    .filter_map(|h| mempool.get_transaction(h))
                    .map(|tx| serialize_transaction(&tx).len())
                    .sum();

                let fee = if let Some(ref storage) = self.storage {
                    let utxo_set = storage.utxos().get_all_utxos().unwrap_or_default();
                    let fee_satoshis = mempool.calculate_transaction_fee(&tx, &utxo_set);
                    fee_satoshis as f64 / 100_000_000.0
                } else {
                    0.0
                };

                Ok(json!({
                    "size": size,
                    "fee": fee,
                    "modifiedfee": fee,
                    "time": current_timestamp(),
                    "height": -1,
                    "descendantcount": descendant_count + 1,
                    "descendantsize": descendant_size + size,
                    "descendantfees": fee, // Simplified
                    "ancestorcount": ancestor_count + 1,
                    "ancestorsize": ancestor_size + size,
                    "ancestorfees": fee, // Simplified
                    "wtxid": txid,
                    "fees": {
                        "base": fee,
                        "modified": fee,
                        "ancestor": fee,
                        "descendant": fee
                    },
                    "depends": ancestors.iter().map(hex::encode).collect::<Vec<_>>(),
                    "spentby": descendants.iter().map(hex::encode).collect::<Vec<_>>(),
                    "bip125-replaceable": false
                }))
            } else {
                Err(crate::rpc::errors::RpcError::invalid_params(format!(
                    "Transaction {txid} not found in mempool"
                )))
            }
        } else {
            Err(crate::rpc::errors::RpcError::internal_error(
                "Mempool not initialized".to_string(),
            ))
        }
    }

    /// Helper: Get ancestors for a transaction
    fn get_ancestors(&self, mempool: &MempoolManager, tx_hash: &Hash) -> Vec<Hash> {
        let mut ancestors = Vec::new();

        if let Some(tx) = mempool.get_transaction(tx_hash) {
            // Find transactions that this transaction depends on (spends their outputs)
            use blvm_protocol::block::calculate_tx_id;
            for input in &tx.inputs {
                // Find transaction that created this output by checking all transactions
                let transactions = mempool.get_transactions();
                for ancestor_tx in transactions {
                    let ancestor_hash = calculate_tx_id(&ancestor_tx);
                    for (idx, _output) in ancestor_tx.outputs.iter().enumerate() {
                        if input.prevout.hash == ancestor_hash
                            && input.prevout.index == idx as u32
                            && !ancestors.contains(&ancestor_hash)
                        {
                            ancestors.push(ancestor_hash);
                        }
                    }
                }
            }
        }

        ancestors
    }

    /// Helper: Get descendants for a transaction
    fn get_descendants(&self, mempool: &MempoolManager, tx_hash: &Hash) -> Vec<Hash> {
        let mut descendants = Vec::new();

        if let Some(tx) = mempool.get_transaction(tx_hash) {
            // Get all output outpoints from this transaction
            let mut output_outpoints = Vec::new();
            for (idx, _output) in tx.outputs.iter().enumerate() {
                output_outpoints.push(blvm_protocol::OutPoint {
                    hash: *tx_hash,
                    index: idx as u32,
                });
            }

            // Find transactions that spend these outputs
            use blvm_protocol::block::calculate_tx_id;
            let transactions = mempool.get_transactions();
            for descendant_tx in transactions {
                let descendant_hash = calculate_tx_id(&descendant_tx);
                for input in &descendant_tx.inputs {
                    if output_outpoints.contains(&input.prevout) {
                        if !descendants.contains(&descendant_hash) {
                            descendants.push(descendant_hash);
                        }
                        break;
                    }
                }
            }
        }

        descendants
    }
}

impl Default for MempoolRpc {
    fn default() -> Self {
        Self::new()
    }
}