Skip to content

Commit 2c5e055

Browse files
authored
refactor: Improve logging for failed p2p connection (#5379)
* refactor: Improve logging for failed p2p connection * refactor: Remove self from `NetworkBase::current_peers_addresses` --------- Signed-off-by: Dmitry Murzin <diralik@yandex.ru>
1 parent ec3c07c commit 2c5e055

File tree

4 files changed

+47
-8
lines changed

4 files changed

+47
-8
lines changed

crates/iroha_core/src/peers_gossiper.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ impl PeersGossiperHandle {
4545

4646
/// Actor which gossips peers addresses.
4747
pub struct PeersGossiper {
48+
/// Id of the current peer
49+
peer_id: PeerId,
4850
/// Peers provided at startup
4951
initial_peers: BTreeMap<PeerId, SocketAddr>,
5052
/// Peers received via gossiping from other peers
@@ -68,6 +70,7 @@ pub struct PeersGossiper {
6870
impl PeersGossiper {
6971
/// Start actor.
7072
pub fn start(
73+
peer_id: PeerId,
7174
trusted_peers: TrustedPeers,
7275
network: IrohaNetwork,
7376
shutdown_signal: ShutdownSignal,
@@ -78,6 +81,7 @@ impl PeersGossiper {
7881
.map(|peer| (peer.id, peer.address))
7982
.collect();
8083
let gossiper = Self {
84+
peer_id,
8185
initial_peers,
8286
gossip_peers: BTreeMap::new(),
8387
current_topology: BTreeSet::new(),
@@ -176,16 +180,17 @@ impl PeersGossiper {
176180

177181
let mut peers = Vec::new();
178182
for (id, address) in &self.initial_peers {
179-
if !online_peers_ids.contains(id) {
180-
peers.push((id.clone(), address.clone()));
181-
}
183+
peers.push((id.clone(), address.clone()));
182184
}
183185
for (id, addresses) in &self.gossip_peers {
184-
if !online_peers_ids.contains(id) {
185-
peers.push((id.clone(), choose_address_majority_rule(addresses)));
186-
}
186+
peers.push((id.clone(), choose_address_majority_rule(addresses)));
187187
}
188188

189+
let peers = peers
190+
.into_iter()
191+
.filter(|(id, _)| !online_peers_ids.contains(id) && id != &self.peer_id)
192+
.collect();
193+
189194
let update = UpdatePeers(peers);
190195
self.network.update_peers_addresses(update);
191196
}

crates/iroha_p2p/src/network.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,8 @@ struct NetworkBase<T: Pload, K: Kex, E: Enc> {
231231
current_conn_id: ConnectionId,
232232
/// Current topology
233233
current_topology: HashSet<PeerId>,
234+
/// Peers which are not yet connected, but should.
235+
///
234236
/// Can have two addresses for same `PeerId`.
235237
/// * One initially provided via config
236238
/// * Second received from other peers via gossiping

crates/iroha_p2p/src/peer.rs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Tokio actor Peer
22
3+
use std::net::SocketAddr;
4+
35
use bytes::{Buf, BufMut, BytesMut};
46
use message::*;
57
use parity_scale_codec::{DecodeAll, Encode};
@@ -110,7 +112,7 @@ mod run {
110112

111113
/// Peer task.
112114
#[allow(clippy::too_many_lines)]
113-
#[log(skip_all, fields(conn_id = peer.connection_id(), peer, disambiguator))]
115+
#[log(skip_all, fields(connection = &peer.log_description(), conn_id = peer.connection_id(), peer, disambiguator))]
114116
pub(super) async fn run<T: Pload, K: Kex, E: Enc, P: Entrypoint<K, E>>(
115117
RunPeerArgs {
116118
peer,
@@ -145,6 +147,7 @@ mod run {
145147
read,
146148
write,
147149
id: connection_id,
150+
..
148151
},
149152
cryptographer,
150153
} = ready_peer;
@@ -298,18 +301,38 @@ mod run {
298301
/// Trait for peer stages that might be used as starting point for peer's [`run`] function.
299302
pub(super) trait Entrypoint<K: Kex, E: Enc>: Handshake<K, E> + Send + 'static {
300303
fn connection_id(&self) -> ConnectionId;
304+
305+
/// Debug description, used for logging
306+
fn log_description(&self) -> String;
301307
}
302308

303309
impl<K: Kex, E: Enc> Entrypoint<K, E> for Connecting {
304310
fn connection_id(&self) -> ConnectionId {
305311
self.connection_id
306312
}
313+
314+
fn log_description(&self) -> String {
315+
format!("outgoing to {}", self.peer_addr)
316+
}
307317
}
308318

309319
impl<K: Kex, E: Enc> Entrypoint<K, E> for ConnectedFrom {
310320
fn connection_id(&self) -> ConnectionId {
311321
self.connection.id
312322
}
323+
324+
fn log_description(&self) -> String {
325+
#[allow(clippy::option_if_let_else)]
326+
match self.connection.remote_addr {
327+
None => "incoming".to_owned(),
328+
Some(remote_addr) => {
329+
// In case of incoming connection,
330+
// only host will have some meaningful value.
331+
// Port will have some random value chosen only for this connection.
332+
format!("incoming from {}", remote_addr.ip())
333+
}
334+
}
335+
}
313336
}
314337

315338
/// Cancellation-safe way to read messages from tcp stream
@@ -859,12 +882,20 @@ pub struct Connection {
859882
pub read: OwnedReadHalf,
860883
/// Writing half of `TcpStream`
861884
pub write: OwnedWriteHalf,
885+
/// Remote addr, for logging purpose.
886+
pub remote_addr: Option<SocketAddr>,
862887
}
863888

864889
impl Connection {
865890
/// Instantiate new connection from `connection_id` and `stream`.
866891
pub fn new(id: ConnectionId, stream: TcpStream) -> Self {
892+
let remote_addr = stream.peer_addr().ok();
867893
let (read, write) = stream.into_split();
868-
Connection { id, read, write }
894+
Connection {
895+
id,
896+
read,
897+
write,
898+
remote_addr,
899+
}
869900
}
870901
}

crates/irohad/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ impl Iroha {
260260
);
261261

262262
let (peers_gossiper, child) = PeersGossiper::start(
263+
config.common.peer.id.clone(),
263264
config.common.trusted_peers.value().clone(),
264265
network.clone(),
265266
supervisor.shutdown_signal(),

0 commit comments

Comments
 (0)