@@ -76,12 +76,15 @@ where
7676 pub ( crate ) async fn sync ( & self , confirmables : Vec < & ( dyn Confirm + Sync ) > ) -> Result < ( ) , Error > {
7777 let client = & * self . blockchain ;
7878
79- let cur_height = client. get_height ( ) . await ?;
79+ let tip_hash= client. get_tip_hash ( ) . await ?;
80+ let tip_block_status = client. get_block_status ( & tip_hash) . await ?;
81+ let tip_height = tip_block_status. height . unwrap_or ( 0 ) ;
8082
8183 let mut locked_last_sync_height = self . last_sync_height . lock ( ) . await ;
82- if cur_height >= locked_last_sync_height. unwrap_or ( 0 ) {
83- self . sync_best_block_updated ( & confirmables, cur_height, & mut locked_last_sync_height)
84- . await ?;
84+ if tip_block_status. in_best_chain && ( tip_height >= locked_last_sync_height. unwrap_or ( 0 ) ) {
85+ self . sync_best_block_updated ( & confirmables, & tip_hash, tip_height) . await ?;
86+ * locked_last_sync_height = Some ( tip_height) ;
87+
8588 self . sync_transactions_confirmed ( & confirmables) . await ?;
8689 self . sync_transaction_unconfirmed ( & confirmables) . await ?;
8790 }
@@ -90,18 +93,15 @@ where
9093 }
9194
9295 async fn sync_best_block_updated (
93- & self , confirmables : & Vec < & ( dyn Confirm + Sync ) > , cur_height : u32 ,
94- locked_last_sync_height : & mut tokio:: sync:: MutexGuard < ' _ , Option < u32 > > ,
96+ & self , confirmables : & Vec < & ( dyn Confirm + Sync ) > , tip_hash : & BlockHash , tip_height : u32
9597 ) -> Result < ( ) , Error > {
9698 let client = & * self . blockchain ;
9799
98100 // Inform the interface of the new block.
99- let cur_block_header = client. get_header ( cur_height ) . await ?;
101+ let tip_block_header = client. get_header_by_hash ( tip_hash ) . await ?;
100102 for c in confirmables {
101- c. best_block_updated ( & cur_block_header , cur_height ) ;
103+ c. best_block_updated ( & tip_block_header , tip_height ) ;
102104 }
103-
104- * * locked_last_sync_height = Some ( cur_height) ;
105105 Ok ( ( ) )
106106 }
107107
@@ -133,20 +133,17 @@ where
133133 for txid in registered_txs {
134134 if let Some ( tx_status) = client. get_tx_status ( & txid) . await ? {
135135 if tx_status. confirmed {
136- if let Some ( tx) = client. get_tx ( & txid) . await ? {
137- if let Some ( block_height) = tx_status. block_height {
138- // TODO: Switch to `get_header_by_hash` once released upstream (https://github.com/bitcoindevkit/rust-esplora-client/pull/17)
139- let block_header = client. get_header ( block_height) . await ?;
136+ if let Some ( block_hash) = tx_status. block_hash {
137+ if let Some ( tx) = client. get_tx ( & txid) . await ? {
138+ let block_header = client. get_header_by_hash ( & block_hash) . await ?;
140139 if let Some ( merkle_proof) = client. get_merkle_proof ( & txid) . await ? {
141- if block_height == merkle_proof. block_height {
142- confirmed_txs. push ( (
143- tx,
144- block_height,
145- block_header,
146- merkle_proof. pos ,
147- ) ) ;
148- continue ;
149- }
140+ confirmed_txs. push ( (
141+ tx,
142+ merkle_proof. block_height ,
143+ block_header,
144+ merkle_proof. pos ,
145+ ) ) ;
146+ continue ;
150147 }
151148 }
152149 }
@@ -175,19 +172,19 @@ where
175172 if spending_tx_status. confirmed {
176173 let spending_txid = output_status. txid . unwrap ( ) ;
177174 if let Some ( spending_tx) = client. get_tx ( & spending_txid) . await ? {
178- let block_height = spending_tx_status. block_height . unwrap ( ) ;
179- // TODO: Switch to ` get_header_by_hash` once released upstream (https://github.com/bitcoindevkit/rust-esplora-client/pull/17)
180- let block_header = client . get_header ( block_height ) . await ? ;
181- if let Some ( merkle_proof ) =
182- client . get_merkle_proof ( & spending_txid ) . await ?
183- {
184- confirmed_txs . push ( (
185- spending_tx ,
186- block_height ,
187- block_header ,
188- merkle_proof . pos ,
189- ) ) ;
190- continue ;
175+ if let Some ( block_hash ) = spending_tx_status. block_hash {
176+ let block_header = client . get_header_by_hash ( & block_hash ) . await ? ;
177+ if let Some ( merkle_proof ) =
178+ client . get_merkle_proof ( & spending_txid ) . await ?
179+ {
180+ confirmed_txs . push ( (
181+ spending_tx ,
182+ merkle_proof . block_height ,
183+ block_header ,
184+ merkle_proof . pos ,
185+ ) ) ;
186+ continue ;
187+ }
191188 }
192189 }
193190 }
@@ -315,9 +312,8 @@ where
315312 self . queued_transactions . lock ( ) . unwrap ( ) . push ( * txid) ;
316313 }
317314
318- fn register_output ( & self , output : WatchedOutput ) -> Option < ( usize , Transaction ) > {
315+ fn register_output ( & self , output : WatchedOutput ) {
319316 self . queued_outputs . lock ( ) . unwrap ( ) . push ( output) ;
320- return None ;
321317 }
322318}
323319
0 commit comments