diff --git a/crates/cli/commands/src/test_vectors/tables.rs b/crates/cli/commands/src/test_vectors/tables.rs index fd7d3b3799d8..acb811b75dfc 100644 --- a/crates/cli/commands/src/test_vectors/tables.rs +++ b/crates/cli/commands/src/test_vectors/tables.rs @@ -72,7 +72,7 @@ pub fn generate_vectors(mut tables: Vec) -> Result<()> { (HeaderNumbers, PER_TABLE, TABLE), (Headers
, PER_TABLE, TABLE), (BlockBodyIndices, PER_TABLE, TABLE), - (BlockOmmers, 100, TABLE), + (BlockOmmers
, 100, TABLE), (TransactionHashNumbers, PER_TABLE, TABLE), (Transactions, 100, TABLE), (PlainStorageState, PER_TABLE, DUPSORT), diff --git a/crates/optimism/storage/src/lib.rs b/crates/optimism/storage/src/lib.rs index 391f26093ba6..0db8f4e20a9d 100644 --- a/crates/optimism/storage/src/lib.rs +++ b/crates/optimism/storage/src/lib.rs @@ -13,7 +13,7 @@ mod tests { use reth_codecs::{test_utils::UnusedBits, validate_bitflag_backwards_compat}; use reth_db_api::models::{ - CompactClientVersion, CompactU256, CompactU64, StoredBlockBodyIndices, StoredBlockOmmers, + CompactClientVersion, CompactU256, CompactU64, StoredBlockBodyIndices, StoredBlockWithdrawals, }; use reth_primitives::{Account, Receipt}; @@ -43,7 +43,6 @@ mod tests { assert_eq!(StageCheckpoint::bitflag_encoded_bytes(), 1); assert_eq!(StageUnitCheckpoint::bitflag_encoded_bytes(), 1); assert_eq!(StoredBlockBodyIndices::bitflag_encoded_bytes(), 1); - assert_eq!(StoredBlockOmmers::bitflag_encoded_bytes(), 0); assert_eq!(StoredBlockWithdrawals::bitflag_encoded_bytes(), 0); assert_eq!(StorageHashingCheckpoint::bitflag_encoded_bytes(), 1); @@ -67,7 +66,6 @@ mod tests { validate_bitflag_backwards_compat!(StageCheckpoint, UnusedBits::NotZero); validate_bitflag_backwards_compat!(StageUnitCheckpoint, UnusedBits::Zero); validate_bitflag_backwards_compat!(StoredBlockBodyIndices, UnusedBits::Zero); - validate_bitflag_backwards_compat!(StoredBlockOmmers, UnusedBits::Zero); validate_bitflag_backwards_compat!(StoredBlockWithdrawals, UnusedBits::Zero); validate_bitflag_backwards_compat!(StorageHashingCheckpoint, UnusedBits::NotZero); } diff --git a/crates/storage/db-api/src/models/blocks.rs b/crates/storage/db-api/src/models/blocks.rs index 0145ceb52b5b..7c4b37b254db 100644 --- a/crates/storage/db-api/src/models/blocks.rs +++ b/crates/storage/db-api/src/models/blocks.rs @@ -8,12 +8,30 @@ use serde::{Deserialize, Serialize}; /// The storage representation of a block's ommers. /// /// It is stored as the headers of the block's uncles. -#[derive(Debug, Default, Eq, PartialEq, Clone, Serialize, Deserialize, Compact)] +#[derive(Debug, Default, Eq, PartialEq, Clone, Serialize, Deserialize)] #[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))] #[add_arbitrary_tests(compact)] -pub struct StoredBlockOmmers { +pub struct StoredBlockOmmers { /// The block headers of this block's uncles. - pub ommers: Vec
, + pub ommers: Vec, +} + +impl Compact for StoredBlockOmmers { + fn to_compact(&self, buf: &mut B) -> usize + where + B: bytes::BufMut + AsMut<[u8]>, + { + let mut buffer = bytes::BytesMut::new(); + self.ommers.to_compact(&mut buffer); + let total_length = buffer.len(); + buf.put(buffer); + total_length + } + + fn from_compact(buf: &[u8], _len: usize) -> (Self, &[u8]) { + let (ommers, new_buf) = Vec::from_compact(buf, buf.len()); + (Self { ommers }, new_buf) + } } /// Hash of the block header. @@ -31,4 +49,18 @@ mod tests { ommer.ommers.push(Header::default()); assert_eq!(ommer.clone(), StoredBlockOmmers::decompress(&ommer.compress()).unwrap()); } + + #[test] + fn fuzz_stored_block_ommers() { + fuzz_test_stored_block_ommers(StoredBlockOmmers::default()) + } + + #[test_fuzz::test_fuzz] + fn fuzz_test_stored_block_ommers(obj: StoredBlockOmmers) { + use reth_codecs::Compact; + let mut buf = vec![]; + let len = obj.to_compact(&mut buf); + let (same_obj, _) = StoredBlockOmmers::from_compact(buf.as_ref(), len); + assert_eq!(obj, same_obj); + } } diff --git a/crates/storage/db-api/src/models/mod.rs b/crates/storage/db-api/src/models/mod.rs index 5d18711922ed..614dc598bdb7 100644 --- a/crates/storage/db-api/src/models/mod.rs +++ b/crates/storage/db-api/src/models/mod.rs @@ -189,9 +189,9 @@ impl Decode for ClientVersion { /// Implements compression for Compact type. macro_rules! impl_compression_for_compact { - ($($name:tt),+) => { + ($($name:ident$(<$($generic:ident),*>)?),+) => { $( - impl Compress for $name { + impl$(<$($generic: core::fmt::Debug + Send + Sync + Compact),*>)? Compress for $name$(<$($generic),*>)? { type Compressed = Vec; fn compress_to_buf>(self, buf: &mut B) { @@ -199,8 +199,8 @@ macro_rules! impl_compression_for_compact { } } - impl Decompress for $name { - fn decompress(value: &[u8]) -> Result<$name, $crate::DatabaseError> { + impl$(<$($generic: core::fmt::Debug + Send + Sync + Compact),*>)? Decompress for $name$(<$($generic),*>)? { + fn decompress(value: &[u8]) -> Result<$name$(<$($generic),*>)?, $crate::DatabaseError> { let (obj, _) = Compact::from_compact(value, value.len()); Ok(obj) } @@ -222,7 +222,7 @@ impl_compression_for_compact!( StoredNibblesSubKey, StorageTrieEntry, StoredBlockBodyIndices, - StoredBlockOmmers, + StoredBlockOmmers, StoredBlockWithdrawals, Bytecode, AccountBeforeTx, @@ -339,7 +339,6 @@ mod tests { assert_eq!(StageCheckpoint::bitflag_encoded_bytes(), 1); assert_eq!(StageUnitCheckpoint::bitflag_encoded_bytes(), 1); assert_eq!(StoredBlockBodyIndices::bitflag_encoded_bytes(), 1); - assert_eq!(StoredBlockOmmers::bitflag_encoded_bytes(), 0); assert_eq!(StoredBlockWithdrawals::bitflag_encoded_bytes(), 0); assert_eq!(StorageHashingCheckpoint::bitflag_encoded_bytes(), 1); @@ -360,7 +359,6 @@ mod tests { validate_bitflag_backwards_compat!(StageCheckpoint, UnusedBits::NotZero); validate_bitflag_backwards_compat!(StageUnitCheckpoint, UnusedBits::Zero); validate_bitflag_backwards_compat!(StoredBlockBodyIndices, UnusedBits::Zero); - validate_bitflag_backwards_compat!(StoredBlockOmmers, UnusedBits::Zero); validate_bitflag_backwards_compat!(StoredBlockWithdrawals, UnusedBits::Zero); validate_bitflag_backwards_compat!(StorageHashingCheckpoint, UnusedBits::NotZero); } diff --git a/crates/storage/db/src/tables/mod.rs b/crates/storage/db/src/tables/mod.rs index a1fea62f0d8b..940bb3aa2596 100644 --- a/crates/storage/db/src/tables/mod.rs +++ b/crates/storage/db/src/tables/mod.rs @@ -139,6 +139,7 @@ macro_rules! tables { impl$(<$($generic),*>)? reth_db_api::table::Table for $name$(<$($generic),*>)? where $value: reth_db_api::table::Value + 'static + $($(,$generic: Send + Sync)*)? { const NAME: &'static str = table_names::$name; const DUPSORT: bool = tables!(@bool $($subkey)?); @@ -314,9 +315,9 @@ tables! { } /// Stores the uncles/ommers of the block. - table BlockOmmers { + table BlockOmmers { type Key = BlockNumber; - type Value = StoredBlockOmmers; + type Value = StoredBlockOmmers; } /// Stores the block withdrawals.