Skip to content

Commit

Permalink
Fix debug assertion failures in networking when genesis root mismatch (
Browse files Browse the repository at this point in the history
…#1778)

* Fix debug assertion failures in networking when genesis root mismatch

* Revert changes to demo

* More fixing
  • Loading branch information
tomaka authored Dec 13, 2021
1 parent 409ea55 commit eefde06
Showing 1 changed file with 32 additions and 25 deletions.
57 changes: 32 additions & 25 deletions src/network/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@ struct NextEventGuarded {
to_process_pre_event: Option<peers::Event<multiaddr::Multiaddr>>,

/// Tuples of `(peer_id, chain_index)` that have been reported as open to the API user.
///
/// This is a subset of the block announce notification protocol substreams that are open.
/// Some substreams might have been opened and have been left out of this map if their
/// handshake was invalid, or had a different genesis hash, or similar problem.
open_chains: hashbrown::HashSet<(PeerId, usize), ahash::RandomState>,
}

Expand Down Expand Up @@ -1534,35 +1538,38 @@ where
)
.await;

// Update the k-buckets, marking the peer as disconnected.
{
let mut ephemeral_guarded = self.ephemeral_guarded.lock().await;
if let Some(mut entry) = ephemeral_guarded.chains[chain_index]
.kbuckets
.entry(peer_id)
.into_occupied()
// The chain is now considered as closed.
let was_open = guarded.open_chains.remove(&(peer_id.clone(), chain_index)); // TODO: cloning :(

if was_open {
// Update the k-buckets, marking the peer as disconnected.
{
entry.set_state(&now, kademlia::kbuckets::PeerState::Disconnected);
let mut ephemeral_guarded = self.ephemeral_guarded.lock().await;
if let Some(mut entry) = ephemeral_guarded.chains[chain_index]
.kbuckets
.entry(peer_id)
.into_occupied()
{
entry.set_state(&now, kademlia::kbuckets::PeerState::Disconnected);
}
}
}

// The chain is now considered as closed.
let _was_removed = guarded.open_chains.remove(&(peer_id.clone(), chain_index)); // TODO: cloning :(
debug_assert!(_was_removed);

// As a slot has been unassigned, wake up the discovery process in order for
// it to be filled.
// TODO: correct?
// TODO: if necessary, mark another peer+substream tuple as desired to fill a slot
self.start_connect_needed.notify_additional(1);
// As a slot has been unassigned, wake up the discovery process in order for
// it to be filled.
// TODO: correct?
// TODO: if necessary, mark another peer+substream tuple as desired to fill a slot
self.start_connect_needed.notify_additional(1);

return Event::ChainDisconnected {
chain_index,
peer_id: match guarded.to_process_pre_event.take().unwrap() {
peers::Event::NotificationsOutClose { peer_id, .. } => peer_id,
_ => unreachable!(),
},
};
return Event::ChainDisconnected {
chain_index,
peer_id: match guarded.to_process_pre_event.take().unwrap() {
peers::Event::NotificationsOutClose { peer_id, .. } => peer_id,
_ => unreachable!(),
},
};
} else {
guarded.to_process_pre_event = None;
}
}

// Other protocol.
Expand Down

0 comments on commit eefde06

Please sign in to comment.