Skip to main content

snarkos_utilities/
node_data.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 std::path::{Path, PathBuf};
17
18/// The filename of the gateway peer cache.
19pub const GATEWAY_PEER_CACHE_FILE: &str = "gateway-peer-cache";
20/// The old filename of the gateway peer cache.
21pub const LEGACY_GATEWAY_PEER_CACHE_FILE: &str = "cached_gateway_peers";
22
23/// The filename of the router peer cache.
24pub const ROUTER_PEER_CACHE_FILE: &str = "router-peer-cache";
25/// The old filename of the router peer cache.
26pub const LEGACY_ROUTER_PEER_CACHE_FILE: &str = "cached_router_peers";
27
28/// The filename of the proposal cache.
29pub const CURRENT_PROPOSAL_CACHE_FILE: &str = "current-proposal-cache";
30/// The filename of the validator whitelist.
31pub const VALIDATOR_WHITELIST_FILE: &str = "validator-whitelist";
32
33/// The filename of the JWT secret for a given address.
34pub fn jwt_secret_file<D: std::fmt::Display>(address: &D) -> PathBuf {
35    PathBuf::from(format!("jwt_secret_{address}.txt"))
36}
37
38/// The old filename of the current proposal cache.
39pub fn legacy_current_proposal_cache_file(network: u16, dev: Option<u16>) -> PathBuf {
40    if let Some(dev) = dev {
41        PathBuf::from(format!(".current-proposal-cache-{network}-{dev}"))
42    } else {
43        PathBuf::from(format!("current-proposal-cache-{network}"))
44    }
45}
46
47/// Tracks information about where the node-specfic configuration files are stored.
48#[derive(Clone, Debug, PartialEq, Eq)]
49pub struct NodeDataDir {
50    path: PathBuf,
51}
52
53impl NodeDataDir {
54    /// Initializes the node data directory the given path.
55    pub fn new(path: PathBuf) -> Self {
56        Self { path }
57    }
58
59    /// Initializes the node data directory to a location suitable for unit/integration tests.
60    pub fn new_test(dev: Option<u16>) -> Self {
61        if let Some(dev) = dev {
62            Self { path: PathBuf::from(format!(".node-data-test-{dev}")) }
63        } else {
64            Self { path: PathBuf::from(".node-data-test") }
65        }
66    }
67
68    /// Initializes the node data directory path to the development path for the specified network and node index.
69    pub fn new_development(network: u16, dev: u16) -> Self {
70        // Use the current directory as the base path, and fall back to the
71        // cargo manifest directory if the current directory is not available.
72        let path = std::env::current_dir()
73            .unwrap_or(PathBuf::from(env!("CARGO_MANIFEST_DIR")))
74            .join(format!(".node-data-{network}-{dev}"));
75
76        Self::new(path)
77    }
78
79    pub fn path(&self) -> &Path {
80        &self.path
81    }
82
83    /// The location to store the previous peer cache.
84    pub fn router_peer_cache_path(&self) -> PathBuf {
85        self.path.join(ROUTER_PEER_CACHE_FILE)
86    }
87
88    pub fn gateway_peer_cache_path(&self) -> PathBuf {
89        self.path.join(GATEWAY_PEER_CACHE_FILE)
90    }
91
92    pub fn validator_whitelist_path(&self) -> PathBuf {
93        self.path.join(VALIDATOR_WHITELIST_FILE)
94    }
95
96    /// The location to store the current proposal cache.
97    pub fn current_proposal_cache_path(&self) -> PathBuf {
98        self.path.join(CURRENT_PROPOSAL_CACHE_FILE)
99    }
100
101    /// The location to store the JWT secret for a given address.
102    pub fn jwt_secret_path<D: std::fmt::Display>(&self, address: &D) -> PathBuf {
103        self.path.join(jwt_secret_file(address))
104    }
105}