Skip to content

Commit

Permalink
ci: use node manager for running local testnets
Browse files Browse the repository at this point in the history
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
jacderida committed Jan 15, 2024
1 parent e2529ab commit 0ff08d0
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 35 deletions.
1 change: 0 additions & 1 deletion .github/workflows/benchmark-prs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ jobs:
node-path: target/release/safenode
faucet-path: target/release/faucet
platform: ubuntu-latest
build: true

- name: Check SAFE_PEERS was set
shell: bash
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/memcheck.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ jobs:

- name: Start a local network
env:
SN_LOG: all
SN_LOG: "all"
uses: maidsafe/sn-local-testnet-action@main
with:
action: start
Expand All @@ -74,7 +74,6 @@ jobs:
faucet-path: target/release/faucet
platform: ubuntu-latest
set-safe-peers: false
build: true
join: true

# In this case we did *not* want SAFE_PEERS to be set to another value by starting the testnet
Expand Down
12 changes: 0 additions & 12 deletions .github/workflows/merge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ jobs:
node-path: target/release/safenode
faucet-path: target/release/faucet
platform: ${{ matrix.os }}
build: true

- name: Check SAFE_PEERS was set
shell: bash
Expand Down Expand Up @@ -279,7 +278,6 @@ jobs:
action: stop
log_file_prefix: safe_test_logs_e2e
platform: ${{ matrix.os }}
build: true

gossipsub:
if: "!startsWith(github.event.head_commit.message, 'chore(release):')"
Expand Down Expand Up @@ -311,7 +309,6 @@ jobs:
node-path: target/release/safenode
faucet-path: target/release/faucet
platform: ${{ matrix.os }}
build: true

- name: Gossipsub - nodes to subscribe to topics, and publish messages
run: cargo test --release -p sn_node --features local-discovery --test msgs_over_gossipsub -- --nocapture
Expand All @@ -324,7 +321,6 @@ jobs:
action: stop
log_file_prefix: safe_test_logs_gossipsub_e2e
platform: ${{ matrix.os }}
build: true

spend_test:
if: "!startsWith(github.event.head_commit.message, 'chore(release):')"
Expand Down Expand Up @@ -357,7 +353,6 @@ jobs:
node-path: target/release/safenode
faucet-path: target/release/faucet
platform: ${{ matrix.os }}
build: true

- name: Check SAFE_PEERS was set
shell: bash
Expand Down Expand Up @@ -396,7 +391,6 @@ jobs:
action: stop
log_file_prefix: safe_test_logs_spend
platform: ${{ matrix.os }}
build: true

churn:
if: "!startsWith(github.event.head_commit.message, 'chore(release):')"
Expand Down Expand Up @@ -437,7 +431,6 @@ jobs:
node-path: target/release/safenode
faucet-path: target/release/faucet
platform: ${{ matrix.os }}
build: true

- name: Check SAFE_PEERS was set
shell: bash
Expand All @@ -464,7 +457,6 @@ jobs:
action: stop
log_file_prefix: safe_test_logs_churn
platform: ${{ matrix.os }}
build: true

- name: Verify restart of nodes using rg
shell: bash
Expand Down Expand Up @@ -559,7 +551,6 @@ jobs:
node-path: target/release/safenode
faucet-path: target/release/faucet
platform: ${{ matrix.os }}
build: true

- name: Check SAFE_PEERS was set
shell: bash
Expand Down Expand Up @@ -593,7 +584,6 @@ jobs:
action: stop
log_file_prefix: safe_test_logs_data_location
platform: ${{ matrix.os }}
build: true

- name: Verify restart of nodes using rg
shell: bash
Expand Down Expand Up @@ -769,7 +759,6 @@ jobs:
node-path: target/release/safenode
faucet-path: target/release/faucet
platform: ubuntu-latest
build: true

- name: Check SAFE_PEERS was set
shell: bash
Expand Down Expand Up @@ -861,4 +850,3 @@ jobs:
action: stop
log_file_prefix: safe_test_logs_heavy_replicate_bench
platform: ubuntu-latest
build: true
5 changes: 0 additions & 5 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ jobs:
target: aarch64-unknown-linux-musl
steps:
- uses: actions/checkout@v4



