feat: introduce ProviderFactoryBuilder (#13989)

This commit is contained in:
Matthias Seitz
2025-01-28 20:34:03 +01:00
committed by GitHub
parent 4653d3dd3a
commit 9bc07cc5bd
6 changed files with 362 additions and 31 deletions

View File

@ -2,16 +2,13 @@ use alloy_consensus::BlockHeader;
use alloy_primitives::{Address, B256};
use alloy_rpc_types_eth::{Filter, FilteredParams};
use reth_chainspec::ChainSpecBuilder;
use reth_db::{open_db_read_only, DatabaseEnv};
use reth_node_ethereum::EthereumNode;
use reth_node_types::NodeTypesWithDBAdapter;
use reth_primitives::{SealedBlock, SealedHeader, TransactionSigned};
use reth_primitives_traits::transaction::signed::SignedTransaction;
use reth_provider::{
providers::StaticFileProvider, AccountReader, BlockReader, BlockSource, HeaderProvider,
ProviderFactory, ReceiptProvider, StateProvider, TransactionsProvider,
providers::ReadOnlyConfig, AccountReader, BlockReader, BlockSource, HeaderProvider,
ReceiptProvider, StateProvider, TransactionsProvider,
};
use std::{path::Path, sync::Arc};
// Providers are zero cost abstractions on top of an opened MDBX Transaction
// exposing a familiar API to query the chain's information without requiring knowledge
@ -22,17 +19,11 @@ use std::{path::Path, sync::Arc};
fn main() -> eyre::Result<()> {
// Opens a RO handle to the database file.
let db_path = std::env::var("RETH_DB_PATH")?;
let db_path = Path::new(&db_path);
let db = open_db_read_only(db_path.join("db").as_path(), Default::default())?;
// Instantiate a provider factory for Ethereum mainnet using the provided DB.
// TODO: Should the DB version include the spec so that you do not need to specify it here?
// Instantiate a provider factory for Ethereum mainnet using the provided DB path.
let spec = ChainSpecBuilder::mainnet().build();
let factory = ProviderFactory::<NodeTypesWithDBAdapter<EthereumNode, Arc<DatabaseEnv>>>::new(
db.into(),
spec.into(),
StaticFileProvider::read_only(db_path.join("static_files"), true)?,
);
let factory = EthereumNode::provider_factory_builder()
.open_read_only(spec.into(), ReadOnlyConfig::from_db_dir(db_path))?;
// This call opens a RO transaction on the database. To write to the DB you'd need to call
// the `provider_rw` function and look for the `Writer` variants of the traits.