Refactor SyncState (#3297)

* Refactor SyncState

Method sync_error() retrun type was simplified.
update_txhashset_download() was  made type safe, which eliminates a runtime enum variant's  check, added an atomic status update
This commit is contained in:
hashmap
2020-04-20 12:30:04 +02:00
committed by GitHub
parent e64e90623b
commit d8c6eef485
7 changed files with 130 additions and 106 deletions
+15 -15
View File
@@ -23,7 +23,9 @@ use std::sync::{Arc, Weak};
use std::thread;
use std::time::Instant;
use crate::chain::{self, BlockStatus, ChainAdapter, Options, SyncState, SyncStatus};
use crate::chain::{
self, BlockStatus, ChainAdapter, Options, SyncState, SyncStatus, TxHashsetDownloadStats,
};
use crate::common::hooks::{ChainEvents, NetEvents};
use crate::common::types::{ChainValidationMode, DandelionEpoch, ServerConfig};
use crate::core::core::hash::{Hash, Hashed};
@@ -399,20 +401,18 @@ impl p2p::ChainAdapter for NetToChainAdapter {
total_size: u64,
) -> bool {
match self.sync_state.status() {
SyncStatus::TxHashsetDownload {
update_time: old_update_time,
downloaded_size: old_downloaded_size,
..
} => self
.sync_state
.update_txhashset_download(SyncStatus::TxHashsetDownload {
start_time,
prev_update_time: old_update_time,
update_time: Utc::now(),
prev_downloaded_size: old_downloaded_size,
downloaded_size,
total_size,
}),
SyncStatus::TxHashsetDownload(prev) => {
self.sync_state
.update_txhashset_download(TxHashsetDownloadStats {
start_time,
prev_update_time: prev.update_time,
update_time: Utc::now(),
prev_downloaded_size: prev.downloaded_size,
downloaded_size,
total_size,
});
true
}
_ => false,
}
}
+23 -31
View File
@@ -68,13 +68,9 @@ impl StateSync {
let mut sync_need_restart = false;
// check sync error
{
let clone = self.sync_state.sync_error();
if let Some(ref sync_error) = *clone.read() {
error!("state_sync: error = {:?}. restart fast sync", sync_error);
sync_need_restart = true;
}
drop(clone);
if let Some(sync_error) = self.sync_state.sync_error() {
error!("state_sync: error = {}. restart fast sync", sync_error);
sync_need_restart = true;
}
// check peer connection status of this sync
@@ -92,15 +88,16 @@ impl StateSync {
// if txhashset downloaded and validated successfully, we switch to BodySync state,
// and we need call state_sync_reset() to make it ready for next possible state sync.
let done = if let SyncStatus::TxHashsetDone = self.sync_state.status() {
self.sync_state.update(SyncStatus::BodySync {
let done = self.sync_state.update_if(
SyncStatus::BodySync {
current_height: 0,
highest_height: 0,
});
true
} else {
false
};
},
|s| match s {
SyncStatus::TxHashsetDone => true,
_ => false,
},
);
if sync_need_restart || done {
self.state_sync_reset();
@@ -137,24 +134,19 @@ impl StateSync {
// to avoid the confusing log,
// update the final HeaderSync state mainly for 'current_height'
{
let status = self.sync_state.status();
if let SyncStatus::HeaderSync { .. } = status {
self.sync_state.update(SyncStatus::HeaderSync {
current_height: header_head.height,
highest_height,
});
}
}
self.sync_state.update_if(
SyncStatus::HeaderSync {
current_height: header_head.height,
highest_height,
},
|s| match s {
SyncStatus::HeaderSync { .. } => true,
_ => false,
},
);
self.sync_state.update(SyncStatus::TxHashsetDownload {
start_time: Utc::now(),
prev_update_time: Utc::now(),
update_time: Utc::now(),
prev_downloaded_size: 0,
downloaded_size: 0,
total_size: 0,
});
self.sync_state
.update(SyncStatus::TxHashsetDownload(Default::default()));
}
}
true