snarkos_node/
node.rs

1// Copyright (c) 2019-2025 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::{Client, Prover, Validator, traits::NodeInterface};
17use snarkos_account::Account;
18use snarkos_node_router::messages::NodeType;
19use snarkvm::prelude::{
20    Address,
21    Network,
22    PrivateKey,
23    ViewKey,
24    block::Block,
25    store::helpers::{memory::ConsensusMemory, rocksdb::ConsensusDB},
26};
27
28use aleo_std::StorageMode;
29use anyhow::Result;
30use std::{
31    net::SocketAddr,
32    sync::{Arc, atomic::AtomicBool},
33};
34
35pub enum Node<N: Network> {
36    /// A validator is a full node, capable of validating blocks.
37    Validator(Arc<Validator<N, ConsensusDB<N>>>),
38    /// A prover is a light node, capable of producing proofs for consensus.
39    Prover(Arc<Prover<N, ConsensusMemory<N>>>),
40    /// A client node is a full node, capable of querying with the network.
41    Client(Arc<Client<N, ConsensusDB<N>>>),
42}
43
44impl<N: Network> Node<N> {
45    /// Initializes a new validator node.
46    pub async fn new_validator(
47        node_ip: SocketAddr,
48        bft_ip: Option<SocketAddr>,
49        rest_ip: Option<SocketAddr>,
50        rest_rps: u32,
51        account: Account<N>,
52        trusted_peers: &[SocketAddr],
53        trusted_validators: &[SocketAddr],
54        genesis: Block<N>,
55        cdn: Option<String>,
56        storage_mode: StorageMode,
57        allow_external_peers: bool,
58        dev_txs: bool,
59        shutdown: Arc<AtomicBool>,
60    ) -> Result<Self> {
61        Ok(Self::Validator(Arc::new(
62            Validator::new(
63                node_ip,
64                bft_ip,
65                rest_ip,
66                rest_rps,
67                account,
68                trusted_peers,
69                trusted_validators,
70                genesis,
71                cdn,
72                storage_mode,
73                allow_external_peers,
74                dev_txs,
75                shutdown,
76            )
77            .await?,
78        )))
79    }
80
81    /// Initializes a new prover node.
82    pub async fn new_prover(
83        node_ip: SocketAddr,
84        account: Account<N>,
85        trusted_peers: &[SocketAddr],
86        genesis: Block<N>,
87        storage_mode: StorageMode,
88        shutdown: Arc<AtomicBool>,
89    ) -> Result<Self> {
90        Ok(Self::Prover(Arc::new(Prover::new(node_ip, account, trusted_peers, genesis, storage_mode, shutdown).await?)))
91    }
92
93    /// Initializes a new client node.
94    pub async fn new_client(
95        node_ip: SocketAddr,
96        rest_ip: Option<SocketAddr>,
97        rest_rps: u32,
98        account: Account<N>,
99        trusted_peers: &[SocketAddr],
100        genesis: Block<N>,
101        cdn: Option<String>,
102        storage_mode: StorageMode,
103        rotate_external_peers: bool,
104        shutdown: Arc<AtomicBool>,
105    ) -> Result<Self> {
106        Ok(Self::Client(Arc::new(
107            Client::new(
108                node_ip,
109                rest_ip,
110                rest_rps,
111                account,
112                trusted_peers,
113                genesis,
114                cdn,
115                storage_mode,
116                rotate_external_peers,
117                shutdown,
118            )
119            .await?,
120        )))
121    }
122
123    /// Returns the node type.
124    pub fn node_type(&self) -> NodeType {
125        match self {
126            Self::Validator(validator) => validator.node_type(),
127            Self::Prover(prover) => prover.node_type(),
128            Self::Client(client) => client.node_type(),
129        }
130    }
131
132    /// Returns the account private key of the node.
133    pub fn private_key(&self) -> &PrivateKey<N> {
134        match self {
135            Self::Validator(node) => node.private_key(),
136            Self::Prover(node) => node.private_key(),
137            Self::Client(node) => node.private_key(),
138        }
139    }
140
141    /// Returns the account view key of the node.
142    pub fn view_key(&self) -> &ViewKey<N> {
143        match self {
144            Self::Validator(node) => node.view_key(),
145            Self::Prover(node) => node.view_key(),
146            Self::Client(node) => node.view_key(),
147        }
148    }
149
150    /// Returns the account address of the node.
151    pub fn address(&self) -> Address<N> {
152        match self {
153            Self::Validator(node) => node.address(),
154            Self::Prover(node) => node.address(),
155            Self::Client(node) => node.address(),
156        }
157    }
158
159    /// Returns `true` if the node is in development mode.
160    pub fn is_dev(&self) -> bool {
161        match self {
162            Self::Validator(node) => node.is_dev(),
163            Self::Prover(node) => node.is_dev(),
164            Self::Client(node) => node.is_dev(),
165        }
166    }
167}