mirror of
https://github.com/mimblewimble/grin.git
synced 2025-01-21 03:21:08 +03:00
Remember to reset sync head on first transition to header sync (#2222)
Also cleanup reset_sync_head code
This commit is contained in:
parent
602bb25ec9
commit
ea51663f9c
2 changed files with 28 additions and 3 deletions
|
@ -213,6 +213,11 @@ impl Chain {
|
||||||
self.txhashset.clone()
|
self.txhashset.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Shared store instance.
|
||||||
|
pub fn store(&self) -> Arc<store::ChainStore> {
|
||||||
|
self.store.clone()
|
||||||
|
}
|
||||||
|
|
||||||
fn log_heads(store: &store::ChainStore) -> Result<(), Error> {
|
fn log_heads(store: &store::ChainStore) -> Result<(), Error> {
|
||||||
let head = store.head()?;
|
let head = store.head()?;
|
||||||
debug!(
|
debug!(
|
||||||
|
@ -241,6 +246,18 @@ impl Chain {
|
||||||
Ok(())
|
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
|
/// Processes a single block, then checks for orphans, processing
|
||||||
/// those as well if they're found
|
/// those as well if they're found
|
||||||
pub fn process_block(&self, b: Block, opts: Options) -> Result<Option<Tip>, Error> {
|
pub fn process_block(&self, b: Block, opts: Options) -> Result<Option<Tip>, Error> {
|
||||||
|
|
|
@ -55,17 +55,25 @@ impl HeaderSync {
|
||||||
| SyncStatus::HeaderSync { .. }
|
| SyncStatus::HeaderSync { .. }
|
||||||
| SyncStatus::TxHashsetDone => true,
|
| SyncStatus::TxHashsetDone => true,
|
||||||
SyncStatus::NoSync | SyncStatus::Initial | SyncStatus::AwaitingPeers(_) => {
|
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();
|
let sync_head = self.chain.get_sync_head().unwrap();
|
||||||
debug!(
|
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.hash(),
|
||||||
sync_head.height,
|
sync_head.height,
|
||||||
header_head.hash(),
|
header_head.hash(),
|
||||||
header_head.height,
|
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.
|
// Rebuild the sync MMR to match our updated sync_head.
|
||||||
self.chain.rebuild_sync_mmr(&header_head).unwrap();
|
self.chain.rebuild_sync_mmr(&header_head).unwrap();
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue