|
| 1 | +use std::sync::LazyLock; |
| 2 | + |
| 3 | +use starknet_api::core::{ClassHash, ContractAddress, Nonce, PATRICIA_KEY_UPPER_BOUND}; |
| 4 | +use starknet_api::hash::HashOutput; |
| 5 | +use starknet_patricia::patricia_merkle_tree::node_data::errors::LeafResult; |
| 6 | +use starknet_patricia::patricia_merkle_tree::node_data::leaf::Leaf; |
| 7 | +use starknet_patricia_storage::db_object::{ |
| 8 | + DBObject, |
| 9 | + EmptyDeserializationContext, |
| 10 | + HasStaticPrefix, |
| 11 | +}; |
| 12 | +use starknet_patricia_storage::errors::{DeserializationError, SerializationError}; |
| 13 | +use starknet_patricia_storage::storage_trait::{DbKeyPrefix, DbValue}; |
| 14 | +use starknet_types_core::felt::Felt; |
| 15 | + |
| 16 | +use crate::block_committer::input::StarknetStorageValue; |
| 17 | +use crate::patricia_merkle_tree::leaf::leaf_impl::ContractState; |
| 18 | +use crate::patricia_merkle_tree::types::CompiledClassHash; |
| 19 | + |
| 20 | +// Wrap the leaves types so that we can implement the [DBObject] trait differently in index |
| 21 | +// layout. |
| 22 | +#[derive(Clone, Debug, Default, Eq, PartialEq, derive_more::AsRef, derive_more::From)] |
| 23 | +pub struct IndexLayoutContractState(pub ContractState); |
| 24 | + |
| 25 | +#[derive(Clone, Debug, Default, Eq, PartialEq, derive_more::AsRef, derive_more::From)] |
| 26 | +pub struct IndexLayoutCompiledClassHash(pub CompiledClassHash); |
| 27 | + |
| 28 | +#[derive(Clone, Debug, Default, Eq, PartialEq, derive_more::From)] |
| 29 | +pub struct IndexLayoutStarknetStorageValue(pub StarknetStorageValue); |
| 30 | + |
| 31 | +/// Set to 2^251 + 1 to avoid collisions with contract addresses prefixes. |
| 32 | +static FIRST_AVAILABLE_PREFIX_FELT: LazyLock<Felt> = |
| 33 | + LazyLock::new(|| Felt::from_hex_unchecked(PATRICIA_KEY_UPPER_BOUND) + Felt::ONE); |
| 34 | + |
| 35 | +/// The db key prefix of nodes in the contracts trie. |
| 36 | +/// |
| 37 | +/// Set to FIRST_AVAILABLE_PREFIX_FELT |
| 38 | +static CONTRACTS_TREE_PREFIX: LazyLock<[u8; 32]> = |
| 39 | + LazyLock::new(|| FIRST_AVAILABLE_PREFIX_FELT.to_bytes_be()); |
| 40 | + |
| 41 | +/// The db key prefix of nodes in the contracts trie. |
| 42 | +/// |
| 43 | +/// Set to FIRST_AVAILABLE_PREFIX_FELT + 1 |
| 44 | +static CLASSES_TREE_PREFIX: LazyLock<[u8; 32]> = |
| 45 | + LazyLock::new(|| (*FIRST_AVAILABLE_PREFIX_FELT + Felt::ONE).to_bytes_be()); |
| 46 | + |
| 47 | +// TODO(Ariel): Delete this enum and use `CommitmentType` instead. |
| 48 | +#[derive(Debug, PartialEq)] |
| 49 | +pub enum TrieType { |
| 50 | + ContractsTrie, |
| 51 | + ClassesTrie, |
| 52 | + StorageTrie(ContractAddress), |
| 53 | +} |
| 54 | + |
| 55 | +impl TrieType { |
| 56 | + fn db_prefix(&self) -> DbKeyPrefix { |
| 57 | + match self { |
| 58 | + Self::ContractsTrie => DbKeyPrefix::new((&CONTRACTS_TREE_PREFIX[..]).into()), |
| 59 | + Self::ClassesTrie => DbKeyPrefix::new((&CLASSES_TREE_PREFIX[..]).into()), |
| 60 | + Self::StorageTrie(contract_address) => { |
| 61 | + let prefix = contract_address.to_bytes_be().to_vec(); |
| 62 | + DbKeyPrefix::new(prefix.into()) |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +macro_rules! impl_has_static_prefix_for_index_layouts { |
| 69 | + ($($ty:ty),* $(,)?) => { |
| 70 | + $( |
| 71 | + impl HasStaticPrefix for $ty { |
| 72 | + type KeyContext = TrieType; |
| 73 | + fn get_static_prefix(key_context: &Self::KeyContext) -> DbKeyPrefix { |
| 74 | + key_context.db_prefix() |
| 75 | + } |
| 76 | + } |
| 77 | + )* |
| 78 | + }; |
| 79 | +} |
| 80 | + |
| 81 | +impl_has_static_prefix_for_index_layouts! { |
| 82 | + IndexLayoutContractState, |
| 83 | + IndexLayoutCompiledClassHash, |
| 84 | + IndexLayoutStarknetStorageValue, |
| 85 | +} |
| 86 | + |
| 87 | +macro_rules! impl_leaf_for_wrappers { |
| 88 | + ($($wrapper:ty => $inner:ty),+ $(,)?) => { |
| 89 | + $( |
| 90 | + impl Leaf for $wrapper { |
| 91 | + type Input = <$inner as Leaf>::Input; |
| 92 | + type Output = <$inner as Leaf>::Output; |
| 93 | + |
| 94 | + fn is_empty(&self) -> bool { |
| 95 | + // assumes `pub struct Wrapper(pub Inner);` |
| 96 | + self.0.is_empty() |
| 97 | + } |
| 98 | + |
| 99 | + async fn create( |
| 100 | + input: Self::Input, |
| 101 | + ) -> LeafResult<(Self, Self::Output)> { |
| 102 | + let (created_leaf, output) = <$inner as Leaf>::create(input).await?; |
| 103 | + Ok((Self(created_leaf), output)) |
| 104 | + } |
| 105 | + } |
| 106 | + )+ |
| 107 | + }; |
| 108 | +} |
| 109 | + |
| 110 | +impl_leaf_for_wrappers!( |
| 111 | + IndexLayoutContractState => ContractState, |
| 112 | + IndexLayoutStarknetStorageValue => StarknetStorageValue, |
| 113 | + IndexLayoutCompiledClassHash => CompiledClassHash, |
| 114 | +); |
| 115 | + |
| 116 | +impl DBObject for IndexLayoutContractState { |
| 117 | + type DeserializeContext = EmptyDeserializationContext; |
| 118 | + fn serialize(&self) -> Result<DbValue, SerializationError> { |
| 119 | + serialize_felts(&[self.0.class_hash.0, self.0.storage_root_hash.0, self.0.nonce.0]) |
| 120 | + } |
| 121 | + |
| 122 | + fn deserialize( |
| 123 | + value: &DbValue, |
| 124 | + _deserialize_context: &Self::DeserializeContext, |
| 125 | + ) -> Result<Self, DeserializationError> { |
| 126 | + let mut cursor: &[u8] = &value.0; |
| 127 | + let err = || DeserializationError::FeltDeserialization(value.clone()); |
| 128 | + |
| 129 | + let class_hash = deserialize_felt(&mut cursor, err)?; |
| 130 | + let storage_root_hash = deserialize_felt(&mut cursor, err)?; |
| 131 | + let nonce = deserialize_felt(&mut cursor, err)?; |
| 132 | + |
| 133 | + Ok(Self(ContractState { |
| 134 | + class_hash: ClassHash(class_hash), |
| 135 | + storage_root_hash: HashOutput(storage_root_hash), |
| 136 | + nonce: Nonce(nonce), |
| 137 | + })) |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +impl DBObject for IndexLayoutCompiledClassHash { |
| 142 | + type DeserializeContext = EmptyDeserializationContext; |
| 143 | + |
| 144 | + fn serialize(&self) -> Result<DbValue, SerializationError> { |
| 145 | + serialize_felts(&[self.0.0]) |
| 146 | + } |
| 147 | + |
| 148 | + fn deserialize( |
| 149 | + value: &DbValue, |
| 150 | + _deserialize_context: &Self::DeserializeContext, |
| 151 | + ) -> Result<Self, DeserializationError> { |
| 152 | + Ok(Self(CompiledClassHash(deserialize_felt(&mut &value.0[..], || { |
| 153 | + DeserializationError::FeltDeserialization(value.clone()) |
| 154 | + })?))) |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +impl DBObject for IndexLayoutStarknetStorageValue { |
| 159 | + type DeserializeContext = EmptyDeserializationContext; |
| 160 | + |
| 161 | + fn serialize(&self) -> Result<DbValue, SerializationError> { |
| 162 | + serialize_felts(&[self.0.0]) |
| 163 | + } |
| 164 | + |
| 165 | + fn deserialize( |
| 166 | + value: &DbValue, |
| 167 | + _deserialize_context: &Self::DeserializeContext, |
| 168 | + ) -> Result<Self, DeserializationError> { |
| 169 | + Ok(Self(StarknetStorageValue(deserialize_felt(&mut &value.0[..], || { |
| 170 | + DeserializationError::FeltDeserialization(value.clone()) |
| 171 | + })?))) |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +fn deserialize_felt( |
| 176 | + cursor: &mut &[u8], |
| 177 | + mk_err: impl Fn() -> DeserializationError, |
| 178 | +) -> Result<Felt, DeserializationError> { |
| 179 | + Felt::deserialize(cursor).ok_or_else(mk_err) |
| 180 | +} |
| 181 | + |
| 182 | +fn serialize_felts(felts: &[Felt]) -> Result<DbValue, SerializationError> { |
| 183 | + let mut buffer = Vec::new(); |
| 184 | + for felt in felts { |
| 185 | + felt.serialize(&mut buffer)?; |
| 186 | + } |
| 187 | + Ok(DbValue(buffer)) |
| 188 | +} |
0 commit comments