fix: reset sync_head to header_head on initial transition to HeaderSync (#1531)

* fix for bug #1524, reset sync_head to header_head on initial transition to HeaderSync.
* using @antiochp fix solution in #1539
This commit is contained in:
Gary Yu 2018-09-20 07:29:24 +08:00 committed by GitHub
parent 56ffbee7e9
commit e1c8dc5a3a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 11 deletions

View file

@ -931,6 +931,14 @@ impl Chain {
.block_exists(&h)
.map_err(|e| ErrorKind::StoreErr(e, "chain block exists".to_owned()).into())
}
/// reset sync_head to header_head
pub fn init_sync_head(&self, header_head: &Tip) -> Result<(), Error> {
let batch = self.store.batch()?;
batch.init_sync_head(header_head)?;
batch.commit()?;
Ok(())
}
}
fn setup_head(

View file

@ -215,9 +215,15 @@ pub fn sync_block_header(
validate_header(&bh, sync_ctx)?;
add_block_header(bh, batch)?;
// now update the header_head (if new header with most work) and the sync_head
// (always)
// Update header_head (but only if this header increases our total known work).
// i.e. Only if this header is now the head of the current "most work" chain.
update_header_head(bh, header_ctx, batch)?;
// Update sync_head regardless of total work.
// We may be syncing a long fork that will *eventually* increase the work
// and become the "most work" chain.
// header_head and sync_head will diverge in this situation until we switch to
// a single "most work" chain.
update_sync_head(bh, sync_ctx, batch)
}

View file

@ -141,18 +141,34 @@ pub fn run_sync(
// run the header sync every 10s
if si.header_sync_due(&header_head) {
header_sync(peers.clone(), chain.clone(), &mut history_locators);
let status = sync_state.status();
match status {
SyncStatus::TxHashsetDownload => (),
_ => {
sync_state.update(SyncStatus::HeaderSync {
current_height: header_head.height,
highest_height: si.highest_height,
});
let update_sync_state = match status {
SyncStatus::TxHashsetDownload => false,
SyncStatus::NoSync | SyncStatus::Initial => {
// Reset sync_head to header_head on transition to HeaderSync,
// but ONLY on initial transition to HeaderSync state.
let sync_head = chain.get_sync_head().unwrap();
debug!(
LOGGER,
"sync: initial transition to HeaderSync. sync_head: {} at {}, reset to: {} at {}",
sync_head.hash(),
sync_head.height,
header_head.hash(),
header_head.height,
);
chain.init_sync_head(&header_head).unwrap();
history_locators.clear();
true
}
_ => true,
};
if update_sync_state {
sync_state.update(SyncStatus::HeaderSync {
current_height: header_head.height,
highest_height: si.highest_height,
});
}
header_sync(peers.clone(), chain.clone(), &mut history_locators);
}
if fast_sync_enabled {