Error handling fixes sync

And error in the sync run loop would just exit it silently. Abort
with a panic instead.

Peer errors (like disconnect) on locator request was bubling up in
the loop. Just logging instead.
This commit is contained in:
Ignotus Peverell 2017-11-21 15:12:38 -05:00
parent 6352dfbac9
commit c325c99b87
No known key found for this signature in database
GPG key ID: 99CD25F39F8F8211
2 changed files with 18 additions and 14 deletions

View file

@ -292,7 +292,10 @@ impl NetToChainAdapter {
let _ = thread::Builder::new() let _ = thread::Builder::new()
.name("syncer".to_string()) .name("syncer".to_string())
.spawn(move || { .spawn(move || {
let _ = arc_sync.run(); let res = arc_sync.run();
if let Err(e) = arc_sync.run() {
panic!("Error during sync, aborting: {:?}", e);
}
}); });
} }

View file

@ -101,7 +101,7 @@ impl Syncer {
// main syncing loop, requests more headers and bodies periodically as long // main syncing loop, requests more headers and bodies periodically as long
// as a peer with higher difficulty exists and we're not fully caught up // as a peer with higher difficulty exists and we're not fully caught up
info!(LOGGER, "Starting sync loop."); info!(LOGGER, "Sync: Starting loop.");
loop { loop {
let tip = self.chain.get_header_head()?; let tip = self.chain.get_header_head()?;
@ -160,7 +160,7 @@ impl Syncer {
let mut blocks_to_download = self.blocks_to_download.lock().unwrap(); let mut blocks_to_download = self.blocks_to_download.lock().unwrap();
// go back the chain and insert for download all blocks we only have the // go back the chain and insert for download all blocks we only have the
// head for // head for
let mut prev_h = header_head.last_block_h; let mut prev_h = header_head.last_block_h;
while prev_h != full_head.last_block_h { while prev_h != full_head.last_block_h {
let header = self.chain.get_block_header(&prev_h)?; let header = self.chain.get_block_header(&prev_h)?;
@ -173,7 +173,7 @@ impl Syncer {
debug!( debug!(
LOGGER, LOGGER,
"Added {} full block hashes to download.", "Sync: Added {} full block hashes to download.",
blocks_to_download.len() blocks_to_download.len()
); );
Ok(()) Ok(())
@ -195,7 +195,7 @@ impl Syncer {
if download.retries < (elapsed / 5) as u8 { if download.retries < (elapsed / 5) as u8 {
debug!( debug!(
LOGGER, LOGGER,
"Retry {} on block {}", "Sync: Retry {} on block {}",
download.retries, download.retries,
download.hash download.hash
); );
@ -205,7 +205,7 @@ impl Syncer {
} }
// consume hashes from blocks to download, place them in downloading and // consume hashes from blocks to download, place them in downloading and
// request them from the network // request them from the network
let mut count = 0; let mut count = 0;
while blocks_to_download.len() > 0 && blocks_downloading.len() < MAX_BODY_DOWNLOADS { while blocks_to_download.len() > 0 && blocks_downloading.len() < MAX_BODY_DOWNLOADS {
let h = blocks_to_download.pop().unwrap(); let h = blocks_to_download.pop().unwrap();
@ -219,7 +219,7 @@ impl Syncer {
} }
debug!( debug!(
LOGGER, LOGGER,
"Requested {} full blocks to download, total left: {}. Current list: {:?}.", "Sync: Requested {} full blocks to download, total left: {}. Current list: {:?}.",
count, count,
blocks_to_download.len(), blocks_to_download.len(),
blocks_downloading.deref(), blocks_downloading.deref(),
@ -249,13 +249,15 @@ impl Syncer {
let p = p.read().unwrap(); let p = p.read().unwrap();
debug!( debug!(
LOGGER, LOGGER,
"Asking peer {} for more block headers, locator: {:?}", "Sync: Asking peer {} for more block headers, locator: {:?}",
p.info.addr, p.info.addr,
locator, locator,
); );
p.send_header_request(locator)?; if let Err(e) = p.send_header_request(locator) {
debug!(LOGGER, "Sync: peer error, will retry");
}
} else { } else {
warn!(LOGGER, "Could not get most worked peer to request headers."); warn!(LOGGER, "Sync: Could not get most worked peer to request headers.");
} }
Ok(()) Ok(())
} }
@ -294,7 +296,7 @@ impl Syncer {
// both nodes share at least one common header hash in the locator // both nodes share at least one common header hash in the locator
heights.push(0); heights.push(0);
debug!(LOGGER, "Loc heights: {:?}", heights); debug!(LOGGER, "Sync: Loc heights: {:?}", heights);
// Iteratively travel the header chain back from our head and retain the // Iteratively travel the header chain back from our head and retain the
// headers at the wanted heights. // headers at the wanted heights.
@ -316,9 +318,8 @@ impl Syncer {
fn request_block(&self, h: Hash) { fn request_block(&self, h: Hash) {
let peer = self.p2p.random_peer().unwrap(); let peer = self.p2p.random_peer().unwrap();
let peer = peer.read().unwrap(); let peer = peer.read().unwrap();
let send_result = peer.send_block_request(h); if let Err(e) = peer.send_block_request(h) {
if let Err(e) = send_result { debug!(LOGGER, "Sync: Error requesting block: {:?}", e);
debug!(LOGGER, "Error requesting block: {:?}", e);
} }
} }
} }