forked from jl777/SuperNET
-
Notifications
You must be signed in to change notification settings - Fork 94
/
peer_store.rs
183 lines (164 loc) · 6.56 KB
/
peer_store.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
use libp2p::{core::ConnectedPoint,
swarm::{derive_prelude::ConnectionEstablished, dummy, ConnectionClosed, ConnectionId, FromSwarm,
NetworkBehaviour},
Multiaddr, PeerId};
use smallvec::SmallVec;
use std::{collections::HashMap, task::Poll};
use void::Void;
/// The ID of an inbound or outbound request.
///
/// Note: [`RequestId`]'s uniqueness is only guaranteed between two
/// inbound and likewise between two outbound requests. There is no
/// uniqueness guarantee in a set of both inbound and outbound
/// [`RequestId`]s nor in a set of inbound or outbound requests
/// originating from different [`Behaviour`]'s.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct RequestId(u64);
/// Internal information tracked for an established connection.
pub struct Connection {
pub id: ConnectionId,
pub address: Option<Multiaddr>,
}
/// A request/response protocol for some message codec.
#[derive(Default)]
pub struct Behaviour {
/// The currently connected peers, their pending outbound and inbound responses and their known,
/// reachable addresses, if any.
pub connected: HashMap<PeerId, SmallVec<[Connection; 2]>>,
/// Externally managed addresses via `add_address` and `remove_address`.
pub addresses: HashMap<PeerId, SmallVec<[Multiaddr; 6]>>,
}
impl Behaviour {
/// Adds a known address for a peer that can be used for
/// dialing attempts by the `Swarm`, i.e. is returned
/// by [`NetworkBehaviour::handle_pending_outbound_connection`].
///
/// Addresses added in this way are only removed by `remove_address`.
pub fn add_address(&mut self, peer: &PeerId, address: Multiaddr) {
self.addresses.entry(*peer).or_default().push(address);
}
/// Removes an address of a peer previously added via `add_address`.
pub fn remove_address(&mut self, peer: &PeerId, address: &Multiaddr) {
let mut last = false;
if let Some(addresses) = self.addresses.get_mut(peer) {
addresses.retain(|a| a != address);
last = addresses.is_empty();
}
if last {
self.addresses.remove(peer);
}
}
/// Checks whether a peer is currently connected.
pub fn is_connected(&self, peer: &PeerId) -> bool {
if let Some(connections) = self.connected.get(peer) {
!connections.is_empty()
} else {
false
}
}
/// Addresses that this behaviour is aware of for this specific peer, and that may allow
/// reaching the peer.
///
/// The addresses will be tried in the order returned by this function, which means that they
/// should be ordered by decreasing likelihood of reachability. In other words, the first
/// address should be the most likely to be reachable.
pub fn addresses_of_peer(&self, peer: &PeerId) -> Vec<Multiaddr> {
let mut addresses = Vec::new();
if let Some(connections) = self.connected.get(peer) {
addresses.extend(connections.iter().filter_map(|c| c.address.clone()))
}
if let Some(more) = self.addresses.get(peer) {
addresses.extend(more.into_iter().cloned());
}
addresses
}
}
impl NetworkBehaviour for Behaviour {
type ConnectionHandler = dummy::ConnectionHandler;
type ToSwarm = Void;
fn handle_established_inbound_connection(
&mut self,
_connection_id: ConnectionId,
_peer: PeerId,
_local_addr: &Multiaddr,
_remote_addr: &Multiaddr,
) -> Result<libp2p::swarm::THandler<Self>, libp2p::swarm::ConnectionDenied> {
Ok(dummy::ConnectionHandler)
}
fn handle_established_outbound_connection(
&mut self,
_connection_id: ConnectionId,
_peer: PeerId,
_addr: &Multiaddr,
_role_override: libp2p::core::Endpoint,
) -> Result<libp2p::swarm::THandler<Self>, libp2p::swarm::ConnectionDenied> {
Ok(dummy::ConnectionHandler)
}
fn on_swarm_event(&mut self, event: libp2p::swarm::FromSwarm<Self::ConnectionHandler>) {
match event {
FromSwarm::ConnectionClosed(cc) => {
let ConnectionClosed {
peer_id,
connection_id,
remaining_established,
..
} = cc;
let connections = self
.connected
.get_mut(&peer_id)
.expect("Expected some established connection to peer before closing.");
connections
.iter()
.position(|c| c.id == connection_id)
.map(|p: usize| connections.remove(p))
.expect("Expected connection to be established before closing.");
debug_assert_eq!(connections.is_empty(), remaining_established == 0);
if connections.is_empty() {
self.connected.remove(&peer_id);
}
},
FromSwarm::ConnectionEstablished(ce) => {
let ConnectionEstablished {
peer_id,
connection_id,
endpoint,
..
} = ce;
let address = match endpoint {
ConnectedPoint::Dialer { address, .. } => Some(address.clone()),
ConnectedPoint::Listener { .. } => None,
};
self.connected.entry(peer_id).or_default().push(Connection {
id: connection_id,
address,
});
},
FromSwarm::AddressChange(_) => {},
FromSwarm::DialFailure(_) => {},
FromSwarm::ListenFailure(_) => {},
FromSwarm::NewListener(_) => {},
FromSwarm::NewListenAddr(_) => {},
FromSwarm::ExpiredListenAddr(_) => {},
FromSwarm::ListenerError(_) => {},
FromSwarm::ListenerClosed(_) => {},
FromSwarm::NewExternalAddrCandidate(_) => {},
FromSwarm::ExternalAddrExpired(_) => {},
FromSwarm::ExternalAddrConfirmed(_) => {},
}
}
fn on_connection_handler_event(
&mut self,
_peer_id: PeerId,
_connection_id: ConnectionId,
event: libp2p::swarm::THandlerOutEvent<Self>,
) {
void::unreachable(event)
}
fn poll(
&mut self,
_cx: &mut std::task::Context<'_>,
_params: &mut impl libp2p::swarm::PollParameters,
) -> std::task::Poll<libp2p::swarm::ToSwarm<Self::ToSwarm, libp2p::swarm::THandlerInEvent<Self>>> {
Poll::Pending
}
}