Skip to main content

snarkos_cli/commands/
start.rs

1// Copyright (c) 2019-2026 Provable Inc.
2// This file is part of the snarkOS library.
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at:
7
8// http://www.apache.org/licenses/LICENSE-2.0
9
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16use crate::helpers::{
17    args::{network_id_parser, parse_node_data_dir},
18    dev::*,
19};
20
21use snarkos_account::Account;
22use snarkos_display::Display;
23use snarkos_node::{
24    Node,
25    bft::MEMORY_POOL_PORT,
26    network::{NodeType, bootstrap_peers},
27    rest::DEFAULT_REST_PORT,
28    router::DEFAULT_NODE_PORT,
29};
30use snarkos_utilities::{NodeDataDir, SignalHandler, jwt_secret_file, node_data};
31
32use snarkvm::{
33    console::{
34        account::{Address, PrivateKey},
35        algorithms::Hash,
36        network::{CanaryV0, MainnetV0, Network, TestnetV0},
37    },
38    ledger::{
39        block::Block,
40        committee::{Committee, MIN_DELEGATOR_STAKE, MIN_VALIDATOR_STAKE},
41        store::{ConsensusStore, helpers::memory::ConsensusMemory},
42    },
43    prelude::{FromBytes, Itertools, ToBits, ToBytes},
44    synthesizer::VM,
45    utilities::to_bytes_le,
46};
47
48use aleo_std::{StorageMode, aleo_ledger_dir};
49use anyhow::{Context, Result, anyhow, bail, ensure};
50use base64::prelude::{BASE64_STANDARD, Engine};
51use clap::Parser;
52use colored::Colorize;
53use core::str::FromStr;
54use indexmap::IndexMap;
55use rand::{Rng, SeedableRng};
56use rand_chacha::ChaChaRng;
57use serde::{Deserialize, Serialize};
58
59use std::{
60    fs,
61    io::IsTerminal,
62    net::{Ipv4Addr, SocketAddr, SocketAddrV4, ToSocketAddrs},
63    path::{Path, PathBuf},
64    sync::{Arc, atomic::AtomicBool},
65};
66use tokio::{
67    runtime::{self, Runtime},
68    sync::mpsc,
69    task,
70};
71use tracing::{debug, info, warn};
72use ureq::http;
73
74/// The recommended minimum number of 'open files' limit for a validator.
75/// Validators should be able to handle at least 1000 concurrent connections, each requiring 2 sockets.
76#[cfg(target_family = "unix")]
77const RECOMMENDED_MIN_NOFILES_LIMIT: u64 = 2048;
78
79// A mapping of `staker_address` to `(validator_address, withdrawal_address, amount)`.
80#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
81pub struct BondedBalances(IndexMap<String, (String, String, u64)>);
82
83impl FromStr for BondedBalances {
84    type Err = serde_json::Error;
85
86    fn from_str(s: &str) -> Result<Self, Self::Err> {
87        serde_json::from_str(s)
88    }
89}
90
91// Starts the snarkOS node.
92#[derive(Clone, Debug, Parser)]
93#[command(
94    // Use kebab-case for all arguments (e.g., use the `private-key` flag for the `private_key` field).
95    // This is already the default, but we specify it in case clap's default changes in the future.
96    rename_all = "kebab-case",
97
98    // Ensure at most one node type is specified.
99    group(clap::ArgGroup::new("node_type").required(false).multiple(false)
100),
101
102    // Ensure all other dev flags can only be set if `--dev` is set.
103    group(clap::ArgGroup::new("dev_flags").required(false).multiple(true).requires("dev")
104),
105    // Ensure any rest flag (including `--rest`) cannot be set
106    // if `--norest` is set.
107    group(clap::ArgGroup::new("rest_flags").required(false).multiple(true).conflicts_with("norest")),
108
109    // Ensure you cannot set --verbosity and --log-filter flags at the same time.
110    group(clap::ArgGroup::new("log_flags").required(false).multiple(false)),
111
112    // Ensure you need to set either --jwt-secret and --jwt-timestamp or --nojwt flags.
113    group(clap::ArgGroup::new("jwt_flags").required(false).multiple(true).conflicts_with("nojwt").conflicts_with("norest")),
114)]
115pub struct Start {
116    /// Specify the network ID of this node
117    /// [options: 0 = mainnet, 1 = testnet, 2 = canary]
118    #[clap(long, default_value_t=MainnetV0::ID, long, value_parser = network_id_parser())]
119    pub network: u16,
120
121    /// Start the node as a prover.
122    #[clap(long, group = "node_type")]
123    pub prover: bool,
124
125    /// Start the node as a client (default).
126    ///
127    /// Client are "full nodes", i.e, validate and execute all blocks they receive, but they do not participate in AleoBFT consensus.
128    #[clap(long, group = "node_type", verbatim_doc_comment)]
129    pub client: bool,
130
131    /// Start the node as a bootstrap client.
132    #[clap(long = "bootstrap-client", group = "node_type", conflicts_with_all = ["peers", "validators"], verbatim_doc_comment)]
133    pub bootstrap_client: bool,
134
135    /// Start the node as a validator.
136    ///
137    /// Validators are "full nodes", like clients, but also participate in AleoBFT.
138    #[clap(long, group = "node_type", verbatim_doc_comment)]
139    pub validator: bool,
140
141    /// Specify the account private key of the node
142    #[clap(long)]
143    pub private_key: Option<String>,
144
145    /// Specify the path to a file containing the account private key of the node
146    #[clap(long = "private-key-file")]
147    pub private_key_file: Option<PathBuf>,
148
149    /// Set the IP address and port used for P2P communication.
150    #[clap(long)]
151    pub node: Option<SocketAddr>,
152
153    /// Set the IP address and port used for BFT communication.
154    /// This argument is only allowed for validator nodes.
155    #[clap(long, requires = "validator")]
156    pub bft: Option<SocketAddr>,
157
158    /// Specify the IP address and port of the peer(s) to connect to (as a comma-separated list).
159    ///
160    /// These peers will be set as "trusted", which means the node will not disconnect from them when performing peer rotation.
161    ///
162    /// Setting peers to "" has the same effect as not setting the flag at all, except when using `--dev`.
163    #[clap(long, verbatim_doc_comment)]
164    pub peers: Option<String>,
165
166    /// Specify the IP address and port of the validator(s) to connect to.
167    #[clap(long)]
168    pub validators: Option<String>,
169
170    /// [DEPRECATED] [NO-OP] Allow untrusted peers (not listed in `--peers`) to connect.
171    ///
172    /// The flag will be ignored by client and prover nodes, as this behavior is always enabled for these types of nodes.
173    #[clap(long, verbatim_doc_comment)]
174    pub allow_external_peers: bool,
175
176    /// [DEPRECATED] [NO-OP] If the flag is set, a client will periodically evict more external peers
177    #[clap(long)]
178    pub rotate_external_peers: bool,
179
180    /// Specify the IP address and port for the REST server
181    #[clap(long, group = "rest_flags")]
182    pub rest: Option<SocketAddr>,
183
184    /// Specify the requests per second (RPS) rate limit per IP for the REST server
185    #[clap(long, default_value_t = 10, group = "rest_flags")]
186    pub rest_rps: u32,
187
188    /// Specify the JWT secret for the REST server (16B, base64-encoded).
189    #[clap(long, group = "jwt_flags")]
190    pub jwt_secret: Option<String>,
191
192    /// Specify the JWT creation timestamp; can be any time in the last 10 years.
193    #[clap(long, group = "jwt_flags")]
194    pub jwt_timestamp: Option<i64>,
195
196    /// If the flag is set, the node will not initialize the REST server.
197    #[clap(long)]
198    pub norest: bool,
199
200    /// If the flag is set, the node will not require JWT authentication for the REST server.
201    #[clap(long, group = "rest_flags")]
202    pub nojwt: bool,
203
204    /// If the flag is set, the node will only connect to trusted peers and validators.
205    #[clap(long)]
206    pub trusted_peers_only: bool,
207
208    /// Write log message to stdout instead of showing a terminal UI.
209    ///
210    /// This is useful, for example, for running a node as a service instead of in the foreground or to pipe its output into a file.
211    #[clap(long, verbatim_doc_comment)]
212    pub nodisplay: bool,
213
214    /// Do not show the Aleo banner and information about the node on startup.
215    #[clap(long, hide = true)]
216    pub nobanner: bool,
217
218    /// Specify the log verbosity of the node.
219    /// [options: 0 (lowest log level) to 6 (highest level)]
220    #[clap(long, default_value_t = 1, group = "log_flags")]
221    pub verbosity: u8,
222
223    /// Set a custom log filtering scheme, e.g., "off,snarkos_bft=trace", to show all log messages of snarkos_bft but nothing else.
224    #[clap(long, group = "log_flags")]
225    pub log_filter: Option<String>,
226
227    /// Specify the path to the file where logs will be stored
228    #[clap(long, default_value_os_t = std::env::temp_dir().join("snarkos.log"))]
229    pub logfile: PathBuf,
230
231    /// Enable the metrics exporter
232    #[cfg(feature = "metrics")]
233    #[clap(long)]
234    pub metrics: bool,
235
236    /// Specify the IP address and port for the metrics exporter
237    #[cfg(feature = "metrics")]
238    #[clap(long, requires = "metrics")]
239    pub metrics_ip: Option<SocketAddr>,
240
241    /// Specify the directory that holds all ledger data, e.g., blocks and transactions.
242    /// This flag overrides the default path, even when `--dev` is set.
243    ///
244    /// The old name for this flag (`--storage`) is DEPRECATED and will eventually be removed.
245    #[clap(long, verbatim_doc_comment, alias = "storage")]
246    pub ledger_storage: Option<PathBuf>,
247
248    /// Specify the directory that holds node-specific data, that is not part of the global ledger.
249    /// This flag overrides the default path, even when `--dev` is set.
250    ///
251    /// That folder may contain sensitive data, such as the JWT secret, and should not be shared with untrusted parties.
252    /// For validators, it also contains the latest proposal cache, which is required to participate in consensus.
253    #[clap(long, verbatim_doc_comment)]
254    pub node_data_storage: Option<PathBuf>,
255
256    /// If specified, the node will automatically save database checkpoints.
257    #[clap(long)]
258    pub auto_db_checkpoints: Option<PathBuf>,
259
260    /// Enables the node to prefetch initial blocks from a CDN
261    #[clap(long, conflicts_with = "nocdn")]
262    pub cdn: Option<http::Uri>,
263
264    /// If the flag is set, the node will not prefetch from a CDN
265    #[clap(long)]
266    pub nocdn: bool,
267
268    /// Enables development mode used to set up test networks.
269    ///
270    /// The purpose of this flag is to run multiple nodes on the same machine and in the same working directory.
271    /// To do this, set the value to a unique ID within the test work. For example if there are four nodes in the network, pass `--dev 0` for the first node, `--dev 1` for the second, and so forth.
272    ///
273    /// If you do not explicitly set the `--peers` flag, this will also populate the set of trusted peers, so that the network is fully connected.
274    /// Additionally, if you do not set the `--rest` or the `--norest` flags, it will also set the REST port to `3030` for the first node, `3031` for the second, and so forth.
275    #[clap(long, verbatim_doc_comment)]
276    pub dev: Option<u16>,
277
278    /// If development mode is enabled, specify the number of genesis validator.
279    #[clap(long, group = "dev_flags", default_value_t=DEVELOPMENT_MODE_NUM_GENESIS_COMMITTEE_MEMBERS)]
280    pub dev_num_validators: u16,
281
282    /// If development mode is enabled, specify the number of clients.
283    /// This is only used by validators to automatically populate their set of trusted peers.
284    ///
285    /// This option cannot be used while also passing the `--peers` flag.
286    #[clap(long, group = "dev_flags", conflicts_with = "peers")]
287    pub dev_num_clients: Option<u16>,
288
289    /// If development mode is enabled, specify whether node 0 should generate traffic to drive the network.
290    #[clap(long, group = "dev_flag")]
291    pub no_dev_txs: bool,
292
293    /// If development mode is enabled, specify the custom bonded balances as a JSON object.
294    #[clap(long, group = "dev_flags")]
295    pub dev_bonded_balances: Option<BondedBalances>,
296
297    /// If the flag is set, the node will attempt to automatically migrate the node data to the new format.
298    #[clap(long)]
299    pub auto_migrate_node_data: bool,
300}
301
302impl Start {
303    /// Starts the snarkOS node and blocks until it terminates.
304    pub fn parse(self) -> Result<String> {
305        // Prepare the shutdown flag.
306        let shutdown: Arc<AtomicBool> = Default::default();
307
308        // Initialize the logger.
309        let log_receiver = crate::helpers::initialize_logger(
310            self.verbosity,
311            &self.log_filter,
312            self.nodisplay,
313            self.logfile.clone(),
314            shutdown.clone(),
315        )
316        .with_context(|| "Failed to set up logger")?;
317
318        // When running in a non-interactive session, disallow the use of the terminal UI.
319        if !std::io::stdout().is_terminal() && !self.nodisplay {
320            anyhow::bail!(
321                "snarkOS cannot use the terminal UI in a non-interactive session. Please restart with `--nodisplay`."
322            );
323        }
324
325        // Initialize the runtime.
326        Self::runtime().block_on(async move {
327            // Error messages.
328            let node_parse_error = || "Failed to start node";
329
330            // Clone the configurations.
331            let mut self_ = self.clone();
332
333            // Parse the node arguments, start it, and block until shutdown.
334            match self_.network {
335                MainnetV0::ID => self_.parse_node::<MainnetV0>(log_receiver).await.with_context(node_parse_error)?,
336                TestnetV0::ID => self_.parse_node::<TestnetV0>(log_receiver).await.with_context(node_parse_error)?,
337                CanaryV0::ID => self_.parse_node::<CanaryV0>(log_receiver).await.with_context(node_parse_error)?,
338                _ => panic!("Invalid network ID specified"),
339            };
340
341            Ok(String::new())
342        })
343    }
344}
345
346impl Start {
347    /// Returns the initial peer(s) to connect to, from the given configurations.
348    fn parse_trusted_addrs(&self, list: &Option<String>) -> Result<Vec<SocketAddr>> {
349        let Some(list) = list else { return Ok(vec![]) };
350
351        match list.is_empty() {
352            // Split on an empty string returns an empty string.
353            true => Ok(vec![]),
354            false => list.split(',').map(resolve_potential_hostnames).collect(),
355        }
356    }
357
358    /// Returns the CDN to prefetch initial blocks from, or `None` if fetching from the CDN is disabled.
359    fn parse_cdn<N: Network>(&self) -> Result<Option<http::Uri>> {
360        // Disable CDN if:
361        //  1. The node is in development mode.
362        //  2. The user has explicitly disabled CDN.
363        //  3. The node is a prover (no need to sync).
364        let no_cdn_reasons = [("--dev", self.dev.is_some()), ("--nocdn", self.nocdn), ("--prover", self.prover)]
365            .into_iter()
366            .filter_map(|(reason, flag_set)| flag_set.then_some(reason))
367            .join(" and ");
368        if !no_cdn_reasons.is_empty() {
369            info!("CDN disabled because the following flags are set: {no_cdn_reasons}.");
370            Ok(None)
371        }
372        // Enable the CDN otherwise.
373        else {
374            // Determine the CDN URL.
375            match &self.cdn {
376                // Use the provided CDN URL if it is not empty.
377                Some(cdn) => match cdn.to_string().is_empty() {
378                    true => Ok(None),
379                    false => Ok(Some(cdn.clone())),
380                },
381                // If no CDN URL is provided, determine the CDN URL based on the network ID.
382                None => {
383                    let uri = format!("{}/{}", snarkos_node_cdn::CDN_BASE_URL, N::SHORT_NAME);
384                    Ok(Some(http::Uri::try_from(&uri).with_context(|| "Unexpected error")?))
385                }
386            }
387        }
388    }
389
390    /// Read the private key directly from an argument or from a filesystem location,
391    /// returning the Aleo account.
392    fn parse_private_key<N: Network>(&self) -> Result<Account<N>> {
393        match self.dev {
394            None => match (&self.private_key, &self.private_key_file) {
395                // Parse the private key directly.
396                (Some(private_key), None) => Account::from_str(private_key.trim()),
397                // Parse the private key from a file.
398                (None, Some(path)) => {
399                    check_permissions(path)?;
400                    Account::from_str(std::fs::read_to_string(path)?.trim())
401                }
402                // Ensure the private key is provided to the CLI, except for clients or nodes in development mode.
403                (None, None) => match self.client {
404                    true => Account::new(&mut rand::thread_rng()),
405                    false => bail!("Missing the '--private-key' or '--private-key-file' argument"),
406                },
407                // Ensure only one private key flag is provided to the CLI.
408                (Some(_), Some(_)) => {
409                    bail!("Cannot use '--private-key' and '--private-key-file' simultaneously, please use only one")
410                }
411            },
412            Some(index) => {
413                let private_key = get_development_key(index)?;
414                if !self.nobanner {
415                    println!(
416                        "🔑 Your development private key for node {index} is {}.\n",
417                        private_key.to_string().bold()
418                    );
419                }
420                Account::try_from(private_key)
421            }
422        }
423    }
424
425    /// Updates the configurations if the node is in development mode.
426    fn parse_development(
427        &mut self,
428        trusted_peers: &mut Vec<SocketAddr>,
429        trusted_validators: &mut Vec<SocketAddr>,
430    ) -> Result<()> {
431        // If `--dev` is not set, return early.
432        let Some(dev) = self.dev else {
433            return Ok(());
434        };
435
436        // Determine the number of development validators.
437        let num_validators = self.dev_num_validators;
438        ensure!(num_validators >= 4, "Value for `dev_num_validators` is too low. Needs to be at least 4.");
439
440        // If `--dev` is set, assume the dev nodes are initialized from 0 to `dev`,
441        // and add each of them to the trusted peers. In addition, set the node IP to `4130 + dev`,
442        // and the REST port to `3030 + dev`.
443        info!("Development mode enabled with index={dev} and num_validators={num_validators}.");
444
445        // Nodes only start as validators if the `--validator` flag is set, because the default mode is "client".
446        let is_validator = self.validator;
447
448        // Ensure the node type and `dev_num_validators` are compatible.
449        if is_validator {
450            ensure!(
451                dev < num_validators,
452                "Development validator index is too high (dev={dev}, dev_num_validators={num_validators})",
453            );
454        }
455        // A dev client or prover is allowed to have an index lower than
456        // `dev_num_validators` in order to have a balance at startup.
457
458        // Add the dev nodes to the trusted validators.
459        if trusted_validators.is_empty() && is_validator {
460            // Validators add all other validators as trusted.
461            for idx in 0..num_validators {
462                if idx == dev {
463                    continue;
464                }
465                trusted_validators.push(SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::LOCALHOST, MEMORY_POOL_PORT + idx)));
466            }
467
468            debug!("Trusted validators set to: {trusted_validators:?}");
469        }
470
471        // Determine if we need to populate `trusted_peers`.
472        if trusted_peers.is_empty() {
473            if is_validator {
474                if let Some(num_clients) = self.dev_num_clients {
475                    // Ensure the clients that added this validator as a trusted peer are able to connect to it.
476                    for client_idx in 0..num_clients {
477                        if get_devnet_validators_for_client(client_idx, num_validators).contains(&dev) {
478                            let node_idx = num_validators + client_idx;
479                            trusted_peers.push(get_devnet_router_address_for_node(node_idx));
480                        }
481                    }
482                } else {
483                    warn!(
484                        "Development validator started without trusted peers or `--dev-num-clients`. No clients will be able to connect to it."
485                    );
486                }
487            } else {
488                // Clients/provers add two validators to connect to.
489                for validator_idx in get_devnet_validators_for_client(dev, num_validators) {
490                    trusted_peers.push(get_devnet_router_address_for_node(validator_idx));
491                }
492            }
493
494            debug!("Trusted peers set to: {trusted_peers:?}");
495        } else {
496            debug!("Trusted peers/validators was set manually. Will not populate them with development addresses.")
497        }
498
499        // Set the node's listening port to `4130 + dev`.
500        //
501        // Note: the `node` flag is an option to detect remote devnet testing.
502        if self.node.is_none() {
503            // Pick 0.0.0.0 here, not localhost.
504            let port = get_devnet_router_address_for_node(dev).port();
505            let address = SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, port));
506            debug!("Setting node address to {address} due to dev={dev}");
507            self.node = Some(address);
508        }
509
510        // If the `norest` flag is not set and the REST IP is not already specified set the REST IP to `3030 + dev`.
511        if !self.norest && self.rest.is_none() {
512            let port = DEFAULT_REST_PORT + dev;
513            debug!("Setting REST port to {port} due to dev={dev}");
514            self.rest = Some(SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, port)));
515        }
516
517        Ok(())
518    }
519
520    /// Returns the path to where the JWT secret for the node is stored.
521    fn jwt_secret_path<N: Network>(node_data_dir: &NodeDataDir, address: &Address<N>) -> PathBuf {
522        node_data_dir.path().join(jwt_secret_file(address))
523    }
524
525    /// Returns an alternative genesis block if the node is in development mode.
526    /// Otherwise, returns the actual genesis block.
527    fn parse_genesis<N: Network>(&self) -> Result<Block<N>> {
528        if self.dev.is_some() {
529            // Determine the number of genesis committee members.
530            let num_committee_members = self.dev_num_validators;
531            ensure!(
532                num_committee_members >= DEVELOPMENT_MODE_NUM_GENESIS_COMMITTEE_MEMBERS,
533                "Number of genesis committee members is too low"
534            );
535
536            // Initialize the (fixed) RNG.
537            let mut rng = ChaChaRng::seed_from_u64(DEVELOPMENT_MODE_RNG_SEED);
538            // Initialize the development private keys.
539            let dev_keys =
540                (0..num_committee_members).map(|_| PrivateKey::<N>::new(&mut rng)).collect::<Result<Vec<_>>>()?;
541            // Initialize the development addresses.
542            let development_addresses = dev_keys.iter().map(Address::<N>::try_from).collect::<Result<Vec<_>>>()?;
543
544            // Construct the committee based on the state of the bonded balances.
545            let (committee, bonded_balances) = match &self.dev_bonded_balances {
546                Some(bonded_balances) => {
547                    // Parse the bonded balances.
548                    let bonded_balances = bonded_balances
549                        .0
550                        .iter()
551                        .map(|(staker_address, (validator_address, withdrawal_address, amount))| {
552                            let staker_addr = Address::<N>::from_str(staker_address)?;
553                            let validator_addr = Address::<N>::from_str(validator_address)?;
554                            let withdrawal_addr = Address::<N>::from_str(withdrawal_address)?;
555                            Ok((staker_addr, (validator_addr, withdrawal_addr, *amount)))
556                        })
557                        .collect::<Result<IndexMap<_, _>>>()?;
558
559                    // Construct the committee members.
560                    let mut members = IndexMap::new();
561                    for (staker_address, (validator_address, _, amount)) in bonded_balances.iter() {
562                        // Ensure that the staking amount is sufficient.
563                        match staker_address == validator_address {
564                            true => ensure!(amount >= &MIN_VALIDATOR_STAKE, "Validator stake is too low"),
565                            false => ensure!(amount >= &MIN_DELEGATOR_STAKE, "Delegator stake is too low"),
566                        }
567
568                        // Ensure that the validator address is included in the list of development addresses.
569                        ensure!(
570                            development_addresses.contains(validator_address),
571                            "Validator address {validator_address} is not included in the list of development addresses"
572                        );
573
574                        // Add or update the validator entry in the list of members
575                        members.entry(*validator_address).and_modify(|(stake, _, _)| *stake += amount).or_insert((
576                            *amount,
577                            true,
578                            rng.gen_range(0..100),
579                        ));
580                    }
581                    // Construct the committee.
582                    let committee = Committee::<N>::new(0u64, members)?;
583                    (committee, bonded_balances)
584                }
585                None => {
586                    // Calculate the committee stake per member.
587                    let stake_per_member =
588                        N::STARTING_SUPPLY.saturating_div(2).saturating_div(num_committee_members as u64);
589                    ensure!(stake_per_member >= MIN_VALIDATOR_STAKE, "Committee stake per member is too low");
590
591                    // Construct the committee members and distribute stakes evenly among committee members.
592                    let members = development_addresses
593                        .iter()
594                        .map(|address| (*address, (stake_per_member, true, rng.gen_range(0..100))))
595                        .collect::<IndexMap<_, _>>();
596
597                    // Construct the bonded balances.
598                    // Note: The withdrawal address is set to the staker address.
599                    let bonded_balances = members
600                        .iter()
601                        .map(|(address, (stake, _, _))| (*address, (*address, *address, *stake)))
602                        .collect::<IndexMap<_, _>>();
603                    // Construct the committee.
604                    let committee = Committee::<N>::new(0u64, members)?;
605
606                    (committee, bonded_balances)
607                }
608            };
609
610            // Ensure that the number of committee members is correct.
611            ensure!(
612                committee.members().len() == num_committee_members as usize,
613                "Number of committee members {} does not match the expected number of members {num_committee_members}",
614                committee.members().len()
615            );
616
617            // Calculate the public balance per validator.
618            let remaining_balance = N::STARTING_SUPPLY.saturating_sub(committee.total_stake());
619            let public_balance_per_validator = remaining_balance.saturating_div(num_committee_members as u64);
620
621            // Construct the public balances with fairly equal distribution.
622            let mut public_balances = dev_keys
623                .iter()
624                .map(|private_key| Ok((Address::try_from(private_key)?, public_balance_per_validator)))
625                .collect::<Result<indexmap::IndexMap<_, _>>>()?;
626
627            // If there is some leftover balance, add it to the 0-th validator.
628            let leftover =
629                remaining_balance.saturating_sub(public_balance_per_validator * num_committee_members as u64);
630            if leftover > 0 {
631                let (_, balance) = public_balances.get_index_mut(0).unwrap();
632                *balance += leftover;
633            }
634
635            // Check if the sum of committee stakes and public balances equals the total starting supply.
636            let public_balances_sum: u64 = public_balances.values().copied().sum();
637            if committee.total_stake() + public_balances_sum != N::STARTING_SUPPLY {
638                bail!("Sum of committee stakes and public balances does not equal total starting supply.");
639            }
640
641            // Construct the genesis block.
642            std::thread::spawn(move || {
643                load_or_compute_genesis(dev_keys[0], committee, public_balances, bonded_balances, &mut rng)
644            })
645            .join()
646            .unwrap()
647        } else {
648            Block::from_bytes_le(N::genesis_bytes())
649        }
650    }
651
652    /// Returns the node type specified in the command-line arguments.
653    /// This will return `NodeType::Client` if no node type was specified by the user.
654    const fn parse_node_type(&self) -> NodeType {
655        if self.validator {
656            NodeType::Validator
657        } else if self.prover {
658            NodeType::Prover
659        } else if self.bootstrap_client {
660            NodeType::BootstrapClient
661        } else {
662            NodeType::Client
663        }
664    }
665
666    /// Start the node and blocks until it terminates.
667    #[rustfmt::skip]
668    async fn parse_node<N: Network>(&mut self, log_receiver: mpsc::Receiver<Vec<u8>>) -> Result<()> {
669        if !self.nobanner {
670            // Print the welcome banner.
671            println!("{}", crate::helpers::welcome_message());
672        }
673
674        // Check if we are running with the lower coinbase and proof targets. This should only be
675        // allowed in --dev mode and should not be allowed in mainnet mode.
676        if cfg!(feature = "test_network") && self.dev.is_none() {
677            bail!("The 'test_network' feature is enabled, but the '--dev' flag is not set");
678        }
679
680        // Parse the trusted peers to connect to.
681        let mut trusted_peers = self.parse_trusted_addrs(&self.peers)?;
682        // Parse the trusted validators to connect to.
683        let mut trusted_validators = self.parse_trusted_addrs(&self.validators)?;
684
685        // Ensure there are no bootstrappers among the trusted peers and validators.
686        let bootstrap_peers = bootstrap_peers::<N>(self.dev.is_some());
687        for trusted in [&mut trusted_peers, &mut trusted_validators] {
688            let initial_peer_count = trusted.len();
689            trusted.retain(|addr| !bootstrap_peers.contains(addr));
690            let final_peer_count = trusted.len();
691            // Warn if this had to be corrected.
692            if final_peer_count != initial_peer_count {
693                warn!(
694                    "Removed some ({}) trusted peers due to them also being bootstrap peers.",
695                    initial_peer_count - final_peer_count
696                );
697            }
698        }
699
700        // Parse the development configurations.
701        self.parse_development(&mut trusted_peers, &mut trusted_validators)?;
702
703        // Determine if the node should sync from CDn..
704        let cdn = self.parse_cdn::<N>().with_context(|| "Failed to parse given CDN URL")?;
705
706        // Parse the genesis block.
707        let start = self.clone();
708        let genesis = task::spawn_blocking(move || start.parse_genesis::<N>()).await??;
709        // Parse the private key of the node.
710        let account = self.parse_private_key::<N>()?;
711        // Parse the node type.
712        let node_type = self.parse_node_type();
713
714        // Parse the node IP or use the default IP/port.
715        let node_ip = self.node.unwrap_or(SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, DEFAULT_NODE_PORT)));
716
717        // Parse the REST IP.
718        let rest_ip = match self.norest {
719            true => None,
720            false => self.rest.or_else(|| Some("0.0.0.0:3030".parse().unwrap())),
721        };
722
723        // Initialize the storage mode.
724        let storage_mode = match &self.ledger_storage {
725            Some(path) => StorageMode::Custom(path.clone()),
726            None => match self.dev {
727                Some(id) => StorageMode::Development(id),
728                None => StorageMode::Production,
729            },
730        };
731
732        // Users may have unintentionally set a custom path for the ledger, but not for the node data.
733        // For validators, we make this an errors, so important files like the proposal cache are stored at the location
734        // exepcted by the node operator.
735        if self.node_data_storage.is_some() && !matches!(storage_mode, StorageMode::Custom(_)) {
736            if node_type == NodeType::Validator {
737                bail!("Custom path set for `--node-data-storage`, but not for `--ledger-storage`.")
738            } else {
739                warn!("Custom path set for `--node-data-storage`, but not for `--ledger-storage`. The latter will use the default path.");   
740            }
741        } else if matches!(storage_mode, StorageMode::Custom(_)) && self.node_data_storage.is_none() {
742            if node_type == NodeType::Validator {
743                bail!("Custom path set for `--ledger-storage`, but not for `--node-data-storage`.");
744            } else {
745                warn!("Custom path set for `--ledger-storage`, but not for `--node-data-storage`. The latter will use the default path.");
746            }
747        }
748
749        // Parse the node data directory.
750        let node_data_dir = parse_node_data_dir(&self.node_data_storage, N::ID, self.dev).with_context(|| "Failed to setup node configuration directory")?;
751
752        // Make sure the directory exists before continue.
753        let data_path = node_data_dir.path();
754        if !data_path.exists() {
755            info!("Creating directore for node data storage at {data_path:?}");
756            std::fs::create_dir_all(data_path)
757                .with_context(|| format!("Failed to create directory for node data storage at {data_path:?}"))?
758        } else if !data_path.is_dir() {
759            bail!("Node data storage location at {data_path:?} is not a directory");
760        } else {
761            debug!("Using existing directory at {data_path:?} for node data storage");
762        }
763
764        // Checks for the old storage format and prints instructions to migrate.
765        // We perform this check after creating the node data directory, so that migrating the data is easier.
766        Self::check_for_old_storage_format(&aleo_ledger_dir(N::ID, &storage_mode), &account.address(), &node_data_dir, self.dev, self.auto_migrate_node_data).with_context(|| "Node still uses the old storage format.")?;
767
768        // Compute the optional REST server JWT.
769        let jwt_token = if self.nojwt {
770            None
771        } else if let Some(jwt_b64) = &self.jwt_secret {
772            // Decode the JWT secret.
773            let jwt_bytes = BASE64_STANDARD.decode(jwt_b64).map_err(|_| anyhow::anyhow!("Invalid JWT secret"))?;
774            if jwt_bytes.len() != 16 {
775                bail!("The JWT secret must be 16 bytes long");
776            }
777            // Create the JWT token based on the given secret.
778            let jwt_token = snarkos_node_rest::Claims::new(account.address(), Some(jwt_bytes), self.jwt_timestamp).to_jwt_string()?;
779            // Store the JWT secret to a file.
780            let path = Self::jwt_secret_path(&node_data_dir, &account.address());
781            std::fs::write(path, &jwt_token)?;
782            // Return the JWT token for optional printing.
783            Some(jwt_token)
784        } else {
785            // Create a random JWT token.
786            let jwt_token = snarkos_node_rest::Claims::new(account.address(), None, self.jwt_timestamp).to_jwt_string()?;
787            // Store the JWT secret to a file.
788            let path = Self::jwt_secret_path(&node_data_dir, &account.address());
789            std::fs::write(path, &jwt_token)? ;
790            // Return the JWT token for optional printing.
791            Some(jwt_token)
792        };
793
794        if !self.nobanner {
795            // Print the Aleo address.
796            println!("👛 Your Aleo address is {}.\n", account.address().to_string().bold());
797            // Print the node type and network.
798            println!(
799                "🧭 Starting {} on {} at {}.\n",
800                node_type.description().bold(),
801                N::NAME.bold(),
802                node_ip.to_string().bold()
803            );
804            // If the node is running a REST server, determine the JWT.
805            if let Some(rest_ip) = rest_ip {
806                println!("🌐 Starting the REST server at {}.\n", rest_ip.to_string().bold());
807                if let Some(jwt_token) = jwt_token {
808                    println!("🔑 Your one-time JWT token is {}\n", jwt_token.dimmed());
809                }
810            }
811        }
812
813        // If the node is a validator, check if the open files limit is lower than recommended.
814        #[cfg(target_family = "unix")]
815        if node_type.is_validator() {
816            crate::helpers::check_open_files_limit(RECOMMENDED_MIN_NOFILES_LIMIT);
817        }
818        // Check if the machine meets the minimum requirements for a validator.
819        crate::helpers::check_validator_machine(node_type);
820
821        // Initialize the metrics.
822        #[cfg(feature = "metrics")]
823        if self.metrics {
824            metrics::initialize_metrics(self.metrics_ip);
825        }
826
827        // Determine whether to generate background transactions in dev mode.
828        let dev_txs = match self.dev {
829            Some(_) => !self.no_dev_txs,
830            None => {
831                // If the `no_dev_txs` flag is set, inform the user that it is ignored.
832                if self.no_dev_txs {
833                    eprintln!("The '--no-dev-txs' flag is ignored because '--dev' is not set");
834                }
835                false
836            }
837        };
838
839        // TODO(kaimast): start the display earlier and show sync progress.
840        if !self.nodisplay && cdn.is_some() {
841            println!("🪧 The terminal UI will not start until the node has finished syncing from the CDN. If this step takes too long, consider restarting with `--nodisplay`.");
842        }
843
844        // Register the signal handler.
845        let signal_handler = SignalHandler::new();
846
847        // Initialize the node.
848        let node = match node_type {
849            NodeType::Validator => Node::new_validator(node_ip, self.bft, rest_ip, self.rest_rps, account, &trusted_peers, &trusted_validators, genesis, cdn, storage_mode, node_data_dir, self.trusted_peers_only, self.auto_db_checkpoints.clone(), dev_txs, self.dev, signal_handler.clone()).await,
850            NodeType::Prover => Node::new_prover(node_ip, account, &trusted_peers, genesis, node_data_dir, self.trusted_peers_only, self.dev, signal_handler.clone()).await,
851            NodeType::Client => Node::new_client(node_ip, rest_ip, self.rest_rps, account, &trusted_peers, genesis, cdn, storage_mode, node_data_dir, self.trusted_peers_only, self.auto_db_checkpoints.clone(), self.dev, signal_handler.clone()).await,
852            NodeType::BootstrapClient => Node::new_bootstrap_client(node_ip, account, *genesis.header(), self.dev).await,
853        }?;
854
855        if !self.nodisplay {
856            Display::start(node.clone(), log_receiver, signal_handler.clone()).with_context(|| "Failed to start the display")?;
857        }
858
859        node.wait_for_signals(&signal_handler).await;
860        Ok(())
861    }
862
863    /// Check if the node is still using the old storage format,
864    /// in which case we print an error and exit.
865    /// We detect this by checking if
866    /// - a peer-cache file exists inside the ledger directory,
867    /// - a current-proposal-cache file exists at the parent directory of the ledger directory
868    /// - a jwt_secret_*.txt file exists at the parent directory of the ledger directory
869    fn check_for_old_storage_format<N: Network>(
870        ledger_dir: &Path,
871        address: &Address<N>,
872        node_data_dir: &NodeDataDir,
873        dev: Option<u16>,
874        auto_migrate: bool,
875    ) -> Result<()> {
876        let ledger_parent_dir = ledger_dir.parent().unwrap();
877
878        // Determine the old paths used for node configuration files.
879        let old_router_cache_path = ledger_dir.join(node_data::LEGACY_ROUTER_PEER_CACHE_FILE);
880        let old_gateway_cache_path = ledger_dir.join(node_data::LEGACY_GATEWAY_PEER_CACHE_FILE);
881        let old_proposal_cache_path = ledger_dir.join(node_data::legacy_current_proposal_cache_file(N::ID, dev));
882        let old_jwt_secret_path = ledger_parent_dir.join(node_data::jwt_secret_file(address));
883
884        if auto_migrate {
885            if old_router_cache_path.exists() {
886                let new_router_cache_path = node_data_dir.router_peer_cache_path();
887                info!("Migrating node data file \"{old_router_cache_path:?}\" to \"{new_router_cache_path:?}\"");
888                fs::rename(old_router_cache_path, new_router_cache_path)
889                    .with_context(|| "Failed to migrate node data file")?;
890            }
891
892            if old_gateway_cache_path.exists() {
893                let new_gateway_cache_path = node_data_dir.gateway_peer_cache_path();
894                info!("Migrating node data file \"{old_gateway_cache_path:?}\" to \"{new_gateway_cache_path:?}\"");
895                fs::rename(old_gateway_cache_path, new_gateway_cache_path)
896                    .with_context(|| "Failed to migrate node data file")?;
897            }
898
899            if old_proposal_cache_path.exists() {
900                let new_proposal_cache_path = node_data_dir.current_proposal_cache_path();
901                info!("Migrating node data file \"{old_proposal_cache_path:?}\" to \"{new_proposal_cache_path:?}\"");
902                fs::rename(old_proposal_cache_path, new_proposal_cache_path)
903                    .with_context(|| "Failed to migrate node data file")?;
904            }
905
906            if old_jwt_secret_path.exists() {
907                let new_jwt_secret_path = node_data_dir.jwt_secret_path(&address);
908                info!("Migrating node data file \"{old_jwt_secret_path:?}\" to \"{new_jwt_secret_path:?}\"");
909                fs::rename(old_jwt_secret_path, new_jwt_secret_path)
910                    .with_context(|| "Failed to migrate node data file")?;
911            }
912        } else {
913            if old_router_cache_path.exists() {
914                let new_router_cache_path = node_data_dir.router_peer_cache_path();
915                bail!(
916                    "Please migrate the node data file \"{old_router_cache_path:?}\" to \"{new_router_cache_path:?}\" before restarting, or restart with `--auto-migrate-node-data`."
917                );
918            }
919
920            if old_gateway_cache_path.exists() {
921                let new_gateway_cache_path = node_data_dir.gateway_peer_cache_path();
922                bail!(
923                    "Please migrate the node data file \"{old_gateway_cache_path:?}\" to \"{new_gateway_cache_path:?}\" before restarting, or restart with `--auto-migrate-node-data`."
924                );
925            }
926
927            if old_proposal_cache_path.exists() {
928                let new_proposal_cache_path = node_data_dir.current_proposal_cache_path();
929                bail!(
930                    "Please migrate the node data file \"{old_proposal_cache_path:?}\" to \"{new_proposal_cache_path:?}\" before restarting, or restart with `--auto-migrate-node-data`."
931                );
932            }
933
934            if old_jwt_secret_path.exists() {
935                let new_jwt_secret_path = node_data_dir.jwt_secret_path(&address);
936                bail!(
937                    "Please migrate the node data file \"{old_jwt_secret_path:?}\" to \"{new_jwt_secret_path:?}\" before restarting, or restart with `--auto-migrate-node-data`."
938                );
939            }
940        }
941
942        Ok(())
943    }
944
945    /// Starts a rayon thread pool and tokio runtime for the node, and returns the tokio `Runtime`.
946    fn runtime() -> Runtime {
947        // Retrieve the number of cores.
948        let num_cores = num_cpus::get();
949
950        // Initialize the number of tokio worker threads, max tokio blocking threads, and rayon cores.
951        // Note: We intentionally set the number of tokio worker threads and number of rayon cores to be
952        // more than the number of physical cores, because the node is expected to be I/O-bound.
953        let (num_tokio_worker_threads, max_tokio_blocking_threads, num_rayon_cores_global) =
954            (2 * num_cores, 512, num_cores);
955
956        // Set up the rayon thread pool.
957        // A custom panic handler is not needed here, as rayon propagates the panic to the calling thread by default (except for `rayon::spawn` which we do not use).
958        rayon::ThreadPoolBuilder::new()
959            .stack_size(8 * 1024 * 1024)
960            .num_threads(num_rayon_cores_global)
961            .build_global()
962            .unwrap();
963
964        // Set up the tokio Runtime.
965        // TODO(kaimast): set up a panic handler here for each worker thread once [`tokio::runtime::Builder::unhandled_panic`](https://docs.rs/tokio/latest/tokio/runtime/struct.Builder.html#method.unhandled_panic) is stabilized.
966        runtime::Builder::new_multi_thread()
967            .enable_all()
968            .thread_stack_size(8 * 1024 * 1024)
969            .worker_threads(num_tokio_worker_threads)
970            .max_blocking_threads(max_tokio_blocking_threads)
971            .build()
972            .expect("Failed to initialize a runtime for the router")
973    }
974}
975
976fn check_permissions(path: &PathBuf) -> Result<(), snarkvm::prelude::Error> {
977    #[cfg(target_family = "unix")]
978    {
979        use std::os::unix::fs::PermissionsExt;
980        ensure!(path.exists(), "The file '{path:?}' does not exist");
981        crate::check_parent_permissions(path)?;
982        let permissions = path.metadata()?.permissions().mode();
983        ensure!(permissions & 0o777 == 0o600, "The file {path:?} must be readable only by the owner (0600)");
984    }
985    Ok(())
986}
987
988/// Loads or computes the genesis block.
989fn load_or_compute_genesis<N: Network>(
990    genesis_private_key: PrivateKey<N>,
991    committee: Committee<N>,
992    public_balances: indexmap::IndexMap<Address<N>, u64>,
993    bonded_balances: indexmap::IndexMap<Address<N>, (Address<N>, Address<N>, u64)>,
994    rng: &mut ChaChaRng,
995) -> Result<Block<N>> {
996    // Construct the preimage.
997    let mut preimage = Vec::new();
998
999    // Input the network ID.
1000    preimage.extend(&N::ID.to_le_bytes());
1001    // Input the genesis coinbase target.
1002    preimage.extend(&to_bytes_le![N::GENESIS_COINBASE_TARGET]?);
1003    // Input the genesis proof target.
1004    preimage.extend(&to_bytes_le![N::GENESIS_PROOF_TARGET]?);
1005
1006    // Input the genesis private key, committee, and public balances.
1007    preimage.extend(genesis_private_key.to_bytes_le()?);
1008    preimage.extend(committee.to_bytes_le()?);
1009    preimage.extend(&to_bytes_le![public_balances.iter().collect::<Vec<(_, _)>>()]?);
1010    preimage.extend(&to_bytes_le![
1011        bonded_balances
1012            .iter()
1013            .flat_map(|(staker, (validator, withdrawal, amount))| to_bytes_le![staker, validator, withdrawal, amount])
1014            .collect::<Vec<_>>()
1015    ]?);
1016
1017    // Input the parameters' metadata based on network
1018    match N::ID {
1019        snarkvm::console::network::MainnetV0::ID => {
1020            preimage.extend(snarkvm::parameters::mainnet::BondValidatorVerifier::METADATA.as_bytes());
1021            preimage.extend(snarkvm::parameters::mainnet::BondPublicVerifier::METADATA.as_bytes());
1022            preimage.extend(snarkvm::parameters::mainnet::UnbondPublicVerifier::METADATA.as_bytes());
1023            preimage.extend(snarkvm::parameters::mainnet::ClaimUnbondPublicVerifier::METADATA.as_bytes());
1024            preimage.extend(snarkvm::parameters::mainnet::SetValidatorStateVerifier::METADATA.as_bytes());
1025            preimage.extend(snarkvm::parameters::mainnet::TransferPrivateVerifier::METADATA.as_bytes());
1026            preimage.extend(snarkvm::parameters::mainnet::TransferPublicVerifier::METADATA.as_bytes());
1027            preimage.extend(snarkvm::parameters::mainnet::TransferPrivateToPublicVerifier::METADATA.as_bytes());
1028            preimage.extend(snarkvm::parameters::mainnet::TransferPublicToPrivateVerifier::METADATA.as_bytes());
1029            preimage.extend(snarkvm::parameters::mainnet::FeePrivateVerifier::METADATA.as_bytes());
1030            preimage.extend(snarkvm::parameters::mainnet::FeePublicVerifier::METADATA.as_bytes());
1031            preimage.extend(snarkvm::parameters::mainnet::InclusionVerifier::METADATA.as_bytes());
1032        }
1033        snarkvm::console::network::TestnetV0::ID => {
1034            preimage.extend(snarkvm::parameters::testnet::BondValidatorVerifier::METADATA.as_bytes());
1035            preimage.extend(snarkvm::parameters::testnet::BondPublicVerifier::METADATA.as_bytes());
1036            preimage.extend(snarkvm::parameters::testnet::UnbondPublicVerifier::METADATA.as_bytes());
1037            preimage.extend(snarkvm::parameters::testnet::ClaimUnbondPublicVerifier::METADATA.as_bytes());
1038            preimage.extend(snarkvm::parameters::testnet::SetValidatorStateVerifier::METADATA.as_bytes());
1039            preimage.extend(snarkvm::parameters::testnet::TransferPrivateVerifier::METADATA.as_bytes());
1040            preimage.extend(snarkvm::parameters::testnet::TransferPublicVerifier::METADATA.as_bytes());
1041            preimage.extend(snarkvm::parameters::testnet::TransferPrivateToPublicVerifier::METADATA.as_bytes());
1042            preimage.extend(snarkvm::parameters::testnet::TransferPublicToPrivateVerifier::METADATA.as_bytes());
1043            preimage.extend(snarkvm::parameters::testnet::FeePrivateVerifier::METADATA.as_bytes());
1044            preimage.extend(snarkvm::parameters::testnet::FeePublicVerifier::METADATA.as_bytes());
1045            preimage.extend(snarkvm::parameters::testnet::InclusionVerifier::METADATA.as_bytes());
1046        }
1047        snarkvm::console::network::CanaryV0::ID => {
1048            preimage.extend(snarkvm::parameters::canary::BondValidatorVerifier::METADATA.as_bytes());
1049            preimage.extend(snarkvm::parameters::canary::BondPublicVerifier::METADATA.as_bytes());
1050            preimage.extend(snarkvm::parameters::canary::UnbondPublicVerifier::METADATA.as_bytes());
1051            preimage.extend(snarkvm::parameters::canary::ClaimUnbondPublicVerifier::METADATA.as_bytes());
1052            preimage.extend(snarkvm::parameters::canary::SetValidatorStateVerifier::METADATA.as_bytes());
1053            preimage.extend(snarkvm::parameters::canary::TransferPrivateVerifier::METADATA.as_bytes());
1054            preimage.extend(snarkvm::parameters::canary::TransferPublicVerifier::METADATA.as_bytes());
1055            preimage.extend(snarkvm::parameters::canary::TransferPrivateToPublicVerifier::METADATA.as_bytes());
1056            preimage.extend(snarkvm::parameters::canary::TransferPublicToPrivateVerifier::METADATA.as_bytes());
1057            preimage.extend(snarkvm::parameters::canary::FeePrivateVerifier::METADATA.as_bytes());
1058            preimage.extend(snarkvm::parameters::canary::FeePublicVerifier::METADATA.as_bytes());
1059            preimage.extend(snarkvm::parameters::canary::InclusionVerifier::METADATA.as_bytes());
1060        }
1061        _ => {
1062            // Unrecognized Network ID
1063            bail!("Unrecognized Network ID: {}", N::ID);
1064        }
1065    }
1066
1067    // Initialize the hasher.
1068    let hasher = snarkvm::console::algorithms::BHP256::<N>::setup("aleo.dev.block")?;
1069    // Compute the hash.
1070    // NOTE: this is a fast-to-compute but *IMPERFECT* identifier for the genesis block;
1071    //       to know the actual genesis block hash, you need to compute the block itself.
1072    let hash = hasher.hash(&preimage.to_bits_le())?.to_string();
1073
1074    // A closure to load the block.
1075    let load_block = |file_path| -> Result<Block<N>> {
1076        // Attempts to load the genesis block file locally.
1077        let buffer = std::fs::read(file_path)?;
1078        // Return the genesis block.
1079        Block::from_bytes_le(&buffer)
1080    };
1081
1082    // Construct the file path.
1083    let file_path = std::env::temp_dir().join(hash);
1084    // Check if the genesis block exists.
1085    if file_path.exists() {
1086        // If the block loads successfully, return it.
1087        if let Ok(block) = load_block(&file_path) {
1088            return Ok(block);
1089        }
1090    }
1091
1092    /* Otherwise, compute the genesis block and store it. */
1093
1094    // Initialize a new VM.
1095    let vm = VM::from(ConsensusStore::<N, ConsensusMemory<N>>::open(StorageMode::new_test(None))?)?;
1096    // Initialize the genesis block.
1097    let block = vm.genesis_quorum(&genesis_private_key, committee, public_balances, bonded_balances, rng)?;
1098    // Write the genesis block to the file.
1099    std::fs::write(&file_path, block.to_bytes_le()?)?;
1100    // Return the genesis block.
1101    Ok(block)
1102}
1103
1104fn resolve_potential_hostnames(ip_or_hostname: &str) -> Result<SocketAddr> {
1105    let trimmed = ip_or_hostname.trim();
1106    match trimmed.to_socket_addrs() {
1107        Ok(mut ip_iter) => {
1108            // A hostname might resolve to multiple IP addresses. We will use only the first one,
1109            // assuming this aligns with the user's expectations.
1110            let Some(ip) = ip_iter.next() else {
1111                return Err(anyhow!("The supplied trusted hostname ('{trimmed}') does not reference any ip."));
1112            };
1113            Ok(ip)
1114        }
1115        Err(e) => Err(anyhow!("The supplied trusted hostname or IP ('{trimmed}') is malformed: {e}")),
1116    }
1117}
1118
1119#[cfg(test)]
1120mod tests {
1121    use super::*;
1122    use crate::commands::{CLI, Command};
1123    use snarkvm::prelude::MainnetV0;
1124
1125    use ureq::http;
1126
1127    type CurrentNetwork = MainnetV0;
1128
1129    #[test]
1130    fn test_parse_trusted_addrs() {
1131        let config = Start::try_parse_from(["snarkos", "--peers", ""].iter()).unwrap();
1132        assert!(config.parse_trusted_addrs(&config.peers).is_ok());
1133        assert!(config.parse_trusted_addrs(&config.peers).unwrap().is_empty());
1134
1135        let config = Start::try_parse_from(["snarkos", "--peers", "1.2.3.4:5"].iter()).unwrap();
1136        assert!(config.parse_trusted_addrs(&config.peers).is_ok());
1137        assert_eq!(config.parse_trusted_addrs(&config.peers).unwrap(), vec![
1138            SocketAddr::from_str("1.2.3.4:5").unwrap()
1139        ]);
1140
1141        let config = Start::try_parse_from(["snarkos", "--peers", "1.2.3.4:5,6.7.8.9:0"].iter()).unwrap();
1142        assert!(config.parse_trusted_addrs(&config.peers).is_ok());
1143        assert_eq!(config.parse_trusted_addrs(&config.peers).unwrap(), vec![
1144            SocketAddr::from_str("1.2.3.4:5").unwrap(),
1145            SocketAddr::from_str("6.7.8.9:0").unwrap()
1146        ]);
1147    }
1148
1149    #[test]
1150    fn test_parse_trusted_validators() {
1151        let config = Start::try_parse_from(["snarkos", "--validators", ""].iter()).unwrap();
1152        assert!(config.parse_trusted_addrs(&config.validators).is_ok());
1153        assert!(config.parse_trusted_addrs(&config.validators).unwrap().is_empty());
1154
1155        let config = Start::try_parse_from(["snarkos", "--validators", "1.2.3.4:5"].iter()).unwrap();
1156        assert!(config.parse_trusted_addrs(&config.validators).is_ok());
1157        assert_eq!(config.parse_trusted_addrs(&config.validators).unwrap(), vec![
1158            SocketAddr::from_str("1.2.3.4:5").unwrap()
1159        ]);
1160
1161        let config = Start::try_parse_from(["snarkos", "--validators", "1.2.3.4:5,6.7.8.9:0"].iter()).unwrap();
1162        assert!(config.parse_trusted_addrs(&config.validators).is_ok());
1163        assert_eq!(config.parse_trusted_addrs(&config.validators).unwrap(), vec![
1164            SocketAddr::from_str("1.2.3.4:5").unwrap(),
1165            SocketAddr::from_str("6.7.8.9:0").unwrap()
1166        ]);
1167    }
1168
1169    #[test]
1170    fn test_parse_log_filter() {
1171        // Ensure we cannot set, both, log-filter and verbosity
1172        let result = Start::try_parse_from(["snarkos", "--verbosity=5", "--log-filter=warn"].iter());
1173        assert!(result.is_err(), "Must not be able to set log-filter and verbosity at the same time");
1174
1175        // Ensure the values are set correctly.
1176        let config = Start::try_parse_from(["snarkos", "--verbosity=5"].iter()).unwrap();
1177        assert_eq!(config.verbosity, 5);
1178        let config = Start::try_parse_from(["snarkos", "--log-filter=snarkos=warn"].iter()).unwrap();
1179        assert_eq!(config.log_filter, Some("snarkos=warn".to_string()));
1180    }
1181
1182    #[test]
1183    fn test_parse_cdn() -> Result<()> {
1184        // Validator (Prod)
1185        let config = Start::try_parse_from(["snarkos", "--validator", "--private-key", "aleo1xx"].iter()).unwrap();
1186        assert!(config.parse_cdn::<CurrentNetwork>()?.is_some());
1187        let config =
1188            Start::try_parse_from(["snarkos", "--validator", "--private-key", "aleo1xx", "--cdn", "url"].iter())
1189                .unwrap();
1190        assert!(config.parse_cdn::<CurrentNetwork>()?.is_some());
1191        let config = Start::try_parse_from(["snarkos", "--validator", "--private-key", "aleo1xx", "--nocdn"].iter())?;
1192        assert!(config.parse_cdn::<CurrentNetwork>()?.is_none());
1193
1194        // Validator (Dev)
1195        let config =
1196            Start::try_parse_from(["snarkos", "--dev", "0", "--validator", "--private-key", "aleo1xx"].iter()).unwrap();
1197        assert!(config.parse_cdn::<CurrentNetwork>()?.is_none());
1198        let config = Start::try_parse_from(
1199            ["snarkos", "--dev", "0", "--validator", "--private-key", "aleo1xx", "--cdn", "url"].iter(),
1200        )
1201        .unwrap();
1202        assert!(config.parse_cdn::<CurrentNetwork>()?.is_none());
1203        let config = Start::try_parse_from(
1204            ["snarkos", "--dev", "0", "--validator", "--private-key", "aleo1xx", "--nocdn"].iter(),
1205        )?;
1206        assert!(config.parse_cdn::<CurrentNetwork>()?.is_none());
1207
1208        // Prover (Prod)
1209        let config = Start::try_parse_from(["snarkos", "--prover", "--private-key", "aleo1xx"].iter())?;
1210        assert!(config.parse_cdn::<CurrentNetwork>()?.is_none());
1211        let config = Start::try_parse_from(["snarkos", "--prover", "--private-key", "aleo1xx", "--cdn", "url"].iter())?;
1212        assert!(config.parse_cdn::<CurrentNetwork>()?.is_none());
1213        let config = Start::try_parse_from(["snarkos", "--prover", "--private-key", "aleo1xx", "--nocdn"].iter())?;
1214        assert!(config.parse_cdn::<CurrentNetwork>()?.is_none());
1215
1216        // Prover (Dev)
1217        let config =
1218            Start::try_parse_from(["snarkos", "--dev", "0", "--prover", "--private-key", "aleo1xx"].iter()).unwrap();
1219        assert!(config.parse_cdn::<CurrentNetwork>()?.is_none());
1220        let config = Start::try_parse_from(
1221            ["snarkos", "--dev", "0", "--prover", "--private-key", "aleo1xx", "--cdn", "url"].iter(),
1222        )
1223        .unwrap();
1224        assert!(config.parse_cdn::<CurrentNetwork>()?.is_none());
1225        let config =
1226            Start::try_parse_from(["snarkos", "--dev", "0", "--prover", "--private-key", "aleo1xx", "--nocdn"].iter())
1227                .unwrap();
1228        assert!(config.parse_cdn::<CurrentNetwork>()?.is_none());
1229
1230        // Client (Prod)
1231        let config = Start::try_parse_from(["snarkos", "--client", "--private-key", "aleo1xx"].iter()).unwrap();
1232        assert!(config.parse_cdn::<CurrentNetwork>()?.is_some());
1233        let config =
1234            Start::try_parse_from(["snarkos", "--client", "--private-key", "aleo1xx", "--cdn", "url"].iter()).unwrap();
1235        assert!(config.parse_cdn::<CurrentNetwork>()?.is_some());
1236        let config =
1237            Start::try_parse_from(["snarkos", "--client", "--private-key", "aleo1xx", "--nocdn"].iter()).unwrap();
1238        assert!(config.parse_cdn::<CurrentNetwork>()?.is_none());
1239
1240        // Client (Dev)
1241        let config =
1242            Start::try_parse_from(["snarkos", "--dev", "0", "--client", "--private-key", "aleo1xx"].iter()).unwrap();
1243        assert!(config.parse_cdn::<CurrentNetwork>()?.is_none());
1244        let config = Start::try_parse_from(
1245            ["snarkos", "--dev", "0", "--client", "--private-key", "aleo1xx", "--cdn", "url"].iter(),
1246        )
1247        .unwrap();
1248        assert!(config.parse_cdn::<CurrentNetwork>()?.is_none());
1249        let config =
1250            Start::try_parse_from(["snarkos", "--dev", "0", "--client", "--private-key", "aleo1xx", "--nocdn"].iter())
1251                .unwrap();
1252        assert!(config.parse_cdn::<CurrentNetwork>()?.is_none());
1253
1254        // Default (Prod)
1255        let config = Start::try_parse_from(["snarkos"].iter()).unwrap();
1256        assert!(config.parse_cdn::<CurrentNetwork>()?.is_some());
1257        let config = Start::try_parse_from(["snarkos", "--cdn", "url"].iter()).unwrap();
1258        assert!(config.parse_cdn::<CurrentNetwork>()?.is_some());
1259        let config = Start::try_parse_from(["snarkos", "--nocdn"].iter()).unwrap();
1260        assert!(config.parse_cdn::<CurrentNetwork>()?.is_none());
1261
1262        // Default (Dev)
1263        let config = Start::try_parse_from(["snarkos", "--dev", "0"].iter()).unwrap();
1264        assert!(config.parse_cdn::<CurrentNetwork>()?.is_none());
1265        let config = Start::try_parse_from(["snarkos", "--dev", "0", "--cdn", "url"].iter()).unwrap();
1266        assert!(config.parse_cdn::<CurrentNetwork>()?.is_none());
1267        let config = Start::try_parse_from(["snarkos", "--dev", "0", "--nocdn"].iter()).unwrap();
1268        assert!(config.parse_cdn::<CurrentNetwork>()?.is_none());
1269
1270        Ok(())
1271    }
1272
1273    #[test]
1274    fn test_parse_development_and_genesis() {
1275        let prod_genesis = Block::from_bytes_le(CurrentNetwork::genesis_bytes()).unwrap();
1276
1277        let mut trusted_peers = vec![];
1278        let mut trusted_validators = vec![];
1279        let mut config = Start::try_parse_from(["snarkos"].iter()).unwrap();
1280        config.parse_development(&mut trusted_peers, &mut trusted_validators).unwrap();
1281        let candidate_genesis = config.parse_genesis::<CurrentNetwork>().unwrap();
1282        assert_eq!(trusted_peers.len(), 0);
1283        assert_eq!(trusted_validators.len(), 0);
1284        assert_eq!(candidate_genesis, prod_genesis);
1285
1286        let _config = Start::try_parse_from(["snarkos", "--dev", ""].iter()).unwrap_err();
1287
1288        // Validator dev mode with default settings.
1289        let mut trusted_peers = vec![];
1290        let mut trusted_validators = vec![];
1291        let mut config = Start::try_parse_from(["snarkos", "--dev", "1", "--validator"].iter()).unwrap();
1292        config.parse_development(&mut trusted_peers, &mut trusted_validators).unwrap();
1293        assert_eq!(config.rest, Some(SocketAddr::from_str("0.0.0.0:3031").unwrap()));
1294        assert_eq!(trusted_validators.len(), 3);
1295
1296        // Validator dev mode with `--rest` flag.
1297        let mut trusted_peers = vec![];
1298        let mut trusted_validators = vec![];
1299        let mut config =
1300            Start::try_parse_from(["snarkos", "--dev", "1", "--rest", "127.0.0.1:8080", "--validator"].iter()).unwrap();
1301        config.parse_development(&mut trusted_peers, &mut trusted_validators).unwrap();
1302        assert_eq!(config.rest, Some(SocketAddr::from_str("127.0.0.1:8080").unwrap()));
1303        assert_eq!(trusted_validators.len(), 3);
1304
1305        // Validator dev mode with `--norest` flag.
1306        let mut trusted_peers = vec![];
1307        let mut trusted_validators = vec![];
1308        let mut config = Start::try_parse_from(["snarkos", "--dev", "1", "--norest", "--validator"].iter()).unwrap();
1309        config.parse_development(&mut trusted_peers, &mut trusted_validators).unwrap();
1310        assert!(config.rest.is_none());
1311        assert_eq!(trusted_validators.len(), 3);
1312
1313        // Client dev node.
1314        let mut trusted_peers = vec![];
1315        let mut trusted_validators = vec![];
1316        let mut config = Start::try_parse_from(["snarkos", "--dev", "5"].iter()).unwrap();
1317        config.parse_development(&mut trusted_peers, &mut trusted_validators).unwrap();
1318        let expected_genesis = config.parse_genesis::<CurrentNetwork>().unwrap();
1319        assert_eq!(config.node, Some(SocketAddr::from_str("0.0.0.0:4135").unwrap()));
1320        assert_eq!(config.rest, Some(SocketAddr::from_str("0.0.0.0:3035").unwrap()));
1321        assert_eq!(trusted_peers.len(), 2);
1322        assert_eq!(trusted_validators.len(), 0);
1323        assert!(!config.validator);
1324        assert!(!config.prover);
1325        assert!(!config.client);
1326        assert_ne!(expected_genesis, prod_genesis);
1327
1328        // Validator dev node with `--private-key` flag.
1329        let mut trusted_peers = vec![];
1330        let mut trusted_validators = vec![];
1331        let mut config =
1332            Start::try_parse_from(["snarkos", "--dev", "1", "--validator", "--private-key", ""].iter()).unwrap();
1333        config.parse_development(&mut trusted_peers, &mut trusted_validators).unwrap();
1334        let genesis = config.parse_genesis::<CurrentNetwork>().unwrap();
1335        assert_eq!(config.node, Some(SocketAddr::from_str("0.0.0.0:4131").unwrap()));
1336        assert_eq!(config.rest, Some(SocketAddr::from_str("0.0.0.0:3031").unwrap()));
1337        assert_eq!(trusted_peers.len(), 0);
1338        assert_eq!(trusted_validators.len(), 3);
1339        assert!(config.validator);
1340        assert!(!config.prover);
1341        assert!(!config.client);
1342        assert_eq!(genesis, expected_genesis);
1343
1344        // Prover dev node with `--private-key` flag.
1345        let mut trusted_peers = vec![];
1346        let mut trusted_validators = vec![];
1347        let mut config =
1348            Start::try_parse_from(["snarkos", "--dev", "6", "--prover", "--private-key", ""].iter()).unwrap();
1349        config.parse_development(&mut trusted_peers, &mut trusted_validators).unwrap();
1350        let genesis = config.parse_genesis::<CurrentNetwork>().unwrap();
1351        assert_eq!(config.node, Some(SocketAddr::from_str("0.0.0.0:4136").unwrap()));
1352        assert_eq!(config.rest, Some(SocketAddr::from_str("0.0.0.0:3036").unwrap()));
1353        assert_eq!(trusted_peers.len(), 2);
1354        assert_eq!(trusted_validators.len(), 0);
1355        assert!(!config.validator);
1356        assert!(config.prover);
1357        assert!(!config.client);
1358        assert_eq!(genesis, expected_genesis);
1359
1360        // Client dev node with `--private-key` flag.
1361        let mut trusted_peers = vec![];
1362        let mut trusted_validators = vec![];
1363        let mut config =
1364            Start::try_parse_from(["snarkos", "--dev", "10", "--client", "--private-key", ""].iter()).unwrap();
1365        config.parse_development(&mut trusted_peers, &mut trusted_validators).unwrap();
1366        let genesis = config.parse_genesis::<CurrentNetwork>().unwrap();
1367        assert_eq!(config.node, Some(SocketAddr::from_str("0.0.0.0:4140").unwrap()));
1368        assert_eq!(config.rest, Some(SocketAddr::from_str("0.0.0.0:3040").unwrap()));
1369        assert_eq!(trusted_peers.len(), 2);
1370        assert_eq!(trusted_validators.len(), 0);
1371        assert!(!config.validator);
1372        assert!(!config.prover);
1373        assert!(config.client);
1374        assert_eq!(genesis, expected_genesis);
1375    }
1376
1377    /// Tests that you cannot pass the `--dev-num-clients` flag while also passing the `--peers` flag.
1378    #[test]
1379    fn test_parse_development_num_clients_and_peers() {
1380        let result = Start::try_parse_from(
1381            ["snarkos", "--validator", "--dev", "1", "--peers", "127.0.0.1:3030", "--dev-num-clients", "1"].iter(),
1382        );
1383        assert!(result.is_err());
1384    }
1385
1386    #[test]
1387    fn clap_snarkos_start() {
1388        let arg_vec = vec![
1389            "snarkos",
1390            "start",
1391            "--nodisplay",
1392            "--dev",
1393            "2",
1394            "--validator",
1395            "--private-key",
1396            "PRIVATE_KEY",
1397            "--cdn",
1398            "CDN",
1399            "--peers",
1400            "IP1,IP2,IP3",
1401            "--validators",
1402            "IP1,IP2,IP3",
1403            "--rest",
1404            "127.0.0.1:3030",
1405        ];
1406        let cli = CLI::parse_from(arg_vec);
1407
1408        let Command::Start(start) = cli.command else {
1409            panic!("Unexpected result of clap parsing!");
1410        };
1411
1412        assert!(start.nodisplay);
1413        assert_eq!(start.dev, Some(2));
1414        assert!(start.validator);
1415        assert_eq!(start.private_key.as_deref(), Some("PRIVATE_KEY"));
1416        assert_eq!(start.cdn, Some(http::Uri::try_from("CDN").unwrap()));
1417        assert_eq!(start.rest, Some("127.0.0.1:3030".parse().unwrap()));
1418        assert_eq!(start.network, 0);
1419        assert_eq!(start.peers, Some("IP1,IP2,IP3".to_string()));
1420        assert_eq!(start.validators, Some("IP1,IP2,IP3".to_string()));
1421    }
1422
1423    /// Ensure two clients do not connect to the same validators.
1424    #[test]
1425    fn test_parse_development_client_validators() {
1426        let mut client1_config =
1427            Start::try_parse_from(["snarkos", "--dev", "10", "--client", "--private-key", ""].iter()).unwrap();
1428        let mut trusted_peers1 = vec![];
1429        let mut trusted_validators1 = vec![];
1430        client1_config.parse_development(&mut trusted_peers1, &mut trusted_validators1).unwrap();
1431
1432        let mut client2_config =
1433            Start::try_parse_from(["snarkos", "--dev", "11", "--client", "--private-key", ""].iter()).unwrap();
1434        let mut trusted_peers2 = vec![];
1435        let mut trusted_validators2 = vec![];
1436        client2_config.parse_development(&mut trusted_peers2, &mut trusted_validators2).unwrap();
1437
1438        assert_ne!(trusted_peers1, trusted_peers2);
1439    }
1440
1441    #[test]
1442    fn parse_peers_when_ips() {
1443        let arg_vec = vec!["snarkos", "start", "--peers", "127.0.0.1:3030,127.0.0.2:3030"];
1444        let cli = CLI::parse_from(arg_vec);
1445
1446        if let Command::Start(start) = cli.command {
1447            let peers = start.parse_trusted_addrs(&start.peers);
1448            assert!(peers.is_ok());
1449            assert_eq!(peers.unwrap().len(), 2, "Expected two peers");
1450        } else {
1451            panic!("Unexpected result of clap parsing!");
1452        }
1453    }
1454
1455    #[test]
1456    fn parse_peers_when_hostnames() {
1457        let arg_vec = vec!["snarkos", "start", "--peers", "www.example.com:4130,www.google.com:4130"];
1458        let cli = CLI::parse_from(arg_vec);
1459
1460        if let Command::Start(start) = cli.command {
1461            let peers = start.parse_trusted_addrs(&start.peers);
1462            assert!(peers.is_ok());
1463            assert_eq!(peers.unwrap().len(), 2, "Expected two peers");
1464        } else {
1465            panic!("Unexpected result of clap parsing!");
1466        }
1467    }
1468
1469    #[test]
1470    fn parse_peers_when_mixed_and_with_whitespaces() {
1471        let arg_vec = vec!["snarkos", "start", "--peers", "  127.0.0.1:3030,  www.google.com:4130 "];
1472        let cli = CLI::parse_from(arg_vec);
1473
1474        if let Command::Start(start) = cli.command {
1475            let peers = start.parse_trusted_addrs(&start.peers);
1476            assert!(peers.is_ok());
1477            assert_eq!(peers.unwrap().len(), 2, "Expected two peers");
1478        } else {
1479            panic!("Unexpected result of clap parsing!");
1480        }
1481    }
1482
1483    #[test]
1484    fn parse_peers_when_unknown_hostname_gracefully() {
1485        let arg_vec = vec!["snarkos", "start", "--peers", "banana.cake.eafafdaeefasdfasd.com"];
1486        let cli = CLI::parse_from(arg_vec);
1487
1488        if let Command::Start(start) = cli.command {
1489            assert!(start.parse_trusted_addrs(&start.peers).is_err());
1490        } else {
1491            panic!("Unexpected result of clap parsing!");
1492        }
1493    }
1494}