summaryrefslogtreecommitdiff
path: root/btc-wire/src/bin/test.rs
blob: 658c7506ffd96d7d4a7ea1fc0518e0a371603b9c (plain)
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
use core::panic;
use std::{collections::HashSet, iter::repeat_with, panic::AssertUnwindSafe};

use bitcoincore_rpc::{
    bitcoin::{Address, Amount, Txid},
    json::GetTransactionResultDetailCategory as Category,
    jsonrpc::{
        error::RpcError,
        serde_json::{json, Value},
    },
    Client, RpcApi,
};
use btc_wire::{
    rpc_patch::RpcErrorCode,
    rpc_utils::{
        common_rpc, default_data_dir, dirty_guess_network, wallet_rpc, Network, CLIENT, WIRE,
    },
    test::rand_key,
    BounceErr, ClientExtended,
};
use owo_colors::OwoColorize;

const RESERVE: &str = "reserve";

/// Instrumentation test
pub fn main() {
    let test_amount = Amount::from_sat(1500);
    let data_dir = default_data_dir();
    // Network check
    let network = dirty_guess_network(&data_dir);
    match network {
        Network::MainNet => {
            panic!("Do not run tests on the mainnet, you are going to loose money")
        }
        Network::TestNet => println!("{}", "Running on testnet, slow network mining".yellow()),
        Network::RegTest => println!("Running on regtest, fast manual mining"),
    }

    // Wallet check
    {
        let existing_wallets: HashSet<String> =
            std::fs::read_dir(data_dir.join(network.dir()).join("wallets"))
                .unwrap()
                .filter_map(|it| it.ok())
                .map(|it| it.file_name().to_string_lossy().to_string())
                .collect();

        let rpc = common_rpc(&data_dir, network).expect("Failed to open common client");
        if !existing_wallets.contains(CLIENT)
            || !existing_wallets.contains(WIRE)
            || !existing_wallets.contains(RESERVE)
        {
            println!("Generate tests wallets");
            // Create wallets
            rpc.create_wallet(WIRE, None, None, None, None).ok();
            rpc.create_wallet(CLIENT, None, None, None, None).ok();
            rpc.create_wallet(RESERVE, None, None, None, None).ok();
        }

        // Load wallets

        rpc.load_wallet(WIRE).ok();
        rpc.load_wallet(CLIENT).ok();
        rpc.load_wallet(RESERVE).ok();
    }

    // Client initialization
    let client_rpc = wallet_rpc(&data_dir, network, CLIENT);
    let wire_rpc = wallet_rpc(&data_dir, network, WIRE);
    let reserve_rpc = wallet_rpc(&data_dir, network, RESERVE);
    let client_addr = client_rpc.get_new_address(None, None).unwrap();
    let wire_addr = wire_rpc.get_new_address(None, None).unwrap();
    let reserve_addr = reserve_rpc.get_new_address(None, None).unwrap();

    let next_block = || {
        match network {
            Network::RegTest => {
                // Manually mine a block
                reserve_rpc.generate_to_address(1, &reserve_addr).unwrap();
            }
            _ => {
                // Wait for next network block
                reserve_rpc.wait_for_new_block(0).ok();
            }
        }
    };

    let wait_for_tx = |rpc: &Client, txs: &[Txid]| {
        let mut count = 0;
        while txs
            .iter()
            .any(|id| rpc.get_transaction(id, None).unwrap().info.confirmations <= 0)
        {
            next_block();
            if count > 3 {
                panic!("Transaction no sended after 4 blocks");
            }
            count += 1;
        }
    };

    // Balance check
    {
        // Transfer all wire money to client
        let wire_balance = wire_rpc.get_balance(None, None).unwrap();
        wire_rpc
            .send_to_address(
                &client_addr,
                wire_balance,
                None,
                None,
                Some(true),
                None,
                None,
                None,
            )
            .ok();
        // Transfer all wire money to client
        let reserve_balance = reserve_rpc.get_balance(None, None).unwrap();
        reserve_rpc
            .send_to_address(
                &client_addr,
                reserve_balance,
                None,
                None,
                Some(true),
                None,
                None,
                None,
            )
            .ok();
        next_block();

        let balance = client_rpc.get_balance(None, None).unwrap();
        let min_balance = test_amount * 3;
        if balance < min_balance {
            println!(
                "{}",
                format_args!(
                    "Client wallet have only {}, {} are required to perform the test",
                    balance, min_balance
                )
                .yellow()
            );
            match network {
                Network::MainNet | Network::TestNet => {
                    if network == Network::MainNet {
                        println!("Send coins to this address: {}", client_addr);
                    } else {
                        println!("Request coins from a faucet such as https://bitcoinfaucet.uo1.net/send.php to this address: {}", client_addr);
                    }
                    println!("Waiting for the transaction...");
                    while client_rpc.get_balance(None, None).unwrap() < min_balance {
                        client_rpc.wait_for_new_block(0).ok();
                    }
                }
                Network::RegTest => {
                    println!("Add 50B to client wallet");
                    client_rpc
                        .generate_to_address(
                            101, /* Need 100 blocks to validate */
                            &client_addr,
                        )
                        .unwrap();
                }
            }
        }

        println!(
            "Initial state:\n{} {}\n{} {}",
            WIRE,
            wire_rpc.get_balance(None, None).unwrap(),
            CLIENT,
            client_rpc.get_balance(None, None).unwrap()
        );
    }

    let mut runner = TestRunner::new();

    runner.test("OpReturn metadata", || {
        // Send metadata
        let msg = "J'aime le chocolat".as_bytes();
        let id = client_rpc
            .send_op_return(&wire_addr, test_amount, msg)
            .unwrap();
        // Check in mempool
        assert!(
            tx_exist(&client_rpc, &id, 0, Category::Send).unwrap(),
            "Not in mempool"
        );
        // Check mined
        wait_for_tx(&client_rpc, &[id]);
        assert!(
            tx_exist(&wire_rpc, &id, 1, Category::Receive).unwrap(),
            "Not mined"
        );
        // Check extract
        let (_, extracted) = wire_rpc.get_tx_op_return(&id).unwrap();
        assert_eq!(msg, extracted, "Corrupted metadata");
    });
    runner.test("SegWit metadata", || {
        // Send metadata
        let key = rand_key();
        let id = client_rpc
            .send_segwit_key(&wire_addr, test_amount, &key)
            .unwrap();
        // Check in mempool
        assert!(
            tx_exist(&client_rpc, &id, 0, Category::Send).unwrap(),
            "Not in mempool"
        );
        // Check mined
        wait_for_tx(&client_rpc, &[id]);
        assert!(
            tx_exist(&wire_rpc, &id, 1, Category::Receive).unwrap(),
            "Not mined"
        );
        // Check extract
        let (_, extracted) = wire_rpc.get_tx_segwit_key(&id).unwrap();
        assert_eq!(key, extracted, "Corrupted metadata");
    });

    let bounce_fee = Amount::from_sat(300);
    runner.test("Bounce simple", || {
        let before = client_rpc.get_balance(None, None).unwrap();
        let send_id = client_rpc
            .send_to_address(&wire_addr, test_amount, None, None, None, None, None, None)
            .unwrap();
        wait_for_tx(&client_rpc, &[send_id]);
        let bounce_id = wire_rpc.bounce(&send_id, bounce_fee).unwrap();
        wait_for_tx(&wire_rpc, &[bounce_id]);
        let bounce_tx_fee = wire_rpc.get_transaction(&bounce_id, None).unwrap().details[0]
            .fee
            .unwrap()
            .abs()
            .to_unsigned()
            .unwrap();
        let send_tx_fee = client_rpc.get_transaction(&send_id, None).unwrap().details[0]
            .fee
            .unwrap()
            .abs()
            .to_unsigned()
            .unwrap();
        let after = client_rpc.get_balance(None, None).unwrap();
        assert!(before >= after);
        assert_eq!(before - after, bounce_tx_fee + bounce_fee + send_tx_fee);
    });
    runner.test("Bounce minimal amount", || {
        let send_id = client_rpc
            .send_to_address(
                &wire_addr,
                Amount::from_sat(294),
                None,
                None,
                None,
                None,
                None,
                None,
            )
            .unwrap();
        wait_for_tx(&client_rpc, &[send_id]);
        assert!(match wire_rpc.bounce(&send_id, bounce_fee) {
            Ok(_) => false,
            Err(err) => match err {
                BounceErr::AmountLessThanFee => true,
                _ => false,
            },
        });
    });
    runner.test("Bounce too small amount", || {
        let send_id = client_rpc
            .send_to_address(
                &wire_addr,
                Amount::from_sat(294) + bounce_fee,
                None,
                None,
                None,
                None,
                None,
                None,
            )
            .unwrap();
        wait_for_tx(&client_rpc, &[send_id]);

        assert!(match wire_rpc.bounce(&send_id, bounce_fee) {
            Ok(_) => false,
            Err(err) => match err {
                BounceErr::RPC(bitcoincore_rpc::Error::JsonRpc(
                    bitcoincore_rpc::jsonrpc::Error::Rpc(RpcError { code, .. }),
                )) => code == RpcErrorCode::RpcWalletInsufficientFunds as i32,
                _ => false,
            },
        });
    });
    runner.test("Bounce complex", || {
        // Generate 6 new addresses
        let addresses: Vec<Address> =
            repeat_with(|| client_rpc.get_new_address(None, None).unwrap())
                .take(6)
                .collect();
        // Send transaction to self with 1, 2 and 3 outputs
        let txs: Vec<Txid> = [&addresses[0..1], &addresses[1..3], &addresses[3..]]
            .into_iter()
            .map(|addresses| {
                let hex: String = client_rpc
                    .call(
                        "createrawtransaction",
                        &[
                            Value::Array(vec![]),
                            Value::Array(
                                addresses
                                    .iter()
                                    .map(|addr| json!({&addr.to_string(): test_amount.as_btc()}))
                                    .collect(),
                            ),
                        ],
                    )
                    .unwrap();
                let funded = client_rpc.fund_raw_transaction(hex, None, None).unwrap();
                let signed = client_rpc
                    .sign_raw_transaction_with_wallet(&funded.hex, None, None)
                    .unwrap();
                client_rpc.send_raw_transaction(&signed.hex).unwrap()
            })
            .collect();
        wait_for_tx(&client_rpc, txs.as_slice());
        let before = client_rpc.get_balance(None, None).unwrap();
        // Send a transaction with multiple input from multiple transaction of different outputs len
        let hex: String = client_rpc
            .call(
                "createrawtransaction",
                &[
                    Value::Array(
                        txs.into_iter()
                            .enumerate()
                            .map(|(i, tx)| {
                                json!({
                                    "txid": tx.to_string(),
                                    "vout": i,
                                })
                            })
                            .collect(),
                    ),
                    json!([
                        {&wire_addr.to_string(): (test_amount*3).as_btc()}
                    ]),
                ],
            )
            .unwrap();
        let funded = client_rpc.fund_raw_transaction(hex, None, None).unwrap();
        let signed = client_rpc
            .sign_raw_transaction_with_wallet(&funded.hex, None, None)
            .unwrap();
        let send_id = client_rpc.send_raw_transaction(&signed.hex).unwrap();
        wait_for_tx(&client_rpc, &[send_id]);
        let bounce_id = wire_rpc.bounce(&send_id, bounce_fee).unwrap();
        wait_for_tx(&wire_rpc, &[bounce_id]);
        let after = client_rpc.get_balance(None, None).unwrap();
        let bounce_tx_fee = wire_rpc.get_transaction(&bounce_id, None).unwrap().details[0]
            .fee
            .unwrap()
            .abs()
            .to_unsigned()
            .unwrap();
        let send_tx_fee = client_rpc.get_transaction(&send_id, None).unwrap().details[0]
            .fee
            .unwrap()
            .abs()
            .to_unsigned()
            .unwrap();
        assert!(before >= after);
        assert_eq!(before - after, bounce_tx_fee + bounce_fee + send_tx_fee);
    });

    runner.conclude();
}

