zebrad: unify sync restart logic

This lets us keep the main loop simple and just write `continue 'sync;`
to keep going.
This commit is contained in:
Henry de Valence 2020-10-21 19:30:22 -07:00
parent 12d25159c6
commit 56fe4f4379
1 changed files with 24 additions and 16 deletions

View File

@ -168,18 +168,26 @@ where
// due to protocol limitations // due to protocol limitations
self.request_genesis().await?; self.request_genesis().await?;
// Distinguishes a restart from a start, so we don't delay when starting
// the sync process, but we can keep restart logic in one place.
let mut started_once = false;
'sync: loop { 'sync: loop {
// Wipe state from prevous iterations. if started_once {
tracing::info!(timeout = ?SYNC_RESTART_TIMEOUT, "waiting to restart sync");
self.prospective_tips = HashSet::new(); self.prospective_tips = HashSet::new();
self.downloads.cancel_all(); self.downloads.cancel_all();
self.update_metrics(); self.update_metrics();
delay_for(SYNC_RESTART_TIMEOUT).await;
} else {
started_once = true;
}
tracing::info!("starting sync, obtaining new tips"); tracing::info!("starting sync, obtaining new tips");
if self.obtain_tips().await.is_err() || self.prospective_tips.is_empty() { if let Err(e) = self.obtain_tips().await {
tracing::warn!("failed to obtain tips, waiting to restart sync"); tracing::warn!(?e);
delay_for(SYNC_RESTART_TIMEOUT).await;
continue 'sync; continue 'sync;
}; }
self.update_metrics(); self.update_metrics();
while !self.prospective_tips.is_empty() { while !self.prospective_tips.is_empty() {
@ -190,8 +198,7 @@ where
tracing::trace!(?hash, "verified and committed block to state"); tracing::trace!(?hash, "verified and committed block to state");
} }
Err(e) => { Err(e) => {
tracing::warn!(?e, "waiting to restart sync"); tracing::warn!(?e);
delay_for(SYNC_RESTART_TIMEOUT).await;
continue 'sync; continue 'sync;
} }
} }
@ -223,8 +230,7 @@ where
tracing::trace!(?hash, "verified and committed block to state"); tracing::trace!(?hash, "verified and committed block to state");
} }
Err(e) => { Err(e) => {
tracing::warn!(?e, "waiting to restart sync"); tracing::warn!(?e);
delay_for(SYNC_RESTART_TIMEOUT).await;
continue 'sync; continue 'sync;
} }
} }
@ -239,12 +245,14 @@ where
"extending tips", "extending tips",
); );
let _ = self.extend_tips().await; if let Err(e) = self.extend_tips().await {
tracing::warn!(?e);
continue 'sync;
}
self.update_metrics(); self.update_metrics();
} }
tracing::info!("exhausted tips, waiting to restart sync"); tracing::info!("exhausted prospective tip set");
delay_for(SYNC_RESTART_TIMEOUT).await;
} }
} }
@ -479,7 +487,7 @@ where
} }
Ok(_) => unreachable!("network returned wrong response"), Ok(_) => unreachable!("network returned wrong response"),
// We ignore this error because we made multiple fanout requests. // We ignore this error because we made multiple fanout requests.
Err(e) => tracing::debug!("{:?}", e), Err(e) => tracing::debug!(?e),
} }
} }
} }