Skip to content

Commit

Permalink
ref: create init module
Browse files Browse the repository at this point in the history
  • Loading branch information
niklaslong committed Sep 16, 2021
1 parent 5ce870d commit 9ef6dd0
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 56 deletions.
2 changes: 1 addition & 1 deletion bin/beacon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ async fn start_server(config: Config) -> anyhow::Result<()> {
let desired_address = address.parse::<SocketAddr>()?;

let node_config = NodeConfig::new(
NodeType::Beacon,
config.node.kind,
desired_address,
config.p2p.min_peers,
config.p2p.max_peers,
Expand Down
2 changes: 1 addition & 1 deletion bin/crawler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ async fn start_server(config: Config) -> anyhow::Result<()> {

let node_config = NodeConfig::new(
None,
NodeType::Crawler,
config.node.kind,
desired_address,
config.p2p.min_peers,
config.p2p.max_peers,
Expand Down
64 changes: 11 additions & 53 deletions bin/snarkos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use snarkos::{
config::{Config, ConfigCli},
display::{initialize_logger, print_welcome},
errors::NodeError,
init::init_storage,
};
use snarkos_consensus::{Consensus, ConsensusParameters, DeserializedLedger, DynLedger, MemoryPool, MerkleLedger};
use snarkos_network::{config::Config as NodeConfig, MinerInstance, Node, NodeType, Sync};
Expand Down Expand Up @@ -72,19 +73,21 @@ async fn start_server(config: Config) -> anyhow::Result<()> {

print_welcome(&config);

let address = format!("{}:{}", config.node.ip, config.node.port);
let desired_address = address.parse::<SocketAddr>()?;
let (path, storage) = match init_storage(&config).await? {
Some(storage) => storage,
None => return Ok(()), // Return if no storage was returned (usually in case of validation).
};

let mut path = config.node.dir.clone();
path.push(&config.node.db);
// Construct the node instance. Note this does not start the network services.
// This is done early on, so that the local address can be discovered
// before any other object (miner, RPC) needs to use it.

if !path.exists() {
fs::create_dir(&path)?;
}
let address = format!("{}:{}", config.node.ip, config.node.port);
let desired_address = address.parse::<SocketAddr>()?;

let node_config = NodeConfig::new(
None,
NodeType::Client,
config.node.kind,
desired_address,
config.p2p.min_peers,
config.p2p.max_peers,
Expand All @@ -93,51 +96,6 @@ async fn start_server(config: Config) -> anyhow::Result<()> {
Duration::from_secs(config.p2p.peer_sync_interval.into()),
)?;

info!("Loading storage at '{}'...", path.to_str().unwrap_or_default());
let storage: DynStorage = {
let mut sqlite_path = path.clone();
sqlite_path.push("sqlite.db");

if config.storage.validate {
error!("validator not implemented for sqlite");
return Ok(());
}

Arc::new(AsyncStorage::new(SqliteStorage::new(&sqlite_path)?))
};

if storage.canon().await?.block_height == 0 {
let mut rocks_identity_path = path.clone();
rocks_identity_path.push("IDENTITY");
if rocks_identity_path.exists() {
info!("Empty sqlite DB with existing rocksdb found, migrating...");
let rocks_storage = RocksDb::open(&path)?;
let rocks_storage: DynStorage = Arc::new(AsyncStorage::new(KeyValueStore::new(rocks_storage)));

snarkos_storage::migrate(&rocks_storage, &storage).await?;
}
}

if let Some(max_head) = config.storage.max_head {
let canon_next = storage.get_block_hash(max_head + 1).await?;
if let Some(canon_next) = canon_next {
storage.decommit_blocks(&canon_next).await?;
}
}

if config.storage.trim {
let now = std::time::Instant::now();
// There shouldn't be issues after validation, but if there are, ignore them.
let _ = snarkos_storage::trim(storage.clone()).await;
info!("Storage trimmed in {}ms", now.elapsed().as_millis());
return Ok(());
}

info!("Storage is ready");

// Construct the node instance. Note this does not start the network services.
// This is done early on, so that the local address can be discovered
// before any other object (miner, RPC) needs to use it.
let mut node = Node::new(node_config, storage.clone()).await?;

if let Some(limit) = config.storage.export {
Expand Down
2 changes: 1 addition & 1 deletion bin/sync_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ async fn start_server(config: Config) -> anyhow::Result<()> {
}

let node_config = NodeConfig::new(
NodeType::SyncProvider,
config.node.kind,
desired_address,
config.p2p.min_peers,
config.p2p.max_peers,
Expand Down
108 changes: 108 additions & 0 deletions snarkos/init.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// Copyright (C) 2019-2021 Aleo Systems Inc.
// This file is part of the snarkOS library.

// The snarkOS library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// The snarkOS library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with the snarkOS library. If not, see <https://www.gnu.org/licenses/>.

use crate::{
cli::CLI,
config::{Config, ConfigCli},
display::{initialize_logger, print_welcome},
errors::NodeError,
};
use snarkos_consensus::{Consensus, ConsensusParameters, DeserializedLedger, DynLedger, MemoryPool, MerkleLedger};
use snarkos_network::{config::Config as NodeConfig, MinerInstance, Node, NodeType, Sync};
use snarkos_rpc::start_rpc_server;
use snarkos_storage::{
export_canon_blocks,
key_value::KeyValueStore,
AsyncStorage,
DynStorage,
RocksDb,
SerialBlock,
SqliteStorage,
VMBlock,
};

use snarkvm_algorithms::{MerkleParameters, CRH, SNARK};
use snarkvm_dpc::{
testnet1::{
instantiated::{Components, Testnet1DPC, Testnet1Transaction},
Testnet1Components,
},
Address,
Block,
DPCScheme,
Network,
};
use snarkvm_parameters::{testnet1::GenesisBlock, Genesis, LedgerMerkleTreeParameters, Parameter};
use snarkvm_posw::PoswMarlin;
use snarkvm_utilities::{to_bytes_le, FromBytes, ToBytes};

use std::{fs, net::SocketAddr, path::PathBuf, str::FromStr, sync::Arc, time::Duration};

use tokio::runtime;

pub async fn init_storage(config: &Config) -> anyhow::Result<Option<(PathBuf, DynStorage)>> {
let mut path = config.node.dir.clone();
path.push(&config.node.db);

if !path.exists() {
fs::create_dir(&path)?;
}

info!("Loading storage at '{}'...", path.to_str().unwrap_or_default());
let storage: DynStorage = {
let mut sqlite_path = path.clone();
sqlite_path.push("sqlite.db");

if config.storage.validate {
error!("validator not implemented for sqlite");
// FIXME: this should probably be an error, perhaps handled at the CLI level.
return Ok(None);
}

Arc::new(AsyncStorage::new(SqliteStorage::new(&sqlite_path)?))
};

if storage.canon().await?.block_height == 0 {
let mut rocks_identity_path = path.clone();
rocks_identity_path.push("IDENTITY");
if rocks_identity_path.exists() {
info!("Empty sqlite DB with existing rocksdb found, migrating...");
let rocks_storage = RocksDb::open(&path)?;
let rocks_storage: DynStorage = Arc::new(AsyncStorage::new(KeyValueStore::new(rocks_storage)));

snarkos_storage::migrate(&rocks_storage, &storage).await?;
}
}

if let Some(max_head) = config.storage.max_head {
let canon_next = storage.get_block_hash(max_head + 1).await?;
if let Some(canon_next) = canon_next {
storage.decommit_blocks(&canon_next).await?;
}
}

if config.storage.trim {
let now = std::time::Instant::now();
// There shouldn't be issues after validation, but if there are, ignore them.
let _ = snarkos_storage::trim(storage.clone()).await;
info!("Storage trimmed in {}ms", now.elapsed().as_millis());
return Ok(None);
}

info!("Storage is ready");

Ok(Some((path, storage)))
}
4 changes: 4 additions & 0 deletions snarkos/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@
#[macro_use]
extern crate thiserror;

#[macro_use]
extern crate tracing;

pub mod cli;
pub mod config;
pub mod display;
pub mod errors;
pub mod init;
pub mod parameters;
pub mod update;

0 comments on commit 9ef6dd0

Please sign in to comment.