Skip to content

Commit

Permalink
chore(checkpoint): code cleanup, remove unused deps, rename snarkos t…
Browse files Browse the repository at this point in the history
…o aleo
  • Loading branch information
Meshiest committed Apr 6, 2024
1 parent 5c5bce3 commit 6977e9f
Show file tree
Hide file tree
Showing 14 changed files with 185 additions and 154 deletions.
2 changes: 0 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions crates/aot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ anyhow.workspace = true
axum.workspace = true
bincode.workspace = true
checkpoint = { workspace = true, features = ["write"] }
chrono.workspace = true
clap.workspace = true
colored.workspace = true
crossterm = { version = "0.27", optional = true }
glob.workspace = true
indexmap.workspace = true
indicatif.workspace = true
lazy_static.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion crates/checkpoint/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ serde.workspace = true
snarkvm = { workspace = true, optional = true }
snarkos-node = { workspace = true, optional = true }
thiserror.workspace = true

tracing.workspace = true


[target.'cfg(all(target_os = "linux", target_arch = "x86_64"))'.dependencies]
tikv-jemallocator = "0.5"
60 changes: 60 additions & 0 deletions crates/checkpoint/src/aleo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
pub use aleo_std::StorageMode;
pub use snarkos_node::bft::{
helpers::Storage, ledger_service::CoreLedgerService, storage_service::BFTMemoryService,
};
use snarkvm::{
console::{network::MainnetV0, program},
ledger::{store::helpers::rocksdb, Ledger},
prelude::Network,
};
pub use snarkvm::{
ledger::{
authority::Authority,
store::{
self, cow_to_cloned, cow_to_copied,
helpers::{Map, MapRead},
BlockStorage, CommitteeStorage, DeploymentStorage, ExecutionStorage, FeeStorage,
FinalizeStorage, InputStorage, OutputStorage, TransactionStorage, TransactionType,
TransitionStorage,
},
},
utilities::{FromBytes, ToBytes},
};

pub type N = MainnetV0;
pub type Db = rocksdb::ConsensusDB<N>;
pub type DbLedger = Ledger<N, Db>;

pub type TransitionID = <N as Network>::TransitionID;
pub type TransactionID = <N as Network>::TransactionID;
pub type BlockHash = <N as Network>::BlockHash;

pub type ProgramID = program::ProgramID<N>;
pub type Identifier = program::Identifier<N>;
pub type Plaintext = program::Plaintext<N>;
pub type Value = program::Value<N>;

pub type BlockDB = rocksdb::BlockDB<N>;
pub type CommitteeDB = rocksdb::CommitteeDB<N>;
pub type DeploymentDB = rocksdb::DeploymentDB<N>;
pub type ExecutionDB = rocksdb::ExecutionDB<N>;
pub type FeeDB = rocksdb::FeeDB<N>;
pub type FinalizeDB = rocksdb::FinalizeDB<N>;
pub type InputDB = rocksdb::InputDB<N>;
pub type OutputDB = rocksdb::OutputDB<N>;
pub type TransactionDB = rocksdb::TransactionDB<N>;
pub type TransitionDB = rocksdb::TransitionDB<N>;

pub type TransitionStore = store::TransitionStore<N, TransitionDB>;
pub type FeeStore = store::FeeStore<N, FeeDB>;

pub trait LazyBytes {
fn bytes(&self) -> [u8; 32];
}
impl LazyBytes for BlockHash {
fn bytes(&self) -> [u8; 32] {
let mut bytes = [0u8; 32];
bytes.copy_from_slice(&self.to_bytes_le().unwrap());
bytes
}
}
43 changes: 14 additions & 29 deletions crates/checkpoint/src/checkpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,18 @@ use std::{io, path::PathBuf, sync::Arc};
use rayon::iter::ParallelIterator;

use crate::{
ledger,
snarkos::{
self, BlockHash, BlockStorage, CommitteeStorage, FinalizeStorage, FromBytes, LazyBytes,
Map, MapRead,
},
CheckpointCheckError, CheckpointContent, CheckpointHeader, CheckpointReadError,
CheckpointRewindError, ROUND_KEY,
aleo::*,
errors::{CheckpointCheckError, CheckpointReadError, CheckpointRewindError},
ledger, CheckpointContent, CheckpointHeader, ROUND_KEY,
};

pub struct Checkpoint {
pub header: CheckpointHeader,
pub content: CheckpointContent,
}

