1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// 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.

#![allow(clippy::too_many_arguments)]

use super::get_bin_path;
use crate::{
    local::{kill_network, run_network, LocalNetworkOptions},
    VerbosityLevel,
};
use color_eyre::{eyre::eyre, Help, Report, Result};
use sn_peers_acquisition::{get_peers_from_args, PeersArgs};
use sn_releases::{ReleaseType, SafeReleaseRepoActions};
use sn_service_management::{
    control::ServiceController, get_local_node_registry_path, NodeRegistry,
};
use std::path::PathBuf;

pub async fn join(
    build: bool,
    count: u16,
    faucet_path: Option<PathBuf>,
    faucet_version: Option<String>,
    interval: u64,
    node_path: Option<PathBuf>,
    node_version: Option<String>,
    peers: PeersArgs,
    skip_validation: bool,
) -> Result<(), Report> {
    println!("=================================================");
    println!("             Joining Local Network               ");
    println!("=================================================");

    let local_node_reg_path = &get_local_node_registry_path()?;
    let mut local_node_registry = NodeRegistry::load(local_node_reg_path)?;

    let release_repo = <dyn SafeReleaseRepoActions>::default_config();
    let faucet_path = get_bin_path(
        build,
        faucet_path,
        ReleaseType::Faucet,
        faucet_version,
        &*release_repo,
    )
    .await?;
    let node_path = get_bin_path(
        build,
        node_path,
        ReleaseType::Safenode,
        node_version,
        &*release_repo,
    )
    .await?;

    // If no peers are obtained we will attempt to join the existing local network, if one
    // is running.
    let peers = match get_peers_from_args(peers).await {
        Ok(peers) => Some(peers),
        Err(e) => match e {
            sn_peers_acquisition::error::Error::PeersNotObtained => None,
            _ => return Err(e.into()),
        },
    };
    let options = LocalNetworkOptions {
        faucet_bin_path: faucet_path,
        interval,
        join: true,
        node_count: count,
        peers,
        safenode_bin_path: node_path,
        skip_validation,
    };
    run_network(options, &mut local_node_registry, &ServiceController {}).await?;
    Ok(())
}

pub fn kill(keep_directories: bool, verbosity: VerbosityLevel) -> Result<()> {
    let local_reg_path = &get_local_node_registry_path()?;
    let local_node_registry = NodeRegistry::load(local_reg_path)?;
    if local_node_registry.nodes.is_empty() {
        println!("No local network is currently running");
    } else {
        if verbosity != VerbosityLevel::Minimal {
            println!("=================================================");
            println!("             Killing Local Network               ");
            println!("=================================================");
        }
        kill_network(&local_node_registry, keep_directories)?;
        std::fs::remove_file(local_reg_path)?;
    }
    Ok(())
}

pub async fn run(
    build: bool,
    clean: bool,
    count: u16,
    faucet_path: Option<PathBuf>,
    faucet_version: Option<String>,
    interval: u64,
    node_path: Option<PathBuf>,
    node_version: Option<String>,
    skip_validation: bool,
    verbosity: VerbosityLevel,
) -> Result<(), Report> {
    // In the clean case, the node registry must be loaded *after* the existing network has
    // been killed, which clears it out.
    let local_node_reg_path = &get_local_node_registry_path()?;
    let mut local_node_registry = if clean {
        let client_data_path = dirs_next::data_dir()
            .ok_or_else(|| eyre!("Could not obtain user's data directory"))?
            .join("safe")
            .join("client");
        if client_data_path.is_dir() {
            std::fs::remove_dir_all(client_data_path)?;
        }
        kill(false, verbosity.clone())?;
        NodeRegistry::load(local_node_reg_path)?
    } else {
        let local_node_registry = NodeRegistry::load(local_node_reg_path)?;
        if !local_node_registry.nodes.is_empty() {
            return Err(eyre!("A local network is already running")
                .suggestion("Use the kill command to destroy the network then try again"));
        }
        local_node_registry
    };

    if verbosity != VerbosityLevel::Minimal {
        println!("=================================================");
        println!("             Launching Local Network             ");
        println!("=================================================");
    }

    let release_repo = <dyn SafeReleaseRepoActions>::default_config();
    let faucet_path = get_bin_path(
        build,
        faucet_path,
        ReleaseType::Faucet,
        faucet_version,
        &*release_repo,
    )
    .await?;
    let node_path = get_bin_path(
        build,
        node_path,
        ReleaseType::Safenode,
        node_version,
        &*release_repo,
    )
    .await?;

    let options = LocalNetworkOptions {
        faucet_bin_path: faucet_path,
        join: false,
        interval,
        node_count: count,
        peers: None,
        safenode_bin_path: node_path,
        skip_validation,
    };
    run_network(options, &mut local_node_registry, &ServiceController {}).await?;

    local_node_registry.save()?;
    Ok(())
}