mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-13 18:41:05 +00:00
Upgrade to libp2p 0.44.0 (#11009)
* Update libp2p to 0.43.0, lru to 0.7.3 * Fix websoket Incoming::Data * Rename ProtocolsHandler -> ConnectionHandler, remove inject_dis/connected, minor fixes * Fix args for inject_connection* callbacks * Fix DialPeer/DialAddress * Fix debug fmt * Add Endpoint to NetworkState * Fix Kad::get_record by key * Fix Sha2_256::digest * Fix IntoConnectionHandler * Fix borrowchk * Fix DialError::WrongPeerId * Remove NodeHandlerWrapperError * Fix KademliaEvent variants * Fix impl Add for String * Fix tabs in network_state * Apply cargo fmt * Fix a typo in req/resp * Fix tests * Fix peer_info:entry.info_expire * Fix PeerInfoBehaviour inject_address_change and inject_connection_closed * Patch libp2p to 0.44.0#6cc3b4e * Fix inject_connection_closed kad, req/resp * Apply cargo fmt * Use libp2p from crates.io * Fix review notes
This commit is contained in:
@@ -49,8 +49,8 @@ use libp2p::{
|
||||
RequestResponseConfig, RequestResponseEvent, RequestResponseMessage, ResponseChannel,
|
||||
},
|
||||
swarm::{
|
||||
protocols_handler::multi::MultiHandler, IntoProtocolsHandler, NetworkBehaviour,
|
||||
NetworkBehaviourAction, PollParameters, ProtocolsHandler,
|
||||
handler::multi::MultiHandler, ConnectionHandler, IntoConnectionHandler, NetworkBehaviour,
|
||||
NetworkBehaviourAction, PollParameters,
|
||||
},
|
||||
};
|
||||
use std::{
|
||||
@@ -381,7 +381,7 @@ impl RequestResponsesBehaviour {
|
||||
&mut self,
|
||||
protocol: String,
|
||||
handler: RequestResponseHandler<GenericCodec>,
|
||||
) -> <RequestResponsesBehaviour as NetworkBehaviour>::ProtocolsHandler {
|
||||
) -> <RequestResponsesBehaviour as NetworkBehaviour>::ConnectionHandler {
|
||||
let mut handlers: HashMap<_, _> = self
|
||||
.protocols
|
||||
.iter_mut()
|
||||
@@ -400,11 +400,13 @@ impl RequestResponsesBehaviour {
|
||||
}
|
||||
|
||||
impl NetworkBehaviour for RequestResponsesBehaviour {
|
||||
type ProtocolsHandler =
|
||||
MultiHandler<String, <RequestResponse<GenericCodec> as NetworkBehaviour>::ProtocolsHandler>;
|
||||
type ConnectionHandler = MultiHandler<
|
||||
String,
|
||||
<RequestResponse<GenericCodec> as NetworkBehaviour>::ConnectionHandler,
|
||||
>;
|
||||
type OutEvent = Event;
|
||||
|
||||
fn new_handler(&mut self) -> Self::ProtocolsHandler {
|
||||
fn new_handler(&mut self) -> Self::ConnectionHandler {
|
||||
let iter = self
|
||||
.protocols
|
||||
.iter_mut()
|
||||
@@ -426,6 +428,7 @@ impl NetworkBehaviour for RequestResponsesBehaviour {
|
||||
conn: &ConnectionId,
|
||||
endpoint: &ConnectedPoint,
|
||||
failed_addresses: Option<&Vec<Multiaddr>>,
|
||||
other_established: usize,
|
||||
) {
|
||||
for (p, _) in self.protocols.values_mut() {
|
||||
NetworkBehaviour::inject_connection_established(
|
||||
@@ -434,32 +437,35 @@ impl NetworkBehaviour for RequestResponsesBehaviour {
|
||||
conn,
|
||||
endpoint,
|
||||
failed_addresses,
|
||||
other_established,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fn inject_connected(&mut self, peer_id: &PeerId) {
|
||||
for (p, _) in self.protocols.values_mut() {
|
||||
NetworkBehaviour::inject_connected(p, peer_id)
|
||||
}
|
||||
}
|
||||
|
||||
fn inject_connection_closed(
|
||||
&mut self,
|
||||
peer_id: &PeerId,
|
||||
conn: &ConnectionId,
|
||||
endpoint: &ConnectedPoint,
|
||||
_handler: <Self::ProtocolsHandler as IntoProtocolsHandler>::Handler,
|
||||
handler: <Self::ConnectionHandler as IntoConnectionHandler>::Handler,
|
||||
remaining_established: usize,
|
||||
) {
|
||||
for (p, _) in self.protocols.values_mut() {
|
||||
let handler = p.new_handler();
|
||||
NetworkBehaviour::inject_connection_closed(p, peer_id, conn, endpoint, handler);
|
||||
}
|
||||
}
|
||||
|
||||
fn inject_disconnected(&mut self, peer_id: &PeerId) {
|
||||
for (p, _) in self.protocols.values_mut() {
|
||||
NetworkBehaviour::inject_disconnected(p, peer_id)
|
||||
for (p_name, event) in handler.into_iter() {
|
||||
if let Some((proto, _)) = self.protocols.get_mut(p_name.as_str()) {
|
||||
proto.inject_connection_closed(
|
||||
peer_id,
|
||||
conn,
|
||||
endpoint,
|
||||
event,
|
||||
remaining_established,
|
||||
)
|
||||
} else {
|
||||
log::error!(
|
||||
target: "sub-libp2p",
|
||||
"inject_connection_closed: no request-response instance registered for protocol {:?}",
|
||||
p_name,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -467,7 +473,7 @@ impl NetworkBehaviour for RequestResponsesBehaviour {
|
||||
&mut self,
|
||||
peer_id: PeerId,
|
||||
connection: ConnectionId,
|
||||
(p_name, event): <Self::ProtocolsHandler as ProtocolsHandler>::OutEvent,
|
||||
(p_name, event): <Self::ConnectionHandler as ConnectionHandler>::OutEvent,
|
||||
) {
|
||||
if let Some((proto, _)) = self.protocols.get_mut(&*p_name) {
|
||||
return proto.inject_event(peer_id, connection, event)
|
||||
@@ -499,7 +505,7 @@ impl NetworkBehaviour for RequestResponsesBehaviour {
|
||||
fn inject_dial_failure(
|
||||
&mut self,
|
||||
peer_id: Option<PeerId>,
|
||||
_: Self::ProtocolsHandler,
|
||||
_: Self::ConnectionHandler,
|
||||
error: &libp2p::swarm::DialError,
|
||||
) {
|
||||
for (p, _) in self.protocols.values_mut() {
|
||||
@@ -536,7 +542,7 @@ impl NetworkBehaviour for RequestResponsesBehaviour {
|
||||
&mut self,
|
||||
cx: &mut Context,
|
||||
params: &mut impl PollParameters,
|
||||
) -> Poll<NetworkBehaviourAction<Self::OutEvent, Self::ProtocolsHandler>> {
|
||||
) -> Poll<NetworkBehaviourAction<Self::OutEvent, Self::ConnectionHandler>> {
|
||||
'poll_all: loop {
|
||||
if let Some(message_request) = self.message_request.take() {
|
||||
// Now we can can poll `MessageRequest` until we get the reputation
|
||||
@@ -677,25 +683,15 @@ impl NetworkBehaviour for RequestResponsesBehaviour {
|
||||
|
||||
// Other events generated by the underlying behaviour are transparently
|
||||
// passed through.
|
||||
NetworkBehaviourAction::DialAddress { address, handler } => {
|
||||
log::error!(
|
||||
"The request-response isn't supposed to start dialing peers"
|
||||
);
|
||||
NetworkBehaviourAction::Dial { opts, handler } => {
|
||||
if opts.get_peer_id().is_none() {
|
||||
log::error!(
|
||||
"The request-response isn't supposed to start dialing addresses"
|
||||
);
|
||||
}
|
||||
let protocol = protocol.to_string();
|
||||
let handler = self.new_handler_with_replacement(protocol, handler);
|
||||
return Poll::Ready(NetworkBehaviourAction::DialAddress {
|
||||
address,
|
||||
handler,
|
||||
})
|
||||
},
|
||||
NetworkBehaviourAction::DialPeer { peer_id, condition, handler } => {
|
||||
let protocol = protocol.to_string();
|
||||
let handler = self.new_handler_with_replacement(protocol, handler);
|
||||
return Poll::Ready(NetworkBehaviourAction::DialPeer {
|
||||
peer_id,
|
||||
condition,
|
||||
handler,
|
||||
})
|
||||
return Poll::Ready(NetworkBehaviourAction::Dial { opts, handler })
|
||||
},
|
||||
NetworkBehaviourAction::NotifyHandler { peer_id, handler, event } =>
|
||||
return Poll::Ready(NetworkBehaviourAction::NotifyHandler {
|
||||
@@ -1146,7 +1142,7 @@ mod tests {
|
||||
// this test, so they wouldn't connect to each other.
|
||||
{
|
||||
let dial_addr = swarms[1].1.clone();
|
||||
Swarm::dial_addr(&mut swarms[0].0, dial_addr).unwrap();
|
||||
Swarm::dial(&mut swarms[0].0, dial_addr).unwrap();
|
||||
}
|
||||
|
||||
let (mut swarm, _, peerset) = swarms.remove(0);
|
||||
@@ -1246,7 +1242,7 @@ mod tests {
|
||||
// this test, so they wouldn't connect to each other.
|
||||
{
|
||||
let dial_addr = swarms[1].1.clone();
|
||||
Swarm::dial_addr(&mut swarms[0].0, dial_addr).unwrap();
|
||||
Swarm::dial(&mut swarms[0].0, dial_addr).unwrap();
|
||||
}
|
||||
|
||||
// Running `swarm[0]` in the background until a `InboundRequest` event happens,
|
||||
@@ -1375,7 +1371,7 @@ mod tests {
|
||||
|
||||
// Ask swarm 1 to dial swarm 2. There isn't any discovery mechanism in place in this test,
|
||||
// so they wouldn't connect to each other.
|
||||
swarm_1.dial_addr(listen_add_2).unwrap();
|
||||
swarm_1.dial(listen_add_2).unwrap();
|
||||
|
||||
// Run swarm 2 in the background, receiving two requests.
|
||||
pool.spawner()
|
||||
|
||||
Reference in New Issue
Block a user