-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ci: use node manager for running local testnets
The local testnet action now uses the node manager to launch local networks. The churn tests were updated to parse the node manager's inventory, which they use to restart nodes. The node registry from the node manager has been transferred to the `sn_protocol` crate to reduce duplication between `safe_network` and `sn-node-manager`, because they both need access to these data structures. The local network deployment inventory is represented using the node registry.
- Loading branch information
Showing
9 changed files
with
143 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
// Copyright (C) 2024 MaidSafe.net limited. | ||
// | ||
// This SAFE Network Software is licensed to you under The General Public License (GPL), version 3. | ||
// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed | ||
// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. Please review the Licences for the specific language governing | ||
// permissions and limitations relating to use of the SAFE Network Software. | ||
|
||
use crate::Error; | ||
use color_eyre::Result; | ||
use libp2p::{Multiaddr, PeerId}; | ||
use serde::de::Error as DeError; | ||
use serde::{Deserialize, Deserializer, Serialize, Serializer}; | ||
use std::io::{Read, Write}; | ||
use std::path::{Path, PathBuf}; | ||
use std::str::FromStr; | ||
|
||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] | ||
pub enum NodeStatus { | ||
/// The node service has been added but not started for the first time | ||
Added, | ||
/// Last time we checked the service was running | ||
Running, | ||
/// The node service has been stopped | ||
Stopped, | ||
/// The node service has been removed | ||
Removed, | ||
} | ||
|
||
fn serialize_peer_id<S>(value: &Option<PeerId>, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
if let Some(peer_id) = value { | ||
return serializer.serialize_str(&peer_id.to_string()); | ||
} | ||
serializer.serialize_none() | ||
} | ||
|
||
fn deserialize_peer_id<'de, D>(deserializer: D) -> Result<Option<PeerId>, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
let s: Option<String> = Option::deserialize(deserializer)?; | ||
if let Some(peer_id_str) = s { | ||
PeerId::from_str(&peer_id_str) | ||
.map(Some) | ||
.map_err(DeError::custom) | ||
} else { | ||
Ok(None) | ||
} | ||
} | ||
|
||
#[derive(Clone, Debug, Serialize, Deserialize)] | ||
pub struct Node { | ||
pub genesis: bool, | ||
pub version: String, | ||
pub service_name: String, | ||
pub user: String, | ||
pub number: u16, | ||
pub port: u16, | ||
pub rpc_port: u16, | ||
pub status: NodeStatus, | ||
pub pid: Option<u32>, | ||
#[serde( | ||
serialize_with = "serialize_peer_id", | ||
deserialize_with = "deserialize_peer_id" | ||
)] | ||
pub peer_id: Option<PeerId>, | ||
pub data_dir_path: Option<PathBuf>, | ||
pub log_dir_path: Option<PathBuf>, | ||
pub safenode_path: Option<PathBuf>, | ||
} | ||
|
||
impl Node { | ||
pub fn get_multiaddr(&self) -> Option<Multiaddr> { | ||
if let Some(peer_id) = self.peer_id { | ||
let peer = format!("/ip4/127.0.0.1/tcp/{}/p2p/{}", self.port, peer_id); | ||
match peer.parse() { | ||
Ok(p) => return Some(p), | ||
Err(_) => return None, | ||
} | ||
} | ||
None | ||
} | ||
} | ||
|
||
#[derive(Clone, Debug, Serialize, Deserialize)] | ||
pub struct NodeRegistry { | ||
pub save_path: PathBuf, | ||
pub nodes: Vec<Node>, | ||
pub faucet_pid: Option<u32>, | ||
} | ||
|
||
impl NodeRegistry { | ||
pub fn save(&self) -> Result<()> { | ||
let json = serde_json::to_string(self)?; | ||
let mut file = std::fs::File::create(self.save_path.clone())?; | ||
file.write_all(json.as_bytes())?; | ||
Ok(()) | ||
} | ||
|
||
pub fn load(path: &Path) -> Result<Self> { | ||
if !path.exists() { | ||
return Ok(NodeRegistry { | ||
save_path: path.to_path_buf(), | ||
nodes: vec![], | ||
faucet_pid: None, | ||
}); | ||
} | ||
let mut file = std::fs::File::open(path)?; | ||
let mut contents = String::new(); | ||
file.read_to_string(&mut contents)?; | ||
let registry = serde_json::from_str(&contents)?; | ||
Ok(registry) | ||
} | ||
} | ||
|
||
pub fn get_local_node_registry_path() -> Result<PathBuf> { | ||
let path = dirs_next::data_dir() | ||
.ok_or_else(|| Error::UserDataDirectoryNotObtainable)? | ||
.join("safe") | ||
.join("local_node_registry.json"); | ||
Ok(path) | ||
} |