/// Check a specific transaction exist in a wallet historic
fn tx_exist(
    rpc: &Client,
    id: &Txid,
    min_confirmation: i32,
    detail: Category,
) -> bitcoincore_rpc::Result<bool> {
    let txs = rpc.list_transactions(None, None, None, None).unwrap();
    let found = txs.into_iter().any(|tx| {
        tx.detail.category == detail
            && tx.info.confirmations >= min_confirmation
            && tx.info.txid == *id
    });
    Ok(found)
}

/// Run test track success and errors
struct TestRunner {
    nb_ok: usize,
    nb_err: usize,
}

impl TestRunner {
    fn new() -> Self {
        Self {
            nb_err: 0,
            nb_ok: 0,
        }
    }

    fn test(&mut self, name: &str, test: impl FnOnce()) {
        println!("{}", name.cyan());

        let result = std::panic::catch_unwind(AssertUnwindSafe(test));
        if result.is_ok() {
            println!("{}", "OK".green());
            self.nb_ok += 1;
        } else {
            dbg!(&result);
            println!("{}", "ERR".red());
            self.nb_err += 1;
        }
    }

    /// Wait for tests completion and print results
    fn conclude(self) {
        println!(
            "Result for {} tests: {} ok and {} err",
            self.nb_ok + self.nb_err,
            self.nb_ok,
            self.nb_err
        );
    }
}