ethcore-sync: fix connection to peers behind chain fork block (#8710)

This commit is contained in:
André Silva 2018-06-02 09:29:44 +01:00 committed by Afri Schoedon
parent 79eb8f7ace
commit 2060ea5de3
2 changed files with 22 additions and 23 deletions

View File

@ -332,14 +332,6 @@ impl SyncHandler {
Ok(()) Ok(())
} }
fn on_peer_confirmed(sync: &mut ChainSync, io: &mut SyncIo, peer_id: PeerId) {
{
let peer = sync.peers.get_mut(&peer_id).expect("Is only called when peer is present in peers");
peer.confirmation = ForkConfirmation::Confirmed;
}
sync.sync_peer(io, peer_id, false);
}
fn on_peer_fork_header(sync: &mut ChainSync, io: &mut SyncIo, peer_id: PeerId, r: &Rlp) -> Result<(), PacketDecodeError> { fn on_peer_fork_header(sync: &mut ChainSync, io: &mut SyncIo, peer_id: PeerId, r: &Rlp) -> Result<(), PacketDecodeError> {
{ {
let peer = sync.peers.get_mut(&peer_id).expect("Is only called when peer is present in peers"); let peer = sync.peers.get_mut(&peer_id).expect("Is only called when peer is present in peers");
@ -349,10 +341,9 @@ impl SyncHandler {
if item_count == 0 || item_count != 1 { if item_count == 0 || item_count != 1 {
trace!(target: "sync", "{}: Chain is too short to confirm the block", peer_id); trace!(target: "sync", "{}: Chain is too short to confirm the block", peer_id);
io.disable_peer(peer_id); peer.confirmation = ForkConfirmation::TooShort;
return Ok(());
}
} else {
let header = r.at(0)?.as_raw(); let header = r.at(0)?.as_raw();
if keccak(&header) != fork_hash { if keccak(&header) != fork_hash {
trace!(target: "sync", "{}: Fork mismatch", peer_id); trace!(target: "sync", "{}: Fork mismatch", peer_id);
@ -361,12 +352,16 @@ impl SyncHandler {
} }
trace!(target: "sync", "{}: Confirmed peer", peer_id); trace!(target: "sync", "{}: Confirmed peer", peer_id);
peer.confirmation = ForkConfirmation::Confirmed;
if !io.chain_overlay().read().contains_key(&fork_number) { if !io.chain_overlay().read().contains_key(&fork_number) {
trace!(target: "sync", "Inserting (fork) block {} header", fork_number); trace!(target: "sync", "Inserting (fork) block {} header", fork_number);
io.chain_overlay().write().insert(fork_number, header.to_vec()); io.chain_overlay().write().insert(fork_number, header.to_vec());
} }
} }
SyncHandler::on_peer_confirmed(sync, io, peer_id); }
sync.sync_peer(io, peer_id, false);
return Ok(()); return Ok(());
} }
@ -686,7 +681,9 @@ impl SyncHandler {
SyncRequester::request_fork_header(sync, io, peer_id, fork_block); SyncRequester::request_fork_header(sync, io, peer_id, fork_block);
}, },
_ => { _ => {
SyncHandler::on_peer_confirmed(sync, io, peer_id); // when there's no `fork_block` defined we initialize the peer with
// `confirmation: ForkConfirmation::Confirmed`.
sync.sync_peer(io, peer_id, false);
} }
} }

View File

@ -294,6 +294,8 @@ pub enum BlockSet {
pub enum ForkConfirmation { pub enum ForkConfirmation {
/// Fork block confirmation pending. /// Fork block confirmation pending.
Unconfirmed, Unconfirmed,
/// Peer's chain is too short to confirm the fork.
TooShort,
/// Fork is confirmed. /// Fork is confirmed.
Confirmed, Confirmed,
} }