Skip to content

Commit

Permalink
Merge pull request #2314 from AleoHQ/feat/custom-storage
Browse files Browse the repository at this point in the history
Integrate StorageMode into ledger-store
  • Loading branch information
howardwu authored Jan 21, 2024
2 parents 290f77d + 647143f commit e57334f
Show file tree
Hide file tree
Showing 33 changed files with 732 additions and 693 deletions.
838 changes: 418 additions & 420 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion algorithms/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ version = "=0.16.17"
optional = true

[dependencies.aleo-std]
version = "0.1.18"
version = "0.1.24"
default-features = false

[dependencies.anyhow]
Expand Down
2 changes: 1 addition & 1 deletion console/collections/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ default-features = false
features = [ "field", "integers" ]

[dependencies.aleo-std]
version = "0.1.18"
version = "0.1.24"
default-features = false

[dependencies.rayon]
Expand Down
2 changes: 1 addition & 1 deletion fields/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ version = "=0.16.17"
default-features = false

[dependencies.aleo-std]
version = "0.1.18"
version = "0.1.24"
default-features = false

[dependencies.anyhow]
Expand Down
2 changes: 1 addition & 1 deletion ledger/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ path = "../synthesizer"
version = "=0.16.17"

[dependencies.aleo-std]
version = "0.1.18"
version = "0.1.24"
default-features = false

[dependencies.anyhow]
Expand Down
2 changes: 1 addition & 1 deletion ledger/coinbase/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ version = "=0.16.17"
default-features = false

[dependencies.aleo-std]
version = "0.1.18"
version = "0.1.24"
default-features = false

[dependencies.anyhow]
Expand Down
1 change: 1 addition & 0 deletions ledger/query/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,5 @@ optional = true
[dependencies.ureq]
version = "2.7.1"
features = [ "json" ]
default-features = false
optional = true
7 changes: 3 additions & 4 deletions ledger/store/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ edition = "2021"

