Skip to content

Commit

Permalink
feat: make ommers table generic over header (#13038)
Browse files Browse the repository at this point in the history
  • Loading branch information
klkvr authored Nov 30, 2024
1 parent ebd413f commit 4634625
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 16 deletions.
2 changes: 1 addition & 1 deletion crates/cli/commands/src/test_vectors/tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub fn generate_vectors(mut tables: Vec<String>) -> Result<()> {
(HeaderNumbers, PER_TABLE, TABLE),
(Headers<Header>, PER_TABLE, TABLE),
(BlockBodyIndices, PER_TABLE, TABLE),
(BlockOmmers, 100, TABLE),
(BlockOmmers<Header>, 100, TABLE),
(TransactionHashNumbers, PER_TABLE, TABLE),
(Transactions<TransactionSignedNoHash>, 100, TABLE),
(PlainStorageState, PER_TABLE, DUPSORT),
Expand Down
4 changes: 1 addition & 3 deletions crates/optimism/storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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);

Expand All @@ -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);
}
Expand Down
38 changes: 35 additions & 3 deletions crates/storage/db-api/src/models/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<H = Header> {
/// The block headers of this block's uncles.
pub ommers: Vec<Header>,
pub ommers: Vec<H>,
}

impl<H: Compact> Compact for StoredBlockOmmers<H> {
fn to_compact<B>(&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.
Expand All @@ -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);
}
}
12 changes: 5 additions & 7 deletions crates/storage/db-api/src/models/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,18 +189,18 @@ 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<u8>;

fn compress_to_buf<B: bytes::BufMut + AsMut<[u8]>>(self, buf: &mut B) {
let _ = Compact::to_compact(&self, buf);
}
}

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)
}
Expand All @@ -222,7 +222,7 @@ impl_compression_for_compact!(
StoredNibblesSubKey,
StorageTrieEntry,
StoredBlockBodyIndices,
StoredBlockOmmers,
StoredBlockOmmers<H>,
StoredBlockWithdrawals,
Bytecode,
AccountBeforeTx,
Expand Down Expand Up @@ -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);

Expand All @@ -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);
}
Expand Down
5 changes: 3 additions & 2 deletions crates/storage/db/src/tables/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?);
Expand Down Expand Up @@ -314,9 +315,9 @@ tables! {
}

/// Stores the uncles/ommers of the block.
table BlockOmmers {
table BlockOmmers<H = Header> {
type Key = BlockNumber;
type Value = StoredBlockOmmers;
type Value = StoredBlockOmmers<H>;
}

/// Stores the block withdrawals.
Expand Down

0 comments on commit 4634625

Please sign in to comment.