@@ -76,12 +76,15 @@ where
76
76
pub ( crate ) async fn sync ( & self , confirmables : Vec < & ( dyn Confirm + Sync ) > ) -> Result < ( ) , Error > {
77
77
let client = & * self . blockchain ;
78
78
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 ) ;
80
82
81
83
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
+
85
88
self . sync_transactions_confirmed ( & confirmables) . await ?;
86
89
self . sync_transaction_unconfirmed ( & confirmables) . await ?;
87
90
}
@@ -90,18 +93,15 @@ where
90
93
}
91
94
92
95
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
95
97
) -> Result < ( ) , Error > {
96
98
let client = & * self . blockchain ;
97
99
98
100
// 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 ?;
100
102
for c in confirmables {
101
- c. best_block_updated ( & cur_block_header , cur_height ) ;
103
+ c. best_block_updated ( & tip_block_header , tip_height ) ;
102
104
}
103
-
104
- * * locked_last_sync_height = Some ( cur_height) ;
105
105
Ok ( ( ) )
106
106
}
107
107
@@ -133,20 +133,17 @@ where
133
133
for txid in registered_txs {
134
134
if let Some ( tx_status) = client. get_tx_status ( & txid) . await ? {
135
135
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 ?;
140
139
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 ;
150
147
}
151
148
}
152
149
}
@@ -175,19 +172,19 @@ where
175
172
if spending_tx_status. confirmed {
176
173
let spending_txid = output_status. txid . unwrap ( ) ;
177
174
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
+ }
191
188
}
192
189
}
193
190
}
@@ -315,9 +312,8 @@ where
315
312
self . queued_transactions . lock ( ) . unwrap ( ) . push ( * txid) ;
316
313
}
317
314
318
- fn register_output ( & self , output : WatchedOutput ) -> Option < ( usize , Transaction ) > {
315
+ fn register_output ( & self , output : WatchedOutput ) {
319
316
self . queued_outputs . lock ( ) . unwrap ( ) . push ( output) ;
320
- return None ;
321
317
}
322
318
}
323
319
0 commit comments