mirror of
https://github.com/paradigmxyz/reth.git
synced 2026-04-30 03:01:58 -04:00
remove lifetime param in storage trait (#5046)
This commit is contained in:
committed by
Matthias Seitz
parent
7bce412370
commit
163ee8c660
@@ -148,9 +148,9 @@ impl Command {
|
||||
}
|
||||
|
||||
/// Find diffs for a table, then analyzing the result
|
||||
fn find_diffs<'a, T: Table>(
|
||||
primary_tx: impl DbTx<'a>,
|
||||
secondary_tx: impl DbTx<'a>,
|
||||
fn find_diffs<T: Table>(
|
||||
primary_tx: impl DbTx,
|
||||
secondary_tx: impl DbTx,
|
||||
output_dir: impl AsRef<Path>,
|
||||
) -> eyre::Result<()>
|
||||
where
|
||||
@@ -231,9 +231,9 @@ where
|
||||
|
||||
/// This diff algorithm is slightly different, it will walk _each_ table, cross-checking for the
|
||||
/// element in the other table.
|
||||
fn find_diffs_advanced<'a, T: Table>(
|
||||
primary_tx: &impl DbTx<'a>,
|
||||
secondary_tx: &impl DbTx<'a>,
|
||||
fn find_diffs_advanced<T: Table>(
|
||||
primary_tx: &impl DbTx,
|
||||
secondary_tx: &impl DbTx,
|
||||
) -> eyre::Result<TableDiffResult<T>>
|
||||
where
|
||||
T::Value: PartialEq,
|
||||
|
||||
@@ -24,9 +24,9 @@ impl Headers {
|
||||
}
|
||||
|
||||
// Generates the dataset to train a zstd dictionary with the most recent rows (at most 1000).
|
||||
fn dataset_for_compression<'tx, T: Table<Key = BlockNumber>>(
|
||||
fn dataset_for_compression<T: Table<Key = BlockNumber>>(
|
||||
&self,
|
||||
tx: &impl DbTx<'tx>,
|
||||
tx: &impl DbTx,
|
||||
range: &RangeInclusive<BlockNumber>,
|
||||
range_len: usize,
|
||||
) -> RethResult<Vec<Vec<u8>>> {
|
||||
@@ -40,11 +40,7 @@ impl Headers {
|
||||
}
|
||||
|
||||
impl Segment for Headers {
|
||||
fn snapshot<'tx>(
|
||||
&self,
|
||||
tx: &impl DbTx<'tx>,
|
||||
range: RangeInclusive<BlockNumber>,
|
||||
) -> RethResult<()> {
|
||||
fn snapshot(&self, tx: &impl DbTx, range: RangeInclusive<BlockNumber>) -> RethResult<()> {
|
||||
let range_len = range.clone().count();
|
||||
let mut jar = prepare_jar::<3, tables::Headers>(
|
||||
tx,
|
||||
|
||||
@@ -18,16 +18,12 @@ pub(crate) type Rows<const COLUMNS: usize> = [Vec<Vec<u8>>; COLUMNS];
|
||||
/// A segment represents a snapshotting of some portion of the data.
|
||||
pub trait Segment {
|
||||
/// Snapshot data using the provided range.
|
||||
fn snapshot<'tx>(
|
||||
&self,
|
||||
tx: &impl DbTx<'tx>,
|
||||
range: RangeInclusive<BlockNumber>,
|
||||
) -> RethResult<()>;
|
||||
fn snapshot(&self, tx: &impl DbTx, range: RangeInclusive<BlockNumber>) -> RethResult<()>;
|
||||
}
|
||||
|
||||
/// Returns a [`NippyJar`] according to the desired configuration.
|
||||
pub(crate) fn prepare_jar<'tx, const COLUMNS: usize, T: Table>(
|
||||
tx: &impl DbTx<'tx>,
|
||||
pub(crate) fn prepare_jar<const COLUMNS: usize, T: Table>(
|
||||
tx: &impl DbTx,
|
||||
segment: SnapshotSegment,
|
||||
filters: Filters,
|
||||
compression: Compression,
|
||||
|
||||
@@ -620,9 +620,9 @@ mod tests {
|
||||
.map_err(|e| e.into())
|
||||
}
|
||||
|
||||
fn insert_storage_entry<'a, TX: DbTxMut<'a>>(
|
||||
fn insert_storage_entry<TX: DbTxMut>(
|
||||
&self,
|
||||
tx: &'a TX,
|
||||
tx: &TX,
|
||||
tid_address: BlockNumberAddress,
|
||||
entry: StorageEntry,
|
||||
hash: bool,
|
||||
|
||||
@@ -198,10 +198,7 @@ impl TestTransaction {
|
||||
}
|
||||
|
||||
/// Inserts a single [SealedHeader] into the corresponding tables of the headers stage.
|
||||
fn insert_header<'a, TX: DbTxMut<'a> + DbTx<'a>>(
|
||||
tx: &'a TX,
|
||||
header: &SealedHeader,
|
||||
) -> Result<(), DbError> {
|
||||
fn insert_header<TX: DbTxMut + DbTx>(tx: &TX, header: &SealedHeader) -> Result<(), DbError> {
|
||||
tx.put::<tables::CanonicalHeaders>(header.number, header.hash())?;
|
||||
tx.put::<tables::HeaderNumbers>(header.hash(), header.number)?;
|
||||
tx.put::<tables::Headers>(header.number, header.clone().unseal())
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
use std::{
|
||||
fmt,
|
||||
marker::PhantomData,
|
||||
ops::{Bound, RangeBounds},
|
||||
};
|
||||
|
||||
@@ -11,7 +10,7 @@ use crate::{
|
||||
};
|
||||
|
||||
/// A read-only cursor over table `T`.
|
||||
pub trait DbCursorRO<'tx, T: Table> {
|
||||
pub trait DbCursorRO<T: Table> {
|
||||
/// Positions the cursor at the first entry in the table, returning it.
|
||||
fn first(&mut self) -> PairResult<T>;
|
||||
|
||||
@@ -38,18 +37,15 @@ pub trait DbCursorRO<'tx, T: Table> {
|
||||
///
|
||||
/// If `start_key` is `None`, then the walker will start from the first entry of the table,
|
||||
/// otherwise it starts at the entry greater than or equal to the provided key.
|
||||
fn walk<'cursor>(
|
||||
&'cursor mut self,
|
||||
start_key: Option<T::Key>,
|
||||
) -> Result<Walker<'cursor, 'tx, T, Self>, DatabaseError>
|
||||
fn walk(&mut self, start_key: Option<T::Key>) -> Result<Walker<'_, T, Self>, DatabaseError>
|
||||
where
|
||||
Self: Sized;
|
||||
|
||||
/// Get an iterator that walks over a range of keys in the table.
|
||||
fn walk_range<'cursor>(
|
||||
&'cursor mut self,
|
||||
fn walk_range(
|
||||
&mut self,
|
||||
range: impl RangeBounds<T::Key>,
|
||||
) -> Result<RangeWalker<'cursor, 'tx, T, Self>, DatabaseError>
|
||||
) -> Result<RangeWalker<'_, T, Self>, DatabaseError>
|
||||
where
|
||||
Self: Sized;
|
||||
|
||||
@@ -57,16 +53,16 @@ pub trait DbCursorRO<'tx, T: Table> {
|
||||
///
|
||||
/// If `start_key` is `None`, then the walker will start from the last entry of the table,
|
||||
/// otherwise it starts at the entry greater than or equal to the provided key.
|
||||
fn walk_back<'cursor>(
|
||||
&'cursor mut self,
|
||||
fn walk_back(
|
||||
&mut self,
|
||||
start_key: Option<T::Key>,
|
||||
) -> Result<ReverseWalker<'cursor, 'tx, T, Self>, DatabaseError>
|
||||
) -> Result<ReverseWalker<'_, T, Self>, DatabaseError>
|
||||
where
|
||||
Self: Sized;
|
||||
}
|
||||
|
||||
/// A read-only cursor over the dup table `T`.
|
||||
pub trait DbDupCursorRO<'tx, T: DupSort> {
|
||||
pub trait DbDupCursorRO<T: DupSort> {
|
||||
/// Positions the cursor at the next KV pair of the table, returning it.
|
||||
fn next_dup(&mut self) -> PairResult<T>;
|
||||
|
||||
@@ -95,17 +91,17 @@ pub trait DbDupCursorRO<'tx, T: DupSort> {
|
||||
/// | `Some` | `None` | [`DbCursorRO::seek()`] |
|
||||
/// | `None` | `Some` | [`DbDupCursorRO::seek_by_key_subkey()`] |
|
||||
/// | `Some` | `Some` | [`DbDupCursorRO::seek_by_key_subkey()`] |
|
||||
fn walk_dup<'cursor>(
|
||||
&'cursor mut self,
|
||||
fn walk_dup(
|
||||
&mut self,
|
||||
key: Option<T::Key>,
|
||||
subkey: Option<T::SubKey>,
|
||||
) -> Result<DupWalker<'cursor, 'tx, T, Self>, DatabaseError>
|
||||
) -> Result<DupWalker<'_, T, Self>, DatabaseError>
|
||||
where
|
||||
Self: Sized;
|
||||
}
|
||||
|
||||
/// Read write cursor over table.
|
||||
pub trait DbCursorRW<'tx, T: Table> {
|
||||
pub trait DbCursorRW<T: Table> {
|
||||
/// Database operation that will update an existing row if a specified value already
|
||||
/// exists in a table, and insert a new row if the specified value doesn't already exist
|
||||
fn upsert(&mut self, key: T::Key, value: T::Value) -> Result<(), DatabaseError>;
|
||||
@@ -125,7 +121,7 @@ pub trait DbCursorRW<'tx, T: Table> {
|
||||
}
|
||||
|
||||
/// Read Write Cursor over DupSorted table.
|
||||
pub trait DbDupCursorRW<'tx, T: DupSort> {
|
||||
pub trait DbDupCursorRW<T: DupSort> {
|
||||
/// Delete all duplicate entries for current key.
|
||||
fn delete_current_duplicates(&mut self) -> Result<(), DatabaseError>;
|
||||
|
||||
@@ -140,28 +136,24 @@ pub trait DbDupCursorRW<'tx, T: DupSort> {
|
||||
/// Reason why we have two lifetimes is to distinguish between `'cursor` lifetime
|
||||
/// and inherited `'tx` lifetime. If there is only one, rust would short circle
|
||||
/// the Cursor lifetime and it wouldn't be possible to use Walker.
|
||||
pub struct Walker<'cursor, 'tx, T: Table, CURSOR: DbCursorRO<'tx, T>> {
|
||||
pub struct Walker<'cursor, T: Table, CURSOR: DbCursorRO<T>> {
|
||||
/// Cursor to be used to walk through the table.
|
||||
cursor: &'cursor mut CURSOR,
|
||||
/// `(key, value)` where to start the walk.
|
||||
start: IterPairResult<T>,
|
||||
/// Phantom data for 'tx. As it is only used for `DbCursorRO`.
|
||||
_tx_phantom: PhantomData<&'tx T>,
|
||||
}
|
||||
|
||||
impl<'tx, T, CURSOR> fmt::Debug for Walker<'_, 'tx, T, CURSOR>
|
||||
impl<T, CURSOR> fmt::Debug for Walker<'_, T, CURSOR>
|
||||
where
|
||||
T: Table,
|
||||
CURSOR: DbCursorRO<'tx, T> + fmt::Debug,
|
||||
CURSOR: DbCursorRO<T> + fmt::Debug,
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("Walker").field("cursor", &self.cursor).field("start", &self.start).finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'cursor, 'tx, T: Table, CURSOR: DbCursorRO<'tx, T>> std::iter::Iterator
|
||||
for Walker<'cursor, 'tx, T, CURSOR>
|
||||
{
|
||||
impl<'cursor, T: Table, CURSOR: DbCursorRO<T>> std::iter::Iterator for Walker<'cursor, T, CURSOR> {
|
||||
type Item = Result<TableRow<T>, DatabaseError>;
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
let start = self.start.take();
|
||||
@@ -173,22 +165,20 @@ impl<'cursor, 'tx, T: Table, CURSOR: DbCursorRO<'tx, T>> std::iter::Iterator
|
||||
}
|
||||
}
|
||||
|
||||
impl<'cursor, 'tx, T: Table, CURSOR: DbCursorRO<'tx, T>> Walker<'cursor, 'tx, T, CURSOR> {
|
||||
impl<'cursor, T: Table, CURSOR: DbCursorRO<T>> Walker<'cursor, T, CURSOR> {
|
||||
/// construct Walker
|
||||
pub fn new(cursor: &'cursor mut CURSOR, start: IterPairResult<T>) -> Self {
|
||||
Self { cursor, start, _tx_phantom: std::marker::PhantomData }
|
||||
Self { cursor, start }
|
||||
}
|
||||
|
||||
/// convert current [`Walker`] to [`ReverseWalker`] which iterates reversely
|
||||
pub fn rev(self) -> ReverseWalker<'cursor, 'tx, T, CURSOR> {
|
||||
pub fn rev(self) -> ReverseWalker<'cursor, T, CURSOR> {
|
||||
let start = self.cursor.current().transpose();
|
||||
ReverseWalker::new(self.cursor, start)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'cursor, 'tx, T: Table, CURSOR: DbCursorRW<'tx, T> + DbCursorRO<'tx, T>>
|
||||
Walker<'cursor, 'tx, T, CURSOR>
|
||||
{
|
||||
impl<'cursor, T: Table, CURSOR: DbCursorRW<T> + DbCursorRO<T>> Walker<'cursor, T, CURSOR> {
|
||||
/// Delete current item that walker points to.
|
||||
pub fn delete_current(&mut self) -> Result<(), DatabaseError> {
|
||||
self.cursor.delete_current()
|
||||
@@ -197,19 +187,17 @@ impl<'cursor, 'tx, T: Table, CURSOR: DbCursorRW<'tx, T> + DbCursorRO<'tx, T>>
|
||||
|
||||
/// Provides a reverse iterator to `Cursor` when handling `Table`.
|
||||
/// Also check [`Walker`]
|
||||
pub struct ReverseWalker<'cursor, 'tx, T: Table, CURSOR: DbCursorRO<'tx, T>> {
|
||||
pub struct ReverseWalker<'cursor, T: Table, CURSOR: DbCursorRO<T>> {
|
||||
/// Cursor to be used to walk through the table.
|
||||
cursor: &'cursor mut CURSOR,
|
||||
/// `(key, value)` where to start the walk.
|
||||
start: IterPairResult<T>,
|
||||
/// Phantom data for 'tx. As it is only used for `DbCursorRO`.
|
||||
_tx_phantom: PhantomData<&'tx T>,
|
||||
}
|
||||
|
||||
impl<'tx, T, CURSOR> fmt::Debug for ReverseWalker<'_, 'tx, T, CURSOR>
|
||||
impl<T, CURSOR> fmt::Debug for ReverseWalker<'_, T, CURSOR>
|
||||
where
|
||||
T: Table,
|
||||
CURSOR: DbCursorRO<'tx, T> + fmt::Debug,
|
||||
CURSOR: DbCursorRO<T> + fmt::Debug,
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("ReverseWalker")
|
||||
@@ -219,30 +207,28 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<'cursor, 'tx, T: Table, CURSOR: DbCursorRO<'tx, T>> ReverseWalker<'cursor, 'tx, T, CURSOR> {
|
||||
impl<'cursor, T: Table, CURSOR: DbCursorRO<T>> ReverseWalker<'cursor, T, CURSOR> {
|
||||
/// construct ReverseWalker
|
||||
pub fn new(cursor: &'cursor mut CURSOR, start: IterPairResult<T>) -> Self {
|
||||
Self { cursor, start, _tx_phantom: std::marker::PhantomData }
|
||||
Self { cursor, start }
|
||||
}
|
||||
|
||||
/// convert current [`ReverseWalker`] to [`Walker`] which iterate forwardly
|
||||
pub fn forward(self) -> Walker<'cursor, 'tx, T, CURSOR> {
|
||||
pub fn forward(self) -> Walker<'cursor, T, CURSOR> {
|
||||
let start = self.cursor.current().transpose();
|
||||
Walker::new(self.cursor, start)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'cursor, 'tx, T: Table, CURSOR: DbCursorRW<'tx, T> + DbCursorRO<'tx, T>>
|
||||
ReverseWalker<'cursor, 'tx, T, CURSOR>
|
||||
{
|
||||
impl<'cursor, T: Table, CURSOR: DbCursorRW<T> + DbCursorRO<T>> ReverseWalker<'cursor, T, CURSOR> {
|
||||
/// Delete current item that walker points to.
|
||||
pub fn delete_current(&mut self) -> Result<(), DatabaseError> {
|
||||
self.cursor.delete_current()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'cursor, 'tx, T: Table, CURSOR: DbCursorRO<'tx, T>> std::iter::Iterator
|
||||
for ReverseWalker<'cursor, 'tx, T, CURSOR>
|
||||
impl<'cursor, T: Table, CURSOR: DbCursorRO<T>> std::iter::Iterator
|
||||
for ReverseWalker<'cursor, T, CURSOR>
|
||||
{
|
||||
type Item = Result<TableRow<T>, DatabaseError>;
|
||||
|
||||
@@ -258,7 +244,7 @@ impl<'cursor, 'tx, T: Table, CURSOR: DbCursorRO<'tx, T>> std::iter::Iterator
|
||||
|
||||
/// Provides a range iterator to `Cursor` when handling `Table`.
|
||||
/// Also check [`Walker`]
|
||||
pub struct RangeWalker<'cursor, 'tx, T: Table, CURSOR: DbCursorRO<'tx, T>> {
|
||||
pub struct RangeWalker<'cursor, T: Table, CURSOR: DbCursorRO<T>> {
|
||||
/// Cursor to be used to walk through the table.
|
||||
cursor: &'cursor mut CURSOR,
|
||||
/// `(key, value)` where to start the walk.
|
||||
@@ -267,14 +253,12 @@ pub struct RangeWalker<'cursor, 'tx, T: Table, CURSOR: DbCursorRO<'tx, T>> {
|
||||
end_key: Bound<T::Key>,
|
||||
/// flag whether is ended
|
||||
is_done: bool,
|
||||
/// Phantom data for 'tx. As it is only used for `DbCursorRO`.
|
||||
_tx_phantom: PhantomData<&'tx T>,
|
||||
}
|
||||
|
||||
impl<'tx, T, CURSOR> fmt::Debug for RangeWalker<'_, 'tx, T, CURSOR>
|
||||
impl<T, CURSOR> fmt::Debug for RangeWalker<'_, T, CURSOR>
|
||||
where
|
||||
T: Table,
|
||||
CURSOR: DbCursorRO<'tx, T> + fmt::Debug,
|
||||
CURSOR: DbCursorRO<T> + fmt::Debug,
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("RangeWalker")
|
||||
@@ -286,8 +270,8 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<'cursor, 'tx, T: Table, CURSOR: DbCursorRO<'tx, T>> std::iter::Iterator
|
||||
for RangeWalker<'cursor, 'tx, T, CURSOR>
|
||||
impl<'cursor, T: Table, CURSOR: DbCursorRO<T>> std::iter::Iterator
|
||||
for RangeWalker<'cursor, T, CURSOR>
|
||||
{
|
||||
type Item = Result<TableRow<T>, DatabaseError>;
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
@@ -317,7 +301,7 @@ impl<'cursor, 'tx, T: Table, CURSOR: DbCursorRO<'tx, T>> std::iter::Iterator
|
||||
}
|
||||
}
|
||||
|
||||
impl<'cursor, 'tx, T: Table, CURSOR: DbCursorRO<'tx, T>> RangeWalker<'cursor, 'tx, T, CURSOR> {
|
||||
impl<'cursor, T: Table, CURSOR: DbCursorRO<T>> RangeWalker<'cursor, T, CURSOR> {
|
||||
/// construct RangeWalker
|
||||
pub fn new(
|
||||
cursor: &'cursor mut CURSOR,
|
||||
@@ -334,13 +318,11 @@ impl<'cursor, 'tx, T: Table, CURSOR: DbCursorRO<'tx, T>> RangeWalker<'cursor, 't
|
||||
None => true,
|
||||
_ => false,
|
||||
};
|
||||
Self { cursor, start, end_key, is_done, _tx_phantom: std::marker::PhantomData }
|
||||
Self { cursor, start, end_key, is_done }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'cursor, 'tx, T: Table, CURSOR: DbCursorRW<'tx, T> + DbCursorRO<'tx, T>>
|
||||
RangeWalker<'cursor, 'tx, T, CURSOR>
|
||||
{
|
||||
impl<'cursor, T: Table, CURSOR: DbCursorRW<T> + DbCursorRO<T>> RangeWalker<'cursor, T, CURSOR> {
|
||||
/// Delete current item that walker points to.
|
||||
pub fn delete_current(&mut self) -> Result<(), DatabaseError> {
|
||||
self.cursor.delete_current()
|
||||
@@ -352,19 +334,17 @@ impl<'cursor, 'tx, T: Table, CURSOR: DbCursorRW<'tx, T> + DbCursorRO<'tx, T>>
|
||||
/// Reason why we have two lifetimes is to distinguish between `'cursor` lifetime
|
||||
/// and inherited `'tx` lifetime. If there is only one, rust would short circle
|
||||
/// the Cursor lifetime and it wouldn't be possible to use Walker.
|
||||
pub struct DupWalker<'cursor, 'tx, T: DupSort, CURSOR: DbDupCursorRO<'tx, T>> {
|
||||
pub struct DupWalker<'cursor, T: DupSort, CURSOR: DbDupCursorRO<T>> {
|
||||
/// Cursor to be used to walk through the table.
|
||||
pub cursor: &'cursor mut CURSOR,
|
||||
/// Value where to start the walk.
|
||||
pub start: IterPairResult<T>,
|
||||
/// Phantom data for 'tx. As it is only used for `DbDupCursorRO`.
|
||||
pub _tx_phantom: PhantomData<&'tx T>,
|
||||
}
|
||||
|
||||
impl<'tx, T, CURSOR> fmt::Debug for DupWalker<'_, 'tx, T, CURSOR>
|
||||
impl<T, CURSOR> fmt::Debug for DupWalker<'_, T, CURSOR>
|
||||
where
|
||||
T: DupSort,
|
||||
CURSOR: DbDupCursorRO<'tx, T> + fmt::Debug,
|
||||
CURSOR: DbDupCursorRO<T> + fmt::Debug,
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("DupWalker")
|
||||
@@ -374,17 +354,15 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<'cursor, 'tx, T: DupSort, CURSOR: DbCursorRW<'tx, T> + DbDupCursorRO<'tx, T>>
|
||||
DupWalker<'cursor, 'tx, T, CURSOR>
|
||||
{
|
||||
impl<'cursor, T: DupSort, CURSOR: DbCursorRW<T> + DbDupCursorRO<T>> DupWalker<'cursor, T, CURSOR> {
|
||||
/// Delete current item that walker points to.
|
||||
pub fn delete_current(&mut self) -> Result<(), DatabaseError> {
|
||||
self.cursor.delete_current()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'cursor, 'tx, T: DupSort, CURSOR: DbDupCursorRO<'tx, T>> std::iter::Iterator
|
||||
for DupWalker<'cursor, 'tx, T, CURSOR>
|
||||
impl<'cursor, T: DupSort, CURSOR: DbDupCursorRO<T>> std::iter::Iterator
|
||||
for DupWalker<'cursor, T, CURSOR>
|
||||
{
|
||||
type Item = Result<TableRow<T>, DatabaseError>;
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
|
||||
@@ -12,9 +12,9 @@ use std::{fmt::Debug, sync::Arc};
|
||||
/// Sealed trait which cannot be implemented by 3rd parties, exposed only for implementers
|
||||
pub trait DatabaseGAT<'a, __ImplicitBounds: Sealed = Bounds<&'a Self>>: Send + Sync {
|
||||
/// RO database transaction
|
||||
type TX: DbTx<'a> + Send + Sync + Debug;
|
||||
type TX: DbTx + Send + Sync + Debug;
|
||||
/// RW database transaction
|
||||
type TXMut: DbTxMut<'a> + DbTx<'a> + TableImporter<'a> + Send + Sync + Debug;
|
||||
type TXMut: DbTxMut + DbTx + TableImporter + Send + Sync + Debug;
|
||||
}
|
||||
|
||||
/// Main Database trait that spawns transactions to be executed.
|
||||
|
||||
@@ -54,7 +54,7 @@ impl<'a> DbTxMutGAT<'a> for TxMock {
|
||||
type DupCursorMut<T: DupSort> = CursorMock;
|
||||
}
|
||||
|
||||
impl<'a> DbTx<'a> for TxMock {
|
||||
impl DbTx for TxMock {
|
||||
fn get<T: Table>(&self, _key: T::Key) -> Result<Option<T::Value>, DatabaseError> {
|
||||
todo!()
|
||||
}
|
||||
@@ -82,7 +82,7 @@ impl<'a> DbTx<'a> for TxMock {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> DbTxMut<'a> for TxMock {
|
||||
impl DbTxMut for TxMock {
|
||||
fn put<T: Table>(&self, _key: T::Key, _value: T::Value) -> Result<(), DatabaseError> {
|
||||
todo!()
|
||||
}
|
||||
@@ -112,7 +112,7 @@ impl<'a> DbTxMut<'a> for TxMock {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> TableImporter<'a> for TxMock {}
|
||||
impl TableImporter for TxMock {}
|
||||
|
||||
/// Cursor that iterates over table
|
||||
#[derive(Debug)]
|
||||
@@ -120,7 +120,7 @@ pub struct CursorMock {
|
||||
_cursor: u32,
|
||||
}
|
||||
|
||||
impl<'tx, T: Table> DbCursorRO<'tx, T> for CursorMock {
|
||||
impl<T: Table> DbCursorRO<T> for CursorMock {
|
||||
fn first(&mut self) -> PairResult<T> {
|
||||
todo!()
|
||||
}
|
||||
@@ -149,30 +149,27 @@ impl<'tx, T: Table> DbCursorRO<'tx, T> for CursorMock {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn walk<'cursor>(
|
||||
&'cursor mut self,
|
||||
_start_key: Option<T::Key>,
|
||||
) -> Result<Walker<'cursor, 'tx, T, Self>, DatabaseError>
|
||||
fn walk(&mut self, _start_key: Option<T::Key>) -> Result<Walker<'_, T, Self>, DatabaseError>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn walk_range<'cursor>(
|
||||
&'cursor mut self,
|
||||
fn walk_range(
|
||||
&mut self,
|
||||
_range: impl RangeBounds<T::Key>,
|
||||
) -> Result<RangeWalker<'cursor, 'tx, T, Self>, DatabaseError>
|
||||
) -> Result<RangeWalker<'_, T, Self>, DatabaseError>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn walk_back<'cursor>(
|
||||
&'cursor mut self,
|
||||
fn walk_back(
|
||||
&mut self,
|
||||
_start_key: Option<T::Key>,
|
||||
) -> Result<ReverseWalker<'cursor, 'tx, T, Self>, DatabaseError>
|
||||
) -> Result<ReverseWalker<'_, T, Self>, DatabaseError>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
@@ -180,7 +177,7 @@ impl<'tx, T: Table> DbCursorRO<'tx, T> for CursorMock {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tx, T: DupSort> DbDupCursorRO<'tx, T> for CursorMock {
|
||||
impl<T: DupSort> DbDupCursorRO<T> for CursorMock {
|
||||
fn next_dup(&mut self) -> PairResult<T> {
|
||||
todo!()
|
||||
}
|
||||
@@ -201,11 +198,11 @@ impl<'tx, T: DupSort> DbDupCursorRO<'tx, T> for CursorMock {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn walk_dup<'cursor>(
|
||||
&'cursor mut self,
|
||||
fn walk_dup(
|
||||
&mut self,
|
||||
_key: Option<<T>::Key>,
|
||||
_subkey: Option<<T as DupSort>::SubKey>,
|
||||
) -> Result<DupWalker<'cursor, 'tx, T, Self>, DatabaseError>
|
||||
) -> Result<DupWalker<'_, T, Self>, DatabaseError>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
@@ -213,7 +210,7 @@ impl<'tx, T: DupSort> DbDupCursorRO<'tx, T> for CursorMock {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tx, T: Table> DbCursorRW<'tx, T> for CursorMock {
|
||||
impl<T: Table> DbCursorRW<T> for CursorMock {
|
||||
fn upsert(
|
||||
&mut self,
|
||||
_key: <T as Table>::Key,
|
||||
@@ -243,7 +240,7 @@ impl<'tx, T: Table> DbCursorRW<'tx, T> for CursorMock {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tx, T: DupSort> DbDupCursorRW<'tx, T> for CursorMock {
|
||||
impl<T: DupSort> DbDupCursorRW<T> for CursorMock {
|
||||
fn delete_current_duplicates(&mut self) -> Result<(), DatabaseError> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
@@ -102,9 +102,9 @@ pub trait DupSort: Table {
|
||||
}
|
||||
|
||||
/// Allows duplicating tables across databases
|
||||
pub trait TableImporter<'tx>: for<'a> DbTxMut<'a> {
|
||||
pub trait TableImporter: DbTxMut {
|
||||
/// Imports all table data from another transaction.
|
||||
fn import_table<T: Table, R: DbTx<'tx>>(&self, source_tx: &R) -> Result<(), DatabaseError> {
|
||||
fn import_table<T: Table, R: DbTx>(&self, source_tx: &R) -> Result<(), DatabaseError> {
|
||||
let mut destination_cursor = self.cursor_write::<T>()?;
|
||||
|
||||
for kv in source_tx.cursor_read::<T>()?.walk(None)? {
|
||||
@@ -116,7 +116,7 @@ pub trait TableImporter<'tx>: for<'a> DbTxMut<'a> {
|
||||
}
|
||||
|
||||
/// Imports table data from another transaction within a range.
|
||||
fn import_table_with_range<T: Table, R: DbTx<'tx>>(
|
||||
fn import_table_with_range<T: Table, R: DbTx>(
|
||||
&self,
|
||||
source_tx: &R,
|
||||
from: Option<<T as Table>::Key>,
|
||||
@@ -141,7 +141,7 @@ pub trait TableImporter<'tx>: for<'a> DbTxMut<'a> {
|
||||
}
|
||||
|
||||
/// Imports all dupsort data from another transaction.
|
||||
fn import_dupsort<T: DupSort, R: DbTx<'tx>>(&self, source_tx: &R) -> Result<(), DatabaseError> {
|
||||
fn import_dupsort<T: DupSort, R: DbTx>(&self, source_tx: &R) -> Result<(), DatabaseError> {
|
||||
let mut destination_cursor = self.cursor_dup_write::<T>()?;
|
||||
let mut cursor = source_tx.cursor_dup_read::<T>()?;
|
||||
|
||||
|
||||
@@ -11,9 +11,9 @@ use crate::{
|
||||
/// Sealed trait which cannot be implemented by 3rd parties, exposed only for implementers
|
||||
pub trait DbTxGAT<'a, __ImplicitBounds: Sealed = Bounds<&'a Self>>: Send + Sync {
|
||||
/// Cursor GAT
|
||||
type Cursor<T: Table>: DbCursorRO<'a, T> + Send + Sync;
|
||||
type Cursor<T: Table>: DbCursorRO<T> + Send + Sync;
|
||||
/// DupCursor GAT
|
||||
type DupCursor<T: DupSort>: DbDupCursorRO<'a, T> + DbCursorRO<'a, T> + Send + Sync;
|
||||
type DupCursor<T: DupSort>: DbDupCursorRO<T> + DbCursorRO<T> + Send + Sync;
|
||||
}
|
||||
|
||||
/// Implements the GAT method from:
|
||||
@@ -22,18 +22,18 @@ pub trait DbTxGAT<'a, __ImplicitBounds: Sealed = Bounds<&'a Self>>: Send + Sync
|
||||
/// Sealed trait which cannot be implemented by 3rd parties, exposed only for implementers
|
||||
pub trait DbTxMutGAT<'a, __ImplicitBounds: Sealed = Bounds<&'a Self>>: Send + Sync {
|
||||
/// Cursor GAT
|
||||
type CursorMut<T: Table>: DbCursorRW<'a, T> + DbCursorRO<'a, T> + Send + Sync;
|
||||
type CursorMut<T: Table>: DbCursorRW<T> + DbCursorRO<T> + Send + Sync;
|
||||
/// DupCursor GAT
|
||||
type DupCursorMut<T: DupSort>: DbDupCursorRW<'a, T>
|
||||
+ DbCursorRW<'a, T>
|
||||
+ DbDupCursorRO<'a, T>
|
||||
+ DbCursorRO<'a, T>
|
||||
type DupCursorMut<T: DupSort>: DbDupCursorRW<T>
|
||||
+ DbCursorRW<T>
|
||||
+ DbDupCursorRO<T>
|
||||
+ DbCursorRO<T>
|
||||
+ Send
|
||||
+ Sync;
|
||||
}
|
||||
|
||||
/// Read only transaction
|
||||
pub trait DbTx<'tx>: for<'a> DbTxGAT<'a> {
|
||||
pub trait DbTx: for<'a> DbTxGAT<'a> {
|
||||
/// Get value
|
||||
fn get<T: Table>(&self, key: T::Key) -> Result<Option<T::Value>, DatabaseError>;
|
||||
/// Commit for read only transaction will consume and free transaction and allows
|
||||
@@ -52,7 +52,7 @@ pub trait DbTx<'tx>: for<'a> DbTxGAT<'a> {
|
||||
}
|
||||
|
||||
/// Read write transaction that allows writing to database
|
||||
pub trait DbTxMut<'tx>: for<'a> DbTxMutGAT<'a> {
|
||||
pub trait DbTxMut: for<'a> DbTxMutGAT<'a> {
|
||||
/// Put value to database
|
||||
fn put<T: Table>(&self, key: T::Key, value: T::Value) -> Result<(), DatabaseError>;
|
||||
/// Delete value from database
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//! Cursor wrapper for libmdbx-sys.
|
||||
|
||||
use reth_interfaces::db::DatabaseWriteOperation;
|
||||
use std::{borrow::Cow, collections::Bound, marker::PhantomData, ops::RangeBounds};
|
||||
use std::{borrow::Cow, collections::Bound, ops::RangeBounds};
|
||||
|
||||
use crate::{
|
||||
common::{PairResult, ValueOnlyResult},
|
||||
@@ -55,7 +55,7 @@ macro_rules! compress_or_ref {
|
||||
};
|
||||
}
|
||||
|
||||
impl<'tx, K: TransactionKind, T: Table> DbCursorRO<'tx, T> for Cursor<'tx, K, T> {
|
||||
impl<K: TransactionKind, T: Table> DbCursorRO<T> for Cursor<'_, K, T> {
|
||||
fn first(&mut self) -> PairResult<T> {
|
||||
decode!(self.inner.first())
|
||||
}
|
||||
@@ -84,10 +84,7 @@ impl<'tx, K: TransactionKind, T: Table> DbCursorRO<'tx, T> for Cursor<'tx, K, T>
|
||||
decode!(self.inner.get_current())
|
||||
}
|
||||
|
||||
fn walk<'cursor>(
|
||||
&'cursor mut self,
|
||||
start_key: Option<T::Key>,
|
||||
) -> Result<Walker<'cursor, 'tx, T, Self>, DatabaseError>
|
||||
fn walk(&mut self, start_key: Option<T::Key>) -> Result<Walker<'_, T, Self>, DatabaseError>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
@@ -103,10 +100,10 @@ impl<'tx, K: TransactionKind, T: Table> DbCursorRO<'tx, T> for Cursor<'tx, K, T>
|
||||
Ok(Walker::new(self, start))
|
||||
}
|
||||
|
||||
fn walk_range<'cursor>(
|
||||
&'cursor mut self,
|
||||
fn walk_range(
|
||||
&mut self,
|
||||
range: impl RangeBounds<T::Key>,
|
||||
) -> Result<RangeWalker<'cursor, 'tx, T, Self>, DatabaseError>
|
||||
) -> Result<RangeWalker<'_, T, Self>, DatabaseError>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
@@ -123,10 +120,10 @@ impl<'tx, K: TransactionKind, T: Table> DbCursorRO<'tx, T> for Cursor<'tx, K, T>
|
||||
Ok(RangeWalker::new(self, start, range.end_bound().cloned()))
|
||||
}
|
||||
|
||||
fn walk_back<'cursor>(
|
||||
&'cursor mut self,
|
||||
fn walk_back(
|
||||
&mut self,
|
||||
start_key: Option<T::Key>,
|
||||
) -> Result<ReverseWalker<'cursor, 'tx, T, Self>, DatabaseError>
|
||||
) -> Result<ReverseWalker<'_, T, Self>, DatabaseError>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
@@ -141,7 +138,7 @@ impl<'tx, K: TransactionKind, T: Table> DbCursorRO<'tx, T> for Cursor<'tx, K, T>
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tx, K: TransactionKind, T: DupSort> DbDupCursorRO<'tx, T> for Cursor<'tx, K, T> {
|
||||
impl<K: TransactionKind, T: DupSort> DbDupCursorRO<T> for Cursor<'_, K, T> {
|
||||
/// Returns the next `(key, value)` pair of a DUPSORT table.
|
||||
fn next_dup(&mut self) -> PairResult<T> {
|
||||
decode!(self.inner.next_dup())
|
||||
@@ -179,11 +176,11 @@ impl<'tx, K: TransactionKind, T: DupSort> DbDupCursorRO<'tx, T> for Cursor<'tx,
|
||||
/// - None, Some(subkey): like first case, but in the first key
|
||||
/// - None, None: first item in the table
|
||||
/// of a DUPSORT table.
|
||||
fn walk_dup<'cursor>(
|
||||
&'cursor mut self,
|
||||
fn walk_dup(
|
||||
&mut self,
|
||||
key: Option<T::Key>,
|
||||
subkey: Option<T::SubKey>,
|
||||
) -> Result<DupWalker<'cursor, 'tx, T, Self>, DatabaseError> {
|
||||
) -> Result<DupWalker<'_, T, Self>, DatabaseError> {
|
||||
let start = match (key, subkey) {
|
||||
(Some(key), Some(subkey)) => {
|
||||
// encode key and decode it after.
|
||||
@@ -218,11 +215,11 @@ impl<'tx, K: TransactionKind, T: DupSort> DbDupCursorRO<'tx, T> for Cursor<'tx,
|
||||
(None, None) => self.first().transpose(),
|
||||
};
|
||||
|
||||
Ok(DupWalker::<'cursor, 'tx, T, Self> { cursor: self, start, _tx_phantom: PhantomData {} })
|
||||
Ok(DupWalker::<'_, T, Self> { cursor: self, start })
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tx, T: Table> DbCursorRW<'tx, T> for Cursor<'tx, RW, T> {
|
||||
impl<T: Table> DbCursorRW<T> for Cursor<'_, RW, T> {
|
||||
/// Database operation that will update an existing row if a specified value already
|
||||
/// exists in a table, and insert a new row if the specified value doesn't already exist
|
||||
///
|
||||
@@ -274,7 +271,7 @@ impl<'tx, T: Table> DbCursorRW<'tx, T> for Cursor<'tx, RW, T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tx, T: DupSort> DbDupCursorRW<'tx, T> for Cursor<'tx, RW, T> {
|
||||
impl<T: DupSort> DbDupCursorRW<T> for Cursor<'_, RW, T> {
|
||||
fn delete_current_duplicates(&mut self) -> Result<(), DatabaseError> {
|
||||
self.inner.del(WriteFlags::NO_DUP_DATA).map_err(|e| DatabaseError::Delete(e.into()))
|
||||
}
|
||||
|
||||
@@ -79,9 +79,9 @@ impl<'a, K: TransactionKind, E: EnvironmentKind> DbTxMutGAT<'a> for Tx<'_, K, E>
|
||||
type DupCursorMut<T: DupSort> = Cursor<'a, RW, T>;
|
||||
}
|
||||
|
||||
impl<'a, E: EnvironmentKind> TableImporter<'a> for Tx<'_, RW, E> {}
|
||||
impl<E: EnvironmentKind> TableImporter for Tx<'_, RW, E> {}
|
||||
|
||||
impl<'tx, K: TransactionKind, E: EnvironmentKind> DbTx<'tx> for Tx<'tx, K, E> {
|
||||
impl<K: TransactionKind, E: EnvironmentKind> DbTx for Tx<'_, K, E> {
|
||||
fn get<T: Table>(&self, key: T::Key) -> Result<Option<<T as Table>::Value>, DatabaseError> {
|
||||
self.inner
|
||||
.get(self.get_dbi::<T>()?, key.encode().as_ref())
|
||||
@@ -123,7 +123,7 @@ impl<'tx, K: TransactionKind, E: EnvironmentKind> DbTx<'tx> for Tx<'tx, K, E> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: EnvironmentKind> DbTxMut<'_> for Tx<'_, RW, E> {
|
||||
impl<E: EnvironmentKind> DbTxMut for Tx<'_, RW, E> {
|
||||
fn put<T: Table>(&self, key: T::Key, value: T::Value) -> Result<(), DatabaseError> {
|
||||
let key = key.encode();
|
||||
self.inner
|
||||
|
||||
@@ -32,12 +32,12 @@ macro_rules! generate_snapshot_func {
|
||||
/// * `row_count`: Total rows to add to `NippyJar`. Must match row count in `range`.
|
||||
/// * `nippy_jar`: Snapshot object responsible for file generation.
|
||||
#[allow(non_snake_case)]
|
||||
pub fn [<create_snapshot$(_ $tbl)+>]<'tx,
|
||||
pub fn [<create_snapshot$(_ $tbl)+>]<
|
||||
$($tbl: Table<Key=K>,)+
|
||||
K
|
||||
>
|
||||
(
|
||||
tx: &impl DbTx<'tx>,
|
||||
tx: &impl DbTx,
|
||||
range: RangeInclusive<K>,
|
||||
additional: Option<Vec<Box<dyn Iterator<Item = Result<Vec<u8>, Box<dyn StdError + Send + Sync>>>>>>,
|
||||
dict_compression_set: Option<Vec<impl Iterator<Item = Vec<u8>>>>,
|
||||
|
||||
@@ -193,10 +193,7 @@ impl BundleStateWithReceipts {
|
||||
/// # Returns
|
||||
///
|
||||
/// The state root for this [BundleState].
|
||||
pub fn state_root_slow<'a, 'tx, TX: DbTx<'tx>>(
|
||||
&self,
|
||||
tx: &'a TX,
|
||||
) -> Result<B256, StateRootError> {
|
||||
pub fn state_root_slow<TX: DbTx>(&self, tx: &TX) -> Result<B256, StateRootError> {
|
||||
let hashed_post_state = self.hash_state_slow();
|
||||
let (account_prefix_set, storage_prefix_set) = hashed_post_state.construct_prefix_sets();
|
||||
let hashed_cursor_factory = HashedPostStateCursorFactory::new(tx, &hashed_post_state);
|
||||
@@ -338,7 +335,7 @@ impl BundleStateWithReceipts {
|
||||
///
|
||||
/// `omit_changed_check` should be set to true of bundle has some of it data
|
||||
/// detached, This would make some original values not known.
|
||||
pub fn write_to_db<'a, TX: DbTxMut<'a> + DbTx<'a>>(
|
||||
pub fn write_to_db<TX: DbTxMut + DbTx>(
|
||||
self,
|
||||
tx: &TX,
|
||||
is_value_known: OriginalValuesKnown,
|
||||
|
||||
@@ -21,10 +21,7 @@ impl From<StateChangeset> for StateChanges {
|
||||
|
||||
impl StateChanges {
|
||||
/// Write the post state to the database.
|
||||
pub fn write_to_db<'a, TX: DbTxMut<'a> + DbTx<'a>>(
|
||||
mut self,
|
||||
tx: &TX,
|
||||
) -> Result<(), DatabaseError> {
|
||||
pub fn write_to_db<TX: DbTxMut + DbTx>(mut self, tx: &TX) -> Result<(), DatabaseError> {
|
||||
// sort all entries so they can be written to database in more performant way.
|
||||
// and take smaller memory footprint.
|
||||
self.0.accounts.par_sort_by_key(|a| a.0);
|
||||
|
||||
@@ -25,7 +25,7 @@ impl StateReverts {
|
||||
/// Write reverts to database.
|
||||
///
|
||||
/// Note:: Reverts will delete all wiped storage from plain state.
|
||||
pub fn write_to_db<'a, TX: DbTxMut<'a> + DbTx<'a>>(
|
||||
pub fn write_to_db<TX: DbTxMut + DbTx>(
|
||||
self,
|
||||
tx: &TX,
|
||||
first_block: BlockNumber,
|
||||
|
||||
@@ -50,7 +50,7 @@ use std::{
|
||||
};
|
||||
|
||||
/// A [`DatabaseProvider`] that holds a read-only database transaction.
|
||||
pub type DatabaseProviderRO<'this, DB> = DatabaseProvider<'this, <DB as DatabaseGAT<'this>>::TX>;
|
||||
pub type DatabaseProviderRO<'this, DB> = DatabaseProvider<<DB as DatabaseGAT<'this>>::TX>;
|
||||
|
||||
/// A [`DatabaseProvider`] that holds a read-write database transaction.
|
||||
///
|
||||
@@ -58,18 +58,18 @@ pub type DatabaseProviderRO<'this, DB> = DatabaseProvider<'this, <DB as Database
|
||||
/// Once that issue is solved, we can probably revert back to being an alias type.
|
||||
#[derive(Debug)]
|
||||
pub struct DatabaseProviderRW<'this, DB: Database>(
|
||||
pub DatabaseProvider<'this, <DB as DatabaseGAT<'this>>::TXMut>,
|
||||
pub DatabaseProvider<<DB as DatabaseGAT<'this>>::TXMut>,
|
||||
);
|
||||
|
||||
impl<'this, DB: Database> Deref for DatabaseProviderRW<'this, DB> {
|
||||
type Target = DatabaseProvider<'this, <DB as DatabaseGAT<'this>>::TXMut>;
|
||||
type Target = DatabaseProvider<<DB as DatabaseGAT<'this>>::TXMut>;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<'this, DB: Database> DerefMut for DatabaseProviderRW<'this, DB> {
|
||||
impl<DB: Database> DerefMut for DatabaseProviderRW<'_, DB> {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut self.0
|
||||
}
|
||||
@@ -90,21 +90,17 @@ impl<'this, DB: Database> DatabaseProviderRW<'this, DB> {
|
||||
/// A provider struct that fetchs data from the database.
|
||||
/// Wrapper around [`DbTx`] and [`DbTxMut`]. Example: [`HeaderProvider`] [`BlockHashReader`]
|
||||
#[derive(Debug)]
|
||||
pub struct DatabaseProvider<'this, TX>
|
||||
where
|
||||
Self: 'this,
|
||||
{
|
||||
pub struct DatabaseProvider<TX> {
|
||||
/// Database transaction.
|
||||
tx: TX,
|
||||
/// Chain spec
|
||||
chain_spec: Arc<ChainSpec>,
|
||||
_phantom_data: std::marker::PhantomData<&'this TX>,
|
||||
}
|
||||
|
||||
impl<'this, TX: DbTxMut<'this>> DatabaseProvider<'this, TX> {
|
||||
impl<TX: DbTxMut> DatabaseProvider<TX> {
|
||||
/// Creates a provider with an inner read-write transaction.
|
||||
pub fn new_rw(tx: TX, chain_spec: Arc<ChainSpec>) -> Self {
|
||||
Self { tx, chain_spec, _phantom_data: std::marker::PhantomData }
|
||||
Self { tx, chain_spec }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,7 +116,7 @@ impl<'this, TX: DbTxMut<'this>> DatabaseProvider<'this, TX> {
|
||||
/// The boundary shard (the shard is split by the block number) is removed from the database. Any
|
||||
/// indices that are above the block number are filtered out. The boundary shard is returned for
|
||||
/// reinsertion (if it's not empty).
|
||||
fn unwind_history_shards<'a, S, T, C>(
|
||||
fn unwind_history_shards<S, T, C>(
|
||||
cursor: &mut C,
|
||||
start_key: T::Key,
|
||||
block_number: BlockNumber,
|
||||
@@ -129,7 +125,7 @@ fn unwind_history_shards<'a, S, T, C>(
|
||||
where
|
||||
T: Table<Value = BlockNumberList>,
|
||||
T::Key: AsRef<ShardedKey<S>>,
|
||||
C: DbCursorRO<'a, T> + DbCursorRW<'a, T>,
|
||||
C: DbCursorRO<T> + DbCursorRW<T>,
|
||||
{
|
||||
let mut item = cursor.seek_exact(start_key)?;
|
||||
while let Some((sharded_key, list)) = item {
|
||||
@@ -156,10 +152,10 @@ where
|
||||
Ok(Vec::new())
|
||||
}
|
||||
|
||||
impl<'this, TX: DbTx<'this>> DatabaseProvider<'this, TX> {
|
||||
impl<TX: DbTx> DatabaseProvider<TX> {
|
||||
/// Creates a provider with an inner read-only transaction.
|
||||
pub fn new(tx: TX, chain_spec: Arc<ChainSpec>) -> Self {
|
||||
Self { tx, chain_spec, _phantom_data: std::marker::PhantomData }
|
||||
Self { tx, chain_spec }
|
||||
}
|
||||
|
||||
/// Consume `DbTx` or `DbTxMut`.
|
||||
@@ -189,7 +185,7 @@ impl<'this, TX: DbTx<'this>> DatabaseProvider<'this, TX> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'this, TX: DbTxMut<'this> + DbTx<'this>> DatabaseProvider<'this, TX> {
|
||||
impl<TX: DbTxMut + DbTx> DatabaseProvider<TX> {
|
||||
/// Commit database transaction.
|
||||
pub fn commit(self) -> RethResult<bool> {
|
||||
Ok(self.tx.commit()?)
|
||||
@@ -795,13 +791,13 @@ impl<'this, TX: DbTxMut<'this> + DbTx<'this>> DatabaseProvider<'this, TX> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'this, TX: DbTx<'this>> AccountReader for DatabaseProvider<'this, TX> {
|
||||
impl<TX: DbTx> AccountReader for DatabaseProvider<TX> {
|
||||
fn basic_account(&self, address: Address) -> RethResult<Option<Account>> {
|
||||
Ok(self.tx.get::<tables::PlainAccountState>(address)?)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'this, TX: DbTx<'this>> AccountExtReader for DatabaseProvider<'this, TX> {
|
||||
impl<TX: DbTx> AccountExtReader for DatabaseProvider<TX> {
|
||||
fn changed_accounts_with_range(
|
||||
&self,
|
||||
range: impl RangeBounds<BlockNumber>,
|
||||
@@ -845,7 +841,7 @@ impl<'this, TX: DbTx<'this>> AccountExtReader for DatabaseProvider<'this, TX> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'this, TX: DbTx<'this>> ChangeSetReader for DatabaseProvider<'this, TX> {
|
||||
impl<TX: DbTx> ChangeSetReader for DatabaseProvider<TX> {
|
||||
fn account_block_changeset(
|
||||
&self,
|
||||
block_number: BlockNumber,
|
||||
@@ -862,7 +858,7 @@ impl<'this, TX: DbTx<'this>> ChangeSetReader for DatabaseProvider<'this, TX> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'this, TX: DbTx<'this>> HeaderProvider for DatabaseProvider<'this, TX> {
|
||||
impl<TX: DbTx> HeaderProvider for DatabaseProvider<TX> {
|
||||
fn header(&self, block_hash: &BlockHash) -> RethResult<Option<Header>> {
|
||||
if let Some(num) = self.block_number(*block_hash)? {
|
||||
Ok(self.header_by_number(num)?)
|
||||
@@ -928,7 +924,7 @@ impl<'this, TX: DbTx<'this>> HeaderProvider for DatabaseProvider<'this, TX> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'this, TX: DbTx<'this>> BlockHashReader for DatabaseProvider<'this, TX> {
|
||||
impl<TX: DbTx> BlockHashReader for DatabaseProvider<TX> {
|
||||
fn block_hash(&self, number: u64) -> RethResult<Option<B256>> {
|
||||
Ok(self.tx.get::<tables::CanonicalHeaders>(number)?)
|
||||
}
|
||||
@@ -947,7 +943,7 @@ impl<'this, TX: DbTx<'this>> BlockHashReader for DatabaseProvider<'this, TX> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'this, TX: DbTx<'this>> BlockNumReader for DatabaseProvider<'this, TX> {
|
||||
impl<TX: DbTx> BlockNumReader for DatabaseProvider<TX> {
|
||||
fn chain_info(&self) -> RethResult<ChainInfo> {
|
||||
let best_number = self.best_block_number()?;
|
||||
let best_hash = self.block_hash(best_number)?.unwrap_or_default();
|
||||
@@ -970,7 +966,7 @@ impl<'this, TX: DbTx<'this>> BlockNumReader for DatabaseProvider<'this, TX> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'this, TX: DbTx<'this>> BlockReader for DatabaseProvider<'this, TX> {
|
||||
impl<TX: DbTx> BlockReader for DatabaseProvider<TX> {
|
||||
fn find_block_by_hash(&self, hash: B256, source: BlockSource) -> RethResult<Option<Block>> {
|
||||
if source.is_database() {
|
||||
self.block(hash.into())
|
||||
@@ -1145,7 +1141,7 @@ impl<'this, TX: DbTx<'this>> BlockReader for DatabaseProvider<'this, TX> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'this, TX: DbTx<'this>> TransactionsProvider for DatabaseProvider<'this, TX> {
|
||||
impl<TX: DbTx> TransactionsProvider for DatabaseProvider<TX> {
|
||||
fn transaction_id(&self, tx_hash: TxHash) -> RethResult<Option<TxNumber>> {
|
||||
Ok(self.tx.get::<tables::TxHashNumber>(tx_hash)?)
|
||||
}
|
||||
@@ -1294,7 +1290,7 @@ impl<'this, TX: DbTx<'this>> TransactionsProvider for DatabaseProvider<'this, TX
|
||||
}
|
||||
}
|
||||
|
||||
impl<'this, TX: DbTx<'this>> ReceiptProvider for DatabaseProvider<'this, TX> {
|
||||
impl<TX: DbTx> ReceiptProvider for DatabaseProvider<TX> {
|
||||
fn receipt(&self, id: TxNumber) -> RethResult<Option<Receipt>> {
|
||||
Ok(self.tx.get::<tables::Receipts>(id)?)
|
||||
}
|
||||
@@ -1327,7 +1323,7 @@ impl<'this, TX: DbTx<'this>> ReceiptProvider for DatabaseProvider<'this, TX> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'this, TX: DbTx<'this>> WithdrawalsProvider for DatabaseProvider<'this, TX> {
|
||||
impl<TX: DbTx> WithdrawalsProvider for DatabaseProvider<TX> {
|
||||
fn withdrawals_by_block(
|
||||
&self,
|
||||
id: BlockHashOrNumber,
|
||||
@@ -1355,7 +1351,7 @@ impl<'this, TX: DbTx<'this>> WithdrawalsProvider for DatabaseProvider<'this, TX>
|
||||
}
|
||||
}
|
||||
|
||||
impl<'this, TX: DbTx<'this>> EvmEnvProvider for DatabaseProvider<'this, TX> {
|
||||
impl<TX: DbTx> EvmEnvProvider for DatabaseProvider<TX> {
|
||||
fn fill_env_at(
|
||||
&self,
|
||||
cfg: &mut CfgEnv,
|
||||
@@ -1426,7 +1422,7 @@ impl<'this, TX: DbTx<'this>> EvmEnvProvider for DatabaseProvider<'this, TX> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'this, TX: DbTx<'this>> StageCheckpointReader for DatabaseProvider<'this, TX> {
|
||||
impl<TX: DbTx> StageCheckpointReader for DatabaseProvider<TX> {
|
||||
fn get_stage_checkpoint(&self, id: StageId) -> RethResult<Option<StageCheckpoint>> {
|
||||
Ok(self.tx.get::<tables::SyncStage>(id.to_string())?)
|
||||
}
|
||||
@@ -1437,7 +1433,7 @@ impl<'this, TX: DbTx<'this>> StageCheckpointReader for DatabaseProvider<'this, T
|
||||
}
|
||||
}
|
||||
|
||||
impl<'this, TX: DbTxMut<'this>> StageCheckpointWriter for DatabaseProvider<'this, TX> {
|
||||
impl<TX: DbTxMut> StageCheckpointWriter for DatabaseProvider<TX> {
|
||||
/// Save stage checkpoint progress.
|
||||
fn save_stage_checkpoint_progress(&self, id: StageId, checkpoint: Vec<u8>) -> RethResult<()> {
|
||||
Ok(self.tx.put::<tables::SyncStageProgress>(id.to_string(), checkpoint)?)
|
||||
@@ -1470,7 +1466,7 @@ impl<'this, TX: DbTxMut<'this>> StageCheckpointWriter for DatabaseProvider<'this
|
||||
}
|
||||
}
|
||||
|
||||
impl<'this, TX: DbTx<'this>> StorageReader for DatabaseProvider<'this, TX> {
|
||||
impl<TX: DbTx> StorageReader for DatabaseProvider<TX> {
|
||||
fn plainstate_storages(
|
||||
&self,
|
||||
addresses_with_keys: impl IntoIterator<Item = (Address, impl IntoIterator<Item = B256>)>,
|
||||
@@ -1533,7 +1529,7 @@ impl<'this, TX: DbTx<'this>> StorageReader for DatabaseProvider<'this, TX> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'this, TX: DbTxMut<'this> + DbTx<'this>> HashingWriter for DatabaseProvider<'this, TX> {
|
||||
impl<TX: DbTxMut + DbTx> HashingWriter for DatabaseProvider<TX> {
|
||||
fn insert_hashes(
|
||||
&self,
|
||||
range: RangeInclusive<BlockNumber>,
|
||||
@@ -1768,7 +1764,7 @@ impl<'this, TX: DbTxMut<'this> + DbTx<'this>> HashingWriter for DatabaseProvider
|
||||
}
|
||||
}
|
||||
|
||||
impl<'this, TX: DbTxMut<'this> + DbTx<'this>> HistoryWriter for DatabaseProvider<'this, TX> {
|
||||
impl<TX: DbTxMut + DbTx> HistoryWriter for DatabaseProvider<TX> {
|
||||
fn calculate_history_indices(&self, range: RangeInclusive<BlockNumber>) -> RethResult<()> {
|
||||
// account history stage
|
||||
{
|
||||
@@ -1900,7 +1896,7 @@ impl<'this, TX: DbTxMut<'this> + DbTx<'this>> HistoryWriter for DatabaseProvider
|
||||
}
|
||||
}
|
||||
|
||||
impl<'this, TX: DbTxMut<'this> + DbTx<'this>> BlockExecutionWriter for DatabaseProvider<'this, TX> {
|
||||
impl<TX: DbTxMut + DbTx> BlockExecutionWriter for DatabaseProvider<TX> {
|
||||
/// Return range of blocks and its execution result
|
||||
fn get_or_take_block_and_execution_range<const TAKE: bool>(
|
||||
&self,
|
||||
@@ -1999,7 +1995,7 @@ impl<'this, TX: DbTxMut<'this> + DbTx<'this>> BlockExecutionWriter for DatabaseP
|
||||
}
|
||||
}
|
||||
|
||||
impl<'this, TX: DbTxMut<'this> + DbTx<'this>> BlockWriter for DatabaseProvider<'this, TX> {
|
||||
impl<TX: DbTxMut + DbTx> BlockWriter for DatabaseProvider<TX> {
|
||||
fn insert_block(
|
||||
&self,
|
||||
block: SealedBlock,
|
||||
@@ -2134,13 +2130,13 @@ impl<'this, TX: DbTxMut<'this> + DbTx<'this>> BlockWriter for DatabaseProvider<'
|
||||
}
|
||||
}
|
||||
|
||||
impl<'this, TX: DbTx<'this>> PruneCheckpointReader for DatabaseProvider<'this, TX> {
|
||||
impl<TX: DbTx> PruneCheckpointReader for DatabaseProvider<TX> {
|
||||
fn get_prune_checkpoint(&self, segment: PruneSegment) -> RethResult<Option<PruneCheckpoint>> {
|
||||
Ok(self.tx.get::<tables::PruneCheckpoints>(segment)?)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'this, TX: DbTxMut<'this>> PruneCheckpointWriter for DatabaseProvider<'this, TX> {
|
||||
impl<TX: DbTxMut> PruneCheckpointWriter for DatabaseProvider<TX> {
|
||||
fn save_prune_checkpoint(
|
||||
&self,
|
||||
segment: PruneSegment,
|
||||
|
||||
@@ -14,7 +14,6 @@ use reth_interfaces::RethResult;
|
||||
use reth_primitives::{
|
||||
Account, Address, BlockNumber, Bytecode, Bytes, StorageKey, StorageValue, B256,
|
||||
};
|
||||
use std::marker::PhantomData;
|
||||
|
||||
/// State provider for a given block number which takes a tx reference.
|
||||
///
|
||||
@@ -28,15 +27,13 @@ use std::marker::PhantomData;
|
||||
/// - [tables::AccountChangeSet]
|
||||
/// - [tables::StorageChangeSet]
|
||||
#[derive(Debug)]
|
||||
pub struct HistoricalStateProviderRef<'a, 'b, TX: DbTx<'a>> {
|
||||
pub struct HistoricalStateProviderRef<'b, TX: DbTx> {
|
||||
/// Transaction
|
||||
tx: &'b TX,
|
||||
/// Block number is main index for the history state of accounts and storages.
|
||||
block_number: BlockNumber,
|
||||
/// Lowest blocks at which different parts of the state are available.
|
||||
lowest_available_blocks: LowestAvailableBlocks,
|
||||
/// Phantom lifetime `'a`
|
||||
_phantom: PhantomData<&'a TX>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
@@ -47,15 +44,10 @@ pub enum HistoryInfo {
|
||||
MaybeInPlainState,
|
||||
}
|
||||
|
||||
impl<'a, 'b, TX: DbTx<'a>> HistoricalStateProviderRef<'a, 'b, TX> {
|
||||
impl<'b, TX: DbTx> HistoricalStateProviderRef<'b, TX> {
|
||||
/// Create new StateProvider for historical block number
|
||||
pub fn new(tx: &'b TX, block_number: BlockNumber) -> Self {
|
||||
Self {
|
||||
tx,
|
||||
block_number,
|
||||
lowest_available_blocks: Default::default(),
|
||||
_phantom: PhantomData {},
|
||||
}
|
||||
Self { tx, block_number, lowest_available_blocks: Default::default() }
|
||||
}
|
||||
|
||||
/// Create new StateProvider for historical block number and lowest block numbers at which
|
||||
@@ -65,7 +57,7 @@ impl<'a, 'b, TX: DbTx<'a>> HistoricalStateProviderRef<'a, 'b, TX> {
|
||||
block_number: BlockNumber,
|
||||
lowest_available_blocks: LowestAvailableBlocks,
|
||||
) -> Self {
|
||||
Self { tx, block_number, lowest_available_blocks, _phantom: PhantomData {} }
|
||||
Self { tx, block_number, lowest_available_blocks }
|
||||
}
|
||||
|
||||
/// Lookup an account in the AccountHistory table
|
||||
@@ -159,7 +151,7 @@ impl<'a, 'b, TX: DbTx<'a>> HistoricalStateProviderRef<'a, 'b, TX> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'b, TX: DbTx<'a>> AccountReader for HistoricalStateProviderRef<'a, 'b, TX> {
|
||||
impl<'b, TX: DbTx> AccountReader for HistoricalStateProviderRef<'b, TX> {
|
||||
/// Get basic account information.
|
||||
fn basic_account(&self, address: Address) -> RethResult<Option<Account>> {
|
||||
match self.account_history_lookup(address)? {
|
||||
@@ -181,7 +173,7 @@ impl<'a, 'b, TX: DbTx<'a>> AccountReader for HistoricalStateProviderRef<'a, 'b,
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'b, TX: DbTx<'a>> BlockHashReader for HistoricalStateProviderRef<'a, 'b, TX> {
|
||||
impl<'b, TX: DbTx> BlockHashReader for HistoricalStateProviderRef<'b, TX> {
|
||||
/// Get block hash by number.
|
||||
fn block_hash(&self, number: u64) -> RethResult<Option<B256>> {
|
||||
self.tx.get::<tables::CanonicalHeaders>(number).map_err(Into::into)
|
||||
@@ -205,13 +197,13 @@ impl<'a, 'b, TX: DbTx<'a>> BlockHashReader for HistoricalStateProviderRef<'a, 'b
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'b, TX: DbTx<'a>> StateRootProvider for HistoricalStateProviderRef<'a, 'b, TX> {
|
||||
impl<'b, TX: DbTx> StateRootProvider for HistoricalStateProviderRef<'b, TX> {
|
||||
fn state_root(&self, _post_state: &BundleStateWithReceipts) -> RethResult<B256> {
|
||||
Err(ProviderError::StateRootNotAvailableForHistoricalBlock.into())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'b, TX: DbTx<'a>> StateProvider for HistoricalStateProviderRef<'a, 'b, TX> {
|
||||
impl<'b, TX: DbTx> StateProvider for HistoricalStateProviderRef<'b, TX> {
|
||||
/// Get storage.
|
||||
fn storage(
|
||||
&self,
|
||||
@@ -260,26 +252,19 @@ impl<'a, 'b, TX: DbTx<'a>> StateProvider for HistoricalStateProviderRef<'a, 'b,
|
||||
/// State provider for a given block number.
|
||||
/// For more detailed description, see [HistoricalStateProviderRef].
|
||||
#[derive(Debug)]
|
||||
pub struct HistoricalStateProvider<'a, TX: DbTx<'a>> {
|
||||
pub struct HistoricalStateProvider<TX: DbTx> {
|
||||
/// Database transaction
|
||||
tx: TX,
|
||||
/// State at the block number is the main indexer of the state.
|
||||
block_number: BlockNumber,
|
||||
/// Lowest blocks at which different parts of the state are available.
|
||||
lowest_available_blocks: LowestAvailableBlocks,
|
||||
/// Phantom lifetime `'a`
|
||||
_phantom: PhantomData<&'a TX>,
|
||||
}
|
||||
|
||||
impl<'a, TX: DbTx<'a>> HistoricalStateProvider<'a, TX> {
|
||||
impl<TX: DbTx> HistoricalStateProvider<TX> {
|
||||
/// Create new StateProvider for historical block number
|
||||
pub fn new(tx: TX, block_number: BlockNumber) -> Self {
|
||||
Self {
|
||||
tx,
|
||||
block_number,
|
||||
lowest_available_blocks: Default::default(),
|
||||
_phantom: PhantomData {},
|
||||
}
|
||||
Self { tx, block_number, lowest_available_blocks: Default::default() }
|
||||
}
|
||||
|
||||
/// Set the lowest block number at which the account history is available.
|
||||
@@ -302,7 +287,7 @@ impl<'a, TX: DbTx<'a>> HistoricalStateProvider<'a, TX> {
|
||||
|
||||
/// Returns a new provider that takes the `TX` as reference
|
||||
#[inline(always)]
|
||||
fn as_ref<'b>(&'b self) -> HistoricalStateProviderRef<'a, 'b, TX> {
|
||||
fn as_ref(&self) -> HistoricalStateProviderRef<'_, TX> {
|
||||
HistoricalStateProviderRef::new_with_lowest_available_blocks(
|
||||
&self.tx,
|
||||
self.block_number,
|
||||
@@ -312,7 +297,7 @@ impl<'a, TX: DbTx<'a>> HistoricalStateProvider<'a, TX> {
|
||||
}
|
||||
|
||||
// Delegates all provider impls to [HistoricalStateProviderRef]
|
||||
delegate_provider_impls!(HistoricalStateProvider<'a, TX> where [TX: DbTx<'a>]);
|
||||
delegate_provider_impls!(HistoricalStateProvider<TX> where [TX: DbTx]);
|
||||
|
||||
/// Lowest blocks at which different parts of the state are available.
|
||||
/// They may be [Some] if pruning is enabled.
|
||||
@@ -365,8 +350,8 @@ mod tests {
|
||||
|
||||
fn assert_state_provider<T: StateProvider>() {}
|
||||
#[allow(unused)]
|
||||
fn assert_historical_state_provider<'txn, T: DbTx<'txn> + 'txn>() {
|
||||
assert_state_provider::<HistoricalStateProvider<'txn, T>>();
|
||||
fn assert_historical_state_provider<T: DbTx>() {
|
||||
assert_state_provider::<HistoricalStateProvider<T>>();
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -11,32 +11,29 @@ use reth_interfaces::{provider::ProviderError, RethError, RethResult};
|
||||
use reth_primitives::{
|
||||
keccak256, Account, Address, BlockNumber, Bytecode, Bytes, StorageKey, StorageValue, B256,
|
||||
};
|
||||
use std::marker::PhantomData;
|
||||
|
||||
/// State provider over latest state that takes tx reference.
|
||||
#[derive(Debug)]
|
||||
pub struct LatestStateProviderRef<'a, 'b, TX: DbTx<'a>> {
|
||||
pub struct LatestStateProviderRef<'b, TX: DbTx> {
|
||||
/// database transaction
|
||||
db: &'b TX,
|
||||
/// Phantom data over lifetime
|
||||
phantom: PhantomData<&'a TX>,
|
||||
}
|
||||
|
||||
impl<'a, 'b, TX: DbTx<'a>> LatestStateProviderRef<'a, 'b, TX> {
|
||||
impl<'b, TX: DbTx> LatestStateProviderRef<'b, TX> {
|
||||
/// Create new state provider
|
||||
pub fn new(db: &'b TX) -> Self {
|
||||
Self { db, phantom: PhantomData {} }
|
||||
Self { db }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'b, TX: DbTx<'a>> AccountReader for LatestStateProviderRef<'a, 'b, TX> {
|
||||
impl<'b, TX: DbTx> AccountReader for LatestStateProviderRef<'b, TX> {
|
||||
/// Get basic account information.
|
||||
fn basic_account(&self, address: Address) -> RethResult<Option<Account>> {
|
||||
self.db.get::<tables::PlainAccountState>(address).map_err(Into::into)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'b, TX: DbTx<'a>> BlockHashReader for LatestStateProviderRef<'a, 'b, TX> {
|
||||
impl<'b, TX: DbTx> BlockHashReader for LatestStateProviderRef<'b, TX> {
|
||||
/// Get block hash by number.
|
||||
fn block_hash(&self, number: u64) -> RethResult<Option<B256>> {
|
||||
self.db.get::<tables::CanonicalHeaders>(number).map_err(Into::into)
|
||||
@@ -60,13 +57,13 @@ impl<'a, 'b, TX: DbTx<'a>> BlockHashReader for LatestStateProviderRef<'a, 'b, TX
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'b, TX: DbTx<'a>> StateRootProvider for LatestStateProviderRef<'a, 'b, TX> {
|
||||
impl<'b, TX: DbTx> StateRootProvider for LatestStateProviderRef<'b, TX> {
|
||||
fn state_root(&self, bundle_state: &BundleStateWithReceipts) -> RethResult<B256> {
|
||||
bundle_state.state_root_slow(self.db).map_err(|err| RethError::Database(err.into()))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'b, TX: DbTx<'a>> StateProvider for LatestStateProviderRef<'a, 'b, TX> {
|
||||
impl<'b, TX: DbTx> StateProvider for LatestStateProviderRef<'b, TX> {
|
||||
/// Get storage.
|
||||
fn storage(
|
||||
&self,
|
||||
@@ -107,28 +104,26 @@ impl<'a, 'b, TX: DbTx<'a>> StateProvider for LatestStateProviderRef<'a, 'b, TX>
|
||||
|
||||
/// State provider for the latest state.
|
||||
#[derive(Debug)]
|
||||
pub struct LatestStateProvider<'a, TX: DbTx<'a>> {
|
||||
pub struct LatestStateProvider<TX: DbTx> {
|
||||
/// database transaction
|
||||
db: TX,
|
||||
/// Phantom lifetime `'a`
|
||||
_phantom: PhantomData<&'a TX>,
|
||||
}
|
||||
|
||||
impl<'a, TX: DbTx<'a>> LatestStateProvider<'a, TX> {
|
||||
impl<TX: DbTx> LatestStateProvider<TX> {
|
||||
/// Create new state provider
|
||||
pub fn new(db: TX) -> Self {
|
||||
Self { db, _phantom: PhantomData {} }
|
||||
Self { db }
|
||||
}
|
||||
|
||||
/// Returns a new provider that takes the `TX` as reference
|
||||
#[inline(always)]
|
||||
fn as_ref<'b>(&'b self) -> LatestStateProviderRef<'a, 'b, TX> {
|
||||
fn as_ref(&self) -> LatestStateProviderRef<'_, TX> {
|
||||
LatestStateProviderRef::new(&self.db)
|
||||
}
|
||||
}
|
||||
|
||||
// Delegates all provider impls to [LatestStateProviderRef]
|
||||
delegate_provider_impls!(LatestStateProvider<'a, TX> where [TX: DbTx<'a>]);
|
||||
delegate_provider_impls!(LatestStateProvider<TX> where [TX: DbTx]);
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
@@ -136,7 +131,7 @@ mod tests {
|
||||
|
||||
fn assert_state_provider<T: StateProvider>() {}
|
||||
#[allow(unused)]
|
||||
fn assert_latest_state_provider<'txn, T: DbTx<'txn> + 'txn>() {
|
||||
assert_state_provider::<LatestStateProvider<'txn, T>>();
|
||||
fn assert_latest_state_provider<T: DbTx>() {
|
||||
assert_state_provider::<LatestStateProvider<T>>();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ use reth_db::{
|
||||
};
|
||||
use reth_primitives::{Account, StorageEntry, B256};
|
||||
|
||||
impl<'a, 'tx, TX: DbTx<'tx>> HashedCursorFactory for &'a TX {
|
||||
impl<'a, TX: DbTx> HashedCursorFactory for &'a TX {
|
||||
type AccountCursor = <TX as DbTxGAT<'a>>::Cursor<tables::HashedAccount>;
|
||||
type StorageCursor = <TX as DbTxGAT<'a>>::DupCursor<tables::HashedStorage>;
|
||||
|
||||
@@ -19,9 +19,9 @@ impl<'a, 'tx, TX: DbTx<'tx>> HashedCursorFactory for &'a TX {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tx, C> HashedAccountCursor for C
|
||||
impl<C> HashedAccountCursor for C
|
||||
where
|
||||
C: DbCursorRO<'tx, tables::HashedAccount>,
|
||||
C: DbCursorRO<tables::HashedAccount>,
|
||||
{
|
||||
fn seek(&mut self, key: B256) -> Result<Option<(B256, Account)>, reth_db::DatabaseError> {
|
||||
self.seek(key)
|
||||
@@ -32,9 +32,9 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tx, C> HashedStorageCursor for C
|
||||
impl<C> HashedStorageCursor for C
|
||||
where
|
||||
C: DbCursorRO<'tx, tables::HashedStorage> + DbDupCursorRO<'tx, tables::HashedStorage>,
|
||||
C: DbCursorRO<tables::HashedStorage> + DbDupCursorRO<tables::HashedStorage>,
|
||||
{
|
||||
fn is_storage_empty(&mut self, key: B256) -> Result<bool, reth_db::DatabaseError> {
|
||||
Ok(self.seek_exact(key)?.is_none())
|
||||
|
||||
@@ -169,7 +169,7 @@ impl<'a, 'b, TX> HashedPostStateCursorFactory<'a, 'b, TX> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'b, 'tx, TX: DbTx<'tx>> HashedCursorFactory for HashedPostStateCursorFactory<'a, 'b, TX> {
|
||||
impl<'a, 'b, TX: DbTx> HashedCursorFactory for HashedPostStateCursorFactory<'a, 'b, TX> {
|
||||
type AccountCursor =
|
||||
HashedPostStateAccountCursor<'b, <TX as DbTxGAT<'a>>::Cursor<tables::HashedAccount>>;
|
||||
type StorageCursor =
|
||||
@@ -246,9 +246,9 @@ impl<'b, C> HashedPostStateAccountCursor<'b, C> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'b, 'tx, C> HashedAccountCursor for HashedPostStateAccountCursor<'b, C>
|
||||
impl<'b, C> HashedAccountCursor for HashedPostStateAccountCursor<'b, C>
|
||||
where
|
||||
C: DbCursorRO<'tx, tables::HashedAccount>,
|
||||
C: DbCursorRO<tables::HashedAccount>,
|
||||
{
|
||||
/// Seek the next entry for a given hashed account key.
|
||||
///
|
||||
@@ -408,9 +408,9 @@ impl<'b, C> HashedPostStateStorageCursor<'b, C> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'b, 'tx, C> HashedStorageCursor for HashedPostStateStorageCursor<'b, C>
|
||||
impl<'b, C> HashedStorageCursor for HashedPostStateStorageCursor<'b, C>
|
||||
where
|
||||
C: DbCursorRO<'tx, tables::HashedStorage> + DbDupCursorRO<'tx, tables::HashedStorage>,
|
||||
C: DbCursorRO<tables::HashedStorage> + DbDupCursorRO<tables::HashedStorage>,
|
||||
{
|
||||
/// Returns `true` if the account has no storage entries.
|
||||
///
|
||||
|
||||
@@ -35,10 +35,7 @@ impl<'a, TX> PrefixSetLoader<'a, TX> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'b, TX> PrefixSetLoader<'a, TX>
|
||||
where
|
||||
TX: DbTx<'b>,
|
||||
{
|
||||
impl<'a, TX: DbTx> PrefixSetLoader<'a, TX> {
|
||||
/// Load all account and storage changes for the given block range.
|
||||
pub fn load(
|
||||
self,
|
||||
|
||||
@@ -35,9 +35,9 @@ impl<'a, TX> Proof<'a, TX, &'a TX> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'tx, TX, H> Proof<'a, TX, H>
|
||||
impl<'a, TX, H> Proof<'a, TX, H>
|
||||
where
|
||||
TX: DbTx<'tx>,
|
||||
TX: DbTx,
|
||||
H: HashedCursorFactory + Clone,
|
||||
{
|
||||
/// Generate an account proof from intermediate nodes.
|
||||
|
||||
@@ -95,10 +95,7 @@ impl<'a, TX, H> StateRoot<'a, TX, H> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'tx, TX> StateRoot<'a, TX, &'a TX>
|
||||
where
|
||||
TX: DbTx<'tx>,
|
||||
{
|
||||
impl<'a, TX: DbTx> StateRoot<'a, TX, &'a TX> {
|
||||
/// Create a new [StateRoot] instance.
|
||||
pub fn new(tx: &'a TX) -> Self {
|
||||
Self {
|
||||
@@ -180,9 +177,9 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'tx, TX, H> StateRoot<'a, TX, H>
|
||||
impl<'a, TX, H> StateRoot<'a, TX, H>
|
||||
where
|
||||
TX: DbTx<'tx>,
|
||||
TX: DbTx,
|
||||
H: HashedCursorFactory + Clone,
|
||||
{
|
||||
/// Walks the intermediate nodes of existing state trie (if any) and hashed entries. Feeds the
|
||||
@@ -381,10 +378,7 @@ pub struct StorageRoot<'a, TX, H> {
|
||||
pub changed_prefixes: PrefixSet,
|
||||
}
|
||||
|
||||
impl<'a, 'tx, TX> StorageRoot<'a, TX, &'a TX>
|
||||
where
|
||||
TX: DbTx<'tx>,
|
||||
{
|
||||
impl<'a, TX: DbTx> StorageRoot<'a, TX, &'a TX> {
|
||||
/// Creates a new storage root calculator given an raw address.
|
||||
pub fn new(tx: &'a TX, address: Address) -> Self {
|
||||
Self::new_hashed(tx, keccak256(address))
|
||||
@@ -441,9 +435,9 @@ impl<'a, TX, H> StorageRoot<'a, TX, H> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'tx, TX, H> StorageRoot<'a, TX, H>
|
||||
impl<'a, TX, H> StorageRoot<'a, TX, H>
|
||||
where
|
||||
TX: DbTx<'tx>,
|
||||
TX: DbTx,
|
||||
H: HashedCursorFactory,
|
||||
{
|
||||
/// Walks the hashed storage table entries for a given address and calculates the storage root.
|
||||
@@ -559,8 +553,8 @@ mod tests {
|
||||
use reth_provider::{DatabaseProviderRW, ProviderFactory};
|
||||
use std::{collections::BTreeMap, ops::Mul, str::FromStr};
|
||||
|
||||
fn insert_account<'a, TX: DbTxMut<'a>>(
|
||||
tx: &TX,
|
||||
fn insert_account(
|
||||
tx: &impl DbTxMut,
|
||||
address: Address,
|
||||
account: Account,
|
||||
storage: &BTreeMap<B256, U256>,
|
||||
@@ -570,11 +564,7 @@ mod tests {
|
||||
insert_storage(tx, hashed_address, storage);
|
||||
}
|
||||
|
||||
fn insert_storage<'a, TX: DbTxMut<'a>>(
|
||||
tx: &TX,
|
||||
hashed_address: B256,
|
||||
storage: &BTreeMap<B256, U256>,
|
||||
) {
|
||||
fn insert_storage(tx: &impl DbTxMut, hashed_address: B256, storage: &BTreeMap<B256, U256>) {
|
||||
for (k, v) in storage {
|
||||
tx.put::<tables::HashedStorage>(
|
||||
hashed_address,
|
||||
|
||||
@@ -14,9 +14,9 @@ impl<C> AccountTrieCursor<C> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, C> TrieCursor<StoredNibbles> for AccountTrieCursor<C>
|
||||
impl<C> TrieCursor<StoredNibbles> for AccountTrieCursor<C>
|
||||
where
|
||||
C: DbCursorRO<'a, tables::AccountsTrie>,
|
||||
C: DbCursorRO<tables::AccountsTrie>,
|
||||
{
|
||||
fn seek_exact(
|
||||
&mut self,
|
||||
|
||||
@@ -24,9 +24,9 @@ impl<C> StorageTrieCursor<C> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, C> TrieCursor<StoredNibblesSubKey> for StorageTrieCursor<C>
|
||||
impl<C> TrieCursor<StoredNibblesSubKey> for StorageTrieCursor<C>
|
||||
where
|
||||
C: DbDupCursorRO<'a, tables::StoragesTrie> + DbCursorRO<'a, tables::StoragesTrie>,
|
||||
C: DbDupCursorRO<tables::StoragesTrie> + DbCursorRO<tables::StoragesTrie>,
|
||||
{
|
||||
fn seek_exact(
|
||||
&mut self,
|
||||
|
||||
@@ -105,10 +105,7 @@ impl TrieUpdates {
|
||||
}
|
||||
|
||||
/// Flush updates all aggregated updates to the database.
|
||||
pub fn flush<'a, 'tx, TX>(self, tx: &'a TX) -> Result<(), reth_db::DatabaseError>
|
||||
where
|
||||
TX: DbTx<'tx> + DbTxMut<'tx>,
|
||||
{
|
||||
pub fn flush(self, tx: &(impl DbTx + DbTxMut)) -> Result<(), reth_db::DatabaseError> {
|
||||
if self.trie_operations.is_empty() {
|
||||
return Ok(())
|
||||
}
|
||||
|
||||
@@ -145,9 +145,9 @@ The `Database` trait also implements the `DatabaseGAT` trait which defines two a
|
||||
/// Sealed trait which cannot be implemented by 3rd parties, exposed only for implementers
|
||||
pub trait DatabaseGAT<'a, __ImplicitBounds: Sealed = Bounds<&'a Self>>: Send + Sync {
|
||||
/// RO database transaction
|
||||
type TX: DbTx<'a> + Send + Sync;
|
||||
type TX: DbTx + Send + Sync;
|
||||
/// RW database transaction
|
||||
type TXMut: DbTxMut<'a> + DbTx<'a> + Send + Sync;
|
||||
type TXMut: DbTxMut + DbTx + Send + Sync;
|
||||
}
|
||||
```
|
||||
|
||||
@@ -161,7 +161,7 @@ The `TX` type can be any type that implements the `DbTx` trait, which provides a
|
||||
|
||||
```rust ignore
|
||||
/// Read only transaction
|
||||
pub trait DbTx<'tx>: for<'a> DbTxGAT<'a> {
|
||||
pub trait DbTx: for<'a> DbTxGAT<'a> {
|
||||
/// Get value
|
||||
fn get<T: Table>(&self, key: T::Key) -> Result<Option<T::Value>, Error>;
|
||||
/// Commit for read only transaction will consume and free transaction and allows
|
||||
@@ -180,7 +180,7 @@ The `TXMut` type can be any type that implements the `DbTxMut` trait, which prov
|
||||
|
||||
```rust ignore
|
||||
/// Read write transaction that allows writing to database
|
||||
pub trait DbTxMut<'tx>: for<'a> DbTxMutGAT<'a> {
|
||||
pub trait DbTxMut: for<'a> DbTxMutGAT<'a> {
|
||||
/// Put value to database
|
||||
fn put<T: Table>(&self, key: T::Key, value: T::Value) -> Result<(), Error>;
|
||||
/// Delete value from database
|
||||
@@ -227,7 +227,7 @@ impl<'a, DB: Database> Deref for Transaction<'a, DB> {
|
||||
}
|
||||
```
|
||||
|
||||
The `Transaction` struct implements the `Deref` trait, which returns a reference to its `tx` field, which is a `TxMut`. Recall that `TxMut` is a generic type on the `DatabaseGAT` trait, which is defined as `type TXMut: DbTxMut<'a> + DbTx<'a> + Send + Sync;`, giving it access to all of the functions available to `DbTx`, including the `DbTx::get()` function.
|
||||
The `Transaction` struct implements the `Deref` trait, which returns a reference to its `tx` field, which is a `TxMut`. Recall that `TxMut` is a generic type on the `DatabaseGAT` trait, which is defined as `type TXMut: DbTxMut + DbTx + Send + Sync;`, giving it access to all of the functions available to `DbTx`, including the `DbTx::get()` function.
|
||||
|
||||
Notice that the function uses a [turbofish](https://techblog.tonsser.com/posts/what-is-rusts-turbofish) to define which table to use when passing in the `key` to the `DbTx::get()` function. Taking a quick look at the function definition, a generic `T` is defined that implements the `Table` trait mentioned at the beginning of this chapter.
|
||||
|
||||
|
||||
@@ -150,10 +150,7 @@ pub struct State(BTreeMap<Address, Account>);
|
||||
|
||||
impl State {
|
||||
/// Write the state to the database.
|
||||
pub fn write_to_db<'a, Tx>(&self, tx: &'a Tx) -> Result<(), Error>
|
||||
where
|
||||
Tx: DbTxMut<'a>,
|
||||
{
|
||||
pub fn write_to_db(&self, tx: &impl DbTxMut) -> Result<(), Error> {
|
||||
for (&address, account) in self.0.iter() {
|
||||
let hashed_address = keccak256(address);
|
||||
let has_code = !account.code.is_empty();
|
||||
@@ -211,10 +208,7 @@ impl Account {
|
||||
/// Check that the account matches what is in the database.
|
||||
///
|
||||
/// In case of a mismatch, `Err(Error::Assertion)` is returned.
|
||||
pub fn assert_db<'a, Tx>(&self, address: Address, tx: &'a Tx) -> Result<(), Error>
|
||||
where
|
||||
Tx: DbTx<'a>,
|
||||
{
|
||||
pub fn assert_db(&self, address: Address, tx: &impl DbTx) -> Result<(), Error> {
|
||||
let account = tx.get::<tables::PlainAccountState>(address)?.ok_or_else(|| {
|
||||
Error::Assertion(format!("Expected account ({address:?}) is missing from DB: {self:?}"))
|
||||
})?;
|
||||
|
||||
Reference in New Issue
Block a user