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::rocksdb::{self, CommitteeMap, DataMap, Database, MapID, NestedDataMap, ProgramMap}, 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 /// A RocksDB finalize storage. 34 #[derive(Clone)] 35 pub struct FinalizeDB<N: Network> { 36 /// The committee store. 37 committee_store: CommitteeStore<N, CommitteeDB<N>>, 38 /// The program ID map. 39 program_id_map: DataMap<ProgramID<N>, IndexSet<Identifier<N>>>, 40 /// The key-value map. 41 key_value_map: NestedDataMap<(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 FinalizeDB<N> { 48 type CommitteeStorage = CommitteeDB<N>; 49 type ProgramIDMap = DataMap<ProgramID<N>, IndexSet<Identifier<N>>>; 50 type KeyValueMap = NestedDataMap<(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, CommitteeDB<N>>::open(storage.clone())?; 57 // Return the finalize storage. 58 Ok(Self { 59 committee_store, 60 program_id_map: rocksdb::RocksDB::open_map(N::ID, storage.clone(), MapID::Program(ProgramMap::ProgramID))?, 61 key_value_map: rocksdb::RocksDB::open_nested_map(N::ID, storage.clone(), MapID::Program(ProgramMap::KeyValueID))?, 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 /// A RocksDB committee storage. 88 #[derive(Clone)] 89 pub struct CommitteeDB<N: Network> { 90 /// The current round map. 91 current_round_map: DataMap<u8, u64>, 92 /// The round to height map. 93 round_to_height_map: DataMap<u64, u32>, 94 /// The committee map. 95 committee_map: DataMap<u32, Committee<N>>, 96 /// The storage mode. 97 storage_mode: StorageMode, 98 } 99 100 #[rustfmt::skip] 101 impl<N: Network> CommitteeStorage<N> for CommitteeDB<N> { 102 type CurrentRoundMap = DataMap<u8, u64>; 103 type RoundToHeightMap = DataMap<u64, u32>; 104 type CommitteeMap = DataMap<u32, Committee<N>>; 105 106 /// Initializes the committee storage. 107 fn open<S: Into<StorageMode>>(storage: S) -> Result<Self> { 108 let storage = storage.into(); 109 Ok(Self { 110 current_round_map: rocksdb::RocksDB::open_map(N::ID, storage.clone(), MapID::Committee(CommitteeMap::CurrentRound))?, 111 round_to_height_map: rocksdb::RocksDB::open_map(N::ID, storage.clone(), MapID::Committee(CommitteeMap::RoundToHeight))?, 112 committee_map: rocksdb::RocksDB::open_map(N::ID, storage.clone(), MapID::Committee(CommitteeMap::Committee))?, 113 storage_mode: storage, 114 }) 115 } 116 117 /// Returns the current round map. 118 fn current_round_map(&self) -> &Self::CurrentRoundMap { 119 &self.current_round_map 120 } 121 122 /// Returns the round to height map. 123 fn round_to_height_map(&self) -> &Self::RoundToHeightMap { 124 &self.round_to_height_map 125 } 126 127 /// Returns the committee map. 128 fn committee_map(&self) -> &Self::CommitteeMap { 129 &self.committee_map 130 } 131 132 /// Returns the storage mode. 133 fn storage_mode(&self) -> &StorageMode { 134 &self.storage_mode 135 } 136 }