[features]
default = [ "indexmap/rayon", "rayon" ]
rocks = [ "aleo-std", "once_cell", "rocksdb", "tracing" ]
rocks = [ "once_cell", "rocksdb", "tracing" ]
serial = [
"console/serial",
"ledger-block/serial",
Expand Down Expand Up @@ -79,10 +79,9 @@ package = "snarkvm-synthesizer-snark"
path = "../../synthesizer/snark"
version = "=0.16.17"

[dependencies.aleo-std]
version = "0.1.18"
[dependencies.aleo-std-storage]
version = "0.1.7"
default-features = false
optional = true

[dependencies.anyhow]
version = "1.0.73"
Expand Down
21 changes: 11 additions & 10 deletions ledger/store/src/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ use ledger_coinbase::{CoinbaseSolution, ProverSolution, PuzzleCommitment};
use ledger_narwhal_batch_certificate::BatchCertificate;
use synthesizer_program::Program;

use aleo_std_storage::StorageMode;
use anyhow::Result;
use parking_lot::RwLock;
use std::{borrow::Cow, io::Cursor, sync::Arc};
Expand Down Expand Up @@ -207,7 +208,7 @@ pub trait BlockStorage<N: Network>: 'static + Clone + Send + Sync {
type TransitionStorage: TransitionStorage<N>;

/// Initializes the block storage.
fn open(dev: Option<u16>) -> Result<Self>;
fn open<S: Clone + Into<StorageMode>>(storage: S) -> Result<Self>;

/// Returns the state root map.
fn state_root_map(&self) -> &Self::StateRootMap;
Expand Down Expand Up @@ -246,10 +247,10 @@ pub trait BlockStorage<N: Network>: 'static + Clone + Send + Sync {
fn transition_store(&self) -> &TransitionStore<N, Self::TransitionStorage> {
self.transaction_store().transition_store()
}
/// Returns the optional development ID.
fn dev(&self) -> Option<u16> {
debug_assert!(self.transaction_store().dev() == self.transition_store().dev());
self.transition_store().dev()
/// Returns the storage mode.
fn storage_mode(&self) -> &StorageMode {
debug_assert!(self.transaction_store().storage_mode() == self.transition_store().storage_mode());
self.transition_store().storage_mode()
}

/// Starts an atomic batch write operation.
Expand Down Expand Up @@ -1004,9 +1005,9 @@ pub struct BlockStore<N: Network, B: BlockStorage<N>> {

impl<N: Network, B: BlockStorage<N>> BlockStore<N, B> {
/// Initializes the block store.
pub fn open(dev: Option<u16>) -> Result<Self> {
pub fn open<S: Clone + Into<StorageMode>>(storage: S) -> Result<Self> {
// Initialize the block storage.
let storage = B::open(dev)?;
let storage = B::open(storage)?;

// Compute the block tree.
let tree = {
Expand Down Expand Up @@ -1143,9 +1144,9 @@ impl<N: Network, B: BlockStorage<N>> BlockStore<N, B> {
self.storage.finish_atomic()
}

/// Returns the optional development ID.
pub fn dev(&self) -> Option<u16> {
self.storage.dev()
/// Returns the storage mode.
pub fn storage_mode(&self) -> &StorageMode {
self.storage.storage_mode()
}
}

Expand Down
23 changes: 12 additions & 11 deletions ledger/store/src/consensus/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use crate::{
};
use console::network::prelude::*;

use aleo_std_storage::StorageMode;
use anyhow::Result;
use core::marker::PhantomData;

Expand All @@ -39,7 +40,7 @@ pub trait ConsensusStorage<N: Network>: 'static + Clone + Send + Sync {
type TransitionStorage: TransitionStorage<N>;

/// Initializes the consensus storage.
fn open(dev: Option<u16>) -> Result<Self>;
fn open<S: Clone + Into<StorageMode>>(storage: S) -> Result<Self>;

/// Returns the finalize storage.
fn finalize_store(&self) -> &FinalizeStore<N, Self::FinalizeStorage>;
Expand All @@ -53,11 +54,11 @@ pub trait ConsensusStorage<N: Network>: 'static + Clone + Send + Sync {
fn transition_store(&self) -> &TransitionStore<N, Self::TransitionStorage> {
self.block_store().transition_store()
}
/// Returns the optional development ID.
fn dev(&self) -> Option<u16> {
debug_assert!(self.block_store().dev() == self.transaction_store().dev());
debug_assert!(self.transaction_store().dev() == self.transition_store().dev());
self.transition_store().dev()
/// Returns the storage mode.
fn storage_mode(&self) -> &StorageMode {
debug_assert!(self.block_store().storage_mode() == self.transaction_store().storage_mode());
debug_assert!(self.transaction_store().storage_mode() == self.transition_store().storage_mode());
self.transition_store().storage_mode()
}

/// Starts an atomic batch write operation.
Expand Down Expand Up @@ -113,9 +114,9 @@ pub struct ConsensusStore<N: Network, C: ConsensusStorage<N>> {

impl<N: Network, C: ConsensusStorage<N>> ConsensusStore<N, C> {
/// Initializes the consensus store.
pub fn open(dev: Option<u16>) -> Result<Self> {
pub fn open<S: Clone + Into<StorageMode>>(storage: S) -> Result<Self> {
// Initialize the consensus storage.
let storage = C::open(dev)?;
let storage = C::open(storage.clone())?;
// Return the consensus store.
Ok(Self { storage, _phantom: PhantomData })
}
Expand Down Expand Up @@ -180,8 +181,8 @@ impl<N: Network, C: ConsensusStorage<N>> ConsensusStore<N, C> {
self.storage.finish_atomic()
}

/// Returns the optional development ID.
pub fn dev(&self) -> Option<u16> {
self.storage.dev()
/// Returns the storage mode.
pub fn storage_mode(&self) -> &StorageMode {
self.storage.storage_mode()
}
}
6 changes: 4 additions & 2 deletions ledger/store/src/helpers/memory/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ use ledger_authority::Authority;
use ledger_block::{Header, Ratifications, Rejected};
use ledger_coinbase::{CoinbaseSolution, PuzzleCommitment};

use aleo_std_storage::StorageMode;

/// An in-memory block storage.
#[derive(Clone)]
pub struct BlockMemory<N: Network> {
Expand Down Expand Up @@ -82,9 +84,9 @@ impl<N: Network> BlockStorage<N> for BlockMemory<N> {
type TransitionStorage = TransitionMemory<N>;

/// Initializes the block storage.
fn open(dev: Option<u16>) -> Result<Self> {
fn open<S: Clone + Into<StorageMode>>(storage: S) -> Result<Self> {
// Initialize the transition store.
let transition_store = TransitionStore::<N, TransitionMemory<N>>::open(dev)?;
let transition_store = TransitionStore::<N, TransitionMemory<N>>::open(storage)?;
// Initialize the transaction store.
let transaction_store = TransactionStore::<N, TransactionMemory<N>>::open(transition_store)?;
// Return the block storage.
Expand Down
8 changes: 5 additions & 3 deletions ledger/store/src/helpers/memory/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ use crate::{
};
use console::prelude::*;

use aleo_std_storage::StorageMode;

/// An in-memory consensus storage.
#[derive(Clone)]
pub struct ConsensusMemory<N: Network> {
Expand All @@ -37,11 +39,11 @@ impl<N: Network> ConsensusStorage<N> for ConsensusMemory<N> {
type TransitionStorage = TransitionMemory<N>;

/// Initializes the consensus storage.
fn open(dev: Option<u16>) -> Result<Self> {
fn open<S: Clone + Into<StorageMode>>(storage: S) -> Result<Self> {
// Initialize the finalize store.
let finalize_store = FinalizeStore::<N, FinalizeMemory<N>>::open(dev)?;
let finalize_store = FinalizeStore::<N, FinalizeMemory<N>>::open(storage.clone())?;
// Initialize the block store.
let block_store = BlockStore::<N, BlockMemory<N>>::open(dev)?;
let block_store = BlockStore::<N, BlockMemory<N>>::open(storage)?;
// Return the consensus storage.
Ok(Self {
finalize_store,
Expand Down
31 changes: 16 additions & 15 deletions ledger/store/src/helpers/memory/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use console::{
};
use ledger_committee::Committee;

use aleo_std_storage::StorageMode;
use indexmap::IndexSet;

/// An in-memory finalize storage.
Expand All @@ -37,8 +38,8 @@ pub struct FinalizeMemory<N: Network> {
program_id_map: MemoryMap<ProgramID<N>, IndexSet<Identifier<N>>>,
/// The key-value map.
key_value_map: NestedMemoryMap<(ProgramID<N>, Identifier<N>), Plaintext<N>, Value<N>>,
/// The optional development ID.
dev: Option<u16>,
/// The storage mode.
storage_mode: StorageMode,
}

#[rustfmt::skip]
Expand All @@ -48,15 +49,15 @@ impl<N: Network> FinalizeStorage<N> for FinalizeMemory<N> {
type KeyValueMap = NestedMemoryMap<(ProgramID<N>, Identifier<N>), Plaintext<N>, Value<N>>;

/// Initializes the finalize storage.
fn open(dev: Option<u16>) -> Result<Self> {
fn open<S: Clone + Into<StorageMode>>(storage: S) -> Result<Self> {
// Initialize the committee store.
let committee_store = CommitteeStore::<N, CommitteeMemory<N>>::open(dev)?;
let committee_store = CommitteeStore::<N, CommitteeMemory<N>>::open(storage.clone())?;
// Return the finalize store.
Ok(Self {
committee_store,
program_id_map: MemoryMap::default(),
key_value_map: NestedMemoryMap::default(),
dev,
storage_mode: storage.into(),
})
}

Expand All @@ -81,9 +82,9 @@ impl<N: Network> FinalizeStorage<N> for FinalizeMemory<N> {
&self.key_value_map
}

/// Returns the optional development ID.
fn dev(&self) -> Option<u16> {
self.dev
/// Returns the storage mode.
fn storage_mode(&self) -> &StorageMode {
&self.storage_mode
}
}

Expand All @@ -96,8 +97,8 @@ pub struct CommitteeMemory<N: Network> {
round_to_height_map: MemoryMap<u64, u32>,
/// The committee map.
committee_map: MemoryMap<u32, Committee<N>>,
/// The optional development ID.
dev: Option<u16>,
/// The storage mode.
storage_mode: StorageMode,
}

#[rustfmt::skip]
Expand All @@ -107,12 +108,12 @@ impl<N: Network> CommitteeStorage<N> for CommitteeMemory<N> {
type CommitteeMap = MemoryMap<u32, Committee<N>>;

/// Initializes the committee storage.
fn open(dev: Option<u16>) -> Result<Self> {
fn open<S: Clone + Into<StorageMode>>(storage: S) -> Result<Self> {
Ok(Self {
current_round_map: MemoryMap::default(),
round_to_height_map: MemoryMap::default(),
committee_map: MemoryMap::default(),
dev,
storage_mode: storage.into(),
})
}

Expand All @@ -137,8 +138,8 @@ impl<N: Network> CommitteeStorage<N> for CommitteeMemory<N> {
&self.committee_map
}

/// Returns the optional development ID.
fn dev(&self) -> Option<u16> {
self.dev
/// Returns the storage mode.
fn storage_mode(&self) -> &StorageMode {
&self.storage_mode
}
}
Loading

0 comments on commit e57334f

Please sign in to comment.