Skip to content

Commit

Permalink
chore: remove prune_modes from BlockWriter (#9231)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rjected authored Jul 2, 2024
1 parent e95c6db commit 405b730
Show file tree
Hide file tree
Showing 19 changed files with 105 additions and 117 deletions.
8 changes: 6 additions & 2 deletions bin/reth/src/commands/debug_cmd/build_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use reth_provider::{
providers::BlockchainProvider, BlockHashReader, BlockReader, BlockWriter, ChainSpecProvider,
ProviderFactory, StageCheckpointReader, StateProviderFactory,
};
use reth_prune::PruneModes;
use reth_revm::{database::StateProviderDatabase, primitives::EnvKzgSettings};
use reth_rpc_types::engine::{BlobsBundleV1, PayloadAttributes};
use reth_stages::StageId;
Expand Down Expand Up @@ -122,7 +123,11 @@ impl Command {
// configure blockchain tree
let tree_externals =
TreeExternals::new(provider_factory.clone(), Arc::clone(&consensus), executor);
let tree = BlockchainTree::new(tree_externals, BlockchainTreeConfig::default(), None)?;
let tree = BlockchainTree::new(
tree_externals,
BlockchainTreeConfig::default(),
PruneModes::none(),
)?;
let blockchain_tree = Arc::new(ShareableBlockchainTree::new(tree));

// fetch the best block from the database
Expand Down Expand Up @@ -298,7 +303,6 @@ impl Command {
execution_outcome,
hashed_post_state,
trie_updates,
None,
)?;
info!(target: "reth::cli", "Successfully appended built block");
}
Expand Down
1 change: 0 additions & 1 deletion bin/reth/src/commands/debug_cmd/in_memory_merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ impl Command {
.clone()
.try_seal_with_senders()
.map_err(|_| BlockValidationError::SenderRecoveryError)?,
None,
)?;
execution_outcome.write_to_storage(provider_rw.tx_ref(), None, OriginalValuesKnown::No)?;
let storage_lists = provider_rw.changed_storages_with_range(block.number..=block.number)?;
Expand Down
2 changes: 1 addition & 1 deletion bin/reth/src/commands/debug_cmd/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ impl Command {
.map_err(|block| eyre::eyre!("Error sealing block with senders: {block:?}"))?;
trace!(target: "reth::cli", block_number, "Executing block");

provider_rw.insert_block(sealed_block.clone(), None)?;
provider_rw.insert_block(sealed_block.clone())?;

td += sealed_block.difficulty;
let mut executor = executor_provider.batch_executor(
Expand Down
8 changes: 6 additions & 2 deletions bin/reth/src/commands/debug_cmd/replay_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@ impl Command {
// Configure blockchain tree
let tree_externals =
TreeExternals::new(provider_factory.clone(), Arc::clone(&consensus), executor);
let tree = BlockchainTree::new(tree_externals, BlockchainTreeConfig::default(), None)?;
let tree = BlockchainTree::new(
tree_externals,
BlockchainTreeConfig::default(),
PruneModes::none(),
)?;
let blockchain_tree = Arc::new(ShareableBlockchainTree::new(tree));

// Set up the blockchain provider
Expand Down Expand Up @@ -144,7 +148,7 @@ impl Command {
network_client,
Pipeline::builder().build(
provider_factory.clone(),
StaticFileProducer::new(provider_factory.clone(), PruneModes::default()),
StaticFileProducer::new(provider_factory.clone(), PruneModes::none()),
),
blockchain_db.clone(),
Box::new(ctx.task_executor.clone()),
Expand Down
38 changes: 20 additions & 18 deletions crates/blockchain-tree/src/blockchain_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ pub struct BlockchainTree<DB, E> {
externals: TreeExternals<DB, E>,
/// Tree configuration
config: BlockchainTreeConfig,
/// Prune modes.
prune_modes: Option<PruneModes>,
/// Broadcast channel for canon state changes notifications.
canon_state_notification_sender: CanonStateNotificationSender,
/// Metrics for sync stages.
Expand Down Expand Up @@ -115,16 +113,19 @@ where
/// storage space efficiently. It's important to validate this configuration to ensure it does
/// not lead to unintended data loss.
pub fn new(
externals: TreeExternals<DB, E>,
mut externals: TreeExternals<DB, E>,
config: BlockchainTreeConfig,
prune_modes: Option<PruneModes>,
prune_modes: PruneModes,
) -> ProviderResult<Self> {
let max_reorg_depth = config.max_reorg_depth() as usize;
// The size of the broadcast is twice the maximum reorg depth, because at maximum reorg
// depth at least N blocks must be sent at once.
let (canon_state_notification_sender, _receiver) =
tokio::sync::broadcast::channel(max_reorg_depth * 2);

// Set the prune modes argument, on the provider
externals.provider_factory = externals.provider_factory.with_prune_modes(prune_modes);

let last_canonical_hashes =
externals.fetch_latest_canonical_hashes(config.num_of_canonical_hashes() as usize)?;

Expand All @@ -138,7 +139,6 @@ where
config.max_unconnected_blocks(),
),
config,
prune_modes,
canon_state_notification_sender,
sync_metrics_tx: None,
metrics: Default::default(),
Expand Down Expand Up @@ -1258,7 +1258,6 @@ where
state,
hashed_state,
trie_updates,
self.prune_modes.as_ref(),
)
.map_err(|e| CanonicalError::CanonicalCommit(e.to_string()))?;

Expand Down Expand Up @@ -1424,7 +1423,6 @@ mod tests {
provider
.insert_historical_block(
genesis.try_seal_with_senders().expect("invalid tx signature in genesis"),
None,
)
.unwrap();

Expand Down Expand Up @@ -1545,7 +1543,6 @@ mod tests {
SealedBlock::new(chain_spec.sealed_genesis_header(), Default::default())
.try_seal_with_senders()
.unwrap(),
None,
)
.unwrap();
let account = Account { balance: initial_signer_balance, ..Default::default() };
Expand Down Expand Up @@ -1647,7 +1644,7 @@ mod tests {
let mut tree = BlockchainTree::new(
TreeExternals::new(provider_factory, consensus, executor_provider),
BlockchainTreeConfig::default(),
None,
PruneModes::default(),
)
.expect("failed to create tree");

Expand Down Expand Up @@ -1727,7 +1724,8 @@ mod tests {

// make tree
let config = BlockchainTreeConfig::new(1, 2, 3, 2);
let mut tree = BlockchainTree::new(externals, config, None).expect("failed to create tree");
let mut tree = BlockchainTree::new(externals, config, PruneModes::default())
.expect("failed to create tree");
// genesis block 10 is already canonical
tree.make_canonical(B256::ZERO).unwrap();

Expand Down Expand Up @@ -1803,7 +1801,8 @@ mod tests {

// make tree
let config = BlockchainTreeConfig::new(1, 2, 3, 2);
let mut tree = BlockchainTree::new(externals, config, None).expect("failed to create tree");
let mut tree = BlockchainTree::new(externals, config, PruneModes::default())
.expect("failed to create tree");
// genesis block 10 is already canonical
tree.make_canonical(B256::ZERO).unwrap();

Expand Down Expand Up @@ -1888,7 +1887,8 @@ mod tests {

// make tree
let config = BlockchainTreeConfig::new(1, 2, 3, 2);
let mut tree = BlockchainTree::new(externals, config, None).expect("failed to create tree");
let mut tree = BlockchainTree::new(externals, config, PruneModes::default())
.expect("failed to create tree");
// genesis block 10 is already canonical
tree.make_canonical(B256::ZERO).unwrap();

Expand Down Expand Up @@ -1986,7 +1986,8 @@ mod tests {

// make tree
let config = BlockchainTreeConfig::new(1, 2, 3, 2);
let mut tree = BlockchainTree::new(externals, config, None).expect("failed to create tree");
let mut tree = BlockchainTree::new(externals, config, PruneModes::default())
.expect("failed to create tree");

let mut canon_notif = tree.subscribe_canon_state();
// genesis block 10 is already canonical
Expand Down Expand Up @@ -2379,7 +2380,8 @@ mod tests {

// make tree
let config = BlockchainTreeConfig::new(1, 2, 3, 2);
let mut tree = BlockchainTree::new(externals, config, None).expect("failed to create tree");
let mut tree = BlockchainTree::new(externals, config, PruneModes::default())
.expect("failed to create tree");

assert_eq!(
tree.insert_block(block1.clone(), BlockValidationKind::Exhaustive).unwrap(),
Expand All @@ -2399,8 +2401,8 @@ mod tests {
tree.make_canonical(block2.hash()).unwrap();

// restart
let mut tree =
BlockchainTree::new(cloned_externals_1, config, None).expect("failed to create tree");
let mut tree = BlockchainTree::new(cloned_externals_1, config, PruneModes::default())
.expect("failed to create tree");
assert_eq!(tree.block_indices().last_finalized_block(), 0);

let mut block1a = block1;
Expand All @@ -2416,8 +2418,8 @@ mod tests {
tree.finalize_block(block1a.number).unwrap();

// restart
let tree =
BlockchainTree::new(cloned_externals_2, config, None).expect("failed to create tree");
let tree = BlockchainTree::new(cloned_externals_2, config, PruneModes::default())
.expect("failed to create tree");

assert_eq!(tree.block_indices().last_finalized_block(), block1a.number);
}
Expand Down
7 changes: 4 additions & 3 deletions crates/cli/commands/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,10 @@ impl EnvironmentArgs {
static_file_provider: StaticFileProvider,
) -> eyre::Result<ProviderFactory<Arc<DatabaseEnv>>> {
let has_receipt_pruning = config.prune.as_ref().map_or(false, |a| a.has_receipts_pruning());
let factory = ProviderFactory::new(db, self.chain.clone(), static_file_provider);
let prune_modes =
config.prune.as_ref().map(|prune| prune.segments.clone()).unwrap_or_default();
let factory = ProviderFactory::new(db, self.chain.clone(), static_file_provider)
.with_prune_modes(prune_modes.clone());

info!(target: "reth::cli", "Verifying storage consistency.");

Expand All @@ -123,8 +126,6 @@ impl EnvironmentArgs {
return Ok(factory)
}

let prune_modes = config.prune.clone().map(|prune| prune.segments).unwrap_or_default();

// Highly unlikely to happen, and given its destructive nature, it's better to panic
// instead.
assert_ne!(unwind_target, PipelineTarget::Unwind(0), "A static file <> database inconsistency was found that would trigger an unwind to block 0");
Expand Down
1 change: 0 additions & 1 deletion crates/consensus/beacon/src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2159,7 +2159,6 @@ mod tests {
provider
.insert_block(
b.clone().try_seal_with_senders().expect("invalid tx signature in block"),
None,
)
.map(drop)
})
Expand Down
3 changes: 2 additions & 1 deletion crates/consensus/beacon/src/engine/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,8 @@ where
let externals = TreeExternals::new(provider_factory.clone(), consensus, executor_factory);
let config = BlockchainTreeConfig::new(1, 2, 3, 2);
let tree = Arc::new(ShareableBlockchainTree::new(
BlockchainTree::new(externals, config, None).expect("failed to create tree"),
BlockchainTree::new(externals, config, PruneModes::default())
.expect("failed to create tree"),
));
let latest = self.base_config.chain_spec.genesis_header().seal_slow();
let blockchain_provider =
Expand Down
9 changes: 1 addition & 8 deletions crates/engine/tree/src/persistence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,9 @@ impl<DB: Database> Persistence<DB> {
// * indices (already done basically)
// Insert the blocks
for block in blocks {
// TODO: prune modes - a bit unsure that it should be at this level of abstraction and
// not another
//
// ie, an external consumer of providers (or the database task) really does not care
// about pruning, just the node. Maybe we are the biggest user, and use it enough that
// we need a helper, but I'd rather make the pruning behavior more explicit then
let prune_modes = None;
let sealed_block =
block.block().clone().try_with_senders_unchecked(block.senders().clone()).unwrap();
provider_rw.insert_block(sealed_block, prune_modes)?;
provider_rw.insert_block(sealed_block)?;

// Write state and changesets to the database.
// Must be written after blocks because of the receipt lookup.
Expand Down
1 change: 0 additions & 1 deletion crates/exex/exex/src/backfill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,6 @@ mod tests {
outcome_batch,
Default::default(),
Default::default(),
None,
)?;
provider_rw.commit()?;

Expand Down
19 changes: 7 additions & 12 deletions crates/node/builder/src/launch/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,9 @@ impl<R> LaunchContextWith<Attached<WithConfigs, R>> {
self.toml_config().prune.clone().or_else(|| self.node_config().prune_config())
}

/// Returns the configured [`PruneModes`]
pub fn prune_modes(&self) -> Option<PruneModes> {
self.prune_config().map(|config| config.segments)
/// Returns the configured [`PruneModes`], returning the default if no config was available.
pub fn prune_modes(&self) -> PruneModes {
self.prune_config().map(|config| config.segments).unwrap_or_default()
}

/// Returns an initialized [`PrunerBuilder`] based on the configured [`PruneConfig`]
Expand Down Expand Up @@ -364,6 +364,7 @@ where
self.chain_spec(),
StaticFileProvider::read_write(self.data_dir().static_files())?,
)
.with_prune_modes(self.prune_modes())
.with_static_files_metrics();

let has_receipt_pruning =
Expand Down Expand Up @@ -395,14 +396,11 @@ where
NoopBodiesDownloader::default(),
NoopBlockExecutorProvider::default(),
self.toml_config().stages.clone(),
self.prune_modes().unwrap_or_default(),
self.prune_modes(),
))
.build(
factory.clone(),
StaticFileProducer::new(
factory.clone(),
self.prune_modes().unwrap_or_default(),
),
StaticFileProducer::new(factory.clone(), self.prune_modes()),
);

// Unwinds to block
Expand Down Expand Up @@ -705,10 +703,7 @@ where

/// Creates a new [`StaticFileProducer`] with the attached database.
pub fn static_file_producer(&self) -> StaticFileProducer<DB> {
StaticFileProducer::new(
self.provider_factory().clone(),
self.prune_modes().unwrap_or_default(),
)
StaticFileProducer::new(self.provider_factory().clone(), self.prune_modes())
}

/// Returns the current head block.
Expand Down
Loading

0 comments on commit 405b730

Please sign in to comment.