Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sync): address a couple suboptimalities #3099

Merged
merged 3 commits into from
Aug 7, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 14 additions & 15 deletions chain/chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -825,8 +825,18 @@ impl Chain {
return Ok(BlockSyncResponse::None);
}

// Find common block between header chain and block chain.
let mut oldest_height = header_head.height;
let next_epoch_id =
self.get_block_header(&block_head.last_block_hash)?.next_epoch_id().clone();

// Don't run State Sync if header head is not more than one epoch ahead.
if block_head.epoch_id != header_head.epoch_id && next_epoch_id != header_head.epoch_id {
if block_head.height < header_head.height.saturating_sub(block_fetch_horizon) {
// Epochs are different and we are too far from horizon, State Sync is needed
return Ok(BlockSyncResponse::StateNeeded);
}
}

// Find hashes of blocks to sync
let mut current = self.get_block_header(&header_head.last_block_hash).map(|h| h.clone());
while let Ok(header) = current {
if header.height() <= block_head.height {
Expand All @@ -835,21 +845,9 @@ impl Chain {
}
}

oldest_height = header.height();
hashes.push(*header.hash());
current = self.get_previous_header(&header).map(|h| h.clone());
}
let next_epoch_id =
self.get_block_header(&block_head.last_block_hash)?.next_epoch_id().clone();

// Don't run State Sync if header head is not more than one epoch ahead.
if block_head.epoch_id != header_head.epoch_id && next_epoch_id != header_head.epoch_id {
let sync_head = self.sync_head()?;
if oldest_height < sync_head.height.saturating_sub(block_fetch_horizon) {
// Epochs are different and we are too far from horizon, State Sync is needed
return Ok(BlockSyncResponse::StateNeeded);
}
}

// Sort hashes by height
hashes.reverse();
Expand Down Expand Up @@ -904,10 +902,11 @@ impl Chain {
}

pub fn reset_data_pre_state_sync(&mut self, sync_hash: CryptoHash) -> Result<(), Error> {
let head = self.head()?;
// Get header we were syncing into.
let header = self.get_block_header(&sync_hash)?;
let prev_hash = *header.prev_hash();
let gc_height = header.height();
let gc_height = std::cmp::min(head.height + 1, header.height());

// GC all the data from current tail up to `gc_height`
let tail = self.store.tail()?;
Expand Down