|
| 1 | +use std::collections::HashMap; |
| 2 | + |
| 3 | +use starknet_api::core::ContractAddress; |
| 4 | +use starknet_api::hash::HashOutput; |
| 5 | +use starknet_patricia::patricia_merkle_tree::node_data::leaf::LeafModifications; |
| 6 | +use starknet_patricia::patricia_merkle_tree::types::NodeIndex; |
| 7 | +use starknet_patricia_storage::storage_trait::Storage; |
| 8 | + |
| 9 | +use crate::block_committer::input::{ConfigImpl, StarknetStorageValue}; |
| 10 | +use crate::db::forest_trait::{ForestReader, ForestWriter}; |
| 11 | +use crate::forest::filled_forest::FilledForest; |
| 12 | +use crate::forest::forest_errors::ForestResult; |
| 13 | +use crate::forest::original_skeleton_forest::{ForestSortedIndices, OriginalSkeletonForest}; |
| 14 | +use crate::patricia_merkle_tree::leaf::leaf_impl::ContractState; |
| 15 | +use crate::patricia_merkle_tree::types::CompiledClassHash; |
| 16 | + |
| 17 | +pub struct FactsDb<S: Storage> { |
| 18 | + // TODO(Yoav): Define StorageStats trait and impl it here. Then, make the storage field |
| 19 | + // private. |
| 20 | + pub storage: S, |
| 21 | +} |
| 22 | + |
| 23 | +impl<S: Storage> FactsDb<S> { |
| 24 | + pub fn new(storage: S) -> Self { |
| 25 | + Self { storage } |
| 26 | + } |
| 27 | + |
| 28 | + pub fn consume_storage(self) -> S { |
| 29 | + self.storage |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +impl<'a, S: Storage> ForestReader<'a> for FactsDb<S> { |
| 34 | + fn read( |
| 35 | + &mut self, |
| 36 | + contracts_trie_root_hash: HashOutput, |
| 37 | + classes_trie_root_hash: HashOutput, |
| 38 | + storage_updates: &'a HashMap<ContractAddress, LeafModifications<StarknetStorageValue>>, |
| 39 | + classes_updates: &'a LeafModifications<CompiledClassHash>, |
| 40 | + forest_sorted_indices: &'a ForestSortedIndices<'a>, |
| 41 | + config: ConfigImpl, |
| 42 | + ) -> ForestResult<(OriginalSkeletonForest<'a>, HashMap<NodeIndex, ContractState>)> { |
| 43 | + // TODO(Yoav): Move here OriginalSkeletonForest constructor with all the (facts) |
| 44 | + // storage-related functions. |
| 45 | + OriginalSkeletonForest::create( |
| 46 | + &mut self.storage, |
| 47 | + contracts_trie_root_hash, |
| 48 | + classes_trie_root_hash, |
| 49 | + storage_updates, |
| 50 | + classes_updates, |
| 51 | + forest_sorted_indices, |
| 52 | + &config, |
| 53 | + ) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +impl<S: Storage> ForestWriter for FactsDb<S> { |
| 58 | + fn write(&mut self, filled_forest: &FilledForest) -> usize { |
| 59 | + filled_forest.write_to_storage(&mut self.storage) |
| 60 | + } |
| 61 | +} |
0 commit comments