Fix fast sync mode failure when 'sync_state' was switched to 'HeaderSync' (#1286)

This commit is contained in:
Gary Yu 2018-07-24 01:37:35 +08:00 committed by Ignotus Peverell
parent f5b03a6d5a
commit 3b6b85ec57
2 changed files with 46 additions and 15 deletions

View file

@ -229,24 +229,45 @@ impl MessageHandler for Protocol {
let sm_arch: TxHashSetArchive = msg.body()?; let sm_arch: TxHashSetArchive = msg.body()?;
debug!( debug!(
LOGGER, LOGGER,
"handle_payload: txhashset archive for {} at {}", "handle_payload: txhashset archive for {} at {}. size={}",
sm_arch.hash, sm_arch.hash,
sm_arch.height, sm_arch.height,
sm_arch.bytes,
); );
if !self.adapter.txhashset_receive_ready() { if !self.adapter.txhashset_receive_ready() {
error!(
LOGGER,
"handle_payload: txhashset archive received but SyncStatus not on TxHashsetDownload",
);
return Err(Error::BadMessage); return Err(Error::BadMessage);
} }
let mut tmp = env::temp_dir(); let mut tmp = env::temp_dir();
tmp.push("txhashset.zip"); tmp.push("txhashset.zip");
{ let mut save_txhashset_to_file = |file| -> Result<(), Error> {
let mut tmp_zip = File::create(tmp.clone())?; let mut tmp_zip = File::create(file)?;
msg.copy_attachment(sm_arch.bytes as usize, &mut tmp_zip)?; msg.copy_attachment(sm_arch.bytes as usize, &mut tmp_zip)?;
tmp_zip.sync_all()?; tmp_zip.sync_all()?;
Ok(())
};
if let Err(e) = save_txhashset_to_file(tmp.clone()){
error!(
LOGGER,
"handle_payload: txhashset archive save to file fail. err={:?}",
e
);
return Err(e);
} }
trace!(
LOGGER,
"handle_payload: txhashset archive save to file {:?} success",
tmp,
);
let tmp_zip = File::open(tmp)?; let tmp_zip = File::open(tmp)?;
self.adapter.txhashset_write( let res = self.adapter.txhashset_write(
sm_arch.hash, sm_arch.hash,
tmp_zip, tmp_zip,
self.addr, self.addr,
@ -254,9 +275,10 @@ impl MessageHandler for Protocol {
debug!( debug!(
LOGGER, LOGGER,
"handle_payload: txhashset archive for {} at {}, DONE", "handle_payload: txhashset archive for {} at {}, DONE. Data Ok: {}",
sm_arch.hash, sm_arch.hash,
sm_arch.height sm_arch.height,
res
); );
Ok(None) Ok(None)

View file

@ -119,8 +119,14 @@ pub fn run_sync(
// run the header sync every 10s // run the header sync every 10s
if si.header_sync_due(&header_head) { if si.header_sync_due(&header_head) {
header_sync(peers.clone(), chain.clone()); let status = sync_state.status();
sync_state.update(SyncStatus::HeaderSync{current_height: header_head.height, highest_height: si.highest_height}); match status{
SyncStatus::TxHashsetDownload => (),
_ => {
header_sync(peers.clone(), chain.clone());
sync_state.update(SyncStatus::HeaderSync{current_height: header_head.height, highest_height: si.highest_height});
}
};
} }
if fast_sync_enabled { if fast_sync_enabled {
@ -252,12 +258,6 @@ fn fast_sync(peers: Arc<Peers>, chain: Arc<chain::Chain>, header_head: &chain::T
if let Some(peer) = peers.most_work_peer() { if let Some(peer) = peers.most_work_peer() {
if let Ok(p) = peer.try_read() { if let Ok(p) = peer.try_read() {
debug!(
LOGGER,
"Header head before txhashset request: {} / {}",
header_head.height,
header_head.last_block_h
);
// ask for txhashset at 90% of horizon, this still leaves time for download // ask for txhashset at 90% of horizon, this still leaves time for download
// and validation to happen and stay within horizon // and validation to happen and stay within horizon
@ -265,7 +265,16 @@ fn fast_sync(peers: Arc<Peers>, chain: Arc<chain::Chain>, header_head: &chain::T
for _ in 0..(horizon - horizon / 10) { for _ in 0..(horizon - horizon / 10) {
txhashset_head = chain.get_block_header(&txhashset_head.previous).unwrap(); txhashset_head = chain.get_block_header(&txhashset_head.previous).unwrap();
} }
p.send_txhashset_request(txhashset_head.height, txhashset_head.hash()) let bhash = txhashset_head.hash();
debug!(
LOGGER,
"fast_sync: before txhashset request, header head: {} / {}, txhashset_head: {} / {}",
header_head.height,
header_head.last_block_h,
txhashset_head.height,
bhash
);
p.send_txhashset_request(txhashset_head.height, bhash)
.unwrap(); .unwrap();
} }
} }