|
| 1 | +use std::collections::HashMap; |
| 2 | + |
| 3 | +use async_trait::async_trait; |
| 4 | +use starknet_api::block::BlockNumber; |
| 5 | +use starknet_api::hash::HashOutput; |
| 6 | +use starknet_patricia::patricia_merkle_tree::traversal::TraversalResult; |
| 7 | +use starknet_patricia::patricia_merkle_tree::types::NodeIndex; |
| 8 | +use starknet_patricia_storage::errors::SerializationResult; |
| 9 | +use starknet_patricia_storage::storage_trait::{DbHashMap, DbValue}; |
| 10 | + |
| 11 | +use super::{ |
| 12 | + EmptyInitialReadContext, |
| 13 | + ForestMetadataType, |
| 14 | + ForestReader, |
| 15 | + ForestWriterWithMetadata, |
| 16 | + StorageInitializer, |
| 17 | +}; |
| 18 | +use crate::forest::deleted_nodes::DeletedNodes; |
| 19 | +use crate::forest::filled_forest::FilledForest; |
| 20 | +use crate::forest::forest_errors::ForestResult; |
| 21 | +use crate::patricia_merkle_tree::tree::SortedLeafIndices; |
| 22 | +use crate::patricia_merkle_tree::types::StarknetForestProofs; |
| 23 | + |
| 24 | +/// How Patricia proofs and read-keys digest metadata are updated in the same batch as a forest |
| 25 | +/// write. |
| 26 | +pub enum PatriciaProofsUpdates { |
| 27 | + /// Remove read-keys digest + Patricia proofs on revert. |
| 28 | + Delete(BlockNumber), |
| 29 | + /// Persist read-keys digest + Patricia proofs for this block. |
| 30 | + Set { height: BlockNumber, keys_digest: [u8; 32], witnesses: StarknetForestProofs }, |
| 31 | +} |
| 32 | + |
| 33 | +/// Reads committed OS-input witness payload (structured [`StarknetForestProofs`]) for a block |
| 34 | +/// height. |
| 35 | +#[async_trait] |
| 36 | +pub trait ForestReaderWithWitnesses: |
| 37 | + ForestReader<InitialReadContext: EmptyInitialReadContext> + Send |
| 38 | +{ |
| 39 | + async fn read_witnesses( |
| 40 | + &mut self, |
| 41 | + height: BlockNumber, |
| 42 | + ) -> ForestResult<Option<StarknetForestProofs>>; |
| 43 | + |
| 44 | + /// Fetches Patricia witness paths for OS input, optionally staging serialized trie node KVs on |
| 45 | + /// an in-memory overlay so reads match post-commit state before the forest is persisted. |
| 46 | + async fn fetch_patricia_witnesses( |
| 47 | + &mut self, |
| 48 | + classes_trie_root_hash: HashOutput, |
| 49 | + contracts_trie_root_hash: HashOutput, |
| 50 | + class_sorted_leaf_indices: SortedLeafIndices<'_>, |
| 51 | + contract_sorted_leaf_indices: SortedLeafIndices<'_>, |
| 52 | + contract_storage_sorted_leaf_indices: &HashMap<NodeIndex, SortedLeafIndices<'_>>, |
| 53 | + staged_serialized_forest: Option<DbHashMap>, |
| 54 | + ) -> TraversalResult<StarknetForestProofs>; |
| 55 | +} |
| 56 | + |
| 57 | +/// Writes forest + metadata + deleted nodes, and optionally applies [`PatriciaProofsUpdates`] in |
| 58 | +/// the same batch. |
| 59 | +#[async_trait] |
| 60 | +pub trait ForestWriterWithMetadataAndWitnesses: ForestWriterWithMetadata + Send { |
| 61 | + async fn write_with_metadata_and_witnesses( |
| 62 | + &mut self, |
| 63 | + filled_forest: &FilledForest, |
| 64 | + metadata: HashMap<ForestMetadataType, DbValue>, |
| 65 | + deleted_nodes: DeletedNodes, |
| 66 | + patricia_proofs_updates: PatriciaProofsUpdates, |
| 67 | + ) -> SerializationResult<usize>; |
| 68 | +} |
| 69 | + |
| 70 | +/// Forest storage with empty [`ForestReader::InitialReadContext`] plus OS-input witness read/write. |
| 71 | +pub trait ForestStorageWithWitnesses: |
| 72 | + ForestReaderWithWitnesses + ForestWriterWithMetadataAndWitnesses + StorageInitializer |
| 73 | +{ |
| 74 | +} |
| 75 | + |
| 76 | +impl<T> ForestStorageWithWitnesses for T where |
| 77 | + T: ForestReaderWithWitnesses + ForestWriterWithMetadataAndWitnesses + StorageInitializer |
| 78 | +{ |
| 79 | +} |
0 commit comments