blvm-node 0.1.76

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
//! Raw transaction RPC decode/accept paths.

use blvm_node::node::mempool::MempoolManager;
use blvm_node::rpc::rawtx::RawTxRpc;
use blvm_node::storage::Storage;
use blvm_node::{OutPoint, UTXO};
use blvm_protocol::serialization::transaction::serialize_transaction;
use serde_json::json;
use std::sync::Arc;
use tempfile::TempDir;

mod common;
use common::{
    TestBlockBuilder, p2pkh_script, random_hash20, setup_mining_chain, valid_transaction,
};

fn rpc_with_mempool() -> (TempDir, RawTxRpc, String) {
    let temp_dir = TempDir::new().unwrap();
    let storage = Arc::new(Storage::new(temp_dir.path()).unwrap());
    let mempool = Arc::new(MempoolManager::new());
    let tx = valid_transaction();
    use blvm_protocol::block::calculate_tx_id;
    let txid = hex::encode(calculate_tx_id(&tx));
    mempool.add_transaction(tx.clone()).unwrap();
    let rpc = RawTxRpc::with_dependencies(storage, mempool, None, None);
    (temp_dir, rpc, txid)
}

#[tokio::test]
async fn test_decoderawtransaction_round_trip() {
    let tx = valid_transaction();
    let hex_tx = hex::encode(serialize_transaction(&tx));
    let rpc = RawTxRpc::new();
    let decoded = rpc.decoderawtransaction(&json!([hex_tx])).await.unwrap();
    assert_eq!(decoded.get("version").unwrap().as_i64(), Some(1));
    assert!(decoded.get("vin").unwrap().is_array());
    assert!(decoded.get("vout").unwrap().is_array());
}

#[tokio::test]
async fn test_testmempoolaccept_with_mempool_fixture() {
    let tx = valid_transaction();
    let hex_tx = hex::encode(serialize_transaction(&tx));
    let temp_dir = TempDir::new().unwrap();
    let storage = Arc::new(Storage::new(temp_dir.path()).unwrap());
    let mempool = Arc::new(MempoolManager::new());
    let rpc = RawTxRpc::with_dependencies(storage, mempool, None, None);
    let result = rpc.testmempoolaccept(&json!([hex_tx])).await.unwrap();
    let arr = result.as_array().expect("accept results");
    assert_eq!(arr.len(), 1);
    assert!(arr[0].get("allowed").is_some());
}

#[tokio::test]
async fn test_gettxout_with_indexed_utxo() {
    let temp_dir = TempDir::new().unwrap();
    let storage = Arc::new(Storage::new(temp_dir.path()).unwrap());
    let tx = valid_transaction();
    use blvm_protocol::block::calculate_tx_id;
    let tx_hash = calculate_tx_id(&tx);
    let txid_hex = hex::encode(tx_hash);
    storage
        .transactions()
        .index_transaction(&tx, &[0xab; 32], 0, 0)
        .unwrap();
    storage
        .utxos()
        .add_utxo(
            &OutPoint {
                hash: tx_hash,
                index: 0,
            },
            &UTXO {
                value: 1000,
                script_pubkey: vec![0x51].into(),
                height: 0,
                is_coinbase: false,
            },
        )
        .unwrap();
    let rpc = RawTxRpc::with_dependencies(storage, Arc::new(MempoolManager::new()), None, None);
    let out = rpc.gettxout(&json!([txid_hex, 0, false])).await.unwrap();
    assert_eq!(
        out.get("value").unwrap().as_f64(),
        Some(1000.0 / 100_000_000.0)
    );
}

#[tokio::test]
async fn test_getrawtransaction_from_index() {
    let (_dir, rpc, txid) = rpc_with_mempool();
    let result = rpc.getrawtransaction(&json!([txid])).await.unwrap();
    assert!(result.as_str().is_some_and(|hex| !hex.is_empty()) || result.get("hex").is_some());
}

#[tokio::test]
async fn test_createrawtransaction_smoke() {
    let rpc = RawTxRpc::new();
    let txid = "aa".repeat(32);
    let hex_tx = rpc
        .createrawtransaction(&json!([
            [{"txid": txid, "vout": 0}],
            {"bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4": 0.001}
        ]))
        .await
        .unwrap();
    assert!(hex_tx.as_str().unwrap().len() >= 64);
}

