Skip to content

Commit

Permalink
feat: add StaticFileBlockWithdrawals to db-model (#13894)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshieDo authored Jan 21, 2025
1 parent 3317ea1 commit 0a5de2f
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 10 deletions.
6 changes: 5 additions & 1 deletion crates/cli/commands/src/test_vectors/compact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ use reth_codecs::alloy::{
withdrawal::Withdrawal,
};
use reth_db::{
models::{AccountBeforeTx, StoredBlockBodyIndices, StoredBlockOmmers, StoredBlockWithdrawals},
models::{
AccountBeforeTx, StaticFileBlockWithdrawals, StoredBlockBodyIndices, StoredBlockOmmers,
StoredBlockWithdrawals,
},
ClientVersion,
};
use reth_fs_util as fs;
Expand Down Expand Up @@ -110,6 +113,7 @@ compact_types!(
StoredBlockOmmers,
StoredBlockBodyIndices,
StoredBlockWithdrawals,
StaticFileBlockWithdrawals,
// Manual implementations
TransactionSigned,
// Bytecode, // todo revm arbitrary
Expand Down
4 changes: 3 additions & 1 deletion crates/storage/db-api/src/models/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ pub use accounts::*;
pub use blocks::*;
pub use integer_list::IntegerList;
pub use reth_db_models::{
AccountBeforeTx, ClientVersion, StoredBlockBodyIndices, StoredBlockWithdrawals,
blocks::StaticFileBlockWithdrawals, AccountBeforeTx, ClientVersion, StoredBlockBodyIndices,
StoredBlockWithdrawals,
};
pub use sharded_key::ShardedKey;

Expand Down Expand Up @@ -224,6 +225,7 @@ impl_compression_for_compact!(
StoredBlockBodyIndices,
StoredBlockOmmers<H>,
StoredBlockWithdrawals,
StaticFileBlockWithdrawals,
Bytecode,
AccountBeforeTx,
TransactionSigned,
Expand Down
35 changes: 33 additions & 2 deletions crates/storage/db-models/src/blocks.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::ops::Range;

use alloy_eips::eip4895::Withdrawals;
use alloy_primitives::TxNumber;
use bytes::Buf;
use reth_codecs::{add_arbitrary_tests, Compact};
use serde::{Deserialize, Serialize};
use std::ops::Range;

/// Total number of transactions.
pub type NumTransactions = u64;
Expand Down Expand Up @@ -76,6 +76,37 @@ pub struct StoredBlockWithdrawals {
pub withdrawals: Withdrawals,
}

/// A storage representation of block withdrawals that is static file friendly. An inner `None`
/// represents a pre-merge block.
#[derive(Debug, Default, Eq, PartialEq, Clone, Serialize, Deserialize)]
#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
#[add_arbitrary_tests(compact)]
pub struct StaticFileBlockWithdrawals {
/// The block withdrawals. A `None` value represents a pre-merge block.
pub withdrawals: Option<Withdrawals>,
}

impl Compact for StaticFileBlockWithdrawals {
fn to_compact<B>(&self, buf: &mut B) -> usize
where
B: bytes::BufMut + AsMut<[u8]>,
{
buf.put_u8(self.withdrawals.is_some() as u8);
if let Some(withdrawals) = &self.withdrawals {
return 1 + withdrawals.to_compact(buf);
}
1
}
fn from_compact(mut buf: &[u8], _: usize) -> (Self, &[u8]) {
if buf.get_u8() == 1 {
let (w, buf) = Withdrawals::from_compact(buf, buf.len());
(Self { withdrawals: Some(w) }, buf)
} else {
(Self { withdrawals: None }, buf)
}
}
}

#[cfg(test)]
mod tests {
use crate::StoredBlockBodyIndices;
Expand Down
2 changes: 1 addition & 1 deletion crates/storage/db-models/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub use accounts::AccountBeforeTx;

/// Blocks
pub mod blocks;
pub use blocks::{StoredBlockBodyIndices, StoredBlockWithdrawals};
pub use blocks::{StaticFileBlockWithdrawals, StoredBlockBodyIndices, StoredBlockWithdrawals};

/// Client Version
pub mod client_version;
Expand Down
11 changes: 7 additions & 4 deletions crates/storage/db/src/static_file/masks.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use crate::{
add_static_file_mask,
static_file::mask::{ColumnSelectorOne, ColumnSelectorTwo},
BlockBodyIndices, BlockWithdrawals, HeaderTerminalDifficulties,
BlockBodyIndices, HeaderTerminalDifficulties,
};
use alloy_primitives::BlockHash;
use reth_db_api::{models::StoredBlockOmmers, table::Table};
use reth_db_api::{
models::{StaticFileBlockWithdrawals, StoredBlockOmmers},
table::Table,
};

// HEADER MASKS
add_static_file_mask! {
Expand Down Expand Up @@ -53,6 +56,6 @@ add_static_file_mask! {
OmmersMask<H>, StoredBlockOmmers<H>, 0b010
}
add_static_file_mask! {
#[doc = "Mask for a `StoredBlockWithdrawals` from BlockMeta static file segment"]
WithdrawalsMask, <BlockWithdrawals as Table>::Value, 0b100
#[doc = "Mask for a `StaticFileBlockWithdrawals` from BlockMeta static file segment"]
WithdrawalsMask, StaticFileBlockWithdrawals, 0b100
}
5 changes: 4 additions & 1 deletion crates/storage/provider/src/providers/static_file/jar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,10 @@ impl<N: NodePrimitives> WithdrawalsProvider for StaticFileJarProvider<'_, N> {
_: u64,
) -> ProviderResult<Option<Withdrawals>> {
if let Some(num) = id.as_number() {
return Ok(self.cursor()?.get_one::<WithdrawalsMask>(num.into())?.map(|s| s.withdrawals))
return Ok(self
.cursor()?
.get_one::<WithdrawalsMask>(num.into())?
.and_then(|s| s.withdrawals))
}
// Only accepts block number queries
Err(ProviderError::UnsupportedProvider)
Expand Down

0 comments on commit 0a5de2f

Please sign in to comment.