snarkvm_ledger_store/helpers/memory/
program.rs#![allow(clippy::type_complexity)]
use crate::{
CommitteeStorage,
CommitteeStore,
FinalizeStorage,
helpers::memory::{MemoryMap, NestedMemoryMap},
};
use console::{
prelude::*,
program::{Identifier, Plaintext, ProgramID, Value},
};
use ledger_committee::Committee;
use aleo_std_storage::StorageMode;
use indexmap::IndexSet;
#[derive(Clone)]
pub struct FinalizeMemory<N: Network> {
committee_store: CommitteeStore<N, CommitteeMemory<N>>,
program_id_map: MemoryMap<ProgramID<N>, IndexSet<Identifier<N>>>,
key_value_map: NestedMemoryMap<(ProgramID<N>, Identifier<N>), Plaintext<N>, Value<N>>,
storage_mode: StorageMode,
}
#[rustfmt::skip]
impl<N: Network> FinalizeStorage<N> for FinalizeMemory<N> {
type CommitteeStorage = CommitteeMemory<N>;
type ProgramIDMap = MemoryMap<ProgramID<N>, IndexSet<Identifier<N>>>;
type KeyValueMap = NestedMemoryMap<(ProgramID<N>, Identifier<N>), Plaintext<N>, Value<N>>;
fn open<S: Clone + Into<StorageMode>>(storage: S) -> Result<Self> {
let committee_store = CommitteeStore::<N, CommitteeMemory<N>>::open(storage.clone())?;
Ok(Self {
committee_store,
program_id_map: MemoryMap::default(),
key_value_map: NestedMemoryMap::default(),
storage_mode: storage.into(),
})
}
#[cfg(any(test, feature = "test"))]
fn open_testing(_: std::path::PathBuf, dev: Option<u16>) -> Result<Self> {
Self::open(dev)
}
fn committee_store(&self) -> &CommitteeStore<N, Self::CommitteeStorage> {
&self.committee_store
}
fn program_id_map(&self) -> &Self::ProgramIDMap {
&self.program_id_map
}
fn key_value_map(&self) -> &Self::KeyValueMap {
&self.key_value_map
}
fn storage_mode(&self) -> &StorageMode {
&self.storage_mode
}
}
#[derive(Clone)]
pub struct CommitteeMemory<N: Network> {
current_round_map: MemoryMap<u8, u64>,
round_to_height_map: MemoryMap<u64, u32>,
committee_map: MemoryMap<u32, Committee<N>>,
storage_mode: StorageMode,
}
#[rustfmt::skip]
impl<N: Network> CommitteeStorage<N> for CommitteeMemory<N> {
type CurrentRoundMap = MemoryMap<u8, u64>;
type RoundToHeightMap = MemoryMap<u64, u32>;
type CommitteeMap = MemoryMap<u32, Committee<N>>;
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(),
storage_mode: storage.into(),
})
}
#[cfg(any(test, feature = "test"))]
fn open_testing(_: std::path::PathBuf, dev: Option<u16>) -> Result<Self> {
Self::open(dev)
}
fn current_round_map(&self) -> &Self::CurrentRoundMap {
&self.current_round_map
}
fn round_to_height_map(&self) -> &Self::RoundToHeightMap {
&self.round_to_height_map
}
fn committee_map(&self) -> &Self::CommitteeMap {
&self.committee_map
}
fn storage_mode(&self) -> &StorageMode {
&self.storage_mode
}
}