From 29205652b7cdf0c97ba22fb1d9999de9011d5377 Mon Sep 17 00:00:00 2001 From: rymnc <43716372+rymnc@users.noreply.github.com> Date: Mon, 17 Mar 2025 18:38:43 +0530 Subject: [PATCH 1/3] fix(storage): custom impl of EnumCount for MerkleizedColumn --- crates/storage/src/merkle/column.rs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/crates/storage/src/merkle/column.rs b/crates/storage/src/merkle/column.rs index 222df15a25b..4fcd59238d7 100644 --- a/crates/storage/src/merkle/column.rs +++ b/crates/storage/src/merkle/column.rs @@ -19,7 +19,6 @@ use alloc::{ Eq, Hash, enum_iterator::Sequence, - strum_macros::EnumCount, strum_macros::IntoStaticStr, )] pub enum MerkleizedColumn { @@ -31,6 +30,16 @@ pub enum MerkleizedColumn { MerkleMetadataColumn, } +impl strum::EnumCount for MerkleizedColumn +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,9 +51,7 @@ 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 = ::COUNT; /// The start of the merkle data columns. pub const MERKLE_DATA_COLUMNS_START: u32 = u16::MAX as u32; @@ -56,7 +63,9 @@ where Self::MerkleDataColumn(column) => { Self::MERKLE_DATA_COLUMNS_START.wrapping_add(column.as_u32()) } - Self::MerkleMetadataColumn => u32::MAX, + Self::MerkleMetadataColumn => { + Self::COUNT.checked_sub(1).unwrap().try_into().unwrap() + } } } } From 50ad25626ab02973d33fa025cf9cc830e36c363e Mon Sep 17 00:00:00 2001 From: rymnc <43716372+rymnc@users.noreply.github.com> Date: Tue, 18 Mar 2025 14:46:07 +0530 Subject: [PATCH 2/3] fix: const time eval --- crates/storage/src/merkle/column.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/crates/storage/src/merkle/column.rs b/crates/storage/src/merkle/column.rs index 4fcd59238d7..a9247c3d755 100644 --- a/crates/storage/src/merkle/column.rs +++ b/crates/storage/src/merkle/column.rs @@ -56,6 +56,17 @@ where /// 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 + }; + /// Returns the `u32` representation of the `Column`. pub fn as_u32(&self) -> u32 { match self { @@ -63,9 +74,7 @@ where Self::MerkleDataColumn(column) => { Self::MERKLE_DATA_COLUMNS_START.wrapping_add(column.as_u32()) } - Self::MerkleMetadataColumn => { - Self::COUNT.checked_sub(1).unwrap().try_into().unwrap() - } + Self::MerkleMetadataColumn => Self::MERKLE_METADATA_COLUMN, } } } From aa438a22953637afdcddd1e3c0a720703948697d Mon Sep 17 00:00:00 2001 From: rymnc <43716372+rymnc@users.noreply.github.com> Date: Wed, 19 Mar 2025 04:56:17 +0530 Subject: [PATCH 3/3] fix: set merkle metadata to start of count --- crates/storage/src/merkle/column.rs | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/crates/storage/src/merkle/column.rs b/crates/storage/src/merkle/column.rs index a9247c3d755..0faa56c6d30 100644 --- a/crates/storage/src/merkle/column.rs +++ b/crates/storage/src/merkle/column.rs @@ -57,23 +57,21 @@ where 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 - }; + // we set it to 0, since any future column updates will be added after this column + pub const MERKLE_METADATA_COLUMN: u32 = 0; + + #[inline] + fn with_metadata_offset(column: u32) -> u32 { + column.wrapping_add(Self::MERKLE_METADATA_COLUMN + 1) + } /// 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()) - } + Self::TableColumn(column) => Self::with_metadata_offset(column.as_u32()), + Self::MerkleDataColumn(column) => Self::with_metadata_offset( + Self::MERKLE_DATA_COLUMNS_START.wrapping_add(column.as_u32()), + ), Self::MerkleMetadataColumn => Self::MERKLE_METADATA_COLUMN, } }