#[tokio::test]
async fn test_getrawtransaction_verbose_from_index() {
    let temp_dir = TempDir::new().unwrap();
    let storage = Arc::new(Storage::new(temp_dir.path()).unwrap());
    let tx = valid_transaction();
    use blvm_protocol::block::calculate_tx_id;
    let tx_hash = calculate_tx_id(&tx);
    let txid = hex::encode(tx_hash);
    storage
        .transactions()
        .index_transaction(&tx, &[0xde; 32], 0, 0)
        .unwrap();
    let rpc = RawTxRpc::with_dependencies(storage, Arc::new(MempoolManager::new()), None, None);
    let result = rpc.getrawtransaction(&json!([txid, true])).await.unwrap();
    assert!(result.get("txid").is_some());
    assert!(result.get("vin").is_some());
}

#[tokio::test]
async fn test_sendrawtransaction_invalid_hex_errors() {
    let (_dir, rpc, _txid) = rpc_with_mempool();
    assert!(rpc.sendrawtransaction(&json!(["not-hex"])).await.is_err());
}

#[tokio::test]
async fn test_gettxout_missing_utxo_returns_null() {
    let (_dir, rpc, txid) = rpc_with_mempool();
    let out = rpc.gettxout(&json!([txid, 99, true])).await.unwrap();
    assert!(out.is_null());
}

#[tokio::test]
async fn test_gettxoutproof_and_verifytxoutproof_on_chain() {
    let temp_dir = TempDir::new().unwrap();
    let storage = Arc::new(Storage::new(temp_dir.path()).unwrap());
    setup_mining_chain(&storage, 4).unwrap();

    let tx = valid_transaction();
    use blvm_protocol::block::calculate_tx_id;
    let txid = hex::encode(calculate_tx_id(&tx));

    let tip = storage.chain().get_height().unwrap().unwrap_or(0);
    let prev_hash = storage.blocks().get_hash_by_height(tip).unwrap().unwrap();
    let mut block = TestBlockBuilder::new()
        .set_prev_hash(prev_hash)
        .add_coinbase_transaction(p2pkh_script(random_hash20()))
        .add_transaction(tx.clone())
        .build();
    use blvm_protocol::mining::calculate_merkle_root;
    block.header.merkle_root =
        calculate_merkle_root(block.transactions.as_ref()).expect("merkle root");
    let height = tip + 1;
    let block_hash = storage.blocks().get_block_hash(&block);
    storage.blocks().store_block(&block).unwrap();
    storage.blocks().store_height(height, &block_hash).unwrap();
    storage
        .chain()
        .update_tip(&block_hash, &block.header, height)
        .unwrap();
    storage
        .transactions()
        .index_transaction(&tx, &block_hash, height, 1)
        .unwrap();

    let rpc = RawTxRpc::with_dependencies(storage, Arc::new(MempoolManager::new()), None, None);

    let proof = rpc
        .gettxoutproof(&json!([[txid]]))
        .await
        .unwrap()
        .as_str()
        .unwrap()
        .to_string();

    let blockhash_hex = hex::encode(block_hash);
    let verified = rpc
        .verifytxoutproof(&json!([proof, blockhash_hex]))
        .await
        .unwrap();
    let arr = verified.as_array().expect("Core returns txid array");
    assert!(arr.iter().any(|v| v.as_str() == Some(txid.as_str())));

    let verified_core = rpc.verifytxoutproof(&json!([proof])).await.unwrap();
    let core_arr = verified_core.as_array().unwrap();
    assert!(core_arr.iter().any(|v| v.as_str() == Some(txid.as_str())));
}

#[tokio::test]
async fn test_gettxoutproof_missing_params_and_bad_blockhash() {
    let temp_dir = TempDir::new().unwrap();
    let storage = Arc::new(Storage::new(temp_dir.path()).unwrap());
    setup_mining_chain(&storage, 3).unwrap();
    let rpc = RawTxRpc::with_dependencies(storage, Arc::new(MempoolManager::new()), None, None);

    assert!(rpc.gettxoutproof(&json!([])).await.is_err());
    assert!(
        rpc.gettxoutproof(&json!([["aa".repeat(32)], "abcd"]))
            .await
            .is_err()
    );
}

