chore(examples): fix db-access example with RO provider (#11670)

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
This commit is contained in:
0xDmtri
2024-10-11 20:53:34 +02:00
committed by GitHub
parent f2440c7635
commit 5e1bd04941

View File

@ -1,6 +1,7 @@
use alloy_primitives::{Address, Sealable, B256};
use alloy_rpc_types::{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::SealedHeader;
@ -8,7 +9,7 @@ use reth_provider::{
providers::StaticFileProvider, AccountReader, BlockReader, BlockSource, HeaderProvider,
ProviderFactory, ReceiptProvider, StateProvider, TransactionsProvider,
};
use std::path::Path;
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
@ -20,17 +21,16 @@ 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?
let spec = ChainSpecBuilder::mainnet().build();
let factory =
ProviderFactory::<NodeTypesWithDBAdapter<EthereumNode, _>>::new_with_database_path(
db_path,
spec.into(),
Default::default(),
StaticFileProvider::read_only(db_path.join("static_files"), false)?,
)?;
let factory = ProviderFactory::<NodeTypesWithDBAdapter<EthereumNode, Arc<DatabaseEnv>>>::new(
db.into(),
spec.into(),
StaticFileProvider::read_only(db_path.join("static_files"), true)?,
);
// 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.