@@ -171,7 +171,12 @@ enum ForkStep {
171
171
ConnectBlock ( BlockHeaderData ) ,
172
172
}
173
173
174
- async fn find_fork_step ( steps_tx : & mut Vec < ForkStep > , current_header : BlockHeaderData , prev_header : & BlockHeaderData , block_source : & mut dyn BlockSource , cache : & HeaderCache , mainnet : bool ) -> BlockSourceResult < ( ) > {
174
+ /// Walks backwards from `current_header` and `prev_header`, finding the common ancestor. Returns
175
+ /// the steps needed to produce the chain with `current_header` as its tip from the chain with
176
+ /// `prev_header` as its tip. There is no ordering guarantee between different ForkStep types, but
177
+ /// `DisconnectBlock` and `ConnectBlock` are each returned in height-descending order.
178
+ async fn find_fork ( current_header : BlockHeaderData , prev_header : & BlockHeaderData , block_source : & mut dyn BlockSource , cache : & HeaderCache , mainnet : bool ) -> BlockSourceResult < Vec < ForkStep > > {
179
+ let mut steps_tx = Vec :: new ( ) ;
175
180
let mut current = current_header;
176
181
let mut previous = * prev_header;
177
182
loop {
@@ -184,7 +189,7 @@ async fn find_fork_step(steps_tx: &mut Vec<ForkStep>, current_header: BlockHeade
184
189
if current. height - 1 == previous. height &&
185
190
current. header . prev_blockhash == previous. header . block_hash ( ) {
186
191
steps_tx. push ( ForkStep :: ConnectBlock ( current) ) ;
187
- return Ok ( ( ) ) ;
192
+ break ;
188
193
}
189
194
190
195
// Found a chain fork.
@@ -193,7 +198,7 @@ async fn find_fork_step(steps_tx: &mut Vec<ForkStep>, current_header: BlockHeade
193
198
steps_tx. push ( ForkStep :: DisconnectBlock ( previous) ) ;
194
199
steps_tx. push ( ForkStep :: ConnectBlock ( current) ) ;
195
200
steps_tx. push ( ForkStep :: ForkPoint ( fork_point) ) ;
196
- return Ok ( ( ) ) ;
201
+ break ;
197
202
}
198
203
199
204
// Walk back the chain, finding blocks needed to connect and disconnect. Only walk back the
@@ -209,16 +214,7 @@ async fn find_fork_step(steps_tx: &mut Vec<ForkStep>, current_header: BlockHeade
209
214
current = look_up_prev_header ( block_source, & current, cache, mainnet) . await ?;
210
215
}
211
216
}
212
- }
213
-
214
- /// Walks backwards from current_header and prev_header finding the fork and sending ForkStep events
215
- /// into the steps_tx Sender. There is no ordering guarantee between different ForkStep types, but
216
- /// DisconnectBlock and ConnectBlock events are each in reverse, height-descending order.
217
- async fn find_fork ( current_header : BlockHeaderData , prev_header : & BlockHeaderData , block_source : & mut dyn BlockSource , cache : & HeaderCache , mainnet : bool ) -> BlockSourceResult < Vec < ForkStep > > {
218
- let mut steps_tx = Vec :: new ( ) ;
219
- if current_header. header == prev_header. header { return Ok ( steps_tx) ; }
220
217
221
- find_fork_step ( & mut steps_tx, current_header, & prev_header, block_source, cache, mainnet) . await ?;
222
218
Ok ( steps_tx)
223
219
}
224
220
@@ -325,6 +321,7 @@ async fn sync_chain_monitor<CL: ChainListener + Sized>(new_header: BlockHeaderDa
325
321
/// Once you have them all at the same block, you should switch to using MicroSPVClient.
326
322
pub async fn init_sync_chain_monitor < CL : ChainListener + Sized , B : BlockSource > ( new_block : BlockHash , old_block : BlockHash , block_source : & mut B , chain_notifier : & mut CL ) {
327
323
if & old_block[ ..] == & [ 0 ; 32 ] { return ; }
324
+ if old_block == new_block { return ; }
328
325
329
326
let new_header = block_source. get_header ( & new_block, None ) . await . unwrap ( ) ;
330
327
assert_eq ! ( new_header. header. block_hash( ) , new_block) ;
0 commit comments