Skip to content

Commit eb77c8c

Browse files
committed
wip try to implement scanblocks rpc to speed up full wallet scans
1 parent d97875b commit eb77c8c

File tree

2 files changed

+47
-7
lines changed

2 files changed

+47
-7
lines changed

crates/bitcoind_rpc/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ readme = "README.md"
1515
[dependencies]
1616
# For no-std, remember to enable the bitcoin/no-std feature
1717
bitcoin = { version = "0.30", default-features = false }
18-
bitcoincore-rpc = { version = "0.17" }
18+
# bitcoincore-rpc = { version = "0.17" }
19+
bitcoincore-rpc = { git = "https://github.com/chrisguida/rust-bitcoincore-rpc", branch = "feat/scanblocks" }
1920
bdk_chain = { path = "../chain", version = "0.6", default-features = false }
2021

2122
[dev-dependencies]

example-crates/wallet_rpc/src/main.rs

+45-6
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@ use bdk::{
44
SignOptions,
55
};
66
use bdk_bitcoind_rpc::{
7-
bitcoincore_rpc::{Auth, Client, RpcApi},
7+
bitcoincore_rpc::{
8+
bitcoincore_rpc_json::{
9+
ScanBlocksOptions, ScanBlocksRequest, ScanBlocksRequestDescriptor, ScanBlocksResult,
10+
},
11+
Auth, Client, RpcApi,
12+
},
813
Emitter,
914
};
1015
use bdk_file_store::Store;
11-
use std::str::FromStr;
16+
use std::{str::FromStr, time::Duration};
1217

1318
const DB_MAGIC: &str = "bdk-rpc-wallet-example";
1419
const SEND_AMOUNT: u64 = 5000;
@@ -39,13 +44,38 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
3944
let balance = wallet.get_balance();
4045
println!("Wallet balance before syncing: {} sats", balance.total());
4146

42-
let rpc_client = Client::new(&args[1], Auth::UserPass(args[2].clone(), args[3].clone()))?;
47+
let rpc_client = Client::new_with_timeout(
48+
&args[1],
49+
Auth::UserPass(args[2].clone(), args[3].clone()),
50+
Duration::from_secs(3600),
51+
)?;
4352

4453
println!(
4554
"Connected to Bitcoin Core RPC at {:?}",
4655
rpc_client.get_blockchain_info().unwrap()
4756
);
4857

58+
let external_descriptor = ScanBlocksRequestDescriptor::Extended {
59+
desc: external_descriptor.to_string(),
60+
range: None,
61+
};
62+
let internal_descriptor = ScanBlocksRequestDescriptor::Extended {
63+
desc: internal_descriptor.to_string(),
64+
range: None,
65+
};
66+
let descriptors = &[external_descriptor, internal_descriptor];
67+
let request = ScanBlocksRequest {
68+
scanobjects: descriptors,
69+
start_height: None,
70+
stop_height: None,
71+
filtertype: None,
72+
options: Some(ScanBlocksOptions {
73+
filter_false_positives: Some(true),
74+
}),
75+
};
76+
let res: ScanBlocksResult = rpc_client.scan_blocks_blocking(request)?;
77+
println!("scanblocks result: {:?}", res);
78+
4979
wallet.set_lookahead_for_all(args[4].parse::<u32>()?)?;
5080

5181
let chain_tip = wallet.latest_checkpoint();
@@ -54,12 +84,21 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
5484
None => Emitter::from_height(&rpc_client, args[5].parse::<u32>()?),
5585
};
5686

57-
while let Some((height, block)) = emitter.next_block()? {
58-
println!("Applying block {} at height {}", block.block_hash(), height);
59-
wallet.apply_block_relevant(block, height)?;
87+
for bh in res.relevant_blocks {
88+
// self.get_relevant_txs(bh, &conn);
89+
let block = rpc_client.get_block(&bh)?;
90+
let height: u32 = block.bip34_block_height()?.try_into().unwrap();
91+
println!("adding block height {} to wallet", height);
92+
wallet.apply_block_relevant(block.clone(), height)?;
6093
wallet.commit()?;
6194
}
6295

96+
// while let Some((height, block)) = emitter.next_block()? {
97+
// println!("Applying block {} at height {}", block.block_hash(), height);
98+
// wallet.apply_block_relevant(block, height)?;
99+
// wallet.commit()?;
100+
// }
101+
63102
let unconfirmed_txs = emitter.mempool()?;
64103
println!("Applying unconfirmed transactions: ...");
65104
wallet.batch_insert_relevant_unconfirmed(unconfirmed_txs.iter().map(|(tx, time)| (tx, *time)));

0 commit comments

Comments
 (0)