Skip to content

Commit 2635d52

Browse files
committed
Improve import_descriptors test
The current test of the RPC always returns true due to scanning for the timestamp `now`. Move imports to the top of the file and allow unused. Improve the test to be more thorough by creating a new address and then scanning for the descriptor at a later time.
1 parent 6c12b8e commit 2635d52

File tree

1 file changed

+34
-13
lines changed

1 file changed

+34
-13
lines changed

integration_test/tests/wallet.rs

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
//! Tests for methods found under the `== Wallet ==` section of the API docs.
44
55
#![allow(non_snake_case)] // Test names intentionally use double underscore.
6+
#![allow(unused_imports)] // Some imports are only used in specific versions.
67

7-
#[cfg(feature = "TODO")]
8-
use bitcoin::address::{Address, NetworkChecked};
9-
use bitcoin::{Amount, FeeRate, PrivateKey, PublicKey};
8+
use bitcoin::address::{Address, KnownHrp, NetworkChecked};
9+
use bitcoin::{secp256k1, Amount, CompressedPublicKey, FeeRate, PrivateKey, PublicKey};
1010
use integration_test::{Node, NodeExt as _, Wallet};
11-
use node::{mtype, AddressType, ImportMultiRequest, ImportMultiScriptPubKey, ImportMultiTimestamp};
11+
use node::{mtype, AddressType, ImportDescriptorsRequest, ImportMultiRequest, ImportMultiScriptPubKey, ImportMultiTimestamp};
1212
use node::vtype::*; // All the version specific types.
1313
use std::fs;
14+
use std::time::{SystemTime, UNIX_EPOCH};
1415

1516
#[test]
1617
fn wallet__abandon_transaction() {
@@ -313,8 +314,6 @@ fn wallet__import_address() {
313314
#[test]
314315
#[cfg(not(feature = "v20_and_below"))]
315316
fn wallet__import_descriptors() {
316-
use node::{serde_json, ImportDescriptorsRequest};
317-
318317
let node = Node::with_wallet(Wallet::None, &[]);
319318
let wallet_name = "desc_wallet";
320319

@@ -325,15 +324,37 @@ fn wallet__import_descriptors() {
325324
#[cfg(not(feature = "v22_and_below"))]
326325
node.client.create_wallet(wallet_name).expect("create wallet");
327326

328-
let address = node.client.new_address().expect("failed to get new address");
329-
let descriptor = format!("addr({})", address);
327+
node.fund_wallet();
330328

331-
let request = ImportDescriptorsRequest {
332-
desc: descriptor,
333-
timestamp: serde_json::Value::String("now".to_string()),
334-
};
329+
// 1. Get the current time
330+
let start_time = SystemTime::now()
331+
.duration_since(UNIX_EPOCH)
332+
.expect("failed to get current time")
333+
.as_secs();
335334

336-
let _: ImportDescriptors = node.client.import_descriptors(&[request]).expect("importdescriptors");
335+
// 2. Use a known private key, derive the address from it and send some coins to it.
336+
let privkey =
337+
PrivateKey::from_wif("cVt4o7BGAig1UXywgGSmARhxMdzP5qvQsxKkSsc1XEkw3tDTQFpy").unwrap();
338+
let secp = secp256k1::Secp256k1::new();
339+
let pubkey = privkey.public_key(&secp);
340+
let address = Address::p2wpkh(&CompressedPublicKey(pubkey.inner), KnownHrp::Regtest);
341+
let amount = Amount::from_sat(10_000);
342+
let _txid = node.client.send_to_address(&address, amount).expect("sendtoaddress");
343+
344+
// 3. Get the descriptor from the private key.
345+
let raw_descriptor = format!("wpkh({})", privkey.to_wif());
346+
let info = node.client.get_descriptor_info(&raw_descriptor).expect("get_descriptor_info");
347+
let descriptor = format!("{}#{}", raw_descriptor, info.checksum);
348+
349+
// 4. Mine 100 blocks
350+
let mining_address = node.client.new_address().expect("failed to get mining address");
351+
let _blocks = node.client.generate_to_address(100, &mining_address).expect("generatetoaddress");
352+
353+
// 5. Scan for the descriptor using the time from (1)
354+
let request = ImportDescriptorsRequest::new(descriptor, start_time);
355+
let result: ImportDescriptors = node.client.import_descriptors(&[request]).expect("importdescriptors");
356+
assert_eq!(result.0.len(), 1, "should have exactly one import result");
357+
assert!(result.0[0].success);
337358
}
338359

339360
#[test]

0 commit comments

Comments
 (0)