/ ledger / store / src / helpers / memory / program.rs
program.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  #![allow(clippy::type_complexity)]
 17  
 18  use crate::{
 19      CommitteeStorage,
 20      CommitteeStore,
 21      FinalizeStorage,
 22      helpers::memory::{MemoryMap, NestedMemoryMap},
 23  };
 24  use deltavm_ledger_committee::Committee;
 25  use console::{
 26      prelude::*,
 27      program::{Identifier, Plaintext, ProgramID, Value},
 28  };
 29  
 30  use alphastd_storage::StorageMode;
 31  use indexmap::IndexSet;
 32  
 33  /// An in-memory finalize storage.
 34  #[derive(Clone)]
 35  pub struct FinalizeMemory<N: Network> {
 36      /// The committee store.
 37      committee_store: CommitteeStore<N, CommitteeMemory<N>>,
 38      /// The program ID map.
 39      program_id_map: MemoryMap<ProgramID<N>, IndexSet<Identifier<N>>>,
 40      /// The key-value map.
 41      key_value_map: NestedMemoryMap<(ProgramID<N>, Identifier<N>), Plaintext<N>, Value<N>>,
 42      /// The storage mode.
 43      storage_mode: StorageMode,
 44  }
 45  
 46  #[rustfmt::skip]
 47  impl<N: Network> FinalizeStorage<N> for FinalizeMemory<N> {
 48      type CommitteeStorage = CommitteeMemory<N>;
 49      type ProgramIDMap = MemoryMap<ProgramID<N>, IndexSet<Identifier<N>>>;
 50      type KeyValueMap = NestedMemoryMap<(ProgramID<N>, Identifier<N>), Plaintext<N>, Value<N>>;
 51  
 52      /// Initializes the finalize storage.
 53      fn open<S: Into<StorageMode>>(storage: S) -> Result<Self> {
 54          let storage = storage.into();
 55          // Initialize the committee store.
 56          let committee_store = CommitteeStore::<N, CommitteeMemory<N>>::open(storage.clone())?;
 57          // Return the finalize store.
 58          Ok(Self {
 59              committee_store,
 60              program_id_map: MemoryMap::default(),
 61              key_value_map: NestedMemoryMap::default(),
 62              storage_mode: storage,
 63          })
 64      }
 65  
 66      /// Returns the committee store.
 67      fn committee_store(&self) -> &CommitteeStore<N, Self::CommitteeStorage> {
 68          &self.committee_store
 69      }
 70  
 71      /// Returns the program ID map.
 72      fn program_id_map(&self) -> &Self::ProgramIDMap {
 73          &self.program_id_map
 74      }
 75  
 76      /// Returns the key-value map.
 77      fn key_value_map(&self) -> &Self::KeyValueMap {
 78          &self.key_value_map
 79      }
 80  
 81      /// Returns the storage mode.
 82      fn storage_mode(&self) -> &StorageMode {
 83          &self.storage_mode
 84      }
 85  }
 86  
 87  /// An in-memory committee storage.
 88  #[derive(Clone)]
 89  pub struct CommitteeMemory<N: Network> {
 90      /// The current round map.
 91      current_round_map: MemoryMap<u8, u64>,
 92      /// The round to height map.
 93      round_to_height_map: MemoryMap<u64, u32>,
 94      /// The committee map.
 95      committee_map: MemoryMap<u32, Committee<N>>,
 96      /// The storage mode.
 97      storage_mode: StorageMode,
 98  }
 99  
100  #[rustfmt::skip]
101  impl<N: Network> CommitteeStorage<N> for CommitteeMemory<N> {
102      type CurrentRoundMap = MemoryMap<u8, u64>;
103      type RoundToHeightMap = MemoryMap<u64, u32>;
104      type CommitteeMap = MemoryMap<u32, Committee<N>>;
105  
106      /// Initializes the committee storage.
107      fn open<S: Into<StorageMode>>(storage: S) -> Result<Self> {
108          Ok(Self {
109              current_round_map: MemoryMap::default(),
110              round_to_height_map: MemoryMap::default(),
111              committee_map: MemoryMap::default(),
112              storage_mode: storage.into(),
113          })
114      }
115  
116      /// Returns the current round map.
117      fn current_round_map(&self) -> &Self::CurrentRoundMap {
118          &self.current_round_map
119      }
120  
121      /// Returns the round to height map.
122      fn round_to_height_map(&self) -> &Self::RoundToHeightMap {
123          &self.round_to_height_map
124      }
125  
126      /// Returns the committee map.
127      fn committee_map(&self) -> &Self::CommitteeMap {
128          &self.committee_map
129      }
130  
131      /// Returns the storage mode.
132      fn storage_mode(&self) -> &StorageMode {
133          &self.storage_mode
134      }
135  }