use crate::{
helpers::memory::{MemoryMap, TransactionMemory, TransitionMemory},
BlockStorage,
ConfirmedTxType,
TransactionStore,
TransitionStore,
};
use console::{prelude::*, types::Field};
use ledger_authority::Authority;
use ledger_block::{Header, Ratifications, Rejected};
use ledger_coinbase::{CoinbaseSolution, PuzzleCommitment};
#[derive(Clone)]
pub struct BlockMemory<N: Network> {
state_root_map: MemoryMap<u32, N::StateRoot>,
reverse_state_root_map: MemoryMap<N::StateRoot, u32>,
id_map: MemoryMap<u32, N::BlockHash>,
reverse_id_map: MemoryMap<N::BlockHash, u32>,
header_map: MemoryMap<N::BlockHash, Header<N>>,
authority_map: MemoryMap<N::BlockHash, Authority<N>>,
certificate_map: MemoryMap<Field<N>, (u32, u64)>,
ratifications_map: MemoryMap<N::BlockHash, Ratifications<N>>,
solutions_map: MemoryMap<N::BlockHash, Option<CoinbaseSolution<N>>>,
puzzle_commitments_map: MemoryMap<PuzzleCommitment<N>, u32>,
transactions_map: MemoryMap<N::BlockHash, Vec<N::TransactionID>>,
aborted_transaction_ids_map: MemoryMap<N::BlockHash, Vec<N::TransactionID>>,
rejected_or_aborted_transaction_id_map: MemoryMap<N::TransactionID, N::BlockHash>,
confirmed_transactions_map: MemoryMap<N::TransactionID, (N::BlockHash, ConfirmedTxType, Vec<u8>)>,
rejected_deployment_or_execution_map: MemoryMap<Field<N>, Rejected<N>>,
transaction_store: TransactionStore<N, TransactionMemory<N>>,
}
#[rustfmt::skip]
impl<N: Network> BlockStorage<N> for BlockMemory<N> {
type StateRootMap = MemoryMap<u32, N::StateRoot>;
type ReverseStateRootMap = MemoryMap<N::StateRoot, u32>;
type IDMap = MemoryMap<u32, N::BlockHash>;
type ReverseIDMap = MemoryMap<N::BlockHash, u32>;
type HeaderMap = MemoryMap<N::BlockHash, Header<N>>;
type AuthorityMap = MemoryMap<N::BlockHash, Authority<N>>;
type CertificateMap = MemoryMap<Field<N>, (u32, u64)>;
type RatificationsMap = MemoryMap<N::BlockHash, Ratifications<N>>;
type SolutionsMap = MemoryMap<N::BlockHash, Option<CoinbaseSolution<N>>>;
type PuzzleCommitmentsMap = MemoryMap<PuzzleCommitment<N>, u32>;
type TransactionsMap = MemoryMap<N::BlockHash, Vec<N::TransactionID>>;
type AbortedTransactionIDsMap = MemoryMap<N::BlockHash, Vec<N::TransactionID>>;
type RejectedOrAbortedTransactionIDMap = MemoryMap<N::TransactionID, N::BlockHash>;
type ConfirmedTransactionsMap = MemoryMap<N::TransactionID, (N::BlockHash, ConfirmedTxType, Vec<u8>)>;
type RejectedDeploymentOrExecutionMap = MemoryMap<Field<N>, Rejected<N>>;
type TransactionStorage = TransactionMemory<N>;
type TransitionStorage = TransitionMemory<N>;
fn open(dev: Option<u16>) -> Result<Self> {
let transition_store = TransitionStore::<N, TransitionMemory<N>>::open(dev)?;
let transaction_store = TransactionStore::<N, TransactionMemory<N>>::open(transition_store)?;
Ok(Self {
state_root_map: MemoryMap::default(),
reverse_state_root_map: MemoryMap::default(),
id_map: MemoryMap::default(),
reverse_id_map: MemoryMap::default(),
header_map: MemoryMap::default(),
authority_map: MemoryMap::default(),
certificate_map: MemoryMap::default(),
ratifications_map: MemoryMap::default(),
solutions_map: MemoryMap::default(),
puzzle_commitments_map: MemoryMap::default(),
transactions_map: MemoryMap::default(),
aborted_transaction_ids_map: MemoryMap::default(),
rejected_or_aborted_transaction_id_map: MemoryMap::default(),
confirmed_transactions_map: MemoryMap::default(),
rejected_deployment_or_execution_map: MemoryMap::default(),
transaction_store,
})
}
fn state_root_map(&self) -> &Self::StateRootMap {
&self.state_root_map
}
fn reverse_state_root_map(&self) -> &Self::ReverseStateRootMap {
&self.reverse_state_root_map
}
fn id_map(&self) -> &Self::IDMap {
&self.id_map
}
fn reverse_id_map(&self) -> &Self::ReverseIDMap {
&self.reverse_id_map
}
fn header_map(&self) -> &Self::HeaderMap {
&self.header_map
}
fn certificate_map(&self) -> &Self::CertificateMap {
&self.certificate_map
}
fn authority_map(&self) -> &Self::AuthorityMap {
&self.authority_map
}
fn ratifications_map(&self) -> &Self::RatificationsMap {
&self.ratifications_map
}
fn solutions_map(&self) -> &Self::SolutionsMap {
&self.solutions_map
}
fn puzzle_commitments_map(&self) -> &Self::PuzzleCommitmentsMap {
&self.puzzle_commitments_map
}
fn transactions_map(&self) -> &Self::TransactionsMap {
&self.transactions_map
}
fn aborted_transaction_ids_map(&self) -> &Self::AbortedTransactionIDsMap {
&self.aborted_transaction_ids_map
}
fn rejected_or_aborted_transaction_id_map(&self) -> &Self::RejectedOrAbortedTransactionIDMap {
&self.rejected_or_aborted_transaction_id_map
}
fn confirmed_transactions_map(&self) -> &Self::ConfirmedTransactionsMap {
&self.confirmed_transactions_map
}
fn rejected_deployment_or_execution_map(&self) -> &Self::RejectedDeploymentOrExecutionMap {
&self.rejected_deployment_or_execution_map
}
fn transaction_store(&self) -> &TransactionStore<N, Self::TransactionStorage> {
&self.transaction_store
}
}