blvm-node 0.1.7

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
//! Full `NodeAPI` implementation for integration tests (module hub, IPC, etc.).

use async_trait::async_trait;
use std::collections::HashMap;
use std::sync::Arc;

use blvm_node::module::inter_module::api::ModuleAPI;
use blvm_node::module::ipc::protocol::{EventPayload, FileMetadata, ModuleMessage};
use blvm_node::module::metrics::manager::Metric;
use blvm_node::module::process::monitor::ModuleHealth;
use blvm_node::module::timers::manager::{TaskCallback, TaskId, TimerCallback, TimerId};
use blvm_node::module::traits::{
    BlockServeDenylistSnapshot, ChainInfo, EventType, LightningInfo, MempoolSize, ModuleError,
    ModuleInfo, NetworkStats, NodeAPI, PaymentState, PeerInfo, SubmitBlockResult, SyncStatus,
    TxServeDenylistSnapshot,
};
use blvm_node::{Block, BlockHeader, Hash, OutPoint, Transaction, UTXO};

pub struct MockNodeAPI;

#[async_trait]
impl NodeAPI for MockNodeAPI {
    async fn get_block(&self, _hash: &Hash) -> Result<Option<Block>, ModuleError> {
        Ok(Some(Block {
            header: BlockHeader {
                version: 1,
                prev_block_hash: Hash::default(),
                merkle_root: Hash::default(),
                timestamp: 0,
                bits: 0,
                nonce: 0,
            },
            transactions: vec![].into(),
        }))
    }

    async fn get_block_header(&self, _hash: &Hash) -> Result<Option<BlockHeader>, ModuleError> {
        Ok(Some(BlockHeader {
            version: 1,
            prev_block_hash: Hash::default(),
            merkle_root: Hash::default(),
            timestamp: 0,
            bits: 0,
            nonce: 0,
        }))
    }

    async fn get_transaction(&self, _hash: &Hash) -> Result<Option<Transaction>, ModuleError> {
        use blvm_protocol::Natural;
        Ok(Some(Transaction {
            version: Natural::from(1u64),
            inputs: blvm_protocol::tx_inputs![],
            outputs: blvm_protocol::tx_outputs![],
            lock_time: Natural::from(0u64),
        }))
    }

    async fn has_transaction(&self, _hash: &Hash) -> Result<bool, ModuleError> {
        Ok(true)
    }

    async fn get_chain_tip(&self) -> Result<Hash, ModuleError> {
        Ok(Hash::default())
    }

    async fn get_block_height(&self) -> Result<u64, ModuleError> {
        Ok(100)
    }

    async fn get_utxo(&self, _outpoint: &OutPoint) -> Result<Option<UTXO>, ModuleError> {
        Ok(Some(UTXO {
            value: 1000,
            script_pubkey: vec![].into(),
            height: 100,
            is_coinbase: false,
        }))
    }

    async fn subscribe_events(
        &self,
        _event_types: Vec<EventType>,
    ) -> Result<tokio::sync::mpsc::Receiver<ModuleMessage>, ModuleError> {
        let (_tx, rx) = tokio::sync::mpsc::channel(10);
        Ok(rx)
    }

    async fn get_mempool_transactions(&self) -> Result<Vec<Hash>, ModuleError> {
        Ok(vec![])
    }

    async fn get_mempool_transaction(
        &self,
        _tx_hash: &Hash,
    ) -> Result<Option<Transaction>, ModuleError> {
        Ok(None)
    }

    async fn get_mempool_size(&self) -> Result<MempoolSize, ModuleError> {
        Ok(MempoolSize {
            transaction_count: 0,
            size_bytes: 0,
            total_fee_sats: 0,
        })
    }

    async fn get_network_stats(&self) -> Result<NetworkStats, ModuleError> {
        Ok(NetworkStats {
            peer_count: 0,
            hash_rate: 0.0,
            bytes_sent: 0,
            bytes_received: 0,
        })
    }

    async fn get_network_peers(&self) -> Result<Vec<PeerInfo>, ModuleError> {
        Ok(vec![])
    }

