Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

dorimedini-starkware
Copy link
Collaborator

No description provided.

@reviewable-StarkWare
Copy link

This change is Reviewable

Copy link
Collaborator Author

@dorimedini-starkware dorimedini-starkware left a 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:

  1. Define a StoragePrefix trait, that can output the prefix as bytes.
  2. The DBObject get_prefix method should return an impl StoragePrefix
  3. The patricia (generic nodes) and committer (specific leaves) prefixes (PatriciaPrefix and CommitterLeafPrefix, 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.
  4. In general, the get_prefix method requires self - for example, the FilledNode<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 a storage_prefix() -> impl StoragePrefix associated method on the Leaf trait, and use it when implementing DBObject for leaf types.

@dorimedini-starkware dorimedini-starkware force-pushed the 02-07-feat_starknet_patricia_starknet_committer_abstract_storage_prefix_trait branch from 61e7128 to 484f684 Compare February 8, 2025 11:11
Copy link
Collaborator Author

@dorimedini-starkware dorimedini-starkware left a 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:

  1. Define a StoragePrefix trait, that can output the prefix as bytes.
  2. The DBObject get_prefix method should return an impl StoragePrefix
  3. The patricia (generic nodes) and committer (specific leaves) prefixes (PatriciaPrefix and CommitterLeafPrefix, 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.
  4. In general, the get_prefix method requires self - for example, the FilledNode<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 a storage_prefix() -> impl StoragePrefix associated method on the Leaf trait, and use it when implementing DBObject for leaf types.

Correction: StoragePrefix is a struct, not a trait; implemented From<X> for StoragePrefix for the specific prefix enums in (3).

Copy link
Contributor

@AvivYossef-starkware AvivYossef-starkware left a 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: :shipit: complete! all files reviewed, all discussions resolved (waiting on @TzahiTaub)

Copy link
Collaborator Author

@dorimedini-starkware dorimedini-starkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: :shipit: 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)
    }
}

Copy link
Collaborator Author

@dorimedini-starkware dorimedini-starkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: :shipit: 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 the get_prefix method of DBObject.
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants