Skip to content

Commit

Permalink
improve comments
Browse files Browse the repository at this point in the history
  • Loading branch information
shekhirin committed Jul 4, 2024
1 parent 2c253e4 commit 4f1c362
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 28 deletions.
11 changes: 6 additions & 5 deletions crates/prune/prune/src/segments/account_history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ impl<DB: Database> Segment<DB> for AccountHistory {
}

let mut last_changeset_pruned_block = None;
// Deleted account changeset keys (account address) with the highest block number
// Deleted account changeset keys (account addresses) with the highest block number deleted
// for that key
let mut highest_deleted_accounts = HashMap::new();
let (pruned_changesets, done) = provider
.prune_table_with_range::<tables::AccountChangeSets>(
Expand All @@ -80,15 +81,15 @@ impl<DB: Database> Segment<DB> for AccountHistory {
)?;
trace!(target: "pruner", pruned = %pruned_changesets, %done, "Pruned account history (changesets)");

// Sort highest deleted block numbers by account address
// We did not use `BTreeMap` from the beginning, because it's inefficient for hashes
let deleted_keys = highest_deleted_accounts
// Sort highest deleted block numbers by account address and turn them into sharded keys.
// We did not use `BTreeMap` from the beginning, because it's inefficient for hashes.
let highest_sharded_keys = highest_deleted_accounts
.into_iter()
.sorted_unstable()
.map(|(address, block_number)| ShardedKey::new(address, block_number));
let (processed, pruned_indices) = prune_history_indices::<DB, tables::AccountsHistory, _>(
provider,
deleted_keys,
highest_sharded_keys,
|a, b| a.key == b.key,
)?;
trace!(target: "pruner", %processed, pruned = %pruned_indices, %done, "Pruned account history (history)");
Expand Down
35 changes: 18 additions & 17 deletions crates/prune/prune/src/segments/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ use reth_db_api::{
};
use reth_provider::DatabaseProviderRW;

/// Prune history indices according to the provided list of deleted changesets.
/// Prune history indices according to the provided list of highest sharded keys.
///
/// Returns total number of processed (walked) and deleted entities.
pub(crate) fn prune_history_indices<DB, T, SK>(
provider: &DatabaseProviderRW<DB>,
deleted_changesets: impl IntoIterator<Item = T::Key>,
highest_sharded_keys: impl IntoIterator<Item = T::Key>,
key_matches: impl Fn(&T::Key, &T::Key) -> bool,
) -> Result<(usize, usize), DatabaseError>
where
Expand All @@ -28,29 +28,23 @@ where
let mut deleted = 0;
let mut cursor = provider.tx_ref().cursor_write::<RawTable<T>>()?;

// Prune history table:
// 1. If the shard has `highest_block_number` less than or equal to the target block number
// for pruning, delete the shard completely.
// 2. If the shard has `highest_block_number` greater than the target block number for
// pruning, filter block numbers inside the shard which are less than the target
// block number for pruning.
for changeset_key in deleted_changesets {
// Seek to the shard that has the key >= the given changeset key
for sharded_key in highest_sharded_keys {
// Seek to the shard that has the key >= the given sharded key
// TODO: optimize
let result = cursor
.seek(RawKey::new(changeset_key.clone()))?
.seek(RawKey::new(sharded_key.clone()))?
.map(|(key, value)| Result::<_, DatabaseError>::Ok((key.key()?, value)))
.transpose()?;

// Get the highest block number that needs to be deleted for this changeset key
let to_block = changeset_key.as_ref().highest_block_number;
// Get the highest block number that needs to be deleted for this sharded key
let to_block = sharded_key.as_ref().highest_block_number;

// If such shard doesn't exist, skip to the next changeset key
if result.as_ref().map_or(true, |(key, _)| !key_matches(key, &changeset_key)) {
// If such shard doesn't exist, skip to the next sharded key
if result.as_ref().map_or(true, |(key, _)| !key_matches(key, &sharded_key)) {
continue
}

// At this point, we're sure that the shard with the given changeset key exists
// At this point, we're sure that the shard with the given sharded key exists
let (key, raw_blocks): (T::Key, RawValue<BlockNumberList>) = result.unwrap();

deleted += prune_shard(&mut cursor, key, raw_blocks, to_block, &key_matches)?;
Expand All @@ -60,7 +54,7 @@ where
.map(|(k, v)| Result::<_, DatabaseError>::Ok((k.key()?, v)))
.transpose()?
{
if key_matches(&key, &changeset_key) {
if key_matches(&key, &sharded_key) {
deleted += prune_shard(&mut cursor, key, value, to_block, &key_matches)?;
} else {
break
Expand All @@ -73,6 +67,13 @@ where
Ok((processed, deleted))
}

/// Prunes one shard of a history table.
///
/// 1. If the shard has `highest_block_number` less than or equal to the target block number for
/// pruning, delete the shard completely.
/// 2. If the shard has `highest_block_number` greater than the target block number for pruning,
/// filter block numbers inside the shard which are less than the target block number for
/// pruning.
fn prune_shard<C, T, SK>(
cursor: &mut C,
key: T::Key,
Expand Down
13 changes: 7 additions & 6 deletions crates/prune/prune/src/segments/storage_history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ impl<DB: Database> Segment<DB> for StorageHistory {
}

let mut last_changeset_pruned_block = None;
// Deleted storage changeset keys (account address and storage slot) with the highest block
// number
// Deleted storage changeset keys (account addresses and storage slots) with the highest
// block number deleted for that key
let mut highest_deleted_storages = HashMap::new();
let (pruned_changesets, done) = provider
.prune_table_with_range::<tables::StorageChangeSets>(
Expand All @@ -84,16 +84,17 @@ impl<DB: Database> Segment<DB> for StorageHistory {
)?;
trace!(target: "pruner", deleted = %pruned_changesets, %done, "Pruned storage history (changesets)");

// Sort highest deleted block numbers by account address and storage key
// We did not use `BTreeMap` from the beginning, because it's inefficient for hashes
let highest_deleted_storages = highest_deleted_storages.into_iter().sorted_unstable().map(
// Sort highest deleted block numbers by account address and storage key and turn them into
// sharded keys.
// We did not use `BTreeMap` from the beginning, because it's inefficient for hashes.
let highest_sharded_keys = highest_deleted_storages.into_iter().sorted_unstable().map(
|((address, storage_key), block_number)| {
StorageShardedKey::new(address, storage_key, block_number)
},
);
let (processed, pruned_indices) = prune_history_indices::<DB, tables::StoragesHistory, _>(
provider,
highest_deleted_storages,
highest_sharded_keys,
|a, b| a.address == b.address && a.sharded_key.key == b.sharded_key.key,
)?;
trace!(target: "pruner", %processed, deleted = %pruned_indices, %done, "Pruned storage history (history)");
Expand Down

0 comments on commit 4f1c362

Please sign in to comment.