    async fn get_chain_info(&self) -> Result<ChainInfo, ModuleError> {
        Ok(ChainInfo {
            tip_hash: Hash::default(),
            height: 0,
            difficulty: 0,
            chain_work: 0,
            is_synced: false,
        })
    }

    async fn get_block_by_height(&self, _height: u64) -> Result<Option<Block>, ModuleError> {
        Ok(None)
    }

    async fn get_lightning_node_url(&self) -> Result<Option<String>, ModuleError> {
        Ok(None)
    }

    async fn get_lightning_info(&self) -> Result<Option<LightningInfo>, ModuleError> {
        Ok(None)
    }

    async fn get_payment_state(
        &self,
        _payment_id: &str,
    ) -> Result<Option<PaymentState>, ModuleError> {
        Ok(None)
    }

    async fn check_transaction_in_mempool(&self, _tx_hash: &Hash) -> Result<bool, ModuleError> {
        Ok(false)
    }

    async fn get_fee_estimate(&self, _target_blocks: u32) -> Result<u64, ModuleError> {
        Ok(1)
    }

    async fn register_rpc_endpoint(
        &self,
        _method: String,
        _description: String,
    ) -> Result<(), ModuleError> {
        Ok(())
    }

    async fn unregister_rpc_endpoint(&self, _method: &str) -> Result<(), ModuleError> {
        Ok(())
    }

    async fn register_timer(
        &self,
        _interval_seconds: u64,
        _callback: Arc<dyn TimerCallback>,
    ) -> Result<TimerId, ModuleError> {
        Ok(0)
    }

    async fn cancel_timer(&self, _timer_id: TimerId) -> Result<(), ModuleError> {
        Ok(())
    }

    async fn schedule_task(
        &self,
        _delay_seconds: u64,
        _callback: Arc<dyn TaskCallback>,
    ) -> Result<TaskId, ModuleError> {
        Ok(0)
    }

    async fn report_metric(&self, _metric: Metric) -> Result<(), ModuleError> {
        Ok(())
    }

    async fn get_module_metrics(&self, _module_id: &str) -> Result<Vec<Metric>, ModuleError> {
        Ok(vec![])
    }

    async fn get_all_metrics(&self) -> Result<HashMap<String, Vec<Metric>>, ModuleError> {
        Ok(HashMap::new())
    }

    async fn read_file(&self, _path: String) -> Result<Vec<u8>, ModuleError> {
        Ok(vec![])
    }

    async fn write_file(&self, _path: String, _data: Vec<u8>) -> Result<(), ModuleError> {
        Ok(())
    }

    async fn delete_file(&self, _path: String) -> Result<(), ModuleError> {
        Ok(())
    }

    async fn list_directory(&self, _path: String) -> Result<Vec<String>, ModuleError> {
        Ok(vec![])
    }

    async fn create_directory(&self, _path: String) -> Result<(), ModuleError> {
        Ok(())
    }

    async fn get_file_metadata(&self, path: String) -> Result<FileMetadata, ModuleError> {
        Ok(FileMetadata {
            path,
            size: 0,
            is_file: false,
            is_directory: false,
            modified: None,
            created: None,
        })
    }

    async fn initialize_module(
        &self,
        _module_id: String,
        _module_data_dir: std::path::PathBuf,
        _base_data_dir: std::path::PathBuf,
    ) -> Result<(), ModuleError> {
        Ok(())
    }

    async fn discover_modules(&self) -> Result<Vec<ModuleInfo>, ModuleError> {
        Ok(vec![])
    }

    async fn get_module_info(&self, _module_id: &str) -> Result<Option<ModuleInfo>, ModuleError> {
        Ok(None)
    }

    async fn is_module_available(&self, _module_id: &str) -> Result<bool, ModuleError> {
        Ok(false)
    }

    async fn publish_event(
        &self,
        _event_type: EventType,
        _payload: EventPayload,
    ) -> Result<(), ModuleError> {
        Ok(())
    }

    async fn call_module(
        &self,
        _target_module_id: Option<&str>,
        _method: &str,
        _params: Vec<u8>,
    ) -> Result<Vec<u8>, ModuleError> {
        Ok(vec![])
    }