- uses: dtolnay/rust-toolchain@stable
# It's quite slow to install just by building it, but here we need a cross-platform solution.
- shell: bash
Expand Down Expand Up @@ -75,8 +72,6 @@ jobs:
GH_TOKEN: ${{ secrets.VERSION_BUMP_COMMIT_PAT }}

steps:


- uses: actions/checkout@v4
with:
fetch-depth: "0"
Expand Down
20 changes: 8 additions & 12 deletions sn_node/tests/common/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use eyre::{bail, Result};
use lazy_static::lazy_static;
use sn_client::{send, Client};
use sn_peers_acquisition::parse_peer_addr;
use sn_protocol::node_registry::{get_local_node_registry_path, NodeRegistry};
use sn_protocol::test_utils::DeploymentInventory;
use sn_transfers::{create_faucet_wallet, LocalWallet, NanoTokens, Transfer};
use std::{
Expand Down Expand Up @@ -53,18 +54,13 @@ pub fn get_all_rpc_addresses() -> Result<Vec<SocketAddr>> {
match DeploymentInventory::load() {
Ok(inventory) => Ok(inventory.rpc_endpoints),
Err(_) => {
let starting_port = match std::env::var("CHURN_TEST_START_PORT") {
Ok(val) => val.parse()?,
Err(_) => 12000,
};
let mut addresses = Vec::new();
for i in 1..LOCAL_NODE_COUNT + 1 {
let addr = SocketAddr::new(
IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)),
starting_port + i as u16,
);
addresses.push(addr);
}
let local_node_reg_path = &get_local_node_registry_path()?;
let local_node_registry = NodeRegistry::load(local_node_reg_path)?;
let addresses = local_node_registry
.nodes
.iter()
.map(|n| SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), n.rpc_port))
.collect::<Vec<SocketAddr>>();
Ok(addresses)
}
}
Expand Down
6 changes: 3 additions & 3 deletions sn_protocol/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ repository = "https://github.com/maidsafe/safe_network"
version = "0.10.7"

[features]
test-utils=["dirs-next", "serde_json"]
test-utils=[]

[dependencies]
bls = { package = "blsttc", version = "8.0.1" }
bytes = { version = "1.0.1", features = ["serde"] }
color-eyre = "0.6.2"
crdts = { version = "7.3", default-features = false, features = ["merkle"] }
custom_debug = "~0.5.0"
dirs-next = { version = "~2.0.0", optional = true }
dirs-next = "~2.0.0"
hex = "~0.4.3"
libp2p = { version="0.53", features = ["identify", "kad"] }
rmp-serde = "1.1.1"
serde = { version = "1.0.133", features = [ "derive", "rc" ]}
serde_json = {version = "1.0", optional = true }
serde_json = "1.0"
sha2 = "0.10.7"
sn_transfers = { path = "../sn_transfers", version = "0.14.37" }
sn_registers = { path = "../sn_registers", version = "0.3.7" }
Expand Down
4 changes: 4 additions & 0 deletions sn_protocol/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ pub type Result<T> = std::result::Result<T, Error>;
#[derive(Error, Clone, PartialEq, Eq, Serialize, Deserialize, custom_debug::Debug)]
#[non_exhaustive]
pub enum Error {
// ---------- Misc errors
#[error("Could not obtain user's data directory")]
UserDataDirectoryNotObtainable,

// ---------- Chunk Proof errors
#[error("Chunk does not exist {0:?}")]
ChunkDoesNotExist(NetworkAddress),
Expand Down
2 changes: 2 additions & 0 deletions sn_protocol/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ extern crate tracing;
pub mod error;
/// Messages types
pub mod messages;
/// Data structures for node management.
pub mod node_registry;
/// RPC commands to node
pub mod node_rpc;
/// Storage types for spends, chunks and registers.
Expand Down
125 changes: 125 additions & 0 deletions sn_protocol/src/node_registry.rs
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)
}

0 comments on commit 0ff08d0

Please sign in to comment.