#[tokio::test]
async fn test_verifytxoutproof_invalid_proof_errors() {
    let temp_dir = TempDir::new().unwrap();
    let storage = Arc::new(Storage::new(temp_dir.path()).unwrap());
    setup_mining_chain(&storage, 3).unwrap();
    let rpc = RawTxRpc::with_dependencies(storage, Arc::new(MempoolManager::new()), None, None);

    assert!(rpc.verifytxoutproof(&json!([])).await.is_err());
    assert!(
        rpc.verifytxoutproof(&json!(["", "aa".repeat(64)]))
            .await
            .is_err()
    );
    assert!(
        rpc.verifytxoutproof(&json!(["00", "aa".repeat(64)]))
            .await
            .is_err()
    );
}

#[tokio::test]
async fn test_sendrawtransaction_already_in_mempool_errors() {
    let tx = valid_transaction();
    let hex_tx = hex::encode(serialize_transaction(&tx));
    let (_dir, rpc, _txid) = rpc_with_mempool();
    assert!(rpc.sendrawtransaction(&json!([hex_tx])).await.is_err());
}

#[tokio::test]
async fn test_gettransactiondetails_from_mempool() {
    let (_dir, rpc, txid) = rpc_with_mempool();
    let details = rpc
        .get_transaction_details(&json!([txid, true]))
        .await
        .unwrap();
    assert_eq!(details.get("txid").unwrap().as_str(), Some(txid.as_str()));
    assert!(details.get("vin").unwrap().is_array());
    assert!(details.get("hex").unwrap().as_str().unwrap().len() >= 64);
}

fn p2pkh_script_25() -> Vec<u8> {
    let mut script = vec![0x76, 0xa9, 0x14];
    script.extend_from_slice(&[0x11; 20]);
    script.extend_from_slice(&[0x88, 0xac]);
    script
}

#[tokio::test]
async fn test_verbose_raw_transaction_names_scripts_and_returns_a_confirmed_witness() {
    use blvm_protocol::{Block, BlockHeader, Transaction, TransactionInput, TransactionOutput};

    let temp_dir = TempDir::new().unwrap();
    let storage = Arc::new(Storage::new(temp_dir.path()).unwrap());
    let mempool = Arc::new(MempoolManager::new());

    let p2sh = {
        let mut script = vec![0xa9, 0x14];
        script.extend_from_slice(&[0x22; 20]);
        script.push(0x87);
        script
    };
    let witness_program = hex::decode("0014751e76e8199196d454941c45d1b3a323f1433bd6").unwrap();
    let typed = Transaction {
        version: 2,
        inputs: blvm_protocol::tx_inputs![TransactionInput {
            prevout: blvm_node::OutPoint {
                hash: [0x33; 32],
                index: 0,
            },
            script_sig: vec![0x51],
            sequence: 0xffffffff,
        }],
        outputs: blvm_protocol::tx_outputs![
            TransactionOutput {
                value: 10_000,
                script_pubkey: p2pkh_script_25().into(),
            },
            TransactionOutput {
                value: 10_000,
                script_pubkey: p2sh.into(),
            },
            TransactionOutput {
                value: 0,
                script_pubkey: vec![0x6a].into(),
            },
            TransactionOutput {
                value: 10_000,
                script_pubkey: witness_program.clone().into(),
            }
        ],
        lock_time: 0,
    };
    mempool.add_transaction(typed.clone()).unwrap();
    let typed_rpc = blvm_node::storage::hashing::hash_to_rpc_hex(
        &blvm_protocol::block::calculate_tx_id(&typed),
    );
    let rpc = RawTxRpc::with_dependencies(storage.clone(), mempool, None, None);
    let verbose = rpc
        .getrawtransaction(&json!([typed_rpc, true]))
        .await
        .unwrap();
    let vout = verbose["vout"].as_array().unwrap();
    assert_eq!(vout[0]["scriptPubKey"]["type"], "pubkeyhash");
    assert_eq!(vout[1]["scriptPubKey"]["type"], "scripthash");
    assert_eq!(vout[2]["scriptPubKey"]["type"], "nulldata");
    assert_eq!(vout[3]["scriptPubKey"]["type"], "witness_v0_keyhash");

    let details = rpc
        .get_transaction_details(&json!([typed_rpc]))
        .await
        .unwrap();
    assert_eq!(details["vout"][0]["scriptPubKey"]["type"], "pubkeyhash");
    assert_eq!(
        details["vout"][3]["scriptPubKey"]["type"],
        "witness_v0_keyhash"
    );

    let confirmed = Transaction {
        version: 2,
        inputs: blvm_protocol::tx_inputs![TransactionInput {
            prevout: blvm_node::OutPoint {
                hash: [0x44; 32],
                index: 1,
            },
            script_sig: vec![],
            sequence: 0xffffffff,
        }],
        outputs: blvm_protocol::tx_outputs![TransactionOutput {
            value: 20_000,
            script_pubkey: witness_program.into(),
        }],
        lock_time: 0,
    };
    let block = Block {
        header: BlockHeader {
            version: 1,
            prev_block_hash: [0u8; 32],
            merkle_root: [0u8; 32],
            timestamp: 1_231_006_505,
            bits: 0x1d00ffff,
            nonce: 1,
        },
        transactions: vec![confirmed.clone()].into_boxed_slice(),
    };
    let block_hash = storage.blocks().get_block_hash(&block);
    storage.blocks().store_block(&block).unwrap();
    storage
        .blocks()
        .store_witness(&block_hash, &[vec![vec![vec![0x51]]]])
        .unwrap();
    storage
        .transactions()
        .index_transaction(&confirmed, &block_hash, 1, 0)
        .unwrap();
    let confirmed_rpc = blvm_node::storage::hashing::hash_to_rpc_hex(
        &blvm_protocol::block::calculate_tx_id(&confirmed),
    );
    let confirmed_verbose = rpc
        .getrawtransaction(&json!([confirmed_rpc, true]))
        .await
        .unwrap();
    assert_eq!(confirmed_verbose["vin"][0]["txinwitness"][0], "51");
    assert_ne!(
        confirmed_verbose["hash"].as_str().unwrap(),
        confirmed_verbose["txid"].as_str().unwrap()
    );
}

