Skip to content

Commit

Permalink
chore(examples): fix db-access example with RO provider (#11670)
Browse files Browse the repository at this point in the history
Co-authored-by: Matthias Seitz <[email protected]>
  • Loading branch information
0xDmtri and mattsse authored Oct 11, 2024
1 parent f2440c7 commit 5e1bd04
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions examples/db-access/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
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;
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
Expand All @@ -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.
Expand Down

0 comments on commit 5e1bd04

Please sign in to comment.