From cf7f6de70b6ad274c03d405e766ee290c4e28ddb Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Thu, 12 Sep 2024 09:51:38 +0200 Subject: [PATCH] scaffold --- crates/storage/provider/src/providers/mod.rs | 1 + .../providers/typed_blockchain_provider.rs | 79 +++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 crates/storage/provider/src/providers/typed_blockchain_provider.rs diff --git a/crates/storage/provider/src/providers/mod.rs b/crates/storage/provider/src/providers/mod.rs index 652d275f3a..3bbbde30c6 100644 --- a/crates/storage/provider/src/providers/mod.rs +++ b/crates/storage/provider/src/providers/mod.rs @@ -59,6 +59,7 @@ pub use consistent_view::{ConsistentDbView, ConsistentViewError}; mod blockchain_provider; pub use blockchain_provider::BlockchainProvider2; +mod typed_blockchain_provider; /// Helper trait keeping common requirements of providers for [`NodeTypesWithDB`]. pub trait ProviderNodeTypes: NodeTypesWithDB {} diff --git a/crates/storage/provider/src/providers/typed_blockchain_provider.rs b/crates/storage/provider/src/providers/typed_blockchain_provider.rs new file mode 100644 index 0000000000..c5b1eac3cb --- /dev/null +++ b/crates/storage/provider/src/providers/typed_blockchain_provider.rs @@ -0,0 +1,79 @@ +use crate::{providers::StaticFileProvider, DatabaseProvider, DatabaseProviderRO}; +use reth_chain_state::CanonicalInMemoryState; +use reth_db_api::{transaction::DbTx, Database}; +use reth_node_types::NodeTypes; +use reth_primitives::{Block, BlockHashOrNumber}; +use reth_prune_types::PruneModes; +use reth_storage_errors::provider::ProviderResult; + +trait NodeStorage: Clone { + type Node: NodeTypes; + + /// Reads block + fn read_block( + &self, + id: BlockHashOrNumber, + provider: DatabaseProvider, + ) -> ProviderResult>; +} + +/// A common provider that fetches data from a database or static file. +/// +/// This provider implements most provider or provider factory traits. +/// +/// TODO this acts a type that can be used to read and write data from storage, it provides access +/// to the database and static file provider. This manages `DatabaseProvider` and passes this to the +/// storage to read from and write to. +pub struct ProviderFactory2 { + /// Database + db: DB, + /// Static File Provider + static_file_provider: StaticFileProvider, + /// Optional pruning configuration + prune_modes: PruneModes, + /// How to read,write certain types of data + storage: S, +} + +impl ProviderFactory2 +where + DB: Database, +{ + pub fn provider(&self) -> ProviderResult> { + Ok(DatabaseProvider::new( + self.db.tx()?, + // TODO get rid of this: https://github.com/paradigmxyz/reth/issues/10854 + reth_chainspec::DEV.clone(), + self.static_file_provider.clone(), + self.prune_modes.clone(), + )) + } +} + +impl ProviderFactory2 +where + DB: Database, + S: NodeStorage, +{ + fn block(&self, id: BlockHashOrNumber) -> ProviderResult> { + let provider = self.provider()?; + let block = self.storage.read_block(id, provider)?; + Ok(block) + } +} + +/// The main type for interacting with the blockchain. +/// +/// This type serves as the main entry point for interacting with the blockchain and provides data +/// from database storage and from the blockchain tree (pending state etc.) It is a simple wrapper +/// type that holds an instance of the database and the blockchain tree. +#[derive(Debug, Clone)] +pub struct BlockchainProvider3 { + /// Disk storage support for the blockchain. + storage: S, // this will ProviderFactory + /// Tracks the chain info wrt forkchoice updates and in memory canonical + /// state. + pub(super) canonical_in_memory_state: CanonicalInMemoryState, +} + +// TODO implement all traits that storage also supports