#[tokio::test]
async fn test_verifytxoutproof_uses_rpc_hash_order() {
    use blvm_protocol::{Block, BlockHeader, Transaction, TransactionInput, TransactionOutput};

    let temp_dir = TempDir::new().unwrap();
    let storage = Arc::new(Storage::new(temp_dir.path()).unwrap());
    let tx = Transaction {
        version: 1,
        inputs: blvm_protocol::tx_inputs![TransactionInput {
            prevout: blvm_node::OutPoint {
                hash: [0x01; 32],
                index: 0,
            },
            script_sig: vec![0x51],
            sequence: 0xffffffff,
        }],
        outputs: blvm_protocol::tx_outputs![TransactionOutput {
            value: 50_000,
            script_pubkey: vec![0x51].into(),
        }],
        lock_time: 0,
    };
    let block = Block {
        header: BlockHeader {
            version: 1,
            prev_block_hash: [0u8; 32],
            merkle_root: blvm_protocol::block::calculate_tx_id(&tx),
            timestamp: 1_231_006_505,
            bits: 0x1d00ffff,
            nonce: 7,
        },
        transactions: vec![tx.clone()].into_boxed_slice(),
    };
    let block_hash = storage.blocks().get_block_hash(&block);
    storage.blocks().store_block(&block).unwrap();
    storage.blocks().store_height(0, &block_hash).unwrap();
    let rpc = RawTxRpc::with_dependencies(storage, Arc::new(MempoolManager::new()), None, None);
    let txid =
        blvm_node::storage::hashing::hash_to_rpc_hex(&blvm_protocol::block::calculate_tx_id(&tx));
    let block_rpc = blvm_node::storage::hashing::hash_to_rpc_hex(&block_hash);
    let proof = rpc
        .gettxoutproof(&json!([[txid], block_rpc]))
        .await
        .unwrap();
    let matched = rpc
        .verifytxoutproof(&json!([proof.as_str().unwrap(), block_rpc]))
        .await
        .unwrap();
    assert_eq!(matched[0], txid);
}