Remember to reset sync head on first transition to header sync (#2222)

Also cleanup reset_sync_head code
This commit is contained in:
Antioch Peverell 2018-12-26 21:33:01 +00:00 committed by Ignotus Peverell
parent 602bb25ec9
commit ea51663f9c
2 changed files with 28 additions and 3 deletions

View file

@ -213,6 +213,11 @@ impl Chain {
self.txhashset.clone()
}
/// Shared store instance.
pub fn store(&self) -> Arc<store::ChainStore> {
self.store.clone()
}
fn log_heads(store: &store::ChainStore) -> Result<(), Error> {
let head = store.head()?;
debug!(
@ -241,6 +246,18 @@ impl Chain {
Ok(())
}
/// Reset sync_head to current header_head.
/// We do this when we first transition to header_sync to ensure we extend
/// the "sync" header MMR from a known consistent state and to ensure we track
/// the header chain correctly at the fork point.
pub fn reset_sync_head(&self) -> Result<Tip, Error> {
let batch = self.store.batch()?;
batch.reset_sync_head()?;
let head = batch.get_sync_head()?;
batch.commit()?;
Ok(head)
}
/// Processes a single block, then checks for orphans, processing
/// those as well if they're found
pub fn process_block(&self, b: Block, opts: Options) -> Result<Option<Tip>, Error> {

View file

@ -55,17 +55,25 @@ impl HeaderSync {
| SyncStatus::HeaderSync { .. }
| SyncStatus::TxHashsetDone => true,
SyncStatus::NoSync | SyncStatus::Initial | SyncStatus::AwaitingPeers(_) => {
// Reset sync_head to header_head on transition to HeaderSync,
// but ONLY on initial transition to HeaderSync state.
let sync_head = self.chain.get_sync_head().unwrap();
debug!(
"sync: initial transition to HeaderSync. sync_head: {} at {}, reset to: {} at {}",
"sync: initial transition to HeaderSync. sync_head: {} at {}, resetting to: {} at {}",
sync_head.hash(),
sync_head.height,
header_head.hash(),
header_head.height,
);
// Reset sync_head to header_head on transition to HeaderSync,
// but ONLY on initial transition to HeaderSync state.
//
// The header_head and sync_head may diverge here in the presence of a fork
// in the header chain. Ensure we track the new advertised header chain here
// correctly, so reset any previous (and potentially stale) sync_head to match
// our last known "good" header_head.
//
self.chain.reset_sync_head().unwrap();
// Rebuild the sync MMR to match our updated sync_head.
self.chain.rebuild_sync_mmr(&header_head).unwrap();