snarkvm_ledger_store/consensus/
mod.rsuse crate::{
BlockStorage,
BlockStore,
FinalizeStorage,
FinalizeStore,
TransactionStorage,
TransactionStore,
TransitionStorage,
TransitionStore,
};
use console::network::prelude::*;
use aleo_std_storage::StorageMode;
use anyhow::Result;
use core::marker::PhantomData;
pub trait ConsensusStorage<N: Network>: 'static + Clone + Send + Sync {
type FinalizeStorage: FinalizeStorage<N>;
type BlockStorage: BlockStorage<N, TransactionStorage = Self::TransactionStorage, TransitionStorage = Self::TransitionStorage>;
type TransactionStorage: TransactionStorage<N, TransitionStorage = Self::TransitionStorage>;
type TransitionStorage: TransitionStorage<N>;
fn open<S: Clone + Into<StorageMode>>(storage: S) -> Result<Self>;
fn finalize_store(&self) -> &FinalizeStore<N, Self::FinalizeStorage>;
fn block_store(&self) -> &BlockStore<N, Self::BlockStorage>;
fn transaction_store(&self) -> &TransactionStore<N, Self::TransactionStorage> {
self.block_store().transaction_store()
}
fn transition_store(&self) -> &TransitionStore<N, Self::TransitionStorage> {
self.block_store().transition_store()
}
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()
}
fn start_atomic(&self) {
self.finalize_store().start_atomic();
self.block_store().start_atomic();
}
fn is_atomic_in_progress(&self) -> bool {
self.finalize_store().is_atomic_in_progress() || self.block_store().is_atomic_in_progress()
}
fn atomic_checkpoint(&self) {
self.finalize_store().atomic_checkpoint();
self.block_store().atomic_checkpoint();
}
fn clear_latest_checkpoint(&self) {
self.finalize_store().clear_latest_checkpoint();
self.block_store().clear_latest_checkpoint();
}
fn atomic_rewind(&self) {
self.finalize_store().atomic_rewind();
self.block_store().atomic_rewind();
}
fn abort_atomic(&self) {
self.finalize_store().abort_atomic();
self.block_store().abort_atomic();
}
fn finish_atomic(&self) -> Result<()> {
self.finalize_store().finish_atomic()?;
self.block_store().finish_atomic()
}
}
#[derive(Clone)]
pub struct ConsensusStore<N: Network, C: ConsensusStorage<N>> {
storage: C,
_phantom: PhantomData<N>,
}
impl<N: Network, C: ConsensusStorage<N>> ConsensusStore<N, C> {
pub fn open<S: Clone + Into<StorageMode>>(storage: S) -> Result<Self> {
let storage = C::open(storage.clone())?;
Ok(Self { storage, _phantom: PhantomData })
}
pub fn from(storage: C) -> Self {
Self { storage, _phantom: PhantomData }
}
pub fn finalize_store(&self) -> &FinalizeStore<N, C::FinalizeStorage> {
self.storage.finalize_store()
}
pub fn block_store(&self) -> &BlockStore<N, C::BlockStorage> {
self.storage.block_store()
}
pub fn transaction_store(&self) -> &TransactionStore<N, C::TransactionStorage> {
self.storage.transaction_store()
}
pub fn transition_store(&self) -> &TransitionStore<N, C::TransitionStorage> {
self.storage.transition_store()
}
pub fn start_atomic(&self) {
self.storage.start_atomic();
}
pub fn is_atomic_in_progress(&self) -> bool {
self.storage.is_atomic_in_progress()
}
pub fn atomic_checkpoint(&self) {
self.storage.atomic_checkpoint();
}
pub fn clear_latest_checkpoint(&self) {
self.storage.clear_latest_checkpoint();
}
pub fn atomic_rewind(&self) {
self.storage.atomic_rewind();
}
pub fn abort_atomic(&self) {
self.storage.abort_atomic();
}
pub fn finish_atomic(&self) -> Result<()> {
self.storage.finish_atomic()
}
pub fn storage_mode(&self) -> &StorageMode {
self.storage.storage_mode()
}
}