Skip to content

Commit 125914b

Browse files
committed
starknet_committer,starknet_patricia,starknet_patricia_storage: support contract address prefix
1 parent 4b39ed1 commit 125914b

File tree

6 files changed

+18
-14
lines changed

6 files changed

+18
-14
lines changed

crates/starknet_committer/src/db/facts_db/db.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,8 @@ impl<S: Storage> ForestMetadata for FactsDb<S> {
223223
match metadata_type {
224224
ForestMetadataType::CommitmentOffset => DbKey(Self::COMMITMENT_OFFSET_KEY.to_vec()),
225225
ForestMetadataType::StateDiffHash(block_number) => {
226-
let state_diff_hash_key_prefix = DbKeyPrefix::new(Self::STATE_DIFF_HASH_PREFIX);
226+
let state_diff_hash_key_prefix =
227+
DbKeyPrefix::new(Self::STATE_DIFF_HASH_PREFIX.into());
227228
create_db_key(state_diff_hash_key_prefix, &block_number.0.to_be_bytes())
228229
}
229230
}

crates/starknet_committer/src/patricia_merkle_tree/leaf/leaf_serde.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ pub enum CommitterLeafPrefix {
2323
impl From<CommitterLeafPrefix> for DbKeyPrefix {
2424
fn from(value: CommitterLeafPrefix) -> Self {
2525
match value {
26-
CommitterLeafPrefix::StorageLeaf => Self::new(b"starknet_storage_leaf"),
27-
CommitterLeafPrefix::StateTreeLeaf => Self::new(b"contract_state"),
28-
CommitterLeafPrefix::CompiledClassLeaf => Self::new(b"contract_class_leaf"),
26+
CommitterLeafPrefix::StorageLeaf => Self::new(b"starknet_storage_leaf".into()),
27+
CommitterLeafPrefix::StateTreeLeaf => Self::new(b"contract_state".into()),
28+
CommitterLeafPrefix::CompiledClassLeaf => Self::new(b"contract_class_leaf".into()),
2929
}
3030
}
3131
}

crates/starknet_patricia/src/patricia_merkle_tree/external_test_utils.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,15 @@ use crate::patricia_merkle_tree::errors::TypesError;
2626
use crate::patricia_merkle_tree::node_data::errors::{LeafError, LeafResult};
2727
use crate::patricia_merkle_tree::original_skeleton_tree::config::OriginalSkeletonTreeConfig;
2828

29+
pub(crate) const TEST_PREFIX: &[u8] = &[0];
30+
2931
#[derive(Debug, PartialEq, Clone, Copy, Default, Eq)]
3032
pub struct MockLeaf(pub Felt);
3133

3234
impl HasStaticPrefix for MockLeaf {
3335
type KeyContext = EmptyKeyContext;
3436
fn get_static_prefix(_key_context: &Self::KeyContext) -> DbKeyPrefix {
35-
DbKeyPrefix::new(&[0])
37+
DbKeyPrefix::new(TEST_PREFIX.into())
3638
}
3739
}
3840

crates/starknet_patricia/src/patricia_merkle_tree/filled_tree/node_serde.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub enum PatriciaPrefix {
3838
impl From<PatriciaPrefix> for DbKeyPrefix {
3939
fn from(value: PatriciaPrefix) -> Self {
4040
match value {
41-
PatriciaPrefix::InnerNode => Self::new(b"patricia_node"),
41+
PatriciaPrefix::InnerNode => Self::new(b"patricia_node".into()),
4242
PatriciaPrefix::Leaf(prefix) => prefix,
4343
}
4444
}
@@ -60,10 +60,10 @@ impl<L: Leaf> HasDynamicPrefix for FilledNode<L, HashOutput> {
6060
// Inherit the KeyContext from the HasStaticPrefix implementation of the leaf.
6161
type KeyContext = <L as HasStaticPrefix>::KeyContext;
6262

63-
fn get_prefix(&self, _key_context: &Self::KeyContext) -> DbKeyPrefix {
63+
fn get_prefix(&self, key_context: &Self::KeyContext) -> DbKeyPrefix {
6464
match &self.data {
6565
NodeData::Binary(_) | NodeData::Edge(_) => PatriciaPrefix::InnerNode,
66-
NodeData::Leaf(_) => PatriciaPrefix::Leaf(L::get_static_prefix(_key_context)),
66+
NodeData::Leaf(_) => PatriciaPrefix::Leaf(L::get_static_prefix(key_context)),
6767
}
6868
.into()
6969
}

crates/starknet_patricia/src/patricia_merkle_tree/traversal_test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rstest::rstest;
44
use starknet_patricia_storage::db_object::HasStaticPrefix;
55
use starknet_patricia_storage::storage_trait::DbKeyPrefix;
66

7-
use crate::patricia_merkle_tree::external_test_utils::small_tree_index_to_full;
7+
use crate::patricia_merkle_tree::external_test_utils::{small_tree_index_to_full, TEST_PREFIX};
88
use crate::patricia_merkle_tree::node_data::inner_node::{EdgePath, EdgePathLength, PathToBottom};
99
use crate::patricia_merkle_tree::node_data::leaf::Leaf;
1010
use crate::patricia_merkle_tree::traversal::{SubTreeTrait, UnmodifiedChildTraversal};
@@ -49,7 +49,7 @@ impl<'a> SubTreeTrait<'a> for TestSubTree<'a> {
4949
_key_context: &<L as HasStaticPrefix>::KeyContext,
5050
) -> DbKeyPrefix {
5151
// Dummy prefix for testing purposes (we only need a prefix when interacting with storage).
52-
DbKeyPrefix::new(&[0])
52+
DbKeyPrefix::new(TEST_PREFIX.into())
5353
}
5454

5555
fn get_root_suffix(&self) -> Vec<u8> {

crates/starknet_patricia_storage/src/storage_trait.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::borrow::Cow;
12
use std::collections::HashMap;
23
use std::fmt::Display;
34
use std::future::Future;
@@ -172,15 +173,15 @@ impl Storage for NullStorage {
172173
}
173174

174175
#[derive(Debug)]
175-
pub struct DbKeyPrefix(&'static [u8]);
176+
pub struct DbKeyPrefix(Cow<'static, [u8]>);
176177

177178
impl DbKeyPrefix {
178-
pub fn new(prefix: &'static [u8]) -> Self {
179+
pub fn new(prefix: Cow<'static, [u8]>) -> Self {
179180
Self(prefix)
180181
}
181182

182-
pub fn to_bytes(&self) -> &'static [u8] {
183-
self.0
183+
pub fn to_bytes(&self) -> &[u8] {
184+
self.0.as_ref()
184185
}
185186
}
186187

0 commit comments

Comments
 (0)