-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove DiscoveryEvent
and use KademliaEvent
instead
#1377
Changes from 4 commits
bebf65d
ac568be
2b4dcce
6a294fa
bb1b0bf
d20b5be
caa43c3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,6 @@ use libp2p::{ | |
handler::KademliaHandlerProto, | ||
store::MemoryStore, | ||
Kademlia, | ||
KademliaEvent, | ||
QueryId, | ||
}, | ||
mdns::Event as MdnsEvent, | ||
|
@@ -27,11 +26,9 @@ use libp2p::{ | |
Multiaddr, | ||
PeerId, | ||
}; | ||
use libp2p_kad::KademliaEvent; | ||
use std::{ | ||
collections::{ | ||
HashSet, | ||
VecDeque, | ||
}, | ||
collections::HashSet, | ||
pin::Pin, | ||
task::{ | ||
Context, | ||
|
@@ -46,19 +43,6 @@ pub use discovery_config::DiscoveryConfig; | |
|
||
const SIXTY_SECONDS: Duration = Duration::from_secs(60); | ||
|
||
/// Event generated by the `DiscoveryBehaviour`. | ||
#[derive(Debug)] | ||
pub enum DiscoveryEvent { | ||
/// Notify the swarm of an UnroutablePeer | ||
UnroutablePeer(PeerId), | ||
|
||
/// Notify the swarm of a connected peer and its addresses | ||
PeerInfoOnConnect { | ||
peer_id: PeerId, | ||
addresses: Vec<Multiaddr>, | ||
}, | ||
} | ||
|
||
/// NetworkBehavior for discovery of nodes | ||
pub struct DiscoveryBehaviour { | ||
/// List of bootstrap nodes and their addresses | ||
|
@@ -70,9 +54,6 @@ pub struct DiscoveryBehaviour { | |
/// Track the connected peers | ||
connected_peers: HashSet<PeerId>, | ||
|
||
/// Events to report to the swarm | ||
pending_events: VecDeque<DiscoveryEvent>, | ||
|
||
/// For discovery on local network, optionally available | ||
mdns: MdnsWrapper, | ||
|
||
|
@@ -103,7 +84,7 @@ impl DiscoveryBehaviour { | |
|
||
impl NetworkBehaviour for DiscoveryBehaviour { | ||
type ConnectionHandler = KademliaHandlerProto<QueryId>; | ||
type OutEvent = DiscoveryEvent; | ||
type OutEvent = KademliaEvent; | ||
|
||
// Initializes new handler on a new opened connection | ||
fn new_handler(&mut self) -> Self::ConnectionHandler { | ||
|
@@ -131,13 +112,6 @@ impl NetworkBehaviour for DiscoveryBehaviour { | |
}) => { | ||
if *other_established == 0 { | ||
self.connected_peers.insert(*peer_id); | ||
let addresses = self.addresses_of_peer(peer_id); | ||
|
||
self.pending_events | ||
.push_back(DiscoveryEvent::PeerInfoOnConnect { | ||
peer_id: *peer_id, | ||
addresses, | ||
}); | ||
|
||
trace!("Connected to a peer {:?}", peer_id); | ||
} | ||
|
@@ -163,10 +137,6 @@ impl NetworkBehaviour for DiscoveryBehaviour { | |
cx: &mut Context<'_>, | ||
params: &mut impl PollParameters, | ||
) -> Poll<NetworkBehaviourAction<Self::OutEvent, Self::ConnectionHandler>> { | ||
if let Some(next_event) = self.pending_events.pop_front() { | ||
return Poll::Ready(NetworkBehaviourAction::GenerateEvent(next_event)) | ||
} | ||
|
||
// if random walk is enabled poll the stream that will fire when random walk is scheduled | ||
if let Some(next_kad_random_query) = self.next_kad_random_walk.as_mut() { | ||
while next_kad_random_query.poll_unpin(cx).is_ready() { | ||
|
@@ -184,51 +154,20 @@ impl NetworkBehaviour for DiscoveryBehaviour { | |
} | ||
} | ||
|
||
// poll Kademlia behaviour | ||
while let Poll::Ready(kad_action) = self.kademlia.poll(cx, params) { | ||
// poll sub-behaviors | ||
if let Poll::Ready(kad_action) = self.kademlia.poll(cx, params) { | ||
match kad_action { | ||
NetworkBehaviourAction::GenerateEvent( | ||
KademliaEvent::UnroutablePeer { peer }, | ||
) => { | ||
return Poll::Ready(NetworkBehaviourAction::GenerateEvent( | ||
DiscoveryEvent::UnroutablePeer(peer), | ||
)) | ||
} | ||
|
||
NetworkBehaviourAction::Dial { handler, opts } => { | ||
return Poll::Ready(NetworkBehaviourAction::Dial { handler, opts }) | ||
} | ||
NetworkBehaviourAction::CloseConnection { | ||
peer_id, | ||
connection, | ||
} => { | ||
return Poll::Ready(NetworkBehaviourAction::CloseConnection { | ||
peer_id, | ||
connection, | ||
}) | ||
} | ||
NetworkBehaviourAction::NotifyHandler { | ||
peer_id, | ||
handler, | ||
event, | ||
} => { | ||
return Poll::Ready(NetworkBehaviourAction::NotifyHandler { | ||
peer_id, | ||
handler, | ||
event, | ||
}) | ||
} | ||
NetworkBehaviourAction::ReportObservedAddr { address, score } => { | ||
return Poll::Ready(NetworkBehaviourAction::ReportObservedAddr { | ||
address, | ||
score, | ||
}) | ||
} | ||
_ => {} | ||
NetworkBehaviourAction::GenerateEvent(event) => match event { | ||
KademliaEvent::UnroutablePeer { peer } => { | ||
Poll::Ready(NetworkBehaviourAction::GenerateEvent( | ||
KademliaEvent::UnroutablePeer { peer }, | ||
)) | ||
} | ||
_ => Poll::Pending, | ||
}, | ||
_ => Poll::Ready(kad_action), | ||
} | ||
} | ||
|
||
while let Poll::Ready(mdns_event) = self.mdns.poll(cx, params) { | ||
} else if let Poll::Ready(mdns_event) = self.mdns.poll(cx, params) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, the previous iteration would iterate over all mdns events. Let's say, if we had many While right not it is not true. I'm okay to use |
||
match mdns_event { | ||
NetworkBehaviourAction::GenerateEvent(MdnsEvent::Discovered(list)) => { | ||
// inform kademlia of newly discovered local peers | ||
|
@@ -238,27 +177,26 @@ impl NetworkBehaviour for DiscoveryBehaviour { | |
self.kademlia.add_address(&peer_id, multiaddr); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah. if self.connected_peers.len() < self.max_peers_connected {
for (peer_id, multiaddr) in list {
self.kademlia.add_address(&peer_id, multiaddr);
}
} doesn't even work as intended, I think, because it only checks before you add the first peer. |
||
} | ||
} | ||
Poll::Pending | ||
} | ||
NetworkBehaviourAction::ReportObservedAddr { address, score } => { | ||
return Poll::Ready(NetworkBehaviourAction::ReportObservedAddr { | ||
Poll::Ready(NetworkBehaviourAction::ReportObservedAddr { | ||
address, | ||
score, | ||
}) | ||
} | ||
NetworkBehaviourAction::CloseConnection { | ||
peer_id, | ||
connection, | ||
} => { | ||
return Poll::Ready(NetworkBehaviourAction::CloseConnection { | ||
peer_id, | ||
connection, | ||
}) | ||
} | ||
_ => {} | ||
} => Poll::Ready(NetworkBehaviourAction::CloseConnection { | ||
peer_id, | ||
connection, | ||
}), | ||
_ => Poll::Pending, | ||
} | ||
} else { | ||
Poll::Pending | ||
} | ||
|
||
Poll::Pending | ||
} | ||
|
||
/// return list of known addresses for a given peer | ||
|
@@ -311,8 +249,8 @@ mod tests { | |
use super::{ | ||
DiscoveryBehaviour, | ||
DiscoveryConfig, | ||
KademliaEvent, | ||
}; | ||
use crate::discovery::DiscoveryEvent; | ||
use futures::{ | ||
future::poll_fn, | ||
StreamExt, | ||
|
@@ -443,9 +381,9 @@ mod tests { | |
// if peer has connected - remove it from the set | ||
left_to_discover[swarm_index].remove(&peer_id); | ||
} | ||
SwarmEvent::Behaviour(DiscoveryEvent::UnroutablePeer( | ||
peer_id, | ||
)) => { | ||
SwarmEvent::Behaviour(KademliaEvent::UnroutablePeer { | ||
peer: peer_id, | ||
}) => { | ||
// kademlia discovered a peer but does not have it's address | ||
// we simulate Identify happening and provide the address | ||
let unroutable_peer_addr = discovery_swarms | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can simply return
Poll::Ready(kad_action)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had that before, but technically that's not what the code was doing before because it was only matching on the one event variant.
I can switch it if you think it's safe (I think it is too).