Skip to content

Commit

Permalink
improve(storage): relocate l1 origin database codes
Browse files Browse the repository at this point in the history
  • Loading branch information
johntaiko committed Jul 5, 2024

Verified

This commit was signed with the committer’s verified signature.
pingsutw Kevin Su
1 parent 4e22b21 commit 1e843a4
Showing 5 changed files with 61 additions and 81 deletions.
35 changes: 0 additions & 35 deletions crates/storage/provider/src/providers/database/provider.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#[cfg(feature = "taiko")]
use crate::traits::{L1OriginReader, L1OriginWriter};
use crate::{
bundle_state::{BundleStateInit, HashedStateChanges, RevertsInit},
providers::{database::metrics, static_file::StaticFileWriter, StaticFileProvider},
@@ -33,8 +31,6 @@ use reth_db_api::{
use reth_evm::ConfigureEvmEnv;
use reth_execution_types::{Chain, ExecutionOutcome};
use reth_network_p2p::headers::downloader::SyncTarget;
#[cfg(feature = "taiko")]
use reth_primitives::L1Origin;
use reth_primitives::{
keccak256,
revm::{config::revm_spec, env::fill_block_env},
@@ -2870,34 +2866,3 @@ fn range_size_hint(range: &impl RangeBounds<TxNumber>) -> Option<usize> {
};
end.checked_sub(start).map(|x| x as _)
}

#[cfg(feature = "taiko")]
impl<TX: DbTx> L1OriginReader for DatabaseProvider<TX> {
fn read_l1_origin(&self, block_id: u64) -> ProviderResult<Option<L1Origin>> {
let result = self.tx.get::<tables::L1Origins>(L1Origin::key(block_id))?;
if let Some(bytes) = result {
Ok(Some(
serde_json::from_slice(&bytes).map_err(|e| ProviderError::Serde(e.to_string()))?,
))
} else {
Ok(None)
}
}

fn read_head_l1_origin(&self) -> ProviderResult<Option<u64>> {
Ok(self.tx.get::<tables::HeadL1Origin>(L1Origin::head_key())?)
}
}

#[cfg(feature = "taiko")]
impl<TX: DbTxMut> L1OriginWriter for DatabaseProvider<TX> {
fn insert_l1_origin(&self, block_id: u64, l1_origin: L1Origin) -> ProviderResult<()> {
let bytes =
serde_json::to_vec(&l1_origin).map_err(|e| ProviderError::Serde(e.to_string()))?;
Ok(self.tx.put::<tables::L1Origins>(L1Origin::key(block_id), bytes)?)
}

fn insert_head_l1_origin(&self, block_id: u64) -> ProviderResult<()> {
Ok(self.tx.put::<tables::HeadL1Origin>(L1Origin::head_key(), block_id)?)
}
}
45 changes: 11 additions & 34 deletions crates/storage/provider/src/providers/mod.rs
Original file line number Diff line number Diff line change
@@ -2,13 +2,11 @@ use crate::{
AccountReader, BlockHashReader, BlockIdReader, BlockNumReader, BlockReader, BlockReaderIdExt,
BlockSource, BlockchainTreePendingStateProvider, CanonChainTracker, CanonStateNotifications,
CanonStateSubscriptions, ChainSpecProvider, ChangeSetReader, DatabaseProviderFactory,
EvmEnvProvider, FullExecutionDataProvider, HeaderProvider, ProviderError,
PruneCheckpointReader, ReceiptProvider, ReceiptProviderIdExt, RequestsProvider,
DatabaseProviderRwFactory, EvmEnvProvider, FullExecutionDataProvider, HeaderProvider,
ProviderError, PruneCheckpointReader, ReceiptProvider, ReceiptProviderIdExt, RequestsProvider,
StageCheckpointReader, StateProviderBox, StateProviderFactory, StaticFileProviderFactory,
TransactionVariant, TransactionsProvider, TreeViewer, WithdrawalsProvider,
};
#[cfg(feature = "taiko")]
use crate::{L1OriginReader, L1OriginWriter};
use reth_blockchain_tree_api::{
error::{CanonicalError, InsertBlockError},
BlockValidationKind, BlockchainTreeEngine, BlockchainTreeViewer, CanonicalOutcome,
@@ -20,8 +18,6 @@ use reth_db_api::{
models::{AccountBeforeTx, StoredBlockBodyIndices},
};
use reth_evm::ConfigureEvmEnv;
#[cfg(feature = "taiko")]
use reth_primitives::L1Origin;
use reth_primitives::{
Account, Address, Block, BlockHash, BlockHashOrNumber, BlockId, BlockNumHash, BlockNumber,
BlockNumberOrTag, BlockWithSenders, Header, Receipt, SealedBlock, SealedBlockWithSenders,
@@ -160,6 +156,15 @@ where
}
}

impl<DB> DatabaseProviderRwFactory<DB> for BlockchainProvider<DB>
where
DB: Database,
{
fn database_provider_rw(&self) -> ProviderResult<DatabaseProviderRW<DB>> {
self.database.provider_rw()
}
}

impl<DB> StaticFileProviderFactory for BlockchainProvider<DB> {
fn static_file_provider(&self) -> StaticFileProvider {
self.database.static_file_provider()
@@ -923,31 +928,3 @@ where
self.database.provider()?.basic_account(address)
}
}

#[cfg(feature = "taiko")]
impl<DB> L1OriginReader for BlockchainProvider<DB>
where
DB: Database,
{
fn read_l1_origin(&self, block_id: u64) -> ProviderResult<Option<L1Origin>> {
self.database.provider()?.read_l1_origin(block_id)
}

fn read_head_l1_origin(&self) -> ProviderResult<Option<u64>> {
self.database.provider()?.read_head_l1_origin()
}
}

#[cfg(feature = "taiko")]
impl<DB> L1OriginWriter for BlockchainProvider<DB>
where
DB: Database,
{
fn insert_l1_origin(&self, block_id: u64, l1_origin: L1Origin) -> ProviderResult<()> {
self.database.provider_rw()?.insert_l1_origin(block_id, l1_origin)
}

fn insert_head_l1_origin(&self, block_id: u64) -> ProviderResult<()> {
self.database.provider_rw()?.insert_head_l1_origin(block_id)
}
}
8 changes: 7 additions & 1 deletion crates/storage/provider/src/traits/database_provider.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::DatabaseProviderRO;
use crate::{DatabaseProviderRO, DatabaseProviderRW};
use reth_db_api::database::Database;
use reth_storage_errors::provider::ProviderResult;

@@ -7,3 +7,9 @@ pub trait DatabaseProviderFactory<DB: Database> {
/// Create new read-only database provider.
fn database_provider_ro(&self) -> ProviderResult<DatabaseProviderRO<DB>>;
}

/// Database provider factory.
pub trait DatabaseProviderRwFactory<DB: Database> {
/// Create new read-only database provider.
fn database_provider_rw(&self) -> ProviderResult<DatabaseProviderRW<DB>>;
}
2 changes: 1 addition & 1 deletion crates/storage/provider/src/traits/mod.rs
Original file line number Diff line number Diff line change
@@ -34,7 +34,7 @@ mod history;
pub use history::HistoryWriter;

mod database_provider;
pub use database_provider::DatabaseProviderFactory;
pub use database_provider::{DatabaseProviderFactory, DatabaseProviderRwFactory};

mod static_file_provider;
pub use static_file_provider::StaticFileProviderFactory;
52 changes: 42 additions & 10 deletions crates/taiko/storage/src/l1_origin.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
//! The module for L1 origin related data.
use reth_db_api::transaction::{DbTx, DbTxMut};
use reth_db_api::{
database::Database,
transaction::{DbTx, DbTxMut},
};
use reth_primitives::BlockNumber;
use reth_provider::DatabaseProvider;
use reth_provider::{
providers::BlockchainProvider, DatabaseProvider, DatabaseProviderFactory,
DatabaseProviderRwFactory,
};
use reth_storage_errors::provider::ProviderResult;
use reth_taiko_primitives::{HeadL1Origin, HeadL1OriginKey, L1Origin, L1Origins};

@@ -18,14 +24,14 @@ pub trait L1OriginReader: Send + Sync {
#[auto_impl::auto_impl(&, Arc)]
pub trait L1OriginWriter: Send + Sync {
/// Save the L1 origin for the given block hash.
fn save_l1_origin(&self, block_hash: BlockNumber, l1_origin: L1Origin) -> ProviderResult<()>;
fn save_l1_origin(&self, block_number: BlockNumber, l1_origin: L1Origin) -> ProviderResult<()>;
/// Save the head L1 origin.
fn save_head_l1_origin(&self, block_hash: BlockNumber) -> ProviderResult<()>;
fn save_head_l1_origin(&self, block_number: BlockNumber) -> ProviderResult<()>;
}

impl<TX: DbTx> L1OriginReader for DatabaseProvider<TX> {
fn get_l1_origin(&self, block_hash: BlockNumber) -> ProviderResult<Option<L1Origin>> {
Ok(self.tx_ref().get::<L1Origins>(block_hash)?)
fn get_l1_origin(&self, block_number: BlockNumber) -> ProviderResult<Option<L1Origin>> {
Ok(self.tx_ref().get::<L1Origins>(block_number)?)
}

fn get_head_l1_origin(&self) -> ProviderResult<Option<BlockNumber>> {
@@ -34,11 +40,37 @@ impl<TX: DbTx> L1OriginReader for DatabaseProvider<TX> {
}

impl<TX: DbTxMut> L1OriginWriter for DatabaseProvider<TX> {
fn save_l1_origin(&self, block_hash: BlockNumber, l1_origin: L1Origin) -> ProviderResult<()> {
Ok(self.tx_ref().put::<L1Origins>(block_hash, l1_origin)?)
fn save_l1_origin(&self, block_number: BlockNumber, l1_origin: L1Origin) -> ProviderResult<()> {
Ok(self.tx_ref().put::<L1Origins>(block_number, l1_origin)?)
}

fn save_head_l1_origin(&self, block_hash: BlockNumber) -> ProviderResult<()> {
Ok(self.tx_ref().put::<HeadL1Origin>(HeadL1OriginKey, block_hash)?)
fn save_head_l1_origin(&self, block_number: BlockNumber) -> ProviderResult<()> {
Ok(self.tx_ref().put::<HeadL1Origin>(HeadL1OriginKey, block_number)?)
}
}

impl<DB> L1OriginReader for BlockchainProvider<DB>
where
DB: Database,
{
fn get_l1_origin(&self, block_number: BlockNumber) -> ProviderResult<Option<L1Origin>> {
self.database_provider_ro()?.get_l1_origin(block_number)
}

fn get_head_l1_origin(&self) -> ProviderResult<Option<BlockNumber>> {
self.database_provider_ro()?.get_head_l1_origin()
}
}

impl<DB> L1OriginWriter for BlockchainProvider<DB>
where
DB: Database,
{
fn save_l1_origin(&self, block_number: BlockNumber, l1_origin: L1Origin) -> ProviderResult<()> {
self.database_provider_rw()?.save_l1_origin(block_number, l1_origin)
}

fn save_head_l1_origin(&self, block_number: u64) -> ProviderResult<()> {
self.database_provider_rw()?.save_head_l1_origin(block_number)
}
}

0 comments on commit 1e843a4

Please sign in to comment.