From bc0128ab8859874e5fc735ca032b9f21727aaa6f Mon Sep 17 00:00:00 2001 From: Antioch Peverell Date: Wed, 31 Oct 2018 10:29:23 +0000 Subject: [PATCH] be careful resetting sync_head (#1885) * be careful resetting sync_head make sure we have the header corresponding to header_head and that nothing is corrupted from an earlier shutdown * add logging when resetting heads due to missing header --- chain/src/chain.rs | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/chain/src/chain.rs b/chain/src/chain.rs index 9bc893e5f..f08c501c1 100644 --- a/chain/src/chain.rs +++ b/chain/src/chain.rs @@ -1095,8 +1095,26 @@ fn setup_head( Err(e) => return Err(ErrorKind::StoreErr(e, "chain init load head".to_owned()))?, }; - // Reset sync_head to be consistent with current header_head. - batch.reset_sync_head()?; + // Check we have the header corresponding to the header_head. + // If not then something is corrupted and we should reset our header_head. + // Either way we want to reset sync_head to match header_head. + let head = batch.head()?; + let header_head = batch.header_head()?; + if batch.get_block_header(&header_head.last_block_h).is_ok() { + // Reset sync_head to be consistent with current header_head. + batch.reset_sync_head()?; + } else { + // Reset both header_head and sync_head to be consistent with current head. + warn!( + "setup_head: header missing for {}, {}, resetting header_head and sync_head to head: {}, {}", + header_head.last_block_h, + header_head.height, + head.last_block_h, + head.height, + ); + batch.reset_head()?; + } + batch.commit()?; Ok(())