    async fn register_module_api(&self, _api: Arc<dyn ModuleAPI>) -> Result<(), ModuleError> {
        Ok(())
    }

    async fn unregister_module_api(&self) -> Result<(), ModuleError> {
        Ok(())
    }

    async fn send_mesh_packet_to_module(
        &self,
        _module_id: &str,
        _packet_data: Vec<u8>,
        _peer_addr: String,
    ) -> Result<(), ModuleError> {
        Ok(())
    }

    async fn send_mesh_packet_to_peer(
        &self,
        _peer_addr: String,
        _packet_data: Vec<u8>,
    ) -> Result<(), ModuleError> {
        Ok(())
    }

    async fn send_stratum_v2_message_to_peer(
        &self,
        _peer_addr: String,
        _message_data: Vec<u8>,
    ) -> Result<(), ModuleError> {
        Ok(())
    }

    async fn get_module_health(
        &self,
        _module_id: &str,
    ) -> Result<Option<ModuleHealth>, ModuleError> {
        Ok(None)
    }

    async fn get_all_module_health(&self) -> Result<Vec<(String, ModuleHealth)>, ModuleError> {
        Ok(vec![])
    }

    async fn report_module_health(&self, _health: ModuleHealth) -> Result<(), ModuleError> {
        Ok(())
    }

    async fn get_block_template(
        &self,
        _rules: Vec<String>,
        _coinbase_script: Option<Vec<u8>>,
        _coinbase_address: Option<String>,
    ) -> Result<blvm_protocol::mining::BlockTemplate, ModuleError> {
        Err(ModuleError::OperationError(
            "stub get_block_template".into(),
        ))
    }

    async fn merge_block_serve_denylist(&self, _block_hashes: &[Hash]) -> Result<(), ModuleError> {
        Ok(())
    }

    async fn get_block_serve_denylist_snapshot(
        &self,
    ) -> Result<BlockServeDenylistSnapshot, ModuleError> {
        Ok(BlockServeDenylistSnapshot {
            total_count: 0,
            truncated: false,
            hashes: vec![],
        })
    }

    async fn clear_block_serve_denylist(&self) -> Result<(), ModuleError> {
        Ok(())
    }

    async fn replace_block_serve_denylist(
        &self,
        _block_hashes: &[Hash],
    ) -> Result<(), ModuleError> {
        Ok(())
    }

    async fn merge_tx_serve_denylist(&self, _tx_hashes: &[Hash]) -> Result<(), ModuleError> {
        Ok(())
    }

    async fn get_tx_serve_denylist_snapshot(&self) -> Result<TxServeDenylistSnapshot, ModuleError> {
        Ok(TxServeDenylistSnapshot {
            total_count: 0,
            truncated: false,
            hashes: vec![],
        })
    }

    async fn clear_tx_serve_denylist(&self) -> Result<(), ModuleError> {
        Ok(())
    }

    async fn replace_tx_serve_denylist(&self, _tx_hashes: &[Hash]) -> Result<(), ModuleError> {
        Ok(())
    }

    async fn get_sync_status(&self) -> Result<SyncStatus, ModuleError> {
        Ok(SyncStatus {
            phase: "Synced".to_string(),
            progress: 1.0,
            is_synced: true,
            error_message: None,
        })
    }

    async fn ban_peer(
        &self,
        _peer_addr: &str,
        _ban_duration_seconds: Option<u64>,
    ) -> Result<(), ModuleError> {
        Ok(())
    }

    async fn set_block_serve_maintenance_mode(&self, _enabled: bool) -> Result<(), ModuleError> {
        Ok(())
    }

    async fn submit_block(&self, _block: Block) -> Result<SubmitBlockResult, ModuleError> {
        Ok(SubmitBlockResult::Accepted)
    }

    async fn register_core_rpc_override(
        &self,
        _method: String,
        _description: String,
    ) -> Result<(), ModuleError> {
        Ok(())
    }

    async fn unregister_core_rpc_override(&self, _method: &str) -> Result<(), ModuleError> {
        Ok(())
    }
}