Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: search for a snapshot that fulfills a queried BlockHash or TxHash #5373

Merged
merged 62 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
eb00e6d
add snapshots_path to ChainPath
joshieDo Oct 31, 2023
a8ef221
move HighestSnapshots to primitives crate
joshieDo Oct 31, 2023
f1f32d7
add with_highest_tracker to SnapshotProvider
joshieDo Oct 31, 2023
f5cadf8
add a shared snapshot provvider to providerfactory and dbprovider
joshieDo Oct 31, 2023
1acd28f
add snapshot provider to shared blockchain_db
joshieDo Oct 31, 2023
470a6b6
allow unused for now
joshieDo Oct 31, 2023
113ef23
add get_highest_snapshot to SnapshotProvider
joshieDo Oct 31, 2023
73fd00f
move hihgest snapshot channel inside snapshotter
joshieDo Nov 2, 2023
2fef51f
add default receiver to snapshotter
joshieDo Nov 2, 2023
33dc284
replace with with_snapshot_provider on db provider
joshieDo Nov 2, 2023
cda1ca7
use strum for SnapshotSegment, compression and filters
joshieDo Nov 3, 2023
ec89704
snapshotter takes a directory and reads from it
joshieDo Nov 3, 2023
e87c64d
default snapshot filename doesnt have configuration
joshieDo Nov 3, 2023
99c1fa5
create snapshots directory if it doesnt exist
joshieDo Nov 3, 2023
d4b5f61
add run_segment
joshieDo Nov 3, 2023
dd338e5
add type SegmentConfig
joshieDo Nov 3, 2023
00ec5f5
add docs for directory
joshieDo Nov 8, 2023
c1dd713
replace PathBuf usage
joshieDo Nov 8, 2023
3be09cd
Merge remote-tracking branch 'origin/main' into joshie/db-snap-provider
joshieDo Nov 8, 2023
96b255c
Merge branch 'joshie/db-snap-provider' into joshie/db-snap-provider2
joshieDo Nov 8, 2023
b281144
Merge branch 'joshie/db-snap-provider2' into joshie/db-snap-provider3
joshieDo Nov 8, 2023
704bbfd
snapshot file name contains tx range as well
joshieDo Nov 9, 2023
d21f5cd
add get_segment_provider from block and from tx
joshieDo Nov 9, 2023
2e7f810
add find_snapshot to SnapshotProvider
joshieDo Nov 9, 2023
8244219
implement some more methods for SnapshotProvider
joshieDo Nov 9, 2023
d0699f7
Update crates/snapshot/src/snapshotter.rs
joshieDo Nov 13, 2023
892b260
Update crates/snapshot/src/snapshotter.rs
joshieDo Nov 13, 2023
756d00b
Update crates/storage/provider/src/providers/snapshot/manager.rs
joshieDo Nov 13, 2023
5891d0a
Update crates/primitives/src/snapshot/segment.rs
joshieDo Nov 13, 2023
d2c21e9
Update crates/snapshot/src/snapshotter.rs
joshieDo Nov 13, 2023
a11751f
Merge branch 'joshie/db-snap-provider' into joshie/db-snap-provider2
joshieDo Nov 13, 2023
4574c68
add fn block_range to snapshot cmd
joshieDo Nov 13, 2023
d47812a
add read_dir to primitives fs
joshieDo Nov 13, 2023
e1dca84
Merge branch 'joshie/db-snap-provider2' into joshie/db-snap-provider3
joshieDo Nov 13, 2023
01c8beb
Merge branch 'joshie/db-snap-provider3' into joshie/db-snap-provider4
joshieDo Nov 13, 2023
1be9e29
add HighestSnapshots::as_mut(segment)
joshieDo Nov 13, 2023
95e965a
trait Segment requires Default
joshieDo Nov 13, 2023
06d4382
Merge branch 'joshie/db-snap-provider3' into joshie/db-snap-provider4
joshieDo Nov 13, 2023
12aaa26
add tests for snapshot segment filenames
joshieDo Nov 13, 2023
8dded9e
redo parse_filename
joshieDo Nov 13, 2023
4d0f752
add a couple more tests cases
joshieDo Nov 13, 2023
dd272d1
option ? operator
joshieDo Nov 13, 2023
5c8406c
clippy
joshieDo Nov 13, 2023
23b525f
add docs to parse_filename
joshieDo Nov 13, 2023
5886cdf
use OsStr instead on parse_filename
joshieDo Nov 13, 2023
8932ab3
swap some pathbufs for path
joshieDo Nov 13, 2023
7054744
add reth_primitives::fs::rename
joshieDo Nov 14, 2023
a42ebaa
Merge branch 'joshie/db-snap-provider2' into joshie/db-snap-provider3
joshieDo Nov 14, 2023
a865b59
use reth fs rename
joshieDo Nov 14, 2023
77094e5
clippy
joshieDo Nov 14, 2023
4f440b3
remove done TODO reminder
joshieDo Nov 14, 2023
622159b
Update crates/primitives/src/fs.rs
joshieDo Nov 14, 2023
975b47d
fmt
joshieDo Nov 14, 2023
c9ee102
Merge remote-tracking branch 'origin/main' into joshie/db-snap-provider2
joshieDo Nov 14, 2023
2bd3205
Merge branch 'joshie/db-snap-provider2' into joshie/db-snap-provider3
joshieDo Nov 14, 2023
bceba97
Merge remote-tracking branch 'origin/main' into joshie/db-snap-provider3
joshieDo Nov 14, 2023
986abfd
add missing imports
joshieDo Nov 14, 2023
ad06c2b
Merge branch 'joshie/db-snap-provider3' into joshie/db-snap-provider4
joshieDo Nov 14, 2023
92056e6
Merge remote-tracking branch 'origin/main' into joshie/db-snap-provider4
joshieDo Nov 14, 2023
6b2de08
typo
joshieDo Nov 15, 2023
08850f0
add doc to unwrap or 0
joshieDo Nov 15, 2023
8881812
add different missing snapshot variants
joshieDo Nov 15, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 24 additions & 9 deletions bin/reth/src/db/snapshots/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ use reth_primitives::{
BlockHash, ChainSpec, Header, SnapshotSegment,
};
use reth_provider::{
providers::SnapshotProvider, DatabaseProviderRO, HeaderProvider, ProviderError, ProviderFactory,
providers::SnapshotProvider, DatabaseProviderRO, HeaderProvider, ProviderError,
ProviderFactory, TransactionsProviderExt,
};
use reth_snapshot::{segments, segments::Segment};
use std::{
Expand Down Expand Up @@ -38,9 +39,15 @@ impl Command {
segment.snapshot::<DB>(provider, PathBuf::default(), range.clone())?;

// Default name doesn't have any configuration
let tx_range = provider.transaction_range_by_block_range(range.clone())?;
reth_primitives::fs::rename(
SnapshotSegment::Headers.filename(&range),
SnapshotSegment::Headers.filename_with_configuration(filters, compression, &range),
SnapshotSegment::Headers.filename(&range, &tx_range),
SnapshotSegment::Headers.filename_with_configuration(
filters,
compression,
&range,
&tx_range,
),
)?;

Ok(())
Expand All @@ -61,16 +68,24 @@ impl Command {
Filters::WithoutFilters
};

let range = self.block_range();
let block_range = self.block_range();

let mut row_indexes = range.clone().collect::<Vec<_>>();
let mut row_indexes = block_range.clone().collect::<Vec<_>>();
let mut rng = rand::thread_rng();
let path = SnapshotSegment::Headers
.filename_with_configuration(filters, compression, &range)

let tx_range = ProviderFactory::new(open_db_read_only(db_path, log_level)?, chain.clone())
.provider()?
.transaction_range_by_block_range(block_range.clone())?;

let path: PathBuf = SnapshotSegment::Headers
.filename_with_configuration(filters, compression, &block_range, &tx_range)
.into();
let provider = SnapshotProvider::default();
let jar_provider =
provider.get_segment_provider(SnapshotSegment::Headers, self.from, Some(path))?;
let jar_provider = provider.get_segment_provider_from_block(
SnapshotSegment::Headers,
self.from,
Some(&path),
)?;
let mut cursor = jar_provider.cursor()?;

for bench_kind in [BenchKind::Walk, BenchKind::RandomAll] {
Expand Down
30 changes: 19 additions & 11 deletions bin/reth/src/db/snapshots/receipts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,26 @@ impl Command {
inclusion_filter: InclusionFilter,
phf: PerfectHashingFunction,
) -> eyre::Result<()> {
let range = self.block_range();
let block_range = self.block_range();
let filters = if self.with_filters {
Filters::WithFilters(inclusion_filter, phf)
} else {
Filters::WithoutFilters
};

let segment = segments::Receipts::new(compression, filters);

segment.snapshot::<DB>(provider, PathBuf::default(), range.clone())?;
let segment: segments::Receipts = segments::Receipts::new(compression, filters);
segment.snapshot::<DB>(provider, PathBuf::default(), block_range.clone())?;

// Default name doesn't have any configuration
let tx_range = provider.transaction_range_by_block_range(block_range.clone())?;
reth_primitives::fs::rename(
SnapshotSegment::Receipts.filename(&range),
SnapshotSegment::Receipts.filename_with_configuration(filters, compression, &range),
SnapshotSegment::Receipts.filename(&block_range, &tx_range),
SnapshotSegment::Receipts.filename_with_configuration(
filters,
compression,
&block_range,
&tx_range,
),
)?;

Ok(())
Expand All @@ -62,7 +67,7 @@ impl Command {
Filters::WithoutFilters
};

let block_range = self.from..=(self.from + self.block_interval - 1);
let block_range = self.block_range();

let mut rng = rand::thread_rng();

Expand All @@ -72,13 +77,16 @@ impl Command {

let mut row_indexes = tx_range.clone().collect::<Vec<_>>();

let path = SnapshotSegment::Receipts
.filename_with_configuration(filters, compression, &block_range)
let path: PathBuf = SnapshotSegment::Receipts
.filename_with_configuration(filters, compression, &block_range, &tx_range)
.into();

let provider = SnapshotProvider::default();
let jar_provider =
provider.get_segment_provider(SnapshotSegment::Receipts, self.from, Some(path))?;
let jar_provider = provider.get_segment_provider_from_block(
SnapshotSegment::Receipts,
self.from,
Some(&path),
)?;
let mut cursor = jar_provider.cursor()?;

for bench_kind in [BenchKind::Walk, BenchKind::RandomAll] {
Expand Down
27 changes: 18 additions & 9 deletions bin/reth/src/db/snapshots/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl Command {
inclusion_filter: InclusionFilter,
phf: PerfectHashingFunction,
) -> eyre::Result<()> {
let range = self.block_range();
let block_range = self.block_range();
let filters = if self.with_filters {
Filters::WithFilters(inclusion_filter, phf)
} else {
Expand All @@ -36,12 +36,18 @@ impl Command {

let segment = segments::Transactions::new(compression, filters);

segment.snapshot::<DB>(provider, PathBuf::default(), range.clone())?;
segment.snapshot::<DB>(provider, PathBuf::default(), block_range.clone())?;

// Default name doesn't have any configuration
let tx_range = provider.transaction_range_by_block_range(block_range.clone())?;
reth_primitives::fs::rename(
SnapshotSegment::Transactions.filename(&range),
SnapshotSegment::Transactions.filename_with_configuration(filters, compression, &range),
SnapshotSegment::Transactions.filename(&block_range, &tx_range),
SnapshotSegment::Transactions.filename_with_configuration(
filters,
compression,
&block_range,
&tx_range,
),
)?;

Ok(())
Expand All @@ -62,7 +68,7 @@ impl Command {
Filters::WithoutFilters
};

let block_range = self.from..=(self.from + self.block_interval - 1);
let block_range = self.block_range();

let mut rng = rand::thread_rng();

Expand All @@ -72,12 +78,15 @@ impl Command {

let mut row_indexes = tx_range.clone().collect::<Vec<_>>();

let path = SnapshotSegment::Transactions
.filename_with_configuration(filters, compression, &block_range)
let path: PathBuf = SnapshotSegment::Transactions
.filename_with_configuration(filters, compression, &block_range, &tx_range)
.into();
let provider = SnapshotProvider::default();
let jar_provider =
provider.get_segment_provider(SnapshotSegment::Transactions, self.from, Some(path))?;
let jar_provider = provider.get_segment_provider_from_block(
SnapshotSegment::Transactions,
self.from,
Some(&path),
)?;
let mut cursor = jar_provider.cursor()?;

for bench_kind in [BenchKind::Walk, BenchKind::RandomAll] {
Expand Down
4 changes: 2 additions & 2 deletions crates/consensus/common/src/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,8 +489,8 @@ mod tests {
};
use reth_primitives::{
constants::eip4844::DATA_GAS_PER_BLOB, hex_literal::hex, proofs, Account, Address,
BlockBody, BlockHash, BlockHashOrNumber, Bytes, ChainSpecBuilder, ForkCondition, Header,
Signature, TransactionKind, TransactionSigned, Withdrawal, MAINNET, U256,
BlockBody, BlockHash, BlockHashOrNumber, Bytes, ChainSpecBuilder, Header, Signature,
TransactionKind, TransactionSigned, Withdrawal, MAINNET, U256,
};
use std::ops::RangeBounds;

Expand Down
13 changes: 12 additions & 1 deletion crates/interfaces/src/provider.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use reth_primitives::{
Address, BlockHash, BlockHashOrNumber, BlockNumber, GotExpected, TxHashOrNumber, TxNumber, B256,
Address, BlockHash, BlockHashOrNumber, BlockNumber, GotExpected, SnapshotSegment,
TxHashOrNumber, TxNumber, B256,
};
use std::path::PathBuf;
use thiserror::Error;

/// Bundled errors variants thrown by various providers.
Expand Down Expand Up @@ -94,6 +96,15 @@ pub enum ProviderError {
/// Provider does not support this particular request.
#[error("this provider does not support this request")]
UnsupportedProvider,
/// Snapshot file is not found at specified path.
#[error("not able to find {0} snapshot file at {1}")]
MissingSnapshotPath(SnapshotSegment, PathBuf),
/// Snapshot file is not found for requested block.
#[error("not able to find {0} snapshot file for block number {1}")]
MissingSnapshotBlock(SnapshotSegment, BlockNumber),
/// Snapshot file is not found for requested transaction.
#[error("not able to find {0} snapshot file for transaction id {1}")]
MissingSnapshotTx(SnapshotSegment, TxNumber),
}

/// A root mismatch error at a given block height.
Expand Down
31 changes: 30 additions & 1 deletion crates/primitives/src/snapshot/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ mod compression;
mod filters;
mod segment;

use alloy_primitives::BlockNumber;
use alloy_primitives::{BlockNumber, TxNumber};
pub use compression::Compression;
pub use filters::{Filters, InclusionFilter, PerfectHashingFunction};
pub use segment::{SegmentConfig, SegmentHeader, SnapshotSegment};

use crate::fs::FsPathError;
use std::{ops::RangeInclusive, path::Path};

/// Default snapshot block count.
pub const BLOCKS_PER_SNAPSHOT: u64 = 500_000;

Expand All @@ -35,4 +38,30 @@ impl HighestSnapshots {
SnapshotSegment::Receipts => self.receipts,
}
}

/// Returns a mutable reference to a snapshot segment
pub fn as_mut(&mut self, segment: SnapshotSegment) -> &mut Option<BlockNumber> {
match segment {
SnapshotSegment::Headers => &mut self.headers,
SnapshotSegment::Transactions => &mut self.transactions,
SnapshotSegment::Receipts => &mut self.receipts,
}
}
}

/// Given the snapshot's location, it returns an iterator over the existing snapshots in the format
/// of a tuple composed by the segment, block range and transaction range.
pub fn iter_snapshots(
path: impl AsRef<Path>,
) -> Result<
impl Iterator<Item = (SnapshotSegment, RangeInclusive<BlockNumber>, RangeInclusive<TxNumber>)>,
FsPathError,
> {
let entries = crate::fs::read_dir(path.as_ref())?.filter_map(Result::ok);
Ok(entries.filter_map(|entry| {
if entry.metadata().map_or(false, |metadata| metadata.is_file()) {
return SnapshotSegment::parse_filename(&entry.file_name())
}
None
}))
}
Loading