Skip to main content

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