Skip to content
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

feat: add tables_to_generic! macro to convert between table values and types #8518

Merged
merged 1 commit into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions bin/reth/src/commands/db/checksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ impl Command {
start_key: self.start_key,
end_key: self.end_key,
limit: self.limit,
})
})?;
Ok(())
}
}

Expand All @@ -58,8 +59,12 @@ impl<DB: Database> ChecksumViewer<'_, DB> {
pub(crate) const fn new(tool: &'_ DbTool<DB>) -> ChecksumViewer<'_, DB> {
ChecksumViewer { tool, start_key: None, end_key: None, limit: None }
}
}

impl<DB: Database> TableViewer<(u64, Duration)> for ChecksumViewer<'_, DB> {
type Error = eyre::Report;

pub(crate) fn get_checksum<T: Table>(&self) -> Result<(u64, Duration), eyre::Report> {
fn view<T: Table>(&self) -> Result<(u64, Duration), Self::Error> {
let provider =
self.tool.provider_factory.provider()?.disable_long_read_transaction_safety();
let tx = provider.tx_ref();
Expand Down Expand Up @@ -124,17 +129,8 @@ impl<DB: Database> ChecksumViewer<'_, DB> {
let checksum = hasher.finish();
let elapsed = start_time.elapsed();

Ok((checksum, elapsed))
}
}

impl<DB: Database> TableViewer<()> for ChecksumViewer<'_, DB> {
type Error = eyre::Report;

