@@ -6,9 +6,9 @@ use std::{
66
77use bdk_chain:: {
88 bitcoin:: { Address , Network , OutPoint , ScriptBuf , Txid } ,
9- indexed_tx_graph:: IndexedTxGraph ,
10- keychain:: WalletChangeSet ,
11- local_chain:: { CheckPoint , LocalChain } ,
9+ indexed_tx_graph:: { self , IndexedTxGraph } ,
10+ keychain,
11+ local_chain:: { self , CheckPoint , LocalChain } ,
1212 Append , ConfirmationTimeAnchor ,
1313} ;
1414
@@ -23,6 +23,11 @@ use example_cli::{
2323const DB_MAGIC : & [ u8 ] = b"bdk_example_esplora" ;
2424const DB_PATH : & str = ".bdk_esplora_example.db" ;
2525
26+ type ChangeSet = (
27+ local_chain:: ChangeSet ,
28+ indexed_tx_graph:: ChangeSet < ConfirmationTimeAnchor , keychain:: ChangeSet < Keychain > > ,
29+ ) ;
30+
2631#[ derive( Subcommand , Debug , Clone ) ]
2732enum EsploraCommands {
2833 /// Scans the addresses in the wallet using the esplora API.
@@ -60,22 +65,22 @@ pub struct ScanOptions {
6065}
6166
6267fn main ( ) -> anyhow:: Result < ( ) > {
63- let ( args, keymap, index, db, init_changeset) = example_cli :: init :: <
64- EsploraCommands ,
65- WalletChangeSet < Keychain , ConfirmationTimeAnchor > ,
66- > ( DB_MAGIC , DB_PATH ) ? ;
68+ let ( args, keymap, index, db, init_changeset) =
69+ example_cli :: init :: < EsploraCommands , ChangeSet > ( DB_MAGIC , DB_PATH ) ? ;
70+
71+ let ( init_chain_changeset , init_indexed_tx_graph_changeset ) = init_changeset ;
6772
6873 // Contruct `IndexedTxGraph` and `LocalChain` with our initial changeset. They are wrapped in
6974 // `Mutex` to display how they can be used in a multithreaded context. Technically the mutexes
7075 // aren't strictly needed here.
7176 let graph = Mutex :: new ( {
7277 let mut graph = IndexedTxGraph :: new ( index) ;
73- graph. apply_changeset ( init_changeset . indexed_tx_graph ) ;
78+ graph. apply_changeset ( init_indexed_tx_graph_changeset ) ;
7479 graph
7580 } ) ;
7681 let chain = Mutex :: new ( {
7782 let mut chain = LocalChain :: default ( ) ;
78- chain. apply_changeset ( & init_changeset . chain ) ;
83+ chain. apply_changeset ( & init_chain_changeset ) ;
7984 chain
8085 } ) ;
8186
@@ -307,18 +312,17 @@ fn main() -> anyhow::Result<()> {
307312 println ! ( "missing block heights: {:?}" , missing_block_heights) ;
308313
309314 // Here, we actually fetch the missing blocks and create a `local_chain::Update`.
310- let chain_update = client
311- . update_local_chain ( tip, missing_block_heights)
312- . context ( "scanning for blocks" ) ?;
313-
314- println ! ( "new tip: {}" , chain_update. tip. height( ) ) ;
315+ let chain_changeset = {
316+ let chain_update = client
317+ . update_local_chain ( tip, missing_block_heights)
318+ . context ( "scanning for blocks" ) ?;
319+ println ! ( "new tip: {}" , chain_update. tip. height( ) ) ;
320+ chain. lock ( ) . unwrap ( ) . apply_update ( chain_update) ?
321+ } ;
315322
316323 // We persist the changes
317324 let mut db = db. lock ( ) . unwrap ( ) ;
318- db. stage ( WalletChangeSet {
319- chain : chain. lock ( ) . unwrap ( ) . apply_update ( chain_update) ?,
320- indexed_tx_graph : indexed_tx_graph_changeset,
321- } ) ;
325+ db. stage ( ( chain_changeset, indexed_tx_graph_changeset) ) ;
322326 db. commit ( ) ?;
323327 Ok ( ( ) )
324328}
0 commit comments