/ ledger / store / src / helpers / rocksdb / consensus.rs
consensus.rs
 1  // Copyright (c) 2019-2025 Alpha-Delta Network Inc.
 2  // This file is part of the deltavm 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  
16  use crate::{
17      BlockStore,
18      ConsensusStorage,
19      FinalizeStore,
20      helpers::rocksdb::{BlockDB, FinalizeDB, TransactionDB, TransitionDB},
21  };
22  use console::prelude::*;
23  
24  use alphastd_storage::StorageMode;
25  
26  /// An RocksDB consensus storage.
27  #[derive(Clone)]
28  pub struct ConsensusDB<N: Network> {
29      /// The finalize store.
30      finalize_store: FinalizeStore<N, FinalizeDB<N>>,
31      /// The block store.
32      block_store: BlockStore<N, BlockDB<N>>,
33  }
34  
35  #[rustfmt::skip]
36  impl<N: Network> ConsensusStorage<N> for ConsensusDB<N> {
37      type FinalizeStorage = FinalizeDB<N>;
38      type BlockStorage = BlockDB<N>;
39      type TransactionStorage = TransactionDB<N>;
40      type TransitionStorage = TransitionDB<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, FinalizeDB<N>>::open(storage.clone())?;
47          // Initialize the block store.
48          let block_store = BlockStore::<N, BlockDB<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  }