Better handle sync reorgs

This commit is contained in:
arkpar 2016-11-25 12:53:40 +01:00
parent b669dbb390
commit a50974e61b

View File

@ -631,9 +631,13 @@ impl ChainSync {
self.highest_block = Some(header.number()); self.highest_block = Some(header.number());
} }
let mut unknown = false; let mut unknown = false;
let difficulty: U256 = try!(r.val_at(1));
{ {
if let Some(ref mut peer) = self.peers.get_mut(&peer_id) { if let Some(ref mut peer) = self.peers.get_mut(&peer_id) {
peer.latest_hash = header.hash(); peer.latest_hash = header.hash();
if peer.difficulty.map_or(true, |pd| difficulty > pd) {
peer.difficulty = Some(difficulty);
}
} }
} }
if self.last_imported_block > header.number() && self.last_imported_block - header.number() > MAX_NEW_BLOCK_AGE { if self.last_imported_block > header.number() && self.last_imported_block - header.number() > MAX_NEW_BLOCK_AGE {
@ -670,11 +674,9 @@ impl ChainSync {
} else { } else {
trace!(target: "sync", "New unknown block {:?}", h); trace!(target: "sync", "New unknown block {:?}", h);
//TODO: handle too many unknown blocks //TODO: handle too many unknown blocks
let difficulty: U256 = try!(r.val_at(1));
if let Some(ref mut peer) = self.peers.get_mut(&peer_id) { if let Some(ref mut peer) = self.peers.get_mut(&peer_id) {
if peer.difficulty.map_or(true, |pd| difficulty > pd) { if peer.difficulty.map_or(true, |pd| difficulty > pd) {
//self.state = SyncState::ChainHead; //self.state = SyncState::ChainHead;
peer.difficulty = Some(difficulty);
trace!(target: "sync", "Received block {:?} with no known parent. Peer needs syncing...", h); trace!(target: "sync", "Received block {:?} with no known parent. Peer needs syncing...", h);
} }
} }
@ -729,6 +731,7 @@ impl ChainSync {
trace!(target: "sync", "New unknown block hash {:?}", hash); trace!(target: "sync", "New unknown block hash {:?}", hash);
if let Some(ref mut peer) = self.peers.get_mut(&peer_id) { if let Some(ref mut peer) = self.peers.get_mut(&peer_id) {
peer.latest_hash = hash.clone(); peer.latest_hash = hash.clone();
peer.difficulty = None;
} }
max_height = number; max_height = number;
} }
@ -974,6 +977,7 @@ impl ChainSync {
}, },
Err(BlockImportError::Block(BlockError::UnknownParent(_))) if self.state == SyncState::NewBlocks => { Err(BlockImportError::Block(BlockError::UnknownParent(_))) if self.state == SyncState::NewBlocks => {
trace!(target: "sync", "Unknown new block parent, restarting sync"); trace!(target: "sync", "Unknown new block parent, restarting sync");
restart = true;
break; break;
}, },
Err(e) => { Err(e) => {
@ -994,6 +998,11 @@ impl ChainSync {
if self.blocks.is_empty() { if self.blocks.is_empty() {
// complete sync round // complete sync round
trace!(target: "sync", "Sync round complete"); trace!(target: "sync", "Sync round complete");
for (_, ref mut p) in &mut self.peers {
if p.difficulty.is_none() {
p.difficulty = Some(self.syncing_difficulty.clone());
}
}
self.restart(io); self.restart(io);
} }
} }