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        dev: Option<u16>,
60        shutdown: Arc<AtomicBool>,
61    ) -> Result<Self> {
62        Ok(Self::Validator(Arc::new(
63            Validator::new(
64                node_ip,
65                bft_ip,
66                rest_ip,
67                rest_rps,
68                account,
69                trusted_peers,
70                trusted_validators,
71                genesis,
72                cdn,
73                storage_mode,
74                allow_external_peers,
75                dev_txs,
76                dev,
77                shutdown,
78            )
79            .await?,
80        )))
81    }
82
83    /// Initializes a new prover node.
84    pub async fn new_prover(
85        node_ip: SocketAddr,
86        account: Account<N>,
87        trusted_peers: &[SocketAddr],
88        genesis: Block<N>,
89        dev: Option<u16>,
90        shutdown: Arc<AtomicBool>,
91    ) -> Result<Self> {
92        Ok(Self::Prover(Arc::new(Prover::new(node_ip, account, trusted_peers, genesis, dev, shutdown).await?)))
93    }
94
95    /// Initializes a new client node.
96    pub async fn new_client(
97        node_ip: SocketAddr,
98        rest_ip: Option<SocketAddr>,
99        rest_rps: u32,
100        account: Account<N>,
101        trusted_peers: &[SocketAddr],
102        genesis: Block<N>,
103        cdn: Option<String>,
104        storage_mode: StorageMode,
105        rotate_external_peers: bool,
106        dev: Option<u16>,
107        shutdown: Arc<AtomicBool>,
108    ) -> Result<Self> {
109        Ok(Self::Client(Arc::new(
110            Client::new(
111                node_ip,
112                rest_ip,
113                rest_rps,
114                account,
115                trusted_peers,
116                genesis,
117                cdn,
118                storage_mode,
119                rotate_external_peers,
120                dev,
121                shutdown,
122            )
123            .await?,
124        )))
125    }
126
127    /// Returns the node type.
128    pub fn node_type(&self) -> NodeType {
129        match self {
130            Self::Validator(validator) => validator.node_type(),
131            Self::Prover(prover) => prover.node_type(),
132            Self::Client(client) => client.node_type(),
133        }
134    }
135
136    /// Returns the account private key of the node.
137    pub fn private_key(&self) -> &PrivateKey<N> {
138        match self {
139            Self::Validator(node) => node.private_key(),
140            Self::Prover(node) => node.private_key(),
141            Self::Client(node) => node.private_key(),
142        }
143    }
144
145    /// Returns the account view key of the node.
146    pub fn view_key(&self) -> &ViewKey<N> {
147        match self {
148            Self::Validator(node) => node.view_key(),
149            Self::Prover(node) => node.view_key(),
150            Self::Client(node) => node.view_key(),
151        }
152    }
153
154    /// Returns the account address of the node.
155    pub fn address(&self) -> Address<N> {
156        match self {
157            Self::Validator(node) => node.address(),
158            Self::Prover(node) => node.address(),
159            Self::Client(node) => node.address(),
160        }
161    }
162
163    /// Returns `true` if the node is in development mode.
164    pub fn is_dev(&self) -> bool {
165        match self {
166            Self::Validator(node) => node.is_dev(),
167            Self::Prover(node) => node.is_dev(),
168            Self::Client(node) => node.is_dev(),
169        }
170    }
171}