Skip to content

Commit b9797d8

Browse files
committed
Use an explicit stack
1 parent 7cf2a5d commit b9797d8

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

src/chain_sync/chain_follower.rs

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -678,31 +678,33 @@ impl<DB: Blockstore> SyncStateMachine<DB> {
678678
// Mark all descendants of tipsets as bad.
679679
// Remove all bad tipsets from the tipset map.
680680
fn mark_bad_tipset(&mut self, tipset: Arc<FullTipset>, reason: String) {
681-
self.tipsets.remove(tipset.key());
682-
// Mark all blocks in the tipset as bad
683-
for block in tipset.blocks() {
684-
self.bad_block_cache.put(*block.cid(), reason.clone());
685-
}
681+
let mut stack = vec![tipset];
686682

687-
// Find all descendant tipsets (tipsets that have this tipset as a parent)
688-
let mut to_remove = Vec::new();
689-
let mut descendants = Vec::new();
683+
while let Some(tipset) = stack.pop() {
684+
self.tipsets.remove(tipset.key());
685+
// Mark all blocks in the tipset as bad
686+
for block in tipset.blocks() {
687+
self.bad_block_cache.put(*block.cid(), reason.clone());
688+
}
689+
690+
// Find all descendant tipsets (tipsets that have this tipset as a parent)
691+
let mut to_remove = Vec::new();
692+
let mut descendants = Vec::new();
690693

691-
for (key, ts) in self.tipsets.iter() {
692-
if ts.parents() == tipset.key() {
693-
to_remove.push(key.clone());
694-
descendants.push(ts.clone());
694+
for (key, ts) in self.tipsets.iter() {
695+
if ts.parents() == tipset.key() {
696+
to_remove.push(key.clone());
697+
descendants.push(ts.clone());
698+
}
695699
}
696-
}
697700

698-
// Remove bad tipsets from the map
699-
for key in to_remove {
700-
self.tipsets.remove(&key);
701-
}
701+
// Remove bad tipsets from the map
702+
for key in to_remove {
703+
self.tipsets.remove(&key);
704+
}
702705

703-
// Recursively mark descendants as bad
704-
for descendant in descendants {
705-
self.mark_bad_tipset(descendant, reason.clone());
706+
// Mark descendants as bad
707+
stack.extend(descendants);
706708
}
707709
}
708710

0 commit comments

Comments
 (0)