diff --git a/sn_protocol/src/node_registry.rs b/sn_protocol/src/node_registry.rs index 63df4bc21f..4b58c97ea8 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 { @@ -101,6 +139,11 @@ pub struct NodeRegistry { impl NodeRegistry { pub fn save(&self) -> Result<()> { + let path = Path::new(&self.save_path); + if let Some(parent) = path.parent() { + std::fs::create_dir_all(parent)?; + } + let json = serde_json::to_string(self)?; let mut file = std::fs::File::create(self.save_path.clone())?; file.write_all(json.as_bytes())?;