Skip to content

Commit 7defbf1

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 7defbf1

File tree

1 file changed

+37
-12
lines changed

1 file changed

+37
-12
lines changed

integration_test/tests/wallet.rs

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,19 @@
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};
1111
use node::{mtype, AddressType, ImportMultiRequest, ImportMultiScriptPubKey, ImportMultiTimestamp};
12+
13+
#[cfg(not(feature = "v20_and_below"))]
14+
use node::ImportDescriptorsRequest;
15+
1216
use node::vtype::*; // All the version specific types.
1317
use std::fs;
18+
use std::time::{SystemTime, UNIX_EPOCH};
1419

1520
#[test]
1621
fn wallet__abandon_transaction() {
@@ -313,8 +318,6 @@ fn wallet__import_address() {
313318
#[test]
314319
#[cfg(not(feature = "v20_and_below"))]
315320
fn wallet__import_descriptors() {
316-
use node::{serde_json, ImportDescriptorsRequest};
317-
318321
let node = Node::with_wallet(Wallet::None, &[]);
319322
let wallet_name = "desc_wallet";
320323

@@ -325,15 +328,37 @@ fn wallet__import_descriptors() {
325328
#[cfg(not(feature = "v22_and_below"))]
326329
node.client.create_wallet(wallet_name).expect("create wallet");
327330

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

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

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

339364
#[test]

0 commit comments

Comments
 (0)