refactor: remove Transaction and add DatabaseProvider to stages (#3034)

Co-authored-by: Georgios Konstantopoulos <me@gakonst.com>
This commit is contained in:
joshieDo
2023-06-12 23:37:58 +01:00
committed by GitHub
parent cfdd88d392
commit f55d88b8c4
58 changed files with 2326 additions and 2109 deletions

View File

@ -11,11 +11,11 @@
/// Various provider traits.
mod traits;
pub use traits::{
AccountProvider, BlockExecutor, BlockHashProvider, BlockIdProvider, BlockNumProvider,
BlockProvider, BlockProviderIdExt, BlockSource, BlockchainTreePendingStateProvider,
CanonChainTracker, CanonStateNotification, CanonStateNotificationSender,
CanonStateNotifications, CanonStateSubscriptions, EvmEnvProvider, ExecutorFactory,
HeaderProvider, PostStateDataProvider, ReceiptProvider, ReceiptProviderIdExt,
AccountExtProvider, AccountProvider, BlockExecutor, BlockHashProvider, BlockIdProvider,
BlockNumProvider, BlockProvider, BlockProviderIdExt, BlockSource,
BlockchainTreePendingStateProvider, CanonChainTracker, CanonStateNotification,
CanonStateNotificationSender, CanonStateNotifications, CanonStateSubscriptions, EvmEnvProvider,
ExecutorFactory, HeaderProvider, PostStateDataProvider, ReceiptProvider, ReceiptProviderIdExt,
StageCheckpointProvider, StateProvider, StateProviderBox, StateProviderFactory,
StateRootProvider, TransactionsProvider, WithdrawalsProvider,
};
@ -23,8 +23,8 @@ pub use traits::{
/// Provider trait implementations.
pub mod providers;
pub use providers::{
HistoricalStateProvider, HistoricalStateProviderRef, LatestStateProvider,
LatestStateProviderRef, ShareableDatabase,
DatabaseProvider, DatabaseProviderRO, DatabaseProviderRW, HistoricalStateProvider,
HistoricalStateProviderRef, LatestStateProvider, LatestStateProviderRef, ShareableDatabase,
};
/// Execution result
@ -33,7 +33,7 @@ pub use post_state::PostState;
/// Helper types for interacting with the database
mod transaction;
pub use transaction::{Transaction, TransactionError};
pub use transaction::TransactionError;
/// Common database utilities.
mod utils;

View File

@ -18,7 +18,7 @@ use std::{ops::RangeBounds, sync::Arc};
use tracing::trace;
mod provider;
use provider::{DatabaseProvider, DatabaseProviderRO, DatabaseProviderRW};
pub use provider::{DatabaseProvider, DatabaseProviderRO, DatabaseProviderRW};
/// A common provider that fetches data from a database.
///
@ -34,16 +34,17 @@ pub struct ShareableDatabase<DB> {
impl<DB: Database> ShareableDatabase<DB> {
/// Returns a provider with a created `DbTx` inside, which allows fetching data from the
/// database using different types of providers. Example: [`HeaderProvider`]
/// [`BlockHashProvider`]
/// [`BlockHashProvider`]. This may fail if the inner read database transaction fails to open.
pub fn provider(&self) -> Result<DatabaseProviderRO<'_, DB>> {
Ok(DatabaseProvider::new(self.db.tx()?, self.chain_spec.clone()))
}
/// Returns a provider with a created `DbTxMut` inside, which allows fetching and updating
/// data from the database using different types of providers. Example: [`HeaderProvider`]
/// [`BlockHashProvider`]
/// [`BlockHashProvider`]. This may fail if the inner read/write database transaction fails to
/// open.
pub fn provider_rw(&self) -> Result<DatabaseProviderRW<'_, DB>> {
Ok(DatabaseProvider::new_rw(self.db.tx_mut()?, self.chain_spec.clone()))
Ok(DatabaseProviderRW(DatabaseProvider::new_rw(self.db.tx_mut()?, self.chain_spec.clone())))
}
}

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
//! Dummy blocks and data for tests
use crate::{post_state::PostState, Transaction};
use crate::{post_state::PostState, DatabaseProviderRW};
use reth_db::{database::Database, models::StoredBlockBodyIndices, tables};
use reth_primitives::{
hex_literal::hex, Account, BlockNumber, Bytes, Header, Log, Receipt, SealedBlock,
@ -10,9 +10,11 @@ use reth_rlp::Decodable;
use std::collections::BTreeMap;
/// Assert genesis block
pub fn assert_genesis_block<DB: Database>(tx: &Transaction<'_, DB>, g: SealedBlock) {
pub fn assert_genesis_block<DB: Database>(provider: &DatabaseProviderRW<'_, DB>, g: SealedBlock) {
let n = g.number;
let h = H256::zero();
let tx = provider;
// check if all tables are empty
assert_eq!(tx.table::<tables::Headers>().unwrap(), vec![(g.number, g.header.clone().unseal())]);

View File

@ -1,6 +1,7 @@
use auto_impl::auto_impl;
use reth_interfaces::Result;
use reth_primitives::{Account, Address};
use reth_primitives::{Account, Address, BlockNumber};
use std::{collections::BTreeSet, ops::RangeBounds};
/// Account provider
#[auto_impl(&, Arc, Box)]
@ -10,3 +11,22 @@ pub trait AccountProvider: Send + Sync {
/// Returns `None` if the account doesn't exist.
fn basic_account(&self, address: Address) -> Result<Option<Account>>;
}
/// Account provider
#[auto_impl(&, Arc, Box)]
pub trait AccountExtProvider: Send + Sync {
/// Iterate over account changesets and return all account address that were changed.
fn changed_accounts_with_range(
&self,
_range: impl RangeBounds<BlockNumber>,
) -> Result<BTreeSet<Address>>;
/// Get basic account information for multiple accounts. A more efficient version than calling
/// [`AccountProvider::basic_account`] repeatedly.
///
/// Returns `None` if the account doesn't exist.
fn basic_accounts(
&self,
_iter: impl IntoIterator<Item = Address>,
) -> Result<Vec<(Address, Option<Account>)>>;
}

View File

@ -1,7 +1,7 @@
//! Collection of common provider traits.
mod account;
pub use account::AccountProvider;
pub use account::{AccountExtProvider, AccountProvider};
mod block;
pub use block::{BlockProvider, BlockProviderIdExt, BlockSource};

File diff suppressed because it is too large Load Diff