Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Adapt to rust-libp2p#1440.
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman S. Borschel committed Mar 17, 2020
1 parent dd97b14 commit 7e9346c
Show file tree
Hide file tree
Showing 12 changed files with 530 additions and 357 deletions.
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,9 @@ members = [
[profile.release]
# Substrate runtime requires unwinding.
panic = "unwind"

[patch.crates-io]
# libp2p = { path = "../rust-libp2p" }
libp2p = { git = "https://github.com/romanb/rust-libp2p", branch = "multicon" }


31 changes: 11 additions & 20 deletions client/network/src/debug_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use fnv::FnvHashMap;
use futures::prelude::*;
use libp2p::Multiaddr;
use libp2p::core::nodes::listeners::ListenerId;
use libp2p::core::connection::{ConnectionId, ListenerId};
use libp2p::core::{ConnectedPoint, either::EitherOutput, PeerId, PublicKey};
use libp2p::swarm::{IntoProtocolsHandler, IntoProtocolsHandlerSelect, ProtocolsHandler};
use libp2p::swarm::{NetworkBehaviour, NetworkBehaviourAction, PollParameters};
Expand Down Expand Up @@ -205,26 +205,15 @@ impl NetworkBehaviour for DebugInfoBehaviour {
}
}

fn inject_node_event(
fn inject_event(
&mut self,
peer_id: PeerId,
connection: ConnectionId,
event: <<Self::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutEvent
) {
match event {
EitherOutput::First(event) => self.ping.inject_node_event(peer_id, event),
EitherOutput::Second(event) => self.identify.inject_node_event(peer_id, event),
}
}

fn inject_replaced(&mut self, peer_id: PeerId, closed_endpoint: ConnectedPoint, new_endpoint: ConnectedPoint) {
self.ping.inject_replaced(peer_id.clone(), closed_endpoint.clone(), new_endpoint.clone());
self.identify.inject_replaced(peer_id.clone(), closed_endpoint, new_endpoint.clone());

if let Some(entry) = self.nodes_info.get_mut(&peer_id) {
entry.endpoint = new_endpoint;
} else {
error!(target: "sub-libp2p",
"Disconnected from node we were not connected to {:?}", peer_id);
EitherOutput::First(event) => self.ping.inject_event(peer_id, connection, event),
EitherOutput::Second(event) => self.identify.inject_event(peer_id, connection, event),
}
}

Expand Down Expand Up @@ -285,9 +274,10 @@ impl NetworkBehaviour for DebugInfoBehaviour {
return Poll::Ready(NetworkBehaviourAction::DialAddress { address }),
Poll::Ready(NetworkBehaviourAction::DialPeer { peer_id }) =>
return Poll::Ready(NetworkBehaviourAction::DialPeer { peer_id }),
Poll::Ready(NetworkBehaviourAction::SendEvent { peer_id, event }) =>
return Poll::Ready(NetworkBehaviourAction::SendEvent {
Poll::Ready(NetworkBehaviourAction::NotifyHandler { peer_id, handler, event }) =>
return Poll::Ready(NetworkBehaviourAction::NotifyHandler {
peer_id,
handler,
event: EitherOutput::First(event)
}),
Poll::Ready(NetworkBehaviourAction::ReportObservedAddr { address }) =>
Expand All @@ -314,9 +304,10 @@ impl NetworkBehaviour for DebugInfoBehaviour {
return Poll::Ready(NetworkBehaviourAction::DialAddress { address }),
Poll::Ready(NetworkBehaviourAction::DialPeer { peer_id }) =>
return Poll::Ready(NetworkBehaviourAction::DialPeer { peer_id }),
Poll::Ready(NetworkBehaviourAction::SendEvent { peer_id, event }) =>
return Poll::Ready(NetworkBehaviourAction::SendEvent {
Poll::Ready(NetworkBehaviourAction::NotifyHandler { peer_id, handler, event }) =>
return Poll::Ready(NetworkBehaviourAction::NotifyHandler {
peer_id,
handler,
event: EitherOutput::Second(event)
}),
Poll::Ready(NetworkBehaviourAction::ReportObservedAddr { address }) =>
Expand Down
23 changes: 11 additions & 12 deletions client/network/src/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@

use futures::prelude::*;
use futures_timer::Delay;
use libp2p::core::{nodes::listeners::ListenerId, ConnectedPoint, Multiaddr, PeerId, PublicKey};
use libp2p::core::{connection::{ConnectionId, ListenerId}, ConnectedPoint, Multiaddr, PeerId, PublicKey};
use libp2p::swarm::{ProtocolsHandler, NetworkBehaviour, NetworkBehaviourAction, PollParameters};
use libp2p::kad::{Kademlia, KademliaEvent, Quorum, Record};
use libp2p::kad::GetClosestPeersError;
Expand Down Expand Up @@ -149,6 +149,7 @@ impl DiscoveryBehaviour {
/// If we didn't know this address before, also generates a `Discovered` event.
pub fn add_known_address(&mut self, peer_id: PeerId, addr: Multiaddr) {
if self.user_defined.iter().all(|(p, a)| *p != peer_id && *a != addr) {
self.kademlia.add_address(&peer_id, addr.clone());
self.discoveries.push_back(peer_id.clone());
self.user_defined.push((peer_id, addr));
}
Expand Down Expand Up @@ -269,10 +270,6 @@ impl NetworkBehaviour for DiscoveryBehaviour {
NetworkBehaviour::inject_disconnected(&mut self.kademlia, peer_id, endpoint)
}

fn inject_replaced(&mut self, peer_id: PeerId, closed: ConnectedPoint, opened: ConnectedPoint) {
NetworkBehaviour::inject_replaced(&mut self.kademlia, peer_id, closed, opened)
}

fn inject_addr_reach_failure(
&mut self,
peer_id: Option<&PeerId>,
Expand All @@ -282,12 +279,13 @@ impl NetworkBehaviour for DiscoveryBehaviour {
NetworkBehaviour::inject_addr_reach_failure(&mut self.kademlia, peer_id, addr, error)
}

fn inject_node_event(
fn inject_event(
&mut self,
peer_id: PeerId,
connection: ConnectionId,
event: <Self::ProtocolsHandler as ProtocolsHandler>::OutEvent,
) {
NetworkBehaviour::inject_node_event(&mut self.kademlia, peer_id, event)
NetworkBehaviour::inject_event(&mut self.kademlia, peer_id, connection, event)
}

fn inject_new_external_addr(&mut self, addr: &Multiaddr) {
Expand Down Expand Up @@ -340,8 +338,9 @@ impl NetworkBehaviour for DiscoveryBehaviour {
while let Poll::Ready(_) = self.next_kad_random_query.poll_unpin(cx) {
let actually_started = if self.num_connections < self.discovery_only_if_under_num {
let random_peer_id = PeerId::random();
debug!(target: "sub-libp2p", "Libp2p <= Starting random Kademlia request for \
{:?}", random_peer_id);
debug!(target: "sub-libp2p",
"Libp2p <= Starting random Kademlia request for {:?}",
random_peer_id);

self.kademlia.get_closest_peers(random_peer_id);
true
Expand Down Expand Up @@ -453,8 +452,8 @@ impl NetworkBehaviour for DiscoveryBehaviour {
return Poll::Ready(NetworkBehaviourAction::DialAddress { address }),
NetworkBehaviourAction::DialPeer { peer_id } =>
return Poll::Ready(NetworkBehaviourAction::DialPeer { peer_id }),
NetworkBehaviourAction::SendEvent { peer_id, event } =>
return Poll::Ready(NetworkBehaviourAction::SendEvent { peer_id, event }),
NetworkBehaviourAction::NotifyHandler { peer_id, handler, event } =>
return Poll::Ready(NetworkBehaviourAction::NotifyHandler { peer_id, handler, event }),
NetworkBehaviourAction::ReportObservedAddr { address } =>
return Poll::Ready(NetworkBehaviourAction::ReportObservedAddr { address }),
}
Expand Down Expand Up @@ -484,7 +483,7 @@ impl NetworkBehaviour for DiscoveryBehaviour {
return Poll::Ready(NetworkBehaviourAction::DialAddress { address }),
NetworkBehaviourAction::DialPeer { peer_id } =>
return Poll::Ready(NetworkBehaviourAction::DialPeer { peer_id }),
NetworkBehaviourAction::SendEvent { event, .. } =>
NetworkBehaviourAction::NotifyHandler { event, .. } =>
match event {}, // `event` is an enum with no variant
NetworkBehaviourAction::ReportObservedAddr { address } =>
return Poll::Ready(NetworkBehaviourAction::ReportObservedAddr { address }),
Expand Down
15 changes: 6 additions & 9 deletions client/network/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use bytes::{Bytes, BytesMut};
use futures::prelude::*;
use generic_proto::{GenericProto, GenericProtoOut};
use libp2p::{Multiaddr, PeerId};
use libp2p::core::{ConnectedPoint, nodes::listeners::ListenerId};
use libp2p::core::{ConnectedPoint, connection::{ConnectionId, ListenerId}};
use libp2p::swarm::{ProtocolsHandler, IntoProtocolsHandler};
use libp2p::swarm::{NetworkBehaviour, NetworkBehaviourAction, PollParameters};
use sp_core::storage::{StorageKey, ChildInfo};
Expand Down Expand Up @@ -1999,12 +1999,13 @@ impl<B: BlockT, H: ExHashT> NetworkBehaviour for Protocol<B, H> {
self.behaviour.inject_disconnected(peer_id, endpoint)
}

fn inject_node_event(
fn inject_event(
&mut self,
peer_id: PeerId,
connection: ConnectionId,
event: <<Self::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutEvent,
) {
self.behaviour.inject_node_event(peer_id, event)
self.behaviour.inject_event(peer_id, connection, event)
}

fn poll(
Expand Down Expand Up @@ -2059,8 +2060,8 @@ impl<B: BlockT, H: ExHashT> NetworkBehaviour for Protocol<B, H> {
return Poll::Ready(NetworkBehaviourAction::DialAddress { address }),
Poll::Ready(NetworkBehaviourAction::DialPeer { peer_id }) =>
return Poll::Ready(NetworkBehaviourAction::DialPeer { peer_id }),
Poll::Ready(NetworkBehaviourAction::SendEvent { peer_id, event }) =>
return Poll::Ready(NetworkBehaviourAction::SendEvent { peer_id, event }),
Poll::Ready(NetworkBehaviourAction::NotifyHandler { peer_id, handler, event }) =>
return Poll::Ready(NetworkBehaviourAction::NotifyHandler { peer_id, handler, event }),
Poll::Ready(NetworkBehaviourAction::ReportObservedAddr { address }) =>
return Poll::Ready(NetworkBehaviourAction::ReportObservedAddr { address }),
};
Expand Down Expand Up @@ -2093,10 +2094,6 @@ impl<B: BlockT, H: ExHashT> NetworkBehaviour for Protocol<B, H> {
}
}

fn inject_replaced(&mut self, peer_id: PeerId, closed_endpoint: ConnectedPoint, new_endpoint: ConnectedPoint) {
self.behaviour.inject_replaced(peer_id, closed_endpoint, new_endpoint)
}

fn inject_addr_reach_failure(
&mut self,
peer_id: Option<&PeerId>,
Expand Down
8 changes: 7 additions & 1 deletion client/network/src/protocol/block_requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use libp2p::{
ConnectedPoint,
Multiaddr,
PeerId,
connection::ConnectionId,
upgrade::{InboundUpgrade, ReadOneError, UpgradeInfo, Negotiated},
upgrade::{DeniedUpgrade, read_one, write_one}
},
Expand Down Expand Up @@ -270,7 +271,12 @@ where
fn inject_disconnected(&mut self, _peer: &PeerId, _info: ConnectedPoint) {
}

fn inject_node_event(&mut self, peer: PeerId, Request(request, mut stream): Request<NegotiatedSubstream>) {
fn inject_event(
&mut self,
peer: PeerId,
connection: ConnectionId,
Request(request, mut stream): Request<NegotiatedSubstream>
) {
match self.on_block_request(&peer, &request) {
Ok(res) => {
log::trace!("enqueueing block response for peer {} with {} blocks", peer, res.blocks.len());
Expand Down
Loading

0 comments on commit 7e9346c

Please sign in to comment.