-
Notifications
You must be signed in to change notification settings - Fork 2.9k
fix(storage): custom impl of EnumCount for MerkleizedColumn #2875
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
Changes from 7 commits
2920565
50ad256
284d9ad
f7de919
70b46eb
5231d6e
9f62a7a
aa438a2
4ccf140
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,7 +19,6 @@ use alloc::{ | |
| Eq, | ||
| Hash, | ||
| enum_iterator::Sequence, | ||
| strum_macros::EnumCount, | ||
| strum_macros::IntoStaticStr, | ||
| )] | ||
| pub enum MerkleizedColumn<TC> { | ||
|
|
@@ -31,6 +30,16 @@ pub enum MerkleizedColumn<TC> { | |
| MerkleMetadataColumn, | ||
| } | ||
|
|
||
| impl<TC> strum::EnumCount for MerkleizedColumn<TC> | ||
| where | ||
| TC: strum::EnumCount + AsU32, | ||
| { | ||
| /// The total count of variants in the enum. | ||
| /// Since we have two columns for each table column and one for the merkle data, | ||
| /// we have to multiply the count of the table columns by 2 and add one for the merkle metadata. | ||
| const COUNT: usize = TC::COUNT * 2 + 1; | ||
| } | ||
|
|
||
| /// The trait to convert the column to the `u32`. | ||
| pub trait AsU32 { | ||
| /// Returns the `u32` representation of the `Column`. | ||
|
|
@@ -42,21 +51,30 @@ where | |
| TC: strum::EnumCount + AsU32, | ||
| { | ||
| /// The total count of variants in the enum. | ||
| /// Since we have two columns for each table column and one for the merkle data, | ||
| /// we have to multiply the count of the table columns by 2 and add one for the merkle metadata. | ||
| pub const COUNT: usize = TC::COUNT * 2 + 1; | ||
| pub const COUNT: usize = <Self as strum::EnumCount>::COUNT; | ||
|
|
||
| /// The start of the merkle data columns. | ||
| pub const MERKLE_DATA_COLUMNS_START: u32 = u16::MAX as u32; | ||
|
|
||
| /// The merkle metadata column | ||
| pub const MERKLE_METADATA_COLUMN: u32 = { | ||
| assert!(Self::COUNT <= u32::MAX as usize); | ||
| // this is fine, because we already performed an assertion above | ||
| // see https://github.com/rust-lang/rust-clippy/issues/9613 | ||
| #[allow(clippy::cast_possible_truncation)] | ||
| let column_index = (Self::COUNT as u32).saturating_sub(1); | ||
| assert!(column_index > 0); | ||
| column_index | ||
| }; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you do it like this, it means that column id depends on the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. addressed in aa438a2 |
||
|
|
||
| /// Returns the `u32` representation of the `Column`. | ||
| pub fn as_u32(&self) -> u32 { | ||
| match self { | ||
| Self::TableColumn(column) => column.as_u32(), | ||
| Self::MerkleDataColumn(column) => { | ||
| Self::MERKLE_DATA_COLUMNS_START.wrapping_add(column.as_u32()) | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we need to fix this one as well. Seems like the in-memory database implementation assumes these values are contiguous.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nop, we don't need to fix these. the in memory db creates the columns based on the total count. anything in between is fine
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But |
||
| Self::MerkleMetadataColumn => u32::MAX, | ||
| Self::MerkleMetadataColumn => Self::MERKLE_METADATA_COLUMN, | ||
| } | ||
| } | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.