Skip to content

Commit 6f382b4

Browse files
committed
Merge find_fork and find_fork_step
1 parent bd13e30 commit 6f382b4

File tree

1 file changed

+9
-12
lines changed
  • lightning-block-sync/src

1 file changed

+9
-12
lines changed

lightning-block-sync/src/lib.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,12 @@ enum ForkStep {
171171
ConnectBlock(BlockHeaderData),
172172
}
173173

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();
175180
let mut current = current_header;
176181
let mut previous = *prev_header;
177182
loop {
@@ -184,7 +189,7 @@ async fn find_fork_step(steps_tx: &mut Vec<ForkStep>, current_header: BlockHeade
184189
if current.height - 1 == previous.height &&
185190
current.header.prev_blockhash == previous.header.block_hash() {
186191
steps_tx.push(ForkStep::ConnectBlock(current));
187-
return Ok(());
192+
break;
188193
}
189194

190195
// Found a chain fork.
@@ -193,7 +198,7 @@ async fn find_fork_step(steps_tx: &mut Vec<ForkStep>, current_header: BlockHeade
193198
steps_tx.push(ForkStep::DisconnectBlock(previous));
194199
steps_tx.push(ForkStep::ConnectBlock(current));
195200
steps_tx.push(ForkStep::ForkPoint(fork_point));
196-
return Ok(());
201+
break;
197202
}
198203

199204
// 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
209214
current = look_up_prev_header(block_source, &current, cache, mainnet).await?;
210215
}
211216
}
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); }
220217

221-
find_fork_step(&mut steps_tx, current_header, &prev_header, block_source, cache, mainnet).await?;
222218
Ok(steps_tx)
223219
}
224220

@@ -325,6 +321,7 @@ async fn sync_chain_monitor<CL: ChainListener + Sized>(new_header: BlockHeaderDa
325321
/// Once you have them all at the same block, you should switch to using MicroSPVClient.
326322
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) {
327323
if &old_block[..] == &[0; 32] { return; }
324+
if old_block == new_block { return; }
328325

329326
let new_header = block_source.get_header(&new_block, None).await.unwrap();
330327
assert_eq!(new_header.header.block_hash(), new_block);

0 commit comments

Comments
 (0)