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(())