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

fix(stages): update prune sender recovery stage checkpoint #9513

Merged
merged 3 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions book/developers/exex/exex.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ and run it on the Holesky testnet.
1. [How do ExExes work?](./how-it-works.md)
1. [Hello World](./hello-world.md)
1. [Tracking State](./tracking-state.md)
1. [Remote](./remote.md)
5 changes: 4 additions & 1 deletion crates/stages/api/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use reth_errors::{BlockExecutionError, DatabaseError, RethError};
use reth_network_p2p::error::DownloadError;
use reth_primitives_traits::SealedHeader;
use reth_provider::ProviderError;
use reth_prune::{PruneSegmentError, PrunerError};
use reth_prune::{PruneSegment, PruneSegmentError, PrunerError};
use reth_static_file_types::StaticFileSegment;
use thiserror::Error;
use tokio::sync::broadcast::error::SendError;
Expand Down Expand Up @@ -122,6 +122,9 @@ pub enum StageError {
/// Expected static file block number.
static_file: BlockNumber,
},
/// The prune checkpoint for the given segment is missing.
#[error("missing prune checkpoint for {0}")]
MissingPruneCheckpoint(PruneSegment),
/// Internal error
#[error(transparent)]
Internal(#[from] RethError),
Expand Down
33 changes: 28 additions & 5 deletions crates/stages/stages/src/stages/prune.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use reth_db_api::database::Database;
use reth_provider::DatabaseProviderRW;
use reth_prune::{PruneMode, PruneModes, PrunerBuilder};
use reth_provider::{DatabaseProviderRW, PruneCheckpointReader};
use reth_prune::{PruneMode, PruneModes, PruneSegment, PrunerBuilder};
use reth_stages_api::{
ExecInput, ExecOutput, Stage, StageCheckpoint, StageError, StageId, UnwindInput, UnwindOutput,
};
Expand Down Expand Up @@ -49,6 +49,8 @@ impl<DB: Database> Stage<DB> for PruneStage {
if result.is_finished() {
Ok(ExecOutput { checkpoint: StageCheckpoint::new(input.target()), done: true })
} else {
// We cannot set the checkpoint yet, because prune segments may have different highest
// pruned block numbers
Ok(ExecOutput { checkpoint: input.checkpoint(), done: false })
}
}
Expand Down Expand Up @@ -90,7 +92,20 @@ impl<DB: Database> Stage<DB> for PruneSenderRecoveryStage {
provider: &DatabaseProviderRW<DB>,
input: ExecInput,
) -> Result<ExecOutput, StageError> {
self.0.execute(provider, input)
let mut result = self.0.execute(provider, input)?;

// Adjust the checkpoint to the highest pruned block number of the Sender Recovery segment
if !result.done {
let checkpoint = provider
.get_prune_checkpoint(PruneSegment::SenderRecovery)?
.ok_or(StageError::MissingPruneCheckpoint(PruneSegment::SenderRecovery))?;

// `unwrap_or_default` is safe because we know that genesis block doesn't have any
// transactions and senders
result.checkpoint = StageCheckpoint::new(checkpoint.block_number.unwrap_or_default());
}

Ok(result)
}

fn unwind(
Expand Down Expand Up @@ -174,11 +189,19 @@ mod tests {
return Ok(())
}

let provider = self.db.factory.provider()?;

assert!(output.done);
assert_eq!(output.checkpoint.block_number, input.target());
assert_eq!(
output.checkpoint.block_number,
provider
.get_prune_checkpoint(PruneSegment::SenderRecovery)?
.expect("prune checkpoint must exist")
.block_number
.unwrap_or_default()
);

// Verify that the senders are pruned
let provider = self.db.factory.provider()?;
let tx_range =
provider.transaction_range_by_block_range(start_block..=end_block)?;
let senders = self.db.factory.provider()?.senders_by_tx_range(tx_range)?;
Expand Down
Loading