Skip to content

Commit

Permalink
feat: allow the ledger to be loaded from a custom location
Browse files Browse the repository at this point in the history
Signed-off-by: ljedrz <[email protected]>
  • Loading branch information
ljedrz committed Jan 29, 2024
1 parent 1d942fd commit 9e91486
Show file tree
Hide file tree
Showing 19 changed files with 185 additions and 76 deletions.
3 changes: 3 additions & 0 deletions .integration/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ categories = [ "cryptography", "operating-systems" ]
license = "Apache-2.0"
edition = "2021"

[dev-dependencies.aleo-std]
version = "0.1.24"

[dev-dependencies.snarkos-node-cdn]
path = "../node/cdn"

Expand Down
3 changes: 2 additions & 1 deletion .integration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#[cfg(test)]
mod tests {
use aleo_std::StorageMode;
use snarkos_node_cdn::sync_ledger_with_cdn;
use snarkvm::prelude::{
block::Block,
Expand All @@ -38,7 +39,7 @@ mod tests {
// Initialize the genesis block.
let genesis = Block::<CurrentNetwork>::read_le(CurrentNetwork::genesis_bytes()).unwrap();
// Initialize the ledger.
let ledger = Ledger::<_, ConsensusMemory<_>>::load(genesis, None).unwrap();
let ledger = Ledger::<_, ConsensusMemory<_>>::load(genesis, StorageMode::Production).unwrap();
// Perform the sync.
let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async {
Expand Down
13 changes: 10 additions & 3 deletions cli/src/commands/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use aleo_std::StorageMode;

use anyhow::{bail, Result};
use clap::Parser;
use colored::Colorize;
use std::path::PathBuf;

/// Cleans the snarkOS node storage.
#[derive(Debug, Parser)]
Expand All @@ -25,19 +28,23 @@ pub struct Clean {
/// Enables development mode, specify the unique ID of the local node to clean.
#[clap(long)]
pub dev: Option<u16>,
/// Specify the path to a directory containing the ledger
#[clap(long = "path")]
pub path: Option<PathBuf>,
}

impl Clean {
/// Cleans the snarkOS node storage.
pub fn parse(self) -> Result<String> {
// Remove the specified ledger from storage.
Self::remove_ledger(self.network, self.dev)
let mode = if let Some(path) = self.path { StorageMode::Custom(path) } else { StorageMode::from(self.dev) };
Self::remove_ledger(self.network, mode)
}

/// Removes the specified ledger from storage.
pub(crate) fn remove_ledger(network: u16, dev: Option<u16>) -> Result<String> {
pub(crate) fn remove_ledger(network: u16, mode: StorageMode) -> Result<String> {
// Construct the path to the ledger in storage.
let path = aleo_std::aleo_ledger_dir(network, aleo_std::StorageMode::from(dev));
let path = aleo_std::aleo_ledger_dir(network, mode);

// Prepare the path string.
let path_string = format!("(in \"{}\")", path.display()).dimmed();
Expand Down
15 changes: 12 additions & 3 deletions cli/src/commands/developer/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use super::{CurrentAleo, CurrentNetwork, Developer};

use aleo_std::StorageMode;
use snarkvm::{
console::program::ProgramOwner,
prelude::{
Expand All @@ -30,7 +31,7 @@ use snarkvm::{
use anyhow::{bail, Result};
use clap::Parser;
use colored::Colorize;
use std::str::FromStr;
use std::{path::PathBuf, str::FromStr};
use zeroize::Zeroize;

/// Deploys an Aleo program.
Expand Down Expand Up @@ -62,6 +63,9 @@ pub struct Deploy {
/// Store generated deployment transaction to a local file.
#[clap(long)]
store: Option<String>,
/// Specify the path to a directory containing the ledger
#[clap(long = "storage_path")]
storage_path: Option<PathBuf>,
}

impl Drop for Deploy {
Expand All @@ -73,7 +77,7 @@ impl Drop for Deploy {

impl Deploy {
/// Deploys an Aleo program.
pub fn parse(self) -> Result<String> {
pub fn parse(mut self) -> Result<String> {
// Ensure that the user has specified an action.
if !self.dry_run && self.broadcast.is_none() && self.store.is_none() {
bail!("❌ Please specify one of the following actions: --broadcast, --dry-run, --store");
Expand All @@ -100,7 +104,12 @@ impl Deploy {
let rng = &mut rand::thread_rng();

// Initialize the VM.
let store = ConsensusStore::<CurrentNetwork, ConsensusMemory<CurrentNetwork>>::open(None)?;
let storage_mode = if let Some(path) = self.storage_path.take() {
StorageMode::Custom(path)
} else {
StorageMode::Production
};
let store = ConsensusStore::<CurrentNetwork, ConsensusMemory<CurrentNetwork>>::open(storage_mode)?;
let vm = VM::from(store)?;

// Compute the minimum deployment cost.
Expand Down
15 changes: 12 additions & 3 deletions cli/src/commands/developer/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use super::{CurrentNetwork, Developer};

use aleo_std::StorageMode;
use snarkvm::prelude::{
query::Query,
store::{helpers::memory::ConsensusMemory, ConsensusStore},
Expand All @@ -30,7 +31,7 @@ use snarkvm::prelude::{
use anyhow::{anyhow, bail, Result};
use clap::Parser;
use colored::Colorize;
use std::str::FromStr;
use std::{path::PathBuf, str::FromStr};
use zeroize::Zeroize;

/// Executes an Aleo program function.
Expand Down Expand Up @@ -63,6 +64,9 @@ pub struct Execute {
/// Store generated deployment transaction to a local file.
#[clap(long)]
store: Option<String>,
/// Specify the path to a directory containing the ledger
#[clap(long = "storage_path")]
pub storage_path: Option<PathBuf>,
}

impl Drop for Execute {
Expand All @@ -75,7 +79,7 @@ impl Drop for Execute {
impl Execute {
/// Executes an Aleo program function with the provided inputs.
#[allow(clippy::format_in_format_args)]
pub fn parse(self) -> Result<String> {
pub fn parse(mut self) -> Result<String> {
// Ensure that the user has specified an action.
if !self.dry_run && self.broadcast.is_none() && self.store.is_none() {
bail!("❌ Please specify one of the following actions: --broadcast, --dry-run, --store");
Expand All @@ -96,7 +100,12 @@ impl Execute {
let rng = &mut rand::thread_rng();

// Initialize the VM.
let store = ConsensusStore::<CurrentNetwork, ConsensusMemory<CurrentNetwork>>::open(None)?;
let storage_mode = if let Some(path) = self.storage_path.take() {
StorageMode::Custom(path)
} else {
StorageMode::Production
};
let store = ConsensusStore::<CurrentNetwork, ConsensusMemory<CurrentNetwork>>::open(storage_mode)?;
let vm = VM::from(store)?;

// Load the program and it's imports into the process.
Expand Down
15 changes: 12 additions & 3 deletions cli/src/commands/developer/transfer_private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use super::{CurrentNetwork, Developer};

use aleo_std::StorageMode;
use snarkvm::prelude::{
query::Query,
store::{helpers::memory::ConsensusMemory, ConsensusStore},
Expand All @@ -26,7 +27,7 @@ use snarkvm::prelude::{

use anyhow::{bail, Result};
use clap::Parser;
use std::str::FromStr;
use std::{path::PathBuf, str::FromStr};
use zeroize::Zeroize;

/// Executes the `transfer_private` function in the `credits.aleo` program.
Expand Down Expand Up @@ -62,6 +63,9 @@ pub struct TransferPrivate {
/// Store generated deployment transaction to a local file.
#[clap(long)]
store: Option<String>,
/// Specify the path to a directory containing the ledger
#[clap(long = "storage_path")]
pub storage_path: Option<PathBuf>,
}

impl Drop for TransferPrivate {
Expand All @@ -74,7 +78,7 @@ impl Drop for TransferPrivate {
impl TransferPrivate {
/// Creates an Aleo transfer with the provided inputs.
#[allow(clippy::format_in_format_args)]
pub fn parse(self) -> Result<String> {
pub fn parse(mut self) -> Result<String> {
// Ensure that the user has specified an action.
if !self.dry_run && self.broadcast.is_none() && self.store.is_none() {
bail!("❌ Please specify one of the following actions: --broadcast, --dry-run, --store");
Expand All @@ -94,7 +98,12 @@ impl TransferPrivate {
let rng = &mut rand::thread_rng();

// Initialize the VM.
let store = ConsensusStore::<CurrentNetwork, ConsensusMemory<CurrentNetwork>>::open(None)?;
let storage_mode = if let Some(path) = self.storage_path.take() {
StorageMode::Custom(path)
} else {
StorageMode::Production
};
let store = ConsensusStore::<CurrentNetwork, ConsensusMemory<CurrentNetwork>>::open(storage_mode)?;
let vm = VM::from(store)?;

// Prepare the fee.
Expand Down
27 changes: 22 additions & 5 deletions cli/src/commands/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use aleo_std::StorageMode;
use snarkos_account::Account;
use snarkos_display::Display;
use snarkos_node::{bft::MEMORY_POOL_PORT, router::messages::NodeType, Node};
Expand Down Expand Up @@ -123,6 +124,9 @@ pub struct Start {
/// If development mode is enabled, specify the number of genesis validators (default: 4)
#[clap(long)]
pub dev_num_validators: Option<u16>,
/// Specify the path to a directory containing the ledger
#[clap(long = "storage_path")]
pub storage_path: Option<PathBuf>,
}

impl Start {
Expand Down Expand Up @@ -353,7 +357,12 @@ impl Start {
}

// Construct the genesis block.
load_or_compute_genesis(development_private_keys[0], committee, public_balances, &mut rng)
let mode = if let Some(path) = &self.storage_path {
StorageMode::Custom(path.clone())
} else {
StorageMode::Production
};
load_or_compute_genesis(development_private_keys[0], committee, public_balances, &mut rng, mode)
} else {
// If the `dev_num_validators` flag is set, inform the user that it is ignored.
if self.dev_num_validators.is_some() {
Expand Down Expand Up @@ -442,12 +451,19 @@ impl Start {
metrics::initialize_metrics();
}

// Obtain the storage mode.
let storage_mode = if let Some(path) = self.storage_path.take() {
StorageMode::Custom(path)
} else {
StorageMode::from(self.dev)
};
// Initialize the node.

let bft_ip = if self.dev.is_some() { self.bft } else { None };
match node_type {
NodeType::Validator => Node::new_validator(self.node, bft_ip, rest_ip, self.rest_rps, account, &trusted_peers, &trusted_validators, genesis, cdn, self.dev).await,
NodeType::Prover => Node::new_prover(self.node, account, &trusted_peers, genesis, self.dev).await,
NodeType::Client => Node::new_client(self.node, rest_ip, self.rest_rps, account, &trusted_peers, genesis, cdn, self.dev).await,
NodeType::Validator => Node::new_validator(self.node, bft_ip, rest_ip, self.rest_rps, account, &trusted_peers, &trusted_validators, genesis, cdn, storage_mode).await,
NodeType::Prover => Node::new_prover(self.node, account, &trusted_peers, genesis, storage_mode).await,
NodeType::Client => Node::new_client(self.node, rest_ip, self.rest_rps, account, &trusted_peers, genesis, cdn, storage_mode).await,
}
}

Expand Down Expand Up @@ -517,6 +533,7 @@ fn load_or_compute_genesis<N: Network>(
committee: Committee<N>,
public_balances: indexmap::IndexMap<Address<N>, u64>,
rng: &mut ChaChaRng,
storage_mode: StorageMode,
) -> Result<Block<N>> {
// Construct the preimage.
let mut preimage = Vec::new();
Expand Down Expand Up @@ -566,7 +583,7 @@ fn load_or_compute_genesis<N: Network>(
/* Otherwise, compute the genesis block and store it. */

// Initialize a new VM.
let vm = VM::from(ConsensusStore::<N, ConsensusMemory<N>>::open(None)?)?;
let vm = VM::from(ConsensusStore::<N, ConsensusMemory<N>>::open(storage_mode)?)?;
// Initialize the genesis block.
let block = vm.genesis_quorum(&genesis_private_key, committee, public_balances, rng)?;
// Write the genesis block to the file.
Expand Down
3 changes: 3 additions & 0 deletions node/bft/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ edition = "2021"
default = [ ]
metrics = [ "dep:metrics", "snarkos-node-bft-events/metrics" ]

[dependencies.aleo-std]
version = "0.1.24"

[dependencies.anyhow]
version = "1.0.79"

Expand Down
3 changes: 3 additions & 0 deletions node/bft/storage-service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ memory = [ "parking_lot", "tracing" ]
persistent = [ ]
test = [ "memory" ]

[dependencies.aleo-std]
version = "0.1.24"

[dependencies.indexmap]
version = "2.1"
features = [ "serde", "rayon" ]
Expand Down
5 changes: 3 additions & 2 deletions node/bft/storage-service/src/persistent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

use crate::StorageService;
use aleo_std::StorageMode;
use snarkvm::{
ledger::{
narwhal::{BatchHeader, Transmission, TransmissionID},
Expand Down Expand Up @@ -42,8 +43,8 @@ pub struct BFTPersistentStorage<N: Network> {

impl<N: Network> BFTPersistentStorage<N> {
/// Initializes a new BFT persistent storage service.
pub fn open(dev: Option<u16>) -> Result<Self> {
Ok(Self { transmissions: internal::RocksDB::open_map(N::ID, dev, MapID::BFT(BFTMap::Transmissions))? })
pub fn open(storage_mode: StorageMode) -> Result<Self> {
Ok(Self { transmissions: internal::RocksDB::open_map(N::ID, storage_mode, MapID::BFT(BFTMap::Transmissions))? })
}

/// Initializes a new BFT persistent storage service.
Expand Down
3 changes: 2 additions & 1 deletion node/bft/tests/common/primary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::common::{
CurrentNetwork,
TranslucentLedgerService,
};
use aleo_std::StorageMode;
use snarkos_account::Account;
use snarkos_node_bft::{
helpers::{init_primary_channels, PrimarySender, Storage},
Expand Down Expand Up @@ -389,5 +390,5 @@ fn genesis_ledger(
})
.clone();
// Initialize the ledger with the genesis block.
CurrentLedger::load(block, None).unwrap()
CurrentLedger::load(block, StorageMode::Production).unwrap()
}
3 changes: 3 additions & 0 deletions node/consensus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ edition = "2021"
default = [ ]
metrics = [ "dep:metrics" ]

[dependencies.aleo-std]
version = "0.1.24"

[dependencies.anyhow]
version = "1.0.79"

Expand Down
10 changes: 8 additions & 2 deletions node/consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#[macro_use]
extern crate tracing;

use aleo_std::StorageMode;
use snarkos_account::Account;
use snarkos_node_bft::{
helpers::{
Expand Down Expand Up @@ -81,10 +82,15 @@ impl<N: Network> Consensus<N> {
ledger: Arc<dyn LedgerService<N>>,
ip: Option<SocketAddr>,
trusted_validators: &[SocketAddr],
dev: Option<u16>,
storage_mode: StorageMode,
) -> Result<Self> {
// Recover the dev id if present.
let dev = match storage_mode {
StorageMode::Development(id) => Some(id),
_ => None,
};
// Initialize the Narwhal transmissions.
let transmissions = Arc::new(BFTPersistentStorage::open(dev)?);
let transmissions = Arc::new(BFTPersistentStorage::open(storage_mode)?);
// Initialize the Narwhal storage.
let storage = NarwhalStorage::new(ledger.clone(), transmissions, MAX_GC_ROUNDS);
// Initialize the BFT.
Expand Down
Loading

0 comments on commit 9e91486

Please sign in to comment.