From fd2dedc0e13aac99d3d017ef6d3ee69abe9c189b Mon Sep 17 00:00:00 2001 From: Chris O'Neil Date: Tue, 16 Jan 2024 18:58:48 +0000 Subject: [PATCH] chore: include connected peers in node It's useful for the `status` command in the node manager to include the number of connected peers, and it may be useful at some later point to list the peers in a more detailed view. --- sn_protocol/src/node_registry.rs | 38 ++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/sn_protocol/src/node_registry.rs b/sn_protocol/src/node_registry.rs index 63df4bc21f..9f0f26a97e 100644 --- a/sn_protocol/src/node_registry.rs +++ b/sn_protocol/src/node_registry.rs @@ -51,6 +51,39 @@ where } } +fn serialize_connected_peers( + connected_peers: &Option>, + serializer: S, +) -> Result +where + S: Serializer, +{ + match connected_peers { + Some(peers) => { + let peer_strs: Vec = peers.iter().map(|p| p.to_string()).collect(); + serializer.serialize_some(&peer_strs) + } + None => serializer.serialize_none(), + } +} + +fn deserialize_connected_peers<'de, D>(deserializer: D) -> Result>, D::Error> +where + D: Deserializer<'de>, +{ + let vec: Option> = Option::deserialize(deserializer)?; + match vec { + Some(peer_strs) => { + let peers: Result, _> = peer_strs + .into_iter() + .map(|s| PeerId::from_str(&s).map_err(DeError::custom)) + .collect(); + peers.map(Some) + } + None => Ok(None), + } +} + #[derive(Clone, Debug, Serialize, Deserialize)] pub struct Node { pub genesis: bool, @@ -70,6 +103,11 @@ pub struct Node { pub data_dir_path: Option, pub log_dir_path: Option, pub safenode_path: Option, + #[serde( + serialize_with = "serialize_connected_peers", + deserialize_with = "deserialize_connected_peers" + )] + pub connected_peers: Option>, } impl Node {