snarkvm_ledger_store/helpers/memory/
transition.rsuse crate::{InputStorage, InputStore, OutputStorage, OutputStore, TransitionStorage, helpers::memory::MemoryMap};
use console::{
prelude::*,
program::{Ciphertext, Future, Identifier, Plaintext, ProgramID, Record},
types::{Field, Group},
};
use aleo_std_storage::StorageMode;
#[derive(Clone)]
pub struct TransitionMemory<N: Network> {
locator_map: MemoryMap<N::TransitionID, (ProgramID<N>, Identifier<N>)>,
input_store: InputStore<N, InputMemory<N>>,
output_store: OutputStore<N, OutputMemory<N>>,
tpk_map: MemoryMap<N::TransitionID, Group<N>>,
reverse_tpk_map: MemoryMap<Group<N>, N::TransitionID>,
tcm_map: MemoryMap<N::TransitionID, Field<N>>,
reverse_tcm_map: MemoryMap<Field<N>, N::TransitionID>,
scm_map: MemoryMap<N::TransitionID, Field<N>>,
}
#[rustfmt::skip]
impl<N: Network> TransitionStorage<N> for TransitionMemory<N> {
type LocatorMap = MemoryMap<N::TransitionID, (ProgramID<N>, Identifier<N>)>;
type InputStorage = InputMemory<N>;
type OutputStorage = OutputMemory<N>;
type TPKMap = MemoryMap<N::TransitionID, Group<N>>;
type ReverseTPKMap = MemoryMap<Group<N>, N::TransitionID>;
type TCMMap = MemoryMap<N::TransitionID, Field<N>>;
type ReverseTCMMap = MemoryMap<Field<N>, N::TransitionID>;
type SCMMap = MemoryMap<N::TransitionID, Field<N>>;
fn open<S: Clone + Into<StorageMode>>(storage: S) -> Result<Self> {
Ok(Self {
locator_map: MemoryMap::default(),
input_store: InputStore::open(storage.clone())?,
output_store: OutputStore::open(storage)?,
tpk_map: MemoryMap::default(),
reverse_tpk_map: MemoryMap::default(),
tcm_map: MemoryMap::default(),
reverse_tcm_map: MemoryMap::default(),
scm_map: MemoryMap::default(),
})
}
fn locator_map(&self) -> &Self::LocatorMap {
&self.locator_map
}
fn input_store(&self) -> &InputStore<N, Self::InputStorage> {
&self.input_store
}
fn output_store(&self) -> &OutputStore<N, Self::OutputStorage> {
&self.output_store
}
fn tpk_map(&self) -> &Self::TPKMap {
&self.tpk_map
}
fn reverse_tpk_map(&self) -> &Self::ReverseTPKMap {
&self.reverse_tpk_map
}
fn tcm_map(&self) -> &Self::TCMMap {
&self.tcm_map
}
fn reverse_tcm_map(&self) -> &Self::ReverseTCMMap {
&self.reverse_tcm_map
}
fn scm_map(&self) -> &Self::SCMMap {
&self.scm_map
}
}
#[derive(Clone)]
pub struct InputMemory<N: Network> {
id_map: MemoryMap<N::TransitionID, Vec<Field<N>>>,
reverse_id_map: MemoryMap<Field<N>, N::TransitionID>,
constant: MemoryMap<Field<N>, Option<Plaintext<N>>>,
public: MemoryMap<Field<N>, Option<Plaintext<N>>>,
private: MemoryMap<Field<N>, Option<Ciphertext<N>>>,
record: MemoryMap<Field<N>, Field<N>>,
record_tag: MemoryMap<Field<N>, Field<N>>,
external_record: MemoryMap<Field<N>, ()>,
storage_mode: StorageMode,
}
#[rustfmt::skip]
impl<N: Network> InputStorage<N> for InputMemory<N> {
type IDMap = MemoryMap<N::TransitionID, Vec<Field<N>>>;
type ReverseIDMap = MemoryMap<Field<N>, N::TransitionID>;
type ConstantMap = MemoryMap<Field<N>, Option<Plaintext<N>>>;
type PublicMap = MemoryMap<Field<N>, Option<Plaintext<N>>>;
type PrivateMap = MemoryMap<Field<N>, Option<Ciphertext<N>>>;
type RecordMap = MemoryMap<Field<N>, Field<N>>;
type RecordTagMap = MemoryMap<Field<N>, Field<N>>;
type ExternalRecordMap = MemoryMap<Field<N>, ()>;
fn open<S: Clone + Into<StorageMode>>(storage: S) -> Result<Self> {
Ok(Self {
id_map: MemoryMap::default(),
reverse_id_map: MemoryMap::default(),
constant: MemoryMap::default(),
public: MemoryMap::default(),
private: MemoryMap::default(),
record: MemoryMap::default(),
record_tag: MemoryMap::default(),
external_record: MemoryMap::default(),
storage_mode: storage.into(),
})
}
fn id_map(&self) -> &Self::IDMap {
&self.id_map
}
fn reverse_id_map(&self) -> &Self::ReverseIDMap {
&self.reverse_id_map
}
fn constant_map(&self) -> &Self::ConstantMap {
&self.constant
}
fn public_map(&self) -> &Self::PublicMap {
&self.public
}
fn private_map(&self) -> &Self::PrivateMap {
&self.private
}
fn record_map(&self) -> &Self::RecordMap {
&self.record
}
fn record_tag_map(&self) -> &Self::RecordTagMap {
&self.record_tag
}
fn external_record_map(&self) -> &Self::ExternalRecordMap {
&self.external_record
}
fn storage_mode(&self) -> &StorageMode {
&self.storage_mode
}
}
#[derive(Clone)]
#[allow(clippy::type_complexity)]
pub struct OutputMemory<N: Network> {
id_map: MemoryMap<N::TransitionID, Vec<Field<N>>>,
reverse_id_map: MemoryMap<Field<N>, N::TransitionID>,
constant: MemoryMap<Field<N>, Option<Plaintext<N>>>,
public: MemoryMap<Field<N>, Option<Plaintext<N>>>,
private: MemoryMap<Field<N>, Option<Ciphertext<N>>>,
record: MemoryMap<Field<N>, (Field<N>, Option<Record<N, Ciphertext<N>>>)>,
record_nonce: MemoryMap<Group<N>, Field<N>>,
external_record: MemoryMap<Field<N>, ()>,
future: MemoryMap<Field<N>, Option<Future<N>>>,
storage_mode: StorageMode,
}
#[rustfmt::skip]
impl<N: Network> OutputStorage<N> for OutputMemory<N> {
type IDMap = MemoryMap<N::TransitionID, Vec<Field<N>>>;
type ReverseIDMap = MemoryMap<Field<N>, N::TransitionID>;
type ConstantMap = MemoryMap<Field<N>, Option<Plaintext<N>>>;
type PublicMap = MemoryMap<Field<N>, Option<Plaintext<N>>>;
type PrivateMap = MemoryMap<Field<N>, Option<Ciphertext<N>>>;
type RecordMap = MemoryMap<Field<N>, (Field<N>, Option<Record<N, Ciphertext<N>>>)>;
type RecordNonceMap = MemoryMap<Group<N>, Field<N>>;
type ExternalRecordMap = MemoryMap<Field<N>, ()>;
type FutureMap = MemoryMap<Field<N>, Option<Future<N>>>;
fn open<S: Clone + Into<StorageMode>>(storage: S) -> Result<Self> {
Ok(Self {
id_map: Default::default(),
reverse_id_map: Default::default(),
constant: Default::default(),
public: Default::default(),
private: Default::default(),
record: Default::default(),
record_nonce: Default::default(),
external_record: Default::default(),
future: Default::default(),
storage_mode: storage.into(),
})
}
fn id_map(&self) -> &Self::IDMap {
&self.id_map
}
fn reverse_id_map(&self) -> &Self::ReverseIDMap {
&self.reverse_id_map
}
fn constant_map(&self) -> &Self::ConstantMap {
&self.constant
}
fn public_map(&self) -> &Self::PublicMap {
&self.public
}
fn private_map(&self) -> &Self::PrivateMap {
&self.private
}
fn record_map(&self) -> &Self::RecordMap {
&self.record
}
fn record_nonce_map(&self) -> &Self::RecordNonceMap {
&self.record_nonce
}
fn external_record_map(&self) -> &Self::ExternalRecordMap {
&self.external_record
}
fn future_map(&self) -> &Self::FutureMap {
&self.future
}
fn storage_mode(&self) -> &StorageMode {
&self.storage_mode
}
}