impl snarkos::ToBytes for Checkpoint {
fn write_le<W: snarkvm::prelude::Write>(&self, mut writer: W) -> snarkvm::prelude::IoResult<()>
impl ToBytes for Checkpoint {
fn write_le<W: std::io::Write>(&self, mut writer: W) -> std::io::Result<()>
where
Self: Sized,
{
Expand All @@ -40,8 +36,8 @@ impl snarkos::ToBytes for Checkpoint {
}
}

impl snarkos::FromBytes for Checkpoint {
fn read_le<R: snarkvm::prelude::Read>(mut reader: R) -> snarkvm::prelude::IoResult<Self>
impl FromBytes for Checkpoint {
fn read_le<R: std::io::Read>(mut reader: R) -> std::io::Result<Self>
where
Self: Sized,
{
Expand All @@ -68,15 +64,11 @@ impl Checkpoint {
Ok(Self { header, content })
}

pub fn check(
&self,
storage_mode: crate::snarkos::StorageMode,
) -> Result<(), CheckpointCheckError> {
pub fn check(&self, storage_mode: StorageMode) -> Result<(), CheckpointCheckError> {
use CheckpointCheckError::*;

let blocks = snarkos::BlockDB::open(storage_mode.clone()).map_err(StorageOpenError)?;
let committee =
snarkos::CommitteeDB::open(storage_mode.clone()).map_err(StorageOpenError)?;
let blocks = BlockDB::open(storage_mode.clone()).map_err(StorageOpenError)?;
let committee = CommitteeDB::open(storage_mode.clone()).map_err(StorageOpenError)?;
let height = committee.current_height().map_err(ReadLedger)?;

if height <= self.height() {
Expand All @@ -101,8 +93,8 @@ impl Checkpoint {

pub fn rewind(
self,
ledger: &snarkos::DbLedger,
storage_mode: snarkos::StorageMode,
ledger: &DbLedger,
storage_mode: StorageMode,
) -> Result<(), CheckpointRewindError> {
use rayon::iter::IntoParallelIterator;
use CheckpointRewindError::*;
Expand All @@ -116,15 +108,8 @@ impl Checkpoint {

// the act of creating this ledger service with a "max_gc_rounds" set to 0 should clear
// all BFT documents
let ledger_service = Arc::new(snarkos::CoreLedgerService::new(
ledger.clone(),
Default::default(),
));
snarkos::Storage::new(
ledger_service,
Arc::new(snarkos::BFTMemoryService::new()),
0,
);
let ledger_service = Arc::new(CoreLedgerService::new(ledger.clone(), Default::default()));
Storage::new(ledger_service, Arc::new(BFTMemoryService::new()), 0);

// TODO: parallel and test out of order removal by moving the guts of these functions out of the "atomic writes"
((my_height + 1)..=height)
Expand Down
8 changes: 4 additions & 4 deletions crates/checkpoint/src/content.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::{
snarkos::{
aleo::{
FinalizeDB, FinalizeStorage, FromBytes, Identifier, MapRead, Plaintext, ProgramID,
StorageMode, ToBytes, Value,
},
CheckpointContentError as Error,
errors::CheckpointContentError as Error,
};
use anyhow::Result;
use std::path::PathBuf;
Expand Down Expand Up @@ -54,7 +54,7 @@ impl CheckpointContent {
}

impl ToBytes for CheckpointContent {
fn write_le<W: snarkvm::prelude::Write>(&self, mut writer: W) -> snarkvm::prelude::IoResult<()>
fn write_le<W: std::io::Write>(&self, mut writer: W) -> std::io::Result<()>
where
Self: Sized,
{
Expand All @@ -70,7 +70,7 @@ impl ToBytes for CheckpointContent {
}

impl FromBytes for CheckpointContent {
fn read_le<R: snarkvm::prelude::Read>(mut reader: R) -> snarkvm::prelude::IoResult<Self>
fn read_le<R: std::io::Read>(mut reader: R) -> std::io::Result<Self>
where
Self: Sized,
{
Expand Down
44 changes: 42 additions & 2 deletions crates/checkpoint/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,47 @@
use std::io;

use std::{io, path::PathBuf};
use thiserror::Error;

#[derive(Debug, Error)]
pub enum ManagerLoadError {
#[error("invalid storage path: {0}")]
InvalidStoragePath(PathBuf),
#[error("error globbing storage path: {0}")]
GlobError(#[from] glob::PatternError),
}

#[derive(Debug, Error)]
#[cfg(feature = "write")]
pub enum ManagerCullError {
#[error("error opening storage: {0}")]
StorageOpenError(#[source] anyhow::Error),
#[error("error reading ledger: {0}")]
ReadLedger(#[source] anyhow::Error),
}

#[derive(Debug, Error)]
#[cfg(feature = "write")]
pub enum ManagerPollError {
#[error("error reading checkpoint header: {0}")]
Header(#[from] CheckpointHeaderError),
#[error("error reading checkpoint: {0}")]
Read(#[from] CheckpointReadError),
#[error("error inserting checkpoint: {0}")]
Insert(#[from] ManagerInsertError),
}

#[derive(Debug, Error)]
#[cfg(feature = "write")]
pub enum ManagerInsertError {
#[error("invalid storage path: {0}")]
InvalidStoragePath(PathBuf),
#[error("error opening file: {0}")]
FileError(#[source] io::Error),
#[error("error modifying file times: {0}")]
ModifyError(#[source] io::Error),
#[error("error writing file: {0}")]
WriteError(#[source] io::Error),
}

#[derive(Debug, Error)]
#[cfg(feature = "write")]
pub enum CheckpointReadError {
Expand Down
6 changes: 2 additions & 4 deletions crates/checkpoint/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
path::PathBuf,
};

use crate::CheckpointHeaderError::{self as Error, *};
use crate::errors::CheckpointHeaderError::{self as Error, *};

const CHECKPOINT_VERSION: u8 = 2;

Expand Down Expand Up @@ -34,9 +34,7 @@ impl CheckpointHeader {

#[cfg(feature = "write")]
pub fn read_ledger(path: PathBuf) -> Result<Self, Error> {
use crate::snarkos::{
BlockDB, BlockStorage, CommitteeDB, CommitteeStorage, LazyBytes, StorageMode,
};
use crate::aleo::*;
use Error::*;

let commitee = CommitteeDB::open(StorageMode::Custom(path.clone())).map_err(OpenLedger)?;
Expand Down
37 changes: 14 additions & 23 deletions crates/checkpoint/src/ledger.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,24 @@
use std::borrow::Cow;

use super::snarkos::{TransactionID, TransitionID, N};
use aleo_std::StorageMode;
use crate::aleo::*;
use anyhow::{bail, Result};
use snarkvm::ledger::{
authority::Authority,
store::{
helpers::{rocksdb::*, Map, MapRead},
*,
},
};
use std::borrow::Cow;

pub struct Stores {
pub blocks: BlockDB<N>,
pub committee: CommitteeDB<N>,
pub transactions: TransactionDB<N>,
pub deployments: DeploymentDB<N>,
pub executions: ExecutionDB<N>,
pub fees: FeeDB<N>,
pub transition: TransitionDB<N>,
pub finalize: FinalizeDB<N>,
pub inputs: InputDB<N>,
pub outputs: OutputDB<N>,
pub blocks: BlockDB,
pub committee: CommitteeDB,
pub transactions: TransactionDB,
pub deployments: DeploymentDB,
pub executions: ExecutionDB,
pub fees: FeeDB,
pub transition: TransitionDB,
pub finalize: FinalizeDB,
pub inputs: InputDB,
pub outputs: OutputDB,
}

impl Stores {
pub fn open(storage_mode: StorageMode) -> Result<Self> {
let transition_store = TransitionStore::<N, TransitionDB<N>>::open(storage_mode.clone())?;
let fee_store = FeeStore::<N, FeeDB<N>>::open(transition_store.clone())?;
let transition_store = TransitionStore::open(storage_mode.clone())?;
let fee_store = FeeStore::open(transition_store.clone())?;
Ok(Self {
blocks: BlockDB::open(storage_mode.clone())?,
committee: CommitteeDB::open(storage_mode.clone())?,
Expand Down
7 changes: 3 additions & 4 deletions crates/checkpoint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,24 @@ use std::{
#[cfg(test)]
mod retention_tests;

mod errors;
pub mod errors;
mod header;
mod manager;
mod retention;

pub use errors::*;
pub use header::*;
pub use manager::*;
pub use retention::*;

#[cfg(feature = "write")]
pub(crate) mod aleo;
#[cfg(feature = "write")]
mod checkpoint;
#[cfg(feature = "write")]
mod content;
#[cfg(feature = "write")]
mod ledger;
#[cfg(feature = "write")]
pub(crate) mod snarkos;
#[cfg(feature = "write")]
pub use checkpoint::*;
#[cfg(feature = "write")]
pub use content::*;
Expand Down
Loading

0 comments on commit 6977e9f

Please sign in to comment.