snarkvm_ledger_store/helpers/memory/
consensus.rs

1// Copyright (c) 2019-2025 Provable Inc.
2// This file is part of the snarkVM library.
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at:
7
8// http://www.apache.org/licenses/LICENSE-2.0
9
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16use crate::{
17    BlockStore,
18    ConsensusStorage,
19    FinalizeStore,
20    helpers::memory::{BlockMemory, FinalizeMemory, TransactionMemory, TransitionMemory},
21};
22use console::prelude::*;
23
24use aleo_std_storage::StorageMode;
25
26/// An in-memory consensus storage.
27#[derive(Clone)]
28pub struct ConsensusMemory<N: Network> {
29    /// The finalize store.
30    finalize_store: FinalizeStore<N, FinalizeMemory<N>>,
31    /// The block store.
32    block_store: BlockStore<N, BlockMemory<N>>,
33}
34
35#[rustfmt::skip]
36impl<N: Network> ConsensusStorage<N> for ConsensusMemory<N> {
37    type FinalizeStorage = FinalizeMemory<N>;
38    type BlockStorage = BlockMemory<N>;
39    type TransactionStorage = TransactionMemory<N>;
40    type TransitionStorage = TransitionMemory<N>;
41
42    /// Initializes the consensus storage.
43    fn open<S: Into<StorageMode>>(storage: S) -> Result<Self> {
44        let storage = storage.into();
45        // Initialize the finalize store.
46        let finalize_store = FinalizeStore::<N, FinalizeMemory<N>>::open(storage.clone())?;
47        // Initialize the block store.
48        let block_store = BlockStore::<N, BlockMemory<N>>::open(storage)?;
49        // Return the consensus storage.
50        Ok(Self {
51            finalize_store,
52            block_store,
53        })
54    }
55
56    /// Returns the finalize store.
57    fn finalize_store(&self) -> &FinalizeStore<N, Self::FinalizeStorage> {
58        &self.finalize_store
59    }
60
61    /// Returns the block store.
62    fn block_store(&self) -> &BlockStore<N, Self::BlockStorage> {
63        &self.block_store
64    }
65}