fn view<T: Table>(&self) -> Result<(), Self::Error> {
let (checksum, elapsed) = self.get_checksum::<T>()?;
info!("Checksum for table `{}`: {:#x} (elapsed: {:?})", T::NAME, checksum, elapsed);

Ok(())
Ok((checksum, elapsed))
}
}
94 changes: 7 additions & 87 deletions bin/reth/src/commands/db/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,8 @@ use crate::{
};
use clap::Parser;
use reth_db::{
cursor::DbCursorRO, database::Database, open_db_read_only, table::Table, transaction::DbTx,
AccountChangeSets, AccountsHistory, AccountsTrie, BlockBodyIndices, BlockOmmers, BlockRequests,
BlockWithdrawals, Bytecodes, CanonicalHeaders, DatabaseEnv, HashedAccounts, HashedStorages,
HeaderNumbers, HeaderTerminalDifficulties, Headers, PlainAccountState, PlainStorageState,
PruneCheckpoints, Receipts, StageCheckpointProgresses, StageCheckpoints, StorageChangeSets,
StoragesHistory, StoragesTrie, Tables, TransactionBlocks, TransactionHashNumbers,
TransactionSenders, Transactions, VersionHistory,
cursor::DbCursorRO, database::Database, open_db_read_only, table::Table, tables_to_generic,
transaction::DbTx, DatabaseEnv, Tables,
};
use std::{
collections::HashMap,
Expand Down Expand Up @@ -78,86 +73,11 @@ impl Command {
secondary_tx.disable_long_read_transaction_safety();

let output_dir = self.output.clone();
match table {
Tables::CanonicalHeaders => {
find_diffs::<CanonicalHeaders>(primary_tx, secondary_tx, output_dir)?
}
Tables::HeaderTerminalDifficulties => {
find_diffs::<HeaderTerminalDifficulties>(primary_tx, secondary_tx, output_dir)?
}
Tables::HeaderNumbers => {
find_diffs::<HeaderNumbers>(primary_tx, secondary_tx, output_dir)?
}
Tables::Headers => find_diffs::<Headers>(primary_tx, secondary_tx, output_dir)?,
Tables::BlockBodyIndices => {
find_diffs::<BlockBodyIndices>(primary_tx, secondary_tx, output_dir)?
}
Tables::BlockOmmers => {
find_diffs::<BlockOmmers>(primary_tx, secondary_tx, output_dir)?
}
Tables::BlockWithdrawals => {
find_diffs::<BlockWithdrawals>(primary_tx, secondary_tx, output_dir)?
}
Tables::BlockRequests => {
find_diffs::<BlockRequests>(primary_tx, secondary_tx, output_dir)?
}
Tables::TransactionBlocks => {
find_diffs::<TransactionBlocks>(primary_tx, secondary_tx, output_dir)?
}
Tables::Transactions => {
find_diffs::<Transactions>(primary_tx, secondary_tx, output_dir)?
}
Tables::TransactionHashNumbers => {
find_diffs::<TransactionHashNumbers>(primary_tx, secondary_tx, output_dir)?
}
Tables::Receipts => find_diffs::<Receipts>(primary_tx, secondary_tx, output_dir)?,
Tables::PlainAccountState => {
find_diffs::<PlainAccountState>(primary_tx, secondary_tx, output_dir)?
}
Tables::PlainStorageState => {
find_diffs::<PlainStorageState>(primary_tx, secondary_tx, output_dir)?
}
Tables::Bytecodes => find_diffs::<Bytecodes>(primary_tx, secondary_tx, output_dir)?,
Tables::AccountsHistory => {
find_diffs::<AccountsHistory>(primary_tx, secondary_tx, output_dir)?
}
Tables::StoragesHistory => {
find_diffs::<StoragesHistory>(primary_tx, secondary_tx, output_dir)?
}
Tables::AccountChangeSets => {
find_diffs::<AccountChangeSets>(primary_tx, secondary_tx, output_dir)?
}
Tables::StorageChangeSets => {
find_diffs::<StorageChangeSets>(primary_tx, secondary_tx, output_dir)?
}
Tables::HashedAccounts => {
find_diffs::<HashedAccounts>(primary_tx, secondary_tx, output_dir)?
}
Tables::HashedStorages => {
find_diffs::<HashedStorages>(primary_tx, secondary_tx, output_dir)?
}
Tables::AccountsTrie => {
find_diffs::<AccountsTrie>(primary_tx, secondary_tx, output_dir)?
}
Tables::StoragesTrie => {
find_diffs::<StoragesTrie>(primary_tx, secondary_tx, output_dir)?
}
Tables::TransactionSenders => {
find_diffs::<TransactionSenders>(primary_tx, secondary_tx, output_dir)?
}
Tables::StageCheckpoints => {
find_diffs::<StageCheckpoints>(primary_tx, secondary_tx, output_dir)?
}
Tables::StageCheckpointProgresses => {
find_diffs::<StageCheckpointProgresses>(primary_tx, secondary_tx, output_dir)?
}
Tables::PruneCheckpoints => {
find_diffs::<PruneCheckpoints>(primary_tx, secondary_tx, output_dir)?
}
Tables::VersionHistory => {
find_diffs::<VersionHistory>(primary_tx, secondary_tx, output_dir)?
}
};
tables_to_generic!(table, |Table| find_diffs::<Table>(
primary_tx,
secondary_tx,
output_dir
))?;
}

Ok(())
Expand Down
54 changes: 4 additions & 50 deletions bin/reth/src/commands/db/stats.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
use std::time::Duration;

use crate::{commands::db::checksum::ChecksumViewer, utils::DbTool};
use clap::Parser;
use comfy_table::{Cell, Row, Table as ComfyTable};
use eyre::WrapErr;
use human_bytes::human_bytes;
use itertools::Itertools;
use reth_db::{
database::Database, mdbx, static_file::iter_static_files, AccountChangeSets, AccountsHistory,
AccountsTrie, BlockBodyIndices, BlockOmmers, BlockRequests, BlockWithdrawals, Bytecodes,
CanonicalHeaders, DatabaseEnv, HashedAccounts, HashedStorages, HeaderNumbers,
HeaderTerminalDifficulties, Headers, PlainAccountState, PlainStorageState, PruneCheckpoints,
Receipts, StageCheckpointProgresses, StageCheckpoints, StorageChangeSets, StoragesHistory,
StoragesTrie, Tables, TransactionBlocks, TransactionHashNumbers, TransactionSenders,
Transactions, VersionHistory,
database::Database, mdbx, static_file::iter_static_files, DatabaseEnv, TableViewer, Tables,
};
use reth_fs_util as fs;
use reth_node_core::dirs::{ChainPath, DataDirPath};
use reth_primitives::static_file::{find_fixed_range, SegmentRangeInclusive};
use reth_provider::providers::StaticFileProvider;
use tracing::info;
use std::time::Duration;

#[derive(Parser, Debug)]
/// The arguments for the `reth db stats` command
Expand Down Expand Up @@ -322,46 +314,8 @@ impl Command {
let db_tables = Tables::ALL;
let mut total_elapsed = Duration::default();

for db_table in db_tables {
info!("Calculating checksum for table: {}", db_table);

let viewer = ChecksumViewer::new(tool);
let (checksum, elapsed) = match db_table {
Tables::AccountsHistory => viewer.get_checksum::<AccountsHistory>().unwrap(),
Tables::AccountChangeSets => viewer.get_checksum::<AccountChangeSets>().unwrap(),
Tables::AccountsTrie => viewer.get_checksum::<AccountsTrie>().unwrap(),
Tables::BlockBodyIndices => viewer.get_checksum::<BlockBodyIndices>().unwrap(),
Tables::BlockOmmers => viewer.get_checksum::<BlockOmmers>().unwrap(),
Tables::BlockWithdrawals => viewer.get_checksum::<BlockWithdrawals>().unwrap(),
Tables::BlockRequests => viewer.get_checksum::<BlockRequests>().unwrap(),
Tables::Bytecodes => viewer.get_checksum::<Bytecodes>().unwrap(),
Tables::CanonicalHeaders => viewer.get_checksum::<CanonicalHeaders>().unwrap(),
Tables::HashedAccounts => viewer.get_checksum::<HashedAccounts>().unwrap(),
Tables::HashedStorages => viewer.get_checksum::<HashedStorages>().unwrap(),
Tables::HeaderNumbers => viewer.get_checksum::<HeaderNumbers>().unwrap(),
Tables::HeaderTerminalDifficulties => {
viewer.get_checksum::<HeaderTerminalDifficulties>().unwrap()
}
Tables::Headers => viewer.get_checksum::<Headers>().unwrap(),
Tables::PlainAccountState => viewer.get_checksum::<PlainAccountState>().unwrap(),
Tables::PlainStorageState => viewer.get_checksum::<PlainStorageState>().unwrap(),
Tables::PruneCheckpoints => viewer.get_checksum::<PruneCheckpoints>().unwrap(),
Tables::Receipts => viewer.get_checksum::<Receipts>().unwrap(),
Tables::StageCheckpointProgresses => {
viewer.get_checksum::<StageCheckpointProgresses>().unwrap()
}
Tables::StageCheckpoints => viewer.get_checksum::<StageCheckpoints>().unwrap(),
Tables::StorageChangeSets => viewer.get_checksum::<StorageChangeSets>().unwrap(),
Tables::StoragesHistory => viewer.get_checksum::<StoragesHistory>().unwrap(),
Tables::StoragesTrie => viewer.get_checksum::<StoragesTrie>().unwrap(),
Tables::TransactionBlocks => viewer.get_checksum::<TransactionBlocks>().unwrap(),
Tables::TransactionHashNumbers => {
viewer.get_checksum::<TransactionHashNumbers>().unwrap()
}
Tables::TransactionSenders => viewer.get_checksum::<TransactionSenders>().unwrap(),
Tables::Transactions => viewer.get_checksum::<Transactions>().unwrap(),
Tables::VersionHistory => viewer.get_checksum::<VersionHistory>().unwrap(),
};
for &db_table in db_tables {
let (checksum, elapsed) = ChecksumViewer::new(tool).view_rt(db_table).unwrap();

// increment duration for final report
total_elapsed += elapsed;
Expand Down
1 change: 1 addition & 0 deletions crates/storage/db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ pub mod mdbx {

pub use abstraction::*;
pub use reth_storage_errors::db::{DatabaseError, DatabaseWriteOperation};
pub use table::*;
pub use tables::*;
pub use utils::is_database_empty;

Expand Down
36 changes: 34 additions & 2 deletions crates/storage/db/src/tables/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ pub trait TableViewer<R> {
/// The error type returned by the viewer.
type Error;

/// Calls `view` with the correct table type.
fn view_rt(&self, table: Tables) -> Result<R, Self::Error> {
table.view(self)
}

/// Operate on the table in a generic way.
fn view<T: Table>(&self) -> Result<R, Self::Error>;

Expand All @@ -101,8 +106,8 @@ pub trait TableViewer<R> {
}
}

#[macro_export]
/// Defines all the tables in the database.
#[macro_export]
macro_rules! tables {
(@bool) => { false };
(@bool $($t:tt)+) => { true };
Expand Down Expand Up @@ -196,7 +201,7 @@ macro_rules! tables {
/// Allows to operate on specific table type
pub fn view<T, R>(&self, visitor: &T) -> Result<R, T::Error>
where
T: TableViewer<R>,
T: ?Sized + TableViewer<R>,
{
match self {
$(
Expand Down Expand Up @@ -240,6 +245,33 @@ macro_rules! tables {
pub(super) const $name: &'static str = stringify!($name);
)*
}

/// Maps a run-time [`Tables`] enum value to its corresponding compile-time [`Table`] type.
///
/// This is a simpler alternative to [`TableViewer`].
///
/// # Examples
///
/// ```
/// use reth_db::{Table, Tables, tables_to_generic};
///
/// let table = Tables::Headers;
/// let result = tables_to_generic!(table, |GenericTable| GenericTable::TABLE);
/// assert_eq!(result, table);
/// ```
#[macro_export]
macro_rules! tables_to_generic {
($table:expr, |$generic_name:ident| $e:expr) => {
match $table {
$(
Tables::$name => {
use $crate::tables::$name as $generic_name;
$e
},
)*
}
};
}
};
}

Expand Down
Loading