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: remove const generics from cursors and add segment masks #5181

Merged
merged 81 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
81 commits
Select commit Hold shift + click to select a range
5b429fa
move tx_lookup hash calculation to db crate
joshieDo Oct 6, 2023
8090431
update doc on tx hash calc
joshieDo Oct 6, 2023
f5762b4
Merge remote-tracking branch 'origin/main' into joshie/snapshot-bin-2
joshieDo Oct 12, 2023
8eaab06
use databaseprovider instead
joshieDo Oct 12, 2023
95e0574
add find_transaction_range
joshieDo Oct 12, 2023
e03a3c0
cargo deps
joshieDo Oct 12, 2023
16e4457
add TransactionsProviderExt
joshieDo Oct 12, 2023
a6edbc4
add transactions snapshot segment
joshieDo Oct 12, 2023
765ea2e
snapshot benches compare results
joshieDo Oct 12, 2023
1b41008
add TransactionNotFound error
joshieDo Oct 12, 2023
b6789e4
add additional empty provider impls to SnapshotProvider
joshieDo Oct 12, 2023
a900860
add SegmentHeader to snapshot jars
joshieDo Oct 13, 2023
9ae5939
add wip transaction_by_hash
joshieDo Oct 13, 2023
96f77fc
fix doc test
joshieDo Oct 13, 2023
16c33a7
fmt
joshieDo Oct 13, 2023
d14a483
add with_hash to walk bench as well
joshieDo Oct 13, 2023
055a818
fix recover_hashes range
joshieDo Oct 13, 2023
5bef37f
Merge remote-tracking branch 'origin/main' into joshie/snapshot-bin-2
joshieDo Oct 16, 2023
a66b603
fix doc test
joshieDo Oct 16, 2023
f327d91
fix test_snap
joshieDo Oct 16, 2023
5582db3
share dataset_for_compression implementation
joshieDo Oct 16, 2023
13136ed
add snapshot segment
joshieDo Oct 16, 2023
d4c8b33
add receipts command
joshieDo Oct 16, 2023
36fa32f
Merge remote-tracking branch 'origin/main' into joshie/snapshot-bin-2
joshieDo Oct 19, 2023
648d55e
move dictionaries inside NippyJar
joshieDo Oct 21, 2023
bfde955
introduce snapshotjarprovider and dashmap
joshieDo Oct 23, 2023
0beb01a
docs
joshieDo Oct 23, 2023
b4d1d1e
Update bin/reth/src/db/snapshots/receipts.rs
joshieDo Oct 24, 2023
79c72db
Update bin/reth/src/db/snapshots/transactions.rs
joshieDo Oct 24, 2023
8880d6f
Update bin/reth/src/db/snapshots/transactions.rs
joshieDo Oct 24, 2023
6224354
Update bin/reth/src/db/snapshots/receipts.rs
joshieDo Oct 24, 2023
8e56d1d
Update crates/interfaces/src/provider.rs
joshieDo Oct 24, 2023
4fb1871
add transaction_hashes_by_range to provider
joshieDo Oct 24, 2023
9ab6333
add TxHashOrNumber type
joshieDo Oct 24, 2023
c562e0c
Update bin/reth/src/db/snapshots/receipts.rs
joshieDo Oct 24, 2023
27d6ab2
Update bin/reth/src/db/snapshots/transactions.rs
joshieDo Oct 24, 2023
21edb80
Merge remote-tracking branch 'origin/main' into joshie/snapshot-bin-2
joshieDo Oct 24, 2023
3a385ec
Merge branch 'joshie/snapshot-bin-2' into joshie/sprov
joshieDo Oct 24, 2023
2f8d303
clippy
joshieDo Oct 24, 2023
3631dec
Merge branch 'joshie/snapshot-bin-2' into joshie/sprov
joshieDo Oct 24, 2023
f2768f3
small fixes
joshieDo Oct 24, 2023
47e0036
add serde dictionaries
joshieDo Oct 24, 2023
e5e71be
yes clippy
joshieDo Oct 24, 2023
d7c01b2
Merge branch 'joshie/snapshot-bin-2' into joshie/sprov
joshieDo Oct 24, 2023
9cd888d
share mmap handle between snap providers
joshieDo Oct 24, 2023
ab58542
split up snapshot provider mod
joshieDo Oct 24, 2023
6bf07fd
add NippyJarCursor::with_handle
joshieDo Oct 25, 2023
e920318
fix doc
joshieDo Oct 25, 2023
5fd1124
Merge branch 'joshie/snapshot-bin-2' into joshie/sprov
joshieDo Oct 25, 2023
a5e93be
Merge branch 'joshie/sprov' into joshie/snapshot-prov-mod
joshieDo Oct 25, 2023
a820a66
adds SnapshotCursor wrapper struct
joshieDo Oct 25, 2023
908e408
impl HeaderProvider on SnapshotJarProvider
joshieDo Oct 25, 2023
90c3df0
use header selectors
joshieDo Oct 25, 2023
5af040a
remove const generics from nippyjarcursor
joshieDo Oct 25, 2023
2926083
replace const generics on snapshotcursor
joshieDo Oct 25, 2023
63bf235
split db/snapshots crate
joshieDo Oct 25, 2023
8ac1426
spread snapshot cursor mask usage
joshieDo Oct 25, 2023
30c4748
Merge remote-tracking branch 'origin/main' into joshie/snapshot-bin-2
joshieDo Oct 26, 2023
efeb602
fmt
joshieDo Oct 26, 2023
7dab55d
Merge branch 'joshie/snapshot-bin-2' into joshie/sprov
joshieDo Oct 26, 2023
1ef82fa
Merge branch 'joshie/sprov' into joshie/snapshot-prov-mod
joshieDo Oct 26, 2023
2e2cabd
use AsRef Path instead
joshieDo Oct 26, 2023
888e123
Merge remote-tracking branch 'origin/main' into joshie/sprov
joshieDo Oct 26, 2023
bd7c666
add more docs
joshieDo Oct 26, 2023
8f9cbdb
use BLOCKS_PER_SNAPSHOT instead
joshieDo Oct 27, 2023
5e836ff
Merge branch 'joshie/sprov' into joshie/snapshot-prov-mod
joshieDo Oct 27, 2023
584ca49
Merge branch 'joshie/snapshot-prov-mod' into joshie/snap-prov-header
joshieDo Oct 27, 2023
5a252f8
Merge branch 'joshie/snap-prov-header' into joshie/snap-remove-const
joshieDo Oct 27, 2023
9fcbf8c
rename to FIRST SECOND
joshieDo Oct 27, 2023
191d585
fix test_snap
joshieDo Oct 27, 2023
cc85c22
Merge remote-tracking branch 'origin/main' into joshie/snap-prov-header
joshieDo Oct 27, 2023
a9ce696
Merge branch 'joshie/snap-prov-header' into joshie/snap-remove-const
joshieDo Oct 27, 2023
56f36c7
order masks
joshieDo Oct 27, 2023
a288af9
KeyOrNumber::Key rename
joshieDo Oct 27, 2023
48bd002
typo
joshieDo Oct 27, 2023
77b9c64
add segment to segmentheader
joshieDo Oct 27, 2023
7a28a75
fix test_snap
joshieDo Oct 30, 2023
d127658
Merge branch 'joshie/snap-prov-header' into joshie/snap-remove-const
joshieDo Oct 30, 2023
ef64226
Merge remote-tracking branch 'origin/main' into joshie/snap-prov-header
joshieDo Oct 30, 2023
84b8e82
Merge branch 'joshie/snap-prov-header' into joshie/snap-remove-const
joshieDo Oct 30, 2023
a94a6de
Merge remote-tracking branch 'origin/main' into joshie/snap-remove-const
joshieDo Oct 30, 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
34 changes: 13 additions & 21 deletions bin/reth/src/db/snapshots/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ use super::{
Command,
};
use rand::{seq::SliceRandom, Rng};
use reth_db::{database::Database, open_db_read_only, table::Decompress};
use reth_db::{database::Database, open_db_read_only, snapshot::HeaderMask};
use reth_interfaces::db::LogLevel;
use reth_primitives::{
snapshot::{Compression, Filters, InclusionFilter, PerfectHashingFunction},
ChainSpec, Header, SnapshotSegment,
BlockHash, ChainSpec, Header, SnapshotSegment,
};
use reth_provider::{
providers::SnapshotProvider, DatabaseProviderRO, HeaderProvider, ProviderError, ProviderFactory,
Expand Down Expand Up @@ -71,14 +71,9 @@ impl Command {
compression,
|| {
for num in row_indexes.iter() {
Header::decompress(
cursor
.row_by_number_with_cols::<0b01, 2>((num - self.from) as usize)?
.ok_or(ProviderError::HeaderNotFound((*num).into()))?[0],
)?;
// TODO: replace with below when eventually SnapshotProvider re-uses cursor
// provider.header_by_number(num as
// u64)?.ok_or(ProviderError::HeaderNotFound((*num as u64).into()))?;
cursor
.get_one::<HeaderMask<Header>>((*num).into())?
.ok_or(ProviderError::HeaderNotFound((*num).into()))?;
}
Ok(())
},
Expand Down Expand Up @@ -106,11 +101,9 @@ impl Command {
filters,
compression,
|| {
Ok(Header::decompress(
cursor
.row_by_number_with_cols::<0b01, 2>((num - self.from) as usize)?
.ok_or(ProviderError::HeaderNotFound((num as u64).into()))?[0],
)?)
Ok(cursor
.get_one::<HeaderMask<Header>>(num.into())?
.ok_or(ProviderError::HeaderNotFound(num.into()))?)
},
|provider| {
Ok(provider
Expand All @@ -136,14 +129,13 @@ impl Command {
filters,
compression,
|| {
let header = Header::decompress(
cursor
.row_by_key_with_cols::<0b01, 2>(header_hash.as_slice())?
.ok_or(ProviderError::HeaderNotFound(header_hash.into()))?[0],
)?;
let (header, hash) = cursor
.get_two::<HeaderMask<Header, BlockHash>>((&header_hash).into())?
.ok_or(ProviderError::HeaderNotFound(header_hash.into()))?;

// Might be a false positive, so in the real world we have to validate it
assert_eq!(header.hash_slow(), header_hash);
assert_eq!(hash, header_hash);

Ok(header)
},
|provider| {
Expand Down
33 changes: 10 additions & 23 deletions bin/reth/src/db/snapshots/receipts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use super::{
Command, Compression, PerfectHashingFunction,
};
use rand::{seq::SliceRandom, Rng};
use reth_db::{database::Database, open_db_read_only, table::Decompress};
use reth_db::{database::Database, open_db_read_only, snapshot::ReceiptMask};
use reth_interfaces::db::LogLevel;
use reth_primitives::{
snapshot::{Filters, InclusionFilter},
Expand Down Expand Up @@ -81,16 +81,9 @@ impl Command {
compression,
|| {
for num in row_indexes.iter() {
Receipt::decompress(
cursor
.row_by_number_with_cols::<0b1, 1>(
(num - tx_range.start()) as usize,
)?
.ok_or(ProviderError::ReceiptNotFound((*num).into()))?[0],
)?;
// TODO: replace with below when eventually SnapshotProvider re-uses cursor
// provider.receipt(num as
// u64)?.ok_or(ProviderError::ReceiptNotFound((*num).into()))?;
cursor
.get_one::<ReceiptMask<Receipt>>((*num).into())?
.ok_or(ProviderError::ReceiptNotFound((*num).into()))?;
}
Ok(())
},
Expand Down Expand Up @@ -118,11 +111,9 @@ impl Command {
filters,
compression,
|| {
Ok(Receipt::decompress(
cursor
.row_by_number_with_cols::<0b1, 1>((num - tx_range.start()) as usize)?
.ok_or(ProviderError::ReceiptNotFound((num as u64).into()))?[0],
)?)
Ok(cursor
.get_one::<ReceiptMask<Receipt>>(num.into())?
.ok_or(ProviderError::ReceiptNotFound(num.into()))?)
},
|provider| {
Ok(provider
Expand All @@ -148,13 +139,9 @@ impl Command {
filters,
compression,
|| {
let receipt = Receipt::decompress(
cursor
.row_by_key_with_cols::<0b1, 1>(tx_hash.as_slice())?
.ok_or(ProviderError::ReceiptNotFound(tx_hash.into()))?[0],
)?;

Ok(receipt)
Ok(cursor
.get_one::<ReceiptMask<Receipt>>((&tx_hash).into())?
.ok_or(ProviderError::ReceiptNotFound(tx_hash.into()))?)
},
|provider| {
Ok(provider
Expand Down
41 changes: 15 additions & 26 deletions bin/reth/src/db/snapshots/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use super::{
Command, Compression, PerfectHashingFunction,
};
use rand::{seq::SliceRandom, Rng};
use reth_db::{database::Database, open_db_read_only, table::Decompress};
use reth_db::{database::Database, open_db_read_only, snapshot::TransactionMask};
use reth_interfaces::db::LogLevel;
use reth_primitives::{
snapshot::{Filters, InclusionFilter},
Expand Down Expand Up @@ -81,17 +81,10 @@ impl Command {
compression,
|| {
for num in row_indexes.iter() {
TransactionSignedNoHash::decompress(
cursor
.row_by_number_with_cols::<0b1, 1>(
(num - tx_range.start()) as usize,
)?
.ok_or(ProviderError::TransactionNotFound((*num).into()))?[0],
)?
.with_hash();
// TODO: replace with below when eventually SnapshotProvider re-uses cursor
// provider.transaction_by_id(num as
// u64)?.ok_or(ProviderError::TransactionNotFound((*num).into()))?;
cursor
.get_one::<TransactionMask<TransactionSignedNoHash>>((*num).into())?
.ok_or(ProviderError::TransactionNotFound((*num).into()))?
.with_hash();
}
Ok(())
},
Expand Down Expand Up @@ -119,12 +112,10 @@ impl Command {
filters,
compression,
|| {
Ok(TransactionSignedNoHash::decompress(
cursor
.row_by_number_with_cols::<0b1, 1>((num - tx_range.start()) as usize)?
.ok_or(ProviderError::TransactionNotFound((num as u64).into()))?[0],
)?
.with_hash())
Ok(cursor
.get_one::<TransactionMask<TransactionSignedNoHash>>(num.into())?
.ok_or(ProviderError::TransactionNotFound(num.into()))?
.with_hash())
},
|provider| {
Ok(provider
Expand All @@ -150,14 +141,12 @@ impl Command {
filters,
compression,
|| {
let transaction = TransactionSignedNoHash::decompress(
cursor
.row_by_key_with_cols::<0b1, 1>(transaction_hash.as_slice())?
.ok_or(ProviderError::TransactionNotFound(transaction_hash.into()))?[0],
)?;

// Might be a false positive, so in the real world we have to validate it
Ok(transaction.with_hash())
Ok(cursor
.get_one::<TransactionMask<TransactionSignedNoHash>>(
(&transaction_hash).into(),
)?
.ok_or(ProviderError::TransactionNotFound(transaction_hash.into()))?
.with_hash())
},
|provider| {
Ok(provider
Expand Down
105 changes: 105 additions & 0 deletions crates/storage/db/src/snapshot/cursor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
use super::mask::{ColumnSelectorOne, ColumnSelectorThree, ColumnSelectorTwo};
use crate::table::Decompress;
use derive_more::{Deref, DerefMut};
use reth_interfaces::{RethError, RethResult};
use reth_nippy_jar::{MmapHandle, NippyJar, NippyJarCursor};
use reth_primitives::{snapshot::SegmentHeader, B256};

/// Cursor of a snapshot segment.
#[derive(Debug, Deref, DerefMut)]
pub struct SnapshotCursor<'a>(NippyJarCursor<'a, SegmentHeader>);

impl<'a> SnapshotCursor<'a> {
/// Returns a new [`SnapshotCursor`].
pub fn new(
jar: &'a NippyJar<SegmentHeader>,
mmap_handle: MmapHandle,
) -> Result<Self, RethError> {
Ok(Self(NippyJarCursor::with_handle(jar, mmap_handle)?))
}

/// Gets a row of values.
pub fn get(
&mut self,
key_or_num: KeyOrNumber<'_>,
mask: usize,
) -> RethResult<Option<Vec<&'_ [u8]>>> {
let row = match key_or_num {
KeyOrNumber::Key(k) => self.row_by_key_with_cols(k, mask),
KeyOrNumber::Number(n) => {
let offset = self.jar().user_header().start();
if offset > n {
return Ok(None)
}
self.row_by_number_with_cols((n - offset) as usize, mask)
}
}?;

Ok(row)
}

/// Gets one column value from a row.
pub fn get_one<M: ColumnSelectorOne>(
&mut self,
key_or_num: KeyOrNumber<'_>,
) -> RethResult<Option<M::FIRST>> {
let row = self.get(key_or_num, M::MASK)?;

match row {
Some(row) => Ok(Some(M::FIRST::decompress(row[0])?)),
None => Ok(None),
}
}

/// Gets two column values from a row.
pub fn get_two<M: ColumnSelectorTwo>(
&mut self,
key_or_num: KeyOrNumber<'_>,
) -> RethResult<Option<(M::FIRST, M::SECOND)>> {
let row = self.get(key_or_num, M::MASK)?;

match row {
Some(row) => Ok(Some((M::FIRST::decompress(row[0])?, M::SECOND::decompress(row[1])?))),
None => Ok(None),
}
}

/// Gets three column values from a row.
#[allow(clippy::type_complexity)]
pub fn get_three<M: ColumnSelectorThree>(
&mut self,
key_or_num: KeyOrNumber<'_>,
) -> RethResult<Option<(M::FIRST, M::SECOND, M::THIRD)>> {
let row = self.get(key_or_num, M::MASK)?;

match row {
Some(row) => Ok(Some((
M::FIRST::decompress(row[0])?,
M::SECOND::decompress(row[1])?,
M::THIRD::decompress(row[2])?,
))),
None => Ok(None),
}
}
}

/// Either a key _or_ a block/tx number
#[derive(Debug)]
pub enum KeyOrNumber<'a> {
/// A slice used as a key. Usually a block/tx hash
Key(&'a [u8]),
/// A block/tx number
Number(u64),
}

impl<'a> From<&'a B256> for KeyOrNumber<'a> {
fn from(value: &'a B256) -> Self {
KeyOrNumber::Key(value.as_slice())
}
}

impl<'a> From<u64> for KeyOrNumber<'a> {
fn from(value: u64) -> Self {
KeyOrNumber::Number(value)
}
}
Loading