fix: init_sync_head was not behaving as expected (#1624)

Fix #1612
This commit is contained in:
Antioch Peverell 2018-10-01 16:03:06 +01:00 committed by Gary Yu
parent d8ca684260
commit 4d2cbe6596
3 changed files with 9 additions and 17 deletions

View file

@ -975,10 +975,10 @@ impl Chain {
.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> {
/// Reset sync_head to the provided head.
pub fn reset_sync_head(&self, head: &Tip) -> Result<(), Error> {
let batch = self.store.batch()?;
batch.init_sync_head(header_head)?;
batch.save_sync_head(head)?;
batch.commit()?;
Ok(())
}
@ -1074,14 +1074,13 @@ fn setup_head(
Ok(())
})?;
head = tip;
info!(LOGGER, "chain: init: saved genesis: {:?}", genesis.hash());
}
Err(e) => return Err(ErrorKind::StoreErr(e, "chain init load head".to_owned()))?,
};
// Initialize header_head and sync_head as necessary for chain init.
batch.init_sync_head(&head)?;
batch.reset_head()?;
batch.commit()?;
Ok(())

View file

@ -206,21 +206,14 @@ impl<'a> Batch<'a> {
self.db.put_ser(&vec![SYNC_HEAD_PREFIX], t)
}
pub fn init_sync_head(&self, t: &Tip) -> Result<(), Error> {
let header_tip = match self.store.get_header_head() {
Ok(hh) => hh,
Err(store::Error::NotFoundErr(_)) => {
self.save_header_head(t)?;
t.clone()
}
Err(e) => return Err(e),
};
self.save_sync_head(&header_tip)
pub fn reset_sync_head(&self) -> Result<(), Error> {
let head = self.get_header_head()?;
self.save_sync_head(&head)
}
// Reset both header_head and sync_head to the current head of the body chain
pub fn reset_head(&self) -> Result<(), Error> {
let tip = self.store.head()?;
let tip = self.head()?;
self.save_header_head(&tip)?;
self.save_sync_head(&tip)
}

View file

@ -213,7 +213,7 @@ fn do_header_sync(
header_head.hash(),
header_head.height,
);
chain.init_sync_head(&header_head).unwrap();
chain.reset_sync_head(&header_head).unwrap();
history_locators.clear();
true
}