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
use std::{
fmt::{self, Debug, Display, Formatter},
io,
num::NonZeroU32,
};
use derive_more::From;
use libp2p::{
core::connection::{ConnectedPoint, PendingConnectionError},
Multiaddr,
};
use serde::Serialize;
use crate::{
effect::requests::{NetworkInfoRequest, NetworkRequest},
types::NodeId,
};
#[derive(Debug, From, Serialize)]
pub enum Event<P> {
// ========== Events triggered by the libp2p network behavior ==========
/// A connection to the given peer has been opened.
ConnectionEstablished {
/// Identity of the peer that we have connected to.
peer_id: NodeId,
/// Endpoint of the connection that has been opened.
#[serde(skip_serializing)]
endpoint: ConnectedPoint,
/// Number of established connections to this peer, including the one that has just been
/// opened.
num_established: NonZeroU32,
},
/// A connection with the given peer has been closed, possibly as a result of an error.
ConnectionClosed {
/// Identity of the peer that we have connected to.
peer_id: NodeId,
/// Endpoint of the connection that has been closed.
#[serde(skip_serializing)]
endpoint: ConnectedPoint,
/// Number of other remaining connections to this same peer.
num_established: u32,
/// Reason for the disconnection, if it was not a successful active close.
cause: Option<String>,
},
/// Tried to dial an address but it ended up being unreachable.
UnreachableAddress {
/// `NodeId` that we were trying to reach.
peer_id: NodeId,
/// Address that we failed to reach.
address: Multiaddr,
/// Error that has been encountered.
#[serde(skip_serializing)]
error: PendingConnectionError<io::Error>,
/// Number of remaining connection attempts that are being tried for this peer.
attempts_remaining: u32,
},
/// Tried to dial an address but it ended up being unreachable. Contrary to
/// `UnreachableAddress`, we don't know the identity of the peer that we were trying to reach.
UnknownPeerUnreachableAddress {
/// Address that we failed to reach.
address: Multiaddr,
/// Error that has been encountered.
#[serde(skip_serializing)]
error: PendingConnectionError<io::Error>,
},
/// One of our listeners has reported a new local listening address.
NewListenAddress(Multiaddr),
/// One of our listeners has reported the expiration of a listening address.
ExpiredListenAddress(Multiaddr),
/// One of the listeners gracefully closed.
ListenerClosed {
/// The addresses that the listener was listening on. These addresses are now considered
/// expired, similar to if a [`ExpiredListenAddress`](Event::ExpiredListenAddress) event
/// has been generated for each of them.
addresses: Vec<Multiaddr>,
/// Reason for the closure. Contains `Ok(())` if the stream produced `None`, or `Err` if
/// the stream produced an error.
#[serde(skip_serializing)]
reason: Result<(), io::Error>,
},
/// One of the listeners reported a non-fatal error.
ListenerError {
/// The listener error.
#[serde(skip_serializing)]
error: io::Error,
},
// ========== Other events ==========
/// A network request made by a different component.
#[from]
NetworkRequest {
#[serde(skip_serializing)]
request: NetworkRequest<NodeId, P>,
},
/// A network info request made by a different component.
#[from]
NetworkInfoRequest {
#[serde(skip_serializing)]
info_request: NetworkInfoRequest<NodeId>,
},
}
impl<P: Display> Display for Event<P> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
Event::ConnectionEstablished {
peer_id,
endpoint,
num_established,
} => write!(
f,
"connection {} to {} at {:?} established",
num_established, peer_id, endpoint
),
Event::ConnectionClosed {
peer_id,
endpoint,
num_established,
cause: Some(error),
} => write!(
f,
"connection to {} at {:?} closed, {} remaining: {}",
peer_id, endpoint, num_established, error
),
Event::ConnectionClosed {
peer_id,
endpoint,
num_established,
cause: None,
} => write!(
f,
"connection to {} at {:?} closed, {} remaining",
peer_id, endpoint, num_established
),
Event::UnreachableAddress {
peer_id,
address,
error,
attempts_remaining,
} => write!(
f,
"failed to connect to {} at {}, {} attempts remaining: {}",
peer_id, address, attempts_remaining, error
),
Event::UnknownPeerUnreachableAddress { address, error } => {
write!(f, "failed to connect to peer at {}: {}", address, error)
}
Event::NewListenAddress(address) => write!(f, "new listening address {}", address),
Event::ExpiredListenAddress(address) => {
write!(f, "expired listening address {}", address)
}
Event::ListenerClosed {
addresses,
reason: Ok(()),
} => write!(f, "closed listener {:?}", addresses),
Event::ListenerClosed {
addresses,
reason: Err(error),
} => write!(f, "closed listener {:?}: {}", addresses, error),
Event::ListenerError { error } => write!(f, "non-fatal listener error: {}", error),
Event::NetworkRequest { request } => write!(f, "request: {}", request),
Event::NetworkInfoRequest { info_request } => {
write!(f, "info request: {}", info_request)
}
}
}
}