@@ -4,11 +4,16 @@ use bdk::{
4
4
SignOptions ,
5
5
} ;
6
6
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
+ } ,
8
13
Emitter ,
9
14
} ;
10
15
use bdk_file_store:: Store ;
11
- use std:: str:: FromStr ;
16
+ use std:: { str:: FromStr , time :: Duration } ;
12
17
13
18
const DB_MAGIC : & str = "bdk-rpc-wallet-example" ;
14
19
const SEND_AMOUNT : u64 = 5000 ;
@@ -39,13 +44,38 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
39
44
let balance = wallet. get_balance ( ) ;
40
45
println ! ( "Wallet balance before syncing: {} sats" , balance. total( ) ) ;
41
46
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
+ ) ?;
43
52
44
53
println ! (
45
54
"Connected to Bitcoin Core RPC at {:?}" ,
46
55
rpc_client. get_blockchain_info( ) . unwrap( )
47
56
) ;
48
57
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
+
49
79
wallet. set_lookahead_for_all ( args[ 4 ] . parse :: < u32 > ( ) ?) ?;
50
80
51
81
let chain_tip = wallet. latest_checkpoint ( ) ;
@@ -54,12 +84,21 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
54
84
None => Emitter :: from_height ( & rpc_client, args[ 5 ] . parse :: < u32 > ( ) ?) ,
55
85
} ;
56
86
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) ?;
60
93
wallet. commit ( ) ?;
61
94
}
62
95
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
+
63
102
let unconfirmed_txs = emitter. mempool ( ) ?;
64
103
println ! ( "Applying unconfirmed transactions: ..." ) ;
65
104
wallet. batch_insert_relevant_unconfirmed ( unconfirmed_txs. iter ( ) . map ( |( tx, time) | ( tx, * time) ) ) ;
0 commit comments