mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-12 18:11:10 +00:00
Update libp2p to 0.16 (#4928)
* Update libp2p to 0.16
* Actually update to libp2p 0.16 🤦
* Fix missed updates
* Fix peerset tests
This commit is contained in:
@@ -23,8 +23,7 @@ use crate::protocol::{self, light_client_handler, CustomMessageOutcome, Protocol
|
||||
use libp2p::NetworkBehaviour;
|
||||
use libp2p::core::{Multiaddr, PeerId, PublicKey};
|
||||
use libp2p::kad::record;
|
||||
use libp2p::swarm::{NetworkBehaviourAction, NetworkBehaviourEventProcess};
|
||||
use libp2p::core::{nodes::Substream, muxing::StreamMuxerBox};
|
||||
use libp2p::swarm::{NetworkBehaviourAction, NetworkBehaviourEventProcess, PollParameters};
|
||||
use log::debug;
|
||||
use sp_consensus::{BlockOrigin, import_queue::{IncomingBlock, Origin}};
|
||||
use sp_runtime::{traits::{Block as BlockT, NumberFor}, Justification};
|
||||
@@ -39,13 +38,13 @@ pub struct Behaviour<B: BlockT, S: NetworkSpecialization<B>, H: ExHashT> {
|
||||
substrate: Protocol<B, S, H>,
|
||||
/// Periodically pings and identifies the nodes we are connected to, and store information in a
|
||||
/// cache.
|
||||
debug_info: debug_info::DebugInfoBehaviour<Substream<StreamMuxerBox>>,
|
||||
debug_info: debug_info::DebugInfoBehaviour,
|
||||
/// Discovers nodes of the network.
|
||||
discovery: DiscoveryBehaviour<Substream<StreamMuxerBox>>,
|
||||
discovery: DiscoveryBehaviour,
|
||||
/// Block request handling.
|
||||
block_requests: protocol::BlockRequests<Substream<StreamMuxerBox>, B>,
|
||||
block_requests: protocol::BlockRequests<B>,
|
||||
/// Light client request handling.
|
||||
light_client_handler: protocol::LightClientHandler<Substream<StreamMuxerBox>, B>,
|
||||
light_client_handler: protocol::LightClientHandler<B>,
|
||||
/// Queue of events to produce for the outside.
|
||||
#[behaviour(ignore)]
|
||||
events: Vec<BehaviourOut<B>>,
|
||||
@@ -69,8 +68,8 @@ impl<B: BlockT, S: NetworkSpecialization<B>, H: ExHashT> Behaviour<B, S, H> {
|
||||
enable_mdns: bool,
|
||||
allow_private_ipv4: bool,
|
||||
discovery_only_if_under_num: u64,
|
||||
block_requests: protocol::BlockRequests<Substream<StreamMuxerBox>, B>,
|
||||
light_client_handler: protocol::LightClientHandler<Substream<StreamMuxerBox>, B>,
|
||||
block_requests: protocol::BlockRequests<B>,
|
||||
light_client_handler: protocol::LightClientHandler<B>,
|
||||
) -> Self {
|
||||
Behaviour {
|
||||
substrate,
|
||||
@@ -223,7 +222,7 @@ impl<B: BlockT, S: NetworkSpecialization<B>, H: ExHashT> NetworkBehaviourEventPr
|
||||
}
|
||||
|
||||
impl<B: BlockT, S: NetworkSpecialization<B>, H: ExHashT> Behaviour<B, S, H> {
|
||||
fn poll<TEv>(&mut self, _: &mut Context) -> Poll<NetworkBehaviourAction<TEv, BehaviourOut<B>>> {
|
||||
fn poll<TEv>(&mut self, _: &mut Context, _: &mut impl PollParameters) -> Poll<NetworkBehaviourAction<TEv, BehaviourOut<B>>> {
|
||||
if !self.events.is_empty() {
|
||||
return Poll::Ready(NetworkBehaviourAction::GenerateEvent(self.events.remove(0)))
|
||||
}
|
||||
|
||||
@@ -39,11 +39,11 @@ const GARBAGE_COLLECT_INTERVAL: Duration = Duration::from_secs(2 * 60);
|
||||
|
||||
/// Implementation of `NetworkBehaviour` that holds information about nodes in cache for diagnostic
|
||||
/// purposes.
|
||||
pub struct DebugInfoBehaviour<TSubstream> {
|
||||
pub struct DebugInfoBehaviour {
|
||||
/// Periodically ping nodes, and close the connection if it's unresponsive.
|
||||
ping: Ping<TSubstream>,
|
||||
ping: Ping,
|
||||
/// Periodically identifies the remote and responds to incoming requests.
|
||||
identify: Identify<TSubstream>,
|
||||
identify: Identify,
|
||||
/// Information that we know about all nodes.
|
||||
nodes_info: FnvHashMap<PeerId, NodeInfo>,
|
||||
/// Interval at which we perform garbage collection in `nodes_info`.
|
||||
@@ -64,7 +64,7 @@ struct NodeInfo {
|
||||
latest_ping: Option<Duration>,
|
||||
}
|
||||
|
||||
impl<TSubstream> DebugInfoBehaviour<TSubstream> {
|
||||
impl DebugInfoBehaviour {
|
||||
/// Builds a new `DebugInfoBehaviour`.
|
||||
pub fn new(
|
||||
user_agent: String,
|
||||
@@ -151,11 +151,10 @@ pub enum DebugInfoEvent {
|
||||
},
|
||||
}
|
||||
|
||||
impl<TSubstream> NetworkBehaviour for DebugInfoBehaviour<TSubstream>
|
||||
where TSubstream: AsyncRead + AsyncWrite + Unpin + Send + 'static {
|
||||
impl NetworkBehaviour for DebugInfoBehaviour {
|
||||
type ProtocolsHandler = IntoProtocolsHandlerSelect<
|
||||
<Ping<TSubstream> as NetworkBehaviour>::ProtocolsHandler,
|
||||
<Identify<TSubstream> as NetworkBehaviour>::ProtocolsHandler
|
||||
<Ping as NetworkBehaviour>::ProtocolsHandler,
|
||||
<Identify as NetworkBehaviour>::ProtocolsHandler
|
||||
>;
|
||||
type OutEvent = DebugInfoEvent;
|
||||
|
||||
|
||||
@@ -55,8 +55,6 @@ use libp2p::kad::record::{self, store::MemoryStore};
|
||||
#[cfg(not(target_os = "unknown"))]
|
||||
use libp2p::{swarm::toggle::Toggle};
|
||||
#[cfg(not(target_os = "unknown"))]
|
||||
use libp2p::core::{nodes::Substream, muxing::StreamMuxerBox};
|
||||
#[cfg(not(target_os = "unknown"))]
|
||||
use libp2p::mdns::{Mdns, MdnsEvent};
|
||||
use libp2p::multiaddr::Protocol;
|
||||
use log::{debug, info, trace, warn, error};
|
||||
@@ -65,15 +63,15 @@ use std::task::{Context, Poll};
|
||||
use sp_core::hexdisplay::HexDisplay;
|
||||
|
||||
/// Implementation of `NetworkBehaviour` that discovers the nodes on the network.
|
||||
pub struct DiscoveryBehaviour<TSubstream> {
|
||||
pub struct DiscoveryBehaviour {
|
||||
/// User-defined list of nodes and their addresses. Typically includes bootstrap nodes and
|
||||
/// reserved nodes.
|
||||
user_defined: Vec<(PeerId, Multiaddr)>,
|
||||
/// Kademlia requests and answers.
|
||||
kademlia: Kademlia<TSubstream, MemoryStore>,
|
||||
kademlia: Kademlia<MemoryStore>,
|
||||
/// Discovers nodes on the local network.
|
||||
#[cfg(not(target_os = "unknown"))]
|
||||
mdns: Toggle<Mdns<Substream<StreamMuxerBox>>>,
|
||||
mdns: Toggle<Mdns>,
|
||||
/// Stream that fires when we need to perform the next random Kademlia query.
|
||||
next_kad_random_query: Delay,
|
||||
/// After `next_kad_random_query` triggers, the next one triggers after this duration.
|
||||
@@ -91,7 +89,7 @@ pub struct DiscoveryBehaviour<TSubstream> {
|
||||
discovery_only_if_under_num: u64,
|
||||
}
|
||||
|
||||
impl<TSubstream> DiscoveryBehaviour<TSubstream> {
|
||||
impl DiscoveryBehaviour {
|
||||
/// Builds a new `DiscoveryBehaviour`.
|
||||
///
|
||||
/// `user_defined` is a list of known address for nodes that never expire.
|
||||
@@ -207,11 +205,8 @@ pub enum DiscoveryOut {
|
||||
ValuePutFailed(record::Key),
|
||||
}
|
||||
|
||||
impl<TSubstream> NetworkBehaviour for DiscoveryBehaviour<TSubstream>
|
||||
where
|
||||
TSubstream: AsyncRead + AsyncWrite + Unpin,
|
||||
{
|
||||
type ProtocolsHandler = <Kademlia<TSubstream, MemoryStore> as NetworkBehaviour>::ProtocolsHandler;
|
||||
impl NetworkBehaviour for DiscoveryBehaviour {
|
||||
type ProtocolsHandler = <Kademlia<MemoryStore> as NetworkBehaviour>::ProtocolsHandler;
|
||||
type OutEvent = DiscoveryOut;
|
||||
|
||||
fn new_handler(&mut self) -> Self::ProtocolsHandler {
|
||||
|
||||
@@ -20,7 +20,7 @@ use crate::utils::interval;
|
||||
use bytes::{Bytes, BytesMut};
|
||||
use futures::prelude::*;
|
||||
use libp2p::{Multiaddr, PeerId};
|
||||
use libp2p::core::{ConnectedPoint, nodes::{listeners::ListenerId, Substream}, muxing::StreamMuxerBox};
|
||||
use libp2p::core::{ConnectedPoint, nodes::listeners::ListenerId};
|
||||
use libp2p::swarm::{ProtocolsHandler, IntoProtocolsHandler};
|
||||
use libp2p::swarm::{NetworkBehaviour, NetworkBehaviourAction, PollParameters};
|
||||
use sp_core::storage::{StorageKey, ChildInfo};
|
||||
@@ -158,7 +158,7 @@ pub struct Protocol<B: BlockT, S: NetworkSpecialization<B>, H: ExHashT> {
|
||||
/// When asked for a proof of finality, we use this struct to build one.
|
||||
finality_proof_provider: Option<Arc<dyn FinalityProofProvider<B>>>,
|
||||
/// Handles opening the unique substream and sending and receiving raw messages.
|
||||
behaviour: LegacyProto<Substream<StreamMuxerBox>>,
|
||||
behaviour: LegacyProto,
|
||||
/// List of notification protocols that have been registered.
|
||||
registered_notif_protocols: HashSet<ConsensusEngineId>,
|
||||
}
|
||||
@@ -207,7 +207,7 @@ pub struct PeerInfo<B: BlockT> {
|
||||
}
|
||||
|
||||
struct LightDispatchIn<'a> {
|
||||
behaviour: &'a mut LegacyProto<Substream<StreamMuxerBox>>,
|
||||
behaviour: &'a mut LegacyProto,
|
||||
peerset: sc_peerset::PeersetHandle,
|
||||
}
|
||||
|
||||
@@ -347,7 +347,7 @@ pub trait Context<B: BlockT> {
|
||||
|
||||
/// Protocol context.
|
||||
struct ProtocolContext<'a, B: 'a + BlockT, H: 'a + ExHashT> {
|
||||
behaviour: &'a mut LegacyProto<Substream<StreamMuxerBox>>,
|
||||
behaviour: &'a mut LegacyProto,
|
||||
context_data: &'a mut ContextData<B, H>,
|
||||
peerset_handle: &'a sc_peerset::PeersetHandle,
|
||||
}
|
||||
@@ -355,7 +355,7 @@ struct ProtocolContext<'a, B: 'a + BlockT, H: 'a + ExHashT> {
|
||||
impl<'a, B: BlockT + 'a, H: 'a + ExHashT> ProtocolContext<'a, B, H> {
|
||||
fn new(
|
||||
context_data: &'a mut ContextData<B, H>,
|
||||
behaviour: &'a mut LegacyProto<Substream<StreamMuxerBox>>,
|
||||
behaviour: &'a mut LegacyProto,
|
||||
peerset_handle: &'a sc_peerset::PeersetHandle,
|
||||
) -> Self {
|
||||
ProtocolContext { context_data, peerset_handle, behaviour }
|
||||
@@ -913,14 +913,14 @@ impl<B: BlockT, S: NetworkSpecialization<B>, H: ExHashT> Protocol<B, S, H> {
|
||||
if peer.block_request.as_ref().map_or(false, |(t, _)| (tick - *t).as_secs() > REQUEST_TIMEOUT_SEC) {
|
||||
log!(
|
||||
target: "sync",
|
||||
if self.important_peers.contains(&who) { Level::Warn } else { Level::Trace },
|
||||
if self.important_peers.contains(who) { Level::Warn } else { Level::Trace },
|
||||
"Request timeout {}", who
|
||||
);
|
||||
aborting.push(who.clone());
|
||||
} else if peer.obsolete_requests.values().any(|t| (tick - *t).as_secs() > REQUEST_TIMEOUT_SEC) {
|
||||
log!(
|
||||
target: "sync",
|
||||
if self.important_peers.contains(&who) { Level::Warn } else { Level::Trace },
|
||||
if self.important_peers.contains(who) { Level::Warn } else { Level::Trace },
|
||||
"Obsolete timeout {}", who
|
||||
);
|
||||
aborting.push(who.clone());
|
||||
@@ -931,7 +931,7 @@ impl<B: BlockT, S: NetworkSpecialization<B>, H: ExHashT> Protocol<B, S, H> {
|
||||
{
|
||||
log!(
|
||||
target: "sync",
|
||||
if self.important_peers.contains(&who) { Level::Warn } else { Level::Trace },
|
||||
if self.important_peers.contains(who) { Level::Warn } else { Level::Trace },
|
||||
"Handshake timeout {}", who
|
||||
);
|
||||
aborting.push(who.clone());
|
||||
@@ -1835,7 +1835,7 @@ pub enum CustomMessageOutcome<B: BlockT> {
|
||||
}
|
||||
|
||||
fn send_request<B: BlockT, H: ExHashT>(
|
||||
behaviour: &mut LegacyProto<Substream<StreamMuxerBox>>,
|
||||
behaviour: &mut LegacyProto,
|
||||
stats: &mut HashMap<&'static str, PacketStats>,
|
||||
peers: &mut HashMap<PeerId, Peer<B, H>>,
|
||||
who: &PeerId,
|
||||
@@ -1856,7 +1856,7 @@ fn send_request<B: BlockT, H: ExHashT>(
|
||||
}
|
||||
|
||||
fn send_message<B: BlockT>(
|
||||
behaviour: &mut LegacyProto<Substream<StreamMuxerBox>>,
|
||||
behaviour: &mut LegacyProto,
|
||||
stats: &mut HashMap<&'static str, PacketStats>,
|
||||
who: &PeerId,
|
||||
message: Message<B>,
|
||||
@@ -1870,7 +1870,7 @@ fn send_message<B: BlockT>(
|
||||
|
||||
impl<B: BlockT, S: NetworkSpecialization<B>, H: ExHashT> NetworkBehaviour for
|
||||
Protocol<B, S, H> {
|
||||
type ProtocolsHandler = <LegacyProto<Substream<StreamMuxerBox>> as NetworkBehaviour>::ProtocolsHandler;
|
||||
type ProtocolsHandler = <LegacyProto as NetworkBehaviour>::ProtocolsHandler;
|
||||
type OutEvent = CustomMessageOutcome<B>;
|
||||
|
||||
fn new_handler(&mut self) -> Self::ProtocolsHandler {
|
||||
|
||||
@@ -38,7 +38,14 @@ use libp2p::{
|
||||
upgrade::{InboundUpgrade, ReadOneError, UpgradeInfo, Negotiated},
|
||||
upgrade::{DeniedUpgrade, read_one, write_one}
|
||||
},
|
||||
swarm::{NetworkBehaviour, NetworkBehaviourAction, OneShotHandler, PollParameters, SubstreamProtocol}
|
||||
swarm::{
|
||||
NegotiatedSubstream,
|
||||
NetworkBehaviour,
|
||||
NetworkBehaviourAction,
|
||||
OneShotHandler,
|
||||
PollParameters,
|
||||
SubstreamProtocol
|
||||
}
|
||||
};
|
||||
use prost::Message;
|
||||
use sp_runtime::{generic::BlockId, traits::{Block, Header, One, Zero}};
|
||||
@@ -111,20 +118,17 @@ impl Config {
|
||||
}
|
||||
|
||||
/// The block request handling behaviour.
|
||||
pub struct BlockRequests<T, B: Block> {
|
||||
pub struct BlockRequests<B: Block> {
|
||||
/// This behaviour's configuration.
|
||||
config: Config,
|
||||
/// Blockchain client.
|
||||
chain: Arc<dyn Client<B>>,
|
||||
/// Futures sending back the block request response.
|
||||
outgoing: FuturesUnordered<BoxFuture<'static, ()>>,
|
||||
/// Type witness term.
|
||||
_marker: std::marker::PhantomData<T>
|
||||
}
|
||||
|
||||
impl<T, B> BlockRequests<T, B>
|
||||
impl<B> BlockRequests<B>
|
||||
where
|
||||
T: AsyncRead + AsyncWrite + Unpin + Send + 'static,
|
||||
B: Block,
|
||||
{
|
||||
pub fn new(cfg: Config, chain: Arc<dyn Client<B>>) -> Self {
|
||||
@@ -132,7 +136,6 @@ where
|
||||
config: cfg,
|
||||
chain,
|
||||
outgoing: FuturesUnordered::new(),
|
||||
_marker: std::marker::PhantomData
|
||||
}
|
||||
}
|
||||
|
||||
@@ -243,12 +246,11 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, B> NetworkBehaviour for BlockRequests<T, B>
|
||||
impl<B> NetworkBehaviour for BlockRequests<B>
|
||||
where
|
||||
T: AsyncRead + AsyncWrite + Unpin + Send + 'static,
|
||||
B: Block
|
||||
{
|
||||
type ProtocolsHandler = OneShotHandler<T, Protocol, DeniedUpgrade, Request<Negotiated<T>>>;
|
||||
type ProtocolsHandler = OneShotHandler<Protocol, DeniedUpgrade, Request<NegotiatedSubstream>>;
|
||||
type OutEvent = Void;
|
||||
|
||||
fn new_handler(&mut self) -> Self::ProtocolsHandler {
|
||||
@@ -269,7 +271,7 @@ where
|
||||
fn inject_disconnected(&mut self, _peer: &PeerId, _info: ConnectedPoint) {
|
||||
}
|
||||
|
||||
fn inject_node_event(&mut self, peer: PeerId, Request(request, mut stream): Request<Negotiated<T>>) {
|
||||
fn inject_node_event(&mut self, peer: PeerId, Request(request, mut stream): Request<NegotiatedSubstream>) {
|
||||
match self.on_block_request(&peer, &request) {
|
||||
Ok(res) => {
|
||||
log::trace!("enqueueing block response {} for peer {} with {} blocks", res.id, peer, res.blocks.len());
|
||||
|
||||
@@ -25,7 +25,7 @@ use libp2p::swarm::{NetworkBehaviour, NetworkBehaviourAction, PollParameters};
|
||||
use log::{debug, error, trace, warn};
|
||||
use rand::distributions::{Distribution as _, Uniform};
|
||||
use smallvec::SmallVec;
|
||||
use std::{borrow::Cow, collections::hash_map::Entry, cmp, error, marker::PhantomData, mem, pin::Pin};
|
||||
use std::{borrow::Cow, collections::hash_map::Entry, cmp, error, mem, pin::Pin};
|
||||
use std::time::Duration;
|
||||
use wasm_timer::Instant;
|
||||
use std::task::{Context, Poll};
|
||||
@@ -60,7 +60,7 @@ use std::task::{Context, Poll};
|
||||
/// Note that this "banning" system is not an actual ban. If a "banned" node tries to connect to
|
||||
/// us, we accept the connection. The "banning" system is only about delaying dialing attempts.
|
||||
///
|
||||
pub struct LegacyProto< TSubstream> {
|
||||
pub struct LegacyProto {
|
||||
/// List of protocols to open with peers. Never modified.
|
||||
protocol: RegisteredProtocol,
|
||||
|
||||
@@ -80,9 +80,6 @@ pub struct LegacyProto< TSubstream> {
|
||||
|
||||
/// Events to produce from `poll()`.
|
||||
events: SmallVec<[NetworkBehaviourAction<CustomProtoHandlerIn, LegacyProtoOut>; 4]>,
|
||||
|
||||
/// Marker to pin the generics.
|
||||
marker: PhantomData<TSubstream>,
|
||||
}
|
||||
|
||||
/// State of a peer we're connected to.
|
||||
@@ -225,7 +222,7 @@ pub enum LegacyProtoOut {
|
||||
},
|
||||
}
|
||||
|
||||
impl<TSubstream> LegacyProto<TSubstream> {
|
||||
impl LegacyProto {
|
||||
/// Creates a `CustomProtos`.
|
||||
pub fn new(
|
||||
protocol: impl Into<ProtocolId>,
|
||||
@@ -241,7 +238,6 @@ impl<TSubstream> LegacyProto<TSubstream> {
|
||||
incoming: SmallVec::new(),
|
||||
next_incoming_index: sc_peerset::IncomingIndex(0),
|
||||
events: SmallVec::new(),
|
||||
marker: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -605,7 +601,7 @@ impl<TSubstream> LegacyProto<TSubstream> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<TSubstream> DiscoveryNetBehaviour for LegacyProto<TSubstream> {
|
||||
impl DiscoveryNetBehaviour for LegacyProto {
|
||||
fn add_discovered_nodes(&mut self, peer_ids: impl Iterator<Item = PeerId>) {
|
||||
self.peerset.discovered(peer_ids.into_iter().map(|peer_id| {
|
||||
debug!(target: "sub-libp2p", "PSM <= Discovered({:?})", peer_id);
|
||||
@@ -614,11 +610,8 @@ impl<TSubstream> DiscoveryNetBehaviour for LegacyProto<TSubstream> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<TSubstream> NetworkBehaviour for LegacyProto<TSubstream>
|
||||
where
|
||||
TSubstream: AsyncRead + AsyncWrite + Unpin,
|
||||
{
|
||||
type ProtocolsHandler = CustomProtoHandlerProto<TSubstream>;
|
||||
impl NetworkBehaviour for LegacyProto {
|
||||
type ProtocolsHandler = CustomProtoHandlerProto;
|
||||
type OutEvent = LegacyProtoOut;
|
||||
|
||||
fn new_handler(&mut self) -> Self::ProtocolsHandler {
|
||||
|
||||
@@ -18,7 +18,7 @@ use super::upgrade::{RegisteredProtocol, RegisteredProtocolEvent, RegisteredProt
|
||||
use bytes::BytesMut;
|
||||
use futures::prelude::*;
|
||||
use futures_timer::Delay;
|
||||
use libp2p::core::{ConnectedPoint, Negotiated, PeerId, Endpoint};
|
||||
use libp2p::core::{ConnectedPoint, PeerId, Endpoint};
|
||||
use libp2p::core::upgrade::{InboundUpgrade, OutboundUpgrade};
|
||||
use libp2p::swarm::{
|
||||
ProtocolsHandler, ProtocolsHandlerEvent,
|
||||
@@ -26,10 +26,11 @@ use libp2p::swarm::{
|
||||
KeepAlive,
|
||||
ProtocolsHandlerUpgrErr,
|
||||
SubstreamProtocol,
|
||||
NegotiatedSubstream,
|
||||
};
|
||||
use log::{debug, error};
|
||||
use smallvec::{smallvec, SmallVec};
|
||||
use std::{borrow::Cow, error, fmt, io, marker::PhantomData, mem, time::Duration};
|
||||
use std::{borrow::Cow, error, fmt, io, mem, time::Duration};
|
||||
use std::{pin::Pin, task::{Context, Poll}};
|
||||
|
||||
/// Implements the `IntoProtocolsHandler` trait of libp2p.
|
||||
@@ -86,32 +87,22 @@ use std::{pin::Pin, task::{Context, Poll}};
|
||||
/// We consider that we are now "closed" if the remote closes all the existing substreams.
|
||||
/// Re-opening it can then be performed by closing all active substream and re-opening one.
|
||||
///
|
||||
pub struct CustomProtoHandlerProto<TSubstream> {
|
||||
pub struct CustomProtoHandlerProto {
|
||||
/// Configuration for the protocol upgrade to negotiate.
|
||||
protocol: RegisteredProtocol,
|
||||
|
||||
/// Marker to pin the generic type.
|
||||
marker: PhantomData<TSubstream>,
|
||||
}
|
||||
|
||||
impl<TSubstream> CustomProtoHandlerProto<TSubstream>
|
||||
where
|
||||
TSubstream: AsyncRead + AsyncWrite + Unpin,
|
||||
{
|
||||
impl CustomProtoHandlerProto {
|
||||
/// Builds a new `CustomProtoHandlerProto`.
|
||||
pub fn new(protocol: RegisteredProtocol) -> Self {
|
||||
CustomProtoHandlerProto {
|
||||
protocol,
|
||||
marker: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<TSubstream> IntoProtocolsHandler for CustomProtoHandlerProto<TSubstream>
|
||||
where
|
||||
TSubstream: AsyncRead + AsyncWrite + Unpin,
|
||||
{
|
||||
type Handler = CustomProtoHandler<TSubstream>;
|
||||
impl IntoProtocolsHandler for CustomProtoHandlerProto {
|
||||
type Handler = CustomProtoHandler;
|
||||
|
||||
fn inbound_protocol(&self) -> RegisteredProtocol {
|
||||
self.protocol.clone()
|
||||
@@ -132,12 +123,12 @@ where
|
||||
}
|
||||
|
||||
/// The actual handler once the connection has been established.
|
||||
pub struct CustomProtoHandler<TSubstream> {
|
||||
pub struct CustomProtoHandler {
|
||||
/// Configuration for the protocol upgrade to negotiate.
|
||||
protocol: RegisteredProtocol,
|
||||
|
||||
/// State of the communications with the remote.
|
||||
state: ProtocolState<TSubstream>,
|
||||
state: ProtocolState,
|
||||
|
||||
/// Identifier of the node we're talking to. Used only for logging purposes and shouldn't have
|
||||
/// any influence on the behaviour.
|
||||
@@ -155,11 +146,11 @@ pub struct CustomProtoHandler<TSubstream> {
|
||||
}
|
||||
|
||||
/// State of the handler.
|
||||
enum ProtocolState<TSubstream> {
|
||||
enum ProtocolState {
|
||||
/// Waiting for the behaviour to tell the handler whether it is enabled or disabled.
|
||||
Init {
|
||||
/// List of substreams opened by the remote but that haven't been processed yet.
|
||||
substreams: SmallVec<[RegisteredProtocolSubstream<Negotiated<TSubstream>>; 6]>,
|
||||
substreams: SmallVec<[RegisteredProtocolSubstream<NegotiatedSubstream>; 6]>,
|
||||
/// Deadline after which the initialization is abnormally long.
|
||||
init_deadline: Delay,
|
||||
},
|
||||
@@ -175,9 +166,9 @@ enum ProtocolState<TSubstream> {
|
||||
/// If we are in this state, we have sent a `CustomProtocolOpen` message to the outside.
|
||||
Normal {
|
||||
/// The substreams where bidirectional communications happen.
|
||||
substreams: SmallVec<[RegisteredProtocolSubstream<Negotiated<TSubstream>>; 4]>,
|
||||
substreams: SmallVec<[RegisteredProtocolSubstream<NegotiatedSubstream>; 4]>,
|
||||
/// Contains substreams which are being shut down.
|
||||
shutdown: SmallVec<[RegisteredProtocolSubstream<Negotiated<TSubstream>>; 4]>,
|
||||
shutdown: SmallVec<[RegisteredProtocolSubstream<NegotiatedSubstream>; 4]>,
|
||||
},
|
||||
|
||||
/// We are disabled. Contains substreams that are being closed.
|
||||
@@ -185,7 +176,7 @@ enum ProtocolState<TSubstream> {
|
||||
/// outside or we have never sent any `CustomProtocolOpen` in the first place.
|
||||
Disabled {
|
||||
/// List of substreams to shut down.
|
||||
shutdown: SmallVec<[RegisteredProtocolSubstream<Negotiated<TSubstream>>; 6]>,
|
||||
shutdown: SmallVec<[RegisteredProtocolSubstream<NegotiatedSubstream>; 6]>,
|
||||
|
||||
/// If true, we should reactivate the handler after all the substreams in `shutdown` have
|
||||
/// been closed.
|
||||
@@ -257,10 +248,7 @@ pub enum CustomProtoHandlerOut {
|
||||
},
|
||||
}
|
||||
|
||||
impl<TSubstream> CustomProtoHandler<TSubstream>
|
||||
where
|
||||
TSubstream: AsyncRead + AsyncWrite + Unpin,
|
||||
{
|
||||
impl CustomProtoHandler {
|
||||
/// Enables the handler.
|
||||
fn enable(&mut self) {
|
||||
self.state = match mem::replace(&mut self.state, ProtocolState::Poisoned) {
|
||||
@@ -459,7 +447,7 @@ where
|
||||
/// Called by `inject_fully_negotiated_inbound` and `inject_fully_negotiated_outbound`.
|
||||
fn inject_fully_negotiated(
|
||||
&mut self,
|
||||
mut substream: RegisteredProtocolSubstream<Negotiated<TSubstream>>
|
||||
mut substream: RegisteredProtocolSubstream<NegotiatedSubstream>
|
||||
) {
|
||||
self.state = match mem::replace(&mut self.state, ProtocolState::Poisoned) {
|
||||
ProtocolState::Poisoned => {
|
||||
@@ -515,11 +503,9 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<TSubstream> ProtocolsHandler for CustomProtoHandler<TSubstream>
|
||||
where TSubstream: AsyncRead + AsyncWrite + Unpin {
|
||||
impl ProtocolsHandler for CustomProtoHandler {
|
||||
type InEvent = CustomProtoHandlerIn;
|
||||
type OutEvent = CustomProtoHandlerOut;
|
||||
type Substream = TSubstream;
|
||||
type Error = ConnectionKillError;
|
||||
type InboundProtocol = RegisteredProtocol;
|
||||
type OutboundProtocol = RegisteredProtocol;
|
||||
@@ -531,14 +517,14 @@ where TSubstream: AsyncRead + AsyncWrite + Unpin {
|
||||
|
||||
fn inject_fully_negotiated_inbound(
|
||||
&mut self,
|
||||
proto: <Self::InboundProtocol as InboundUpgrade<Negotiated<TSubstream>>>::Output
|
||||
proto: <Self::InboundProtocol as InboundUpgrade<NegotiatedSubstream>>::Output
|
||||
) {
|
||||
self.inject_fully_negotiated(proto);
|
||||
}
|
||||
|
||||
fn inject_fully_negotiated_outbound(
|
||||
&mut self,
|
||||
proto: <Self::OutboundProtocol as OutboundUpgrade<Negotiated<TSubstream>>>::Output,
|
||||
proto: <Self::OutboundProtocol as OutboundUpgrade<NegotiatedSubstream>>::Output,
|
||||
_: Self::OutboundOpenInfo
|
||||
) {
|
||||
self.inject_fully_negotiated(proto);
|
||||
@@ -601,10 +587,7 @@ where TSubstream: AsyncRead + AsyncWrite + Unpin {
|
||||
}
|
||||
}
|
||||
|
||||
impl<TSubstream> fmt::Debug for CustomProtoHandler<TSubstream>
|
||||
where
|
||||
TSubstream: AsyncRead + AsyncWrite + Unpin,
|
||||
{
|
||||
impl fmt::Debug for CustomProtoHandler {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
||||
f.debug_struct("CustomProtoHandler")
|
||||
.finish()
|
||||
@@ -613,10 +596,10 @@ where
|
||||
|
||||
/// Given a list of substreams, tries to shut them down. The substreams that have been successfully
|
||||
/// shut down are removed from the list.
|
||||
fn shutdown_list<TSubstream>
|
||||
(list: &mut SmallVec<impl smallvec::Array<Item = RegisteredProtocolSubstream<Negotiated<TSubstream>>>>,
|
||||
fn shutdown_list
|
||||
(list: &mut SmallVec<impl smallvec::Array<Item = RegisteredProtocolSubstream<NegotiatedSubstream>>>,
|
||||
cx: &mut Context)
|
||||
where TSubstream: AsyncRead + AsyncWrite + Unpin {
|
||||
{
|
||||
'outer: for n in (0..list.len()).rev() {
|
||||
let mut substream = list.swap_remove(n);
|
||||
loop {
|
||||
|
||||
@@ -18,8 +18,8 @@
|
||||
|
||||
use futures::{prelude::*, ready};
|
||||
use codec::{Encode, Decode};
|
||||
use libp2p::core::nodes::{Substream, listeners::ListenerId};
|
||||
use libp2p::core::{ConnectedPoint, transport::boxed::Boxed, muxing::StreamMuxerBox};
|
||||
use libp2p::core::nodes::listeners::ListenerId;
|
||||
use libp2p::core::ConnectedPoint;
|
||||
use libp2p::swarm::{Swarm, ProtocolsHandler, IntoProtocolsHandler};
|
||||
use libp2p::swarm::{PollParameters, NetworkBehaviour, NetworkBehaviourAction};
|
||||
use libp2p::{PeerId, Multiaddr, Transport};
|
||||
@@ -31,11 +31,7 @@ use sp_test_primitives::Block;
|
||||
|
||||
/// Builds two nodes that have each other as bootstrap nodes.
|
||||
/// This is to be used only for testing, and a panic will happen if something goes wrong.
|
||||
fn build_nodes()
|
||||
-> (
|
||||
Swarm<Boxed<(PeerId, StreamMuxerBox), io::Error>, CustomProtoWithAddr>,
|
||||
Swarm<Boxed<(PeerId, StreamMuxerBox), io::Error>, CustomProtoWithAddr>
|
||||
) {
|
||||
fn build_nodes() -> (Swarm<CustomProtoWithAddr>, Swarm<CustomProtoWithAddr>) {
|
||||
let mut out = Vec::with_capacity(2);
|
||||
|
||||
let keypairs: Vec<_> = (0..2).map(|_| libp2p::identity::Keypair::generate_ed25519()).collect();
|
||||
@@ -115,12 +111,12 @@ fn build_nodes()
|
||||
|
||||
/// Wraps around the `CustomBehaviour` network behaviour, and adds hardcoded node addresses to it.
|
||||
struct CustomProtoWithAddr {
|
||||
inner: LegacyProto<Substream<StreamMuxerBox>>,
|
||||
inner: LegacyProto,
|
||||
addrs: Vec<(PeerId, Multiaddr)>,
|
||||
}
|
||||
|
||||
impl std::ops::Deref for CustomProtoWithAddr {
|
||||
type Target = LegacyProto<Substream<StreamMuxerBox>>;
|
||||
type Target = LegacyProto;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.inner
|
||||
@@ -134,9 +130,8 @@ impl std::ops::DerefMut for CustomProtoWithAddr {
|
||||
}
|
||||
|
||||
impl NetworkBehaviour for CustomProtoWithAddr {
|
||||
type ProtocolsHandler =
|
||||
<LegacyProto<Substream<StreamMuxerBox>> as NetworkBehaviour>::ProtocolsHandler;
|
||||
type OutEvent = <LegacyProto<Substream<StreamMuxerBox>> as NetworkBehaviour>::OutEvent;
|
||||
type ProtocolsHandler = <LegacyProto as NetworkBehaviour>::ProtocolsHandler;
|
||||
type OutEvent = <LegacyProto as NetworkBehaviour>::OutEvent;
|
||||
|
||||
fn new_handler(&mut self) -> Self::ProtocolsHandler {
|
||||
self.inner.new_handler()
|
||||
|
||||
@@ -40,7 +40,14 @@ use libp2p::{
|
||||
upgrade::{InboundUpgrade, ReadOneError, UpgradeInfo, Negotiated},
|
||||
upgrade::{OutboundUpgrade, read_one, write_one}
|
||||
},
|
||||
swarm::{NetworkBehaviour, NetworkBehaviourAction, OneShotHandler, PollParameters, SubstreamProtocol}
|
||||
swarm::{
|
||||
NegotiatedSubstream,
|
||||
NetworkBehaviour,
|
||||
NetworkBehaviourAction,
|
||||
OneShotHandler,
|
||||
PollParameters,
|
||||
SubstreamProtocol
|
||||
}
|
||||
};
|
||||
use nohash_hasher::IntMap;
|
||||
use prost::Message;
|
||||
@@ -220,7 +227,7 @@ enum PeerStatus {
|
||||
}
|
||||
|
||||
/// The light client handler behaviour.
|
||||
pub struct LightClientHandler<T, B: Block> {
|
||||
pub struct LightClientHandler<B: Block> {
|
||||
/// This behaviour's configuration.
|
||||
config: Config,
|
||||
/// Blockchain client.
|
||||
@@ -239,13 +246,10 @@ pub struct LightClientHandler<T, B: Block> {
|
||||
next_request_id: u64,
|
||||
/// Handle to use for reporting misbehaviour of peers.
|
||||
peerset: sc_peerset::PeersetHandle,
|
||||
/// Type witness term.
|
||||
_marker: std::marker::PhantomData<T>
|
||||
}
|
||||
|
||||
impl<T, B> LightClientHandler<T, B>
|
||||
impl<B> LightClientHandler<B>
|
||||
where
|
||||
T: AsyncRead + AsyncWrite + Unpin + Send + 'static,
|
||||
B: Block,
|
||||
{
|
||||
/// Construct a new light client handler.
|
||||
@@ -266,7 +270,6 @@ where
|
||||
outstanding: IntMap::default(),
|
||||
next_request_id: 1,
|
||||
peerset,
|
||||
_marker: std::marker::PhantomData
|
||||
}
|
||||
}
|
||||
|
||||
@@ -646,12 +649,11 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, B> NetworkBehaviour for LightClientHandler<T, B>
|
||||
impl<B> NetworkBehaviour for LightClientHandler<B>
|
||||
where
|
||||
T: AsyncRead + AsyncWrite + Unpin + Send + 'static,
|
||||
B: Block
|
||||
{
|
||||
type ProtocolsHandler = OneShotHandler<T, InboundProtocol, OutboundProtocol, Event<Negotiated<T>>>;
|
||||
type ProtocolsHandler = OneShotHandler<InboundProtocol, OutboundProtocol, Event<NegotiatedSubstream>>;
|
||||
type OutEvent = Void;
|
||||
|
||||
fn new_handler(&mut self) -> Self::ProtocolsHandler {
|
||||
@@ -690,7 +692,7 @@ where
|
||||
self.remove_peer(peer)
|
||||
}
|
||||
|
||||
fn inject_node_event(&mut self, peer: PeerId, event: Event<Negotiated<T>>) {
|
||||
fn inject_node_event(&mut self, peer: PeerId, event: Event<NegotiatedSubstream>) {
|
||||
match event {
|
||||
// An incoming request from remote has been received.
|
||||
Event::Request(request, mut stream) => {
|
||||
@@ -1144,8 +1146,8 @@ mod tests {
|
||||
const CHILD_INFO: ChildInfo<'static> = ChildInfo::new_default(b"foobarbaz");
|
||||
|
||||
type Block = sp_runtime::generic::Block<Header<u64, BlakeTwo256>, substrate_test_runtime::Extrinsic>;
|
||||
type Handler = LightClientHandler<SubstreamRef<Arc<StreamMuxerBox>>, Block>;
|
||||
type Swarm = libp2p::swarm::Swarm<Boxed<(PeerId, StreamMuxerBox), io::Error>, Handler>;
|
||||
type Handler = LightClientHandler<Block>;
|
||||
type Swarm = libp2p::swarm::Swarm<Handler>;
|
||||
|
||||
fn empty_proof() -> Vec<u8> {
|
||||
StorageProof::empty().encode()
|
||||
@@ -1210,7 +1212,7 @@ mod tests {
|
||||
( ok: bool
|
||||
, ps: sc_peerset::PeersetHandle
|
||||
, cf: super::Config
|
||||
) -> LightClientHandler<futures::io::Cursor<Vec<u8>>, Block>
|
||||
) -> LightClientHandler<Block>
|
||||
{
|
||||
let client = Arc::new(substrate_test_runtime_client::new());
|
||||
let checker = Arc::new(DummyFetchChecker::new(ok));
|
||||
@@ -1221,10 +1223,7 @@ mod tests {
|
||||
ConnectedPoint::Dialer { address: Multiaddr::empty() }
|
||||
}
|
||||
|
||||
fn poll<T>(mut b: &mut LightClientHandler<T, Block>) -> Poll<NetworkBehaviourAction<OutboundProtocol, Void>>
|
||||
where
|
||||
T: AsyncRead + AsyncWrite + Unpin + Send + 'static
|
||||
{
|
||||
fn poll(mut b: &mut LightClientHandler<Block>) -> Poll<NetworkBehaviourAction<OutboundProtocol, Void>> {
|
||||
let mut p = EmptyPollParams(PeerId::random());
|
||||
match future::poll_fn(|cx| Pin::new(&mut b).poll(cx, &mut p)).now_or_never() {
|
||||
Some(a) => Poll::Ready(a),
|
||||
|
||||
@@ -504,7 +504,7 @@ impl<B: BlockT> LightDispatch<B> where
|
||||
}
|
||||
|
||||
pub fn is_light_response(&self, peer: &PeerId, request_id: message::RequestId) -> bool {
|
||||
self.active_peers.get(&peer).map_or(false, |r| r.id == request_id)
|
||||
self.active_peers.get(peer).map_or(false, |r| r.id == request_id)
|
||||
}
|
||||
|
||||
fn remove(&mut self, peer: PeerId, id: u64) -> Option<Request<B>> {
|
||||
|
||||
@@ -35,7 +35,6 @@ use sp_consensus::import_queue::{BlockImportResult, BlockImportError};
|
||||
use futures::{prelude::*, channel::mpsc};
|
||||
use log::{warn, error, info, trace};
|
||||
use libp2p::{PeerId, Multiaddr, kad::record};
|
||||
use libp2p::core::{transport::boxed::Boxed, muxing::StreamMuxerBox};
|
||||
use libp2p::swarm::{NetworkBehaviour, SwarmBuilder, SwarmEvent};
|
||||
use parking_lot::Mutex;
|
||||
use sc_peerset::PeersetHandle;
|
||||
@@ -854,7 +853,6 @@ impl<B: BlockT + 'static, S: NetworkSpecialization<B>, H: ExHashT> Unpin for Net
|
||||
|
||||
/// The libp2p swarm, customized for our needs.
|
||||
type Swarm<B, S, H> = libp2p::swarm::Swarm<
|
||||
Boxed<(PeerId, StreamMuxerBox), io::Error>,
|
||||
Behaviour<B, S, H>
|
||||
>;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user