-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(starknet_patricia,starknet_committer): abstract storage prefix trait #4019
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: 0 of 9 files reviewed, all discussions resolved
a discussion (no related file):
Basic idea:
- Define a
StoragePrefix
trait, that can output the prefix as bytes. - The DBObject
get_prefix
method should return animpl StoragePrefix
- The patricia (generic nodes) and committer (specific leaves) prefixes (
PatriciaPrefix
andCommitterLeafPrefix
, respectively) should not be defined in the storage area: the patricia crate defines a generic prefix enum (generic in the leaf prefix), and the committer defines the specific leaf prefixes enum. - In general, the
get_prefix
method requiresself
- for example, theFilledNode<L>
prefix depends on the variant of the enum (inner node or leaf). Leaves, on the other hand, do not require an instance - so we define astorage_prefix() -> impl StoragePrefix
associated method on theLeaf
trait, and use it when implementingDBObject
for leaf types.
61e7128
to
484f684
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: 0 of 9 files reviewed, all discussions resolved
a discussion (no related file):
Previously, dorimedini-starkware wrote…
Basic idea:
- Define a
StoragePrefix
trait, that can output the prefix as bytes.- The DBObject
get_prefix
method should return animpl StoragePrefix
- The patricia (generic nodes) and committer (specific leaves) prefixes (
PatriciaPrefix
andCommitterLeafPrefix
, respectively) should not be defined in the storage area: the patricia crate defines a generic prefix enum (generic in the leaf prefix), and the committer defines the specific leaf prefixes enum.- In general, the
get_prefix
method requiresself
- for example, theFilledNode<L>
prefix depends on the variant of the enum (inner node or leaf). Leaves, on the other hand, do not require an instance - so we define astorage_prefix() -> impl StoragePrefix
associated method on theLeaf
trait, and use it when implementingDBObject
for leaf types.
Correction: StoragePrefix
is a struct, not a trait; implemented From<X> for StoragePrefix
for the specific prefix enums in (3).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed 9 of 9 files at r2, all commit messages.
Reviewable status:complete! all files reviewed, all discussions resolved (waiting on @TzahiTaub)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status:
complete! all files reviewed, all discussions resolved (waiting on @TzahiTaub)
crates/starknet_patricia/src/patricia_merkle_tree/node_data/leaf.rs
line 20 at r2 (raw file):
type Output: Send + Debug + 'static; fn storage_prefix() -> StoragePrefix;
occasionally I have a type parameter L
available (impl<L: Leaf>
blocks), but not an instance of a leaf, and I need a prefix. hence, this associated function is required, in addition to the get_prefix
method of DBObject
.
get_prefix
is still required, because sometimes a instance is required to determine the prefix
Code quote:
fn storage_prefix() -> StoragePrefix;
crates/starknet_patricia/src/storage/db_object.rs
line 15 at r2 (raw file):
create_db_key(self.get_prefix(), suffix) } }
annoying issue and some code dup...
what I would like to do is
pub trait HasDbPrefix {
/// Returns the storage key prefix of the DB object.
fn get_prefix(&self) -> StoragePrefix;
}
pub trait DBObject: HasDbPrefix { .. }
and then auto-impl on leaves:
impl<L: Leaf> HasDbPrefix for L {
fn get_prefix(&self) -> StoragePrefix {
Self::storage_prefix()
}
}
however, we can't do that when the HasDbPrefix
trait is not in the same crate as Leaf
Code quote:
pub trait DBObject {
/// Serializes the given value.
fn serialize(&self) -> StorageValue;
/// Returns the storage key prefix of the DB object.
fn get_prefix(&self) -> StoragePrefix;
/// Returns a `StorageKey` from a prefix and a suffix.
fn get_db_key(&self, suffix: &[u8]) -> StorageKey {
create_db_key(self.get_prefix(), suffix)
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status:
complete! all files reviewed, all discussions resolved (waiting on @TzahiTaub)
crates/starknet_patricia/src/patricia_merkle_tree/node_data/leaf.rs
line 20 at r2 (raw file):
Previously, dorimedini-starkware wrote…
occasionally I have a type parameter
L
available (impl<L: Leaf>
blocks), but not an instance of a leaf, and I need a prefix. hence, this associated function is required, in addition to theget_prefix
method ofDBObject
.
get_prefix
is still required, because sometimes a instance is required to determine the prefix
this is later removed here, but for simplicity of this PR (and simplicity of rebasing a PR with file movements up the stack...) I'll keep it as is here
No description provided.