Skip to content

Commit

Permalink
rethresult & retherror
Browse files Browse the repository at this point in the history
  • Loading branch information
shekhirin committed Sep 21, 2023
1 parent 6199c49 commit f63dc8e
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 24 deletions.
4 changes: 2 additions & 2 deletions crates/consensus/beacon/src/engine/hooks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub(crate) use controller::{EngineHooksController, PolledHook};

mod prune;
pub use prune::PruneHook;
use reth_interfaces::RethError;
use reth_interfaces::{RethError, RethResult};

mod snapshot;
pub use snapshot::SnapshotHook;
Expand Down Expand Up @@ -44,7 +44,7 @@ pub trait EngineHook: Send + Sync + 'static {
&mut self,
cx: &mut Context<'_>,
ctx: EngineContext,
) -> Poll<reth_interfaces::Result<(EngineHookEvent, Option<EngineHookAction>)>>;
) -> Poll<RethResult<(EngineHookEvent, Option<EngineHookAction>)>>;

/// Returns [db access level][`EngineHookDBAccessLevel`] the hook needs.
fn db_access_level(&self) -> EngineHookDBAccessLevel;
Expand Down
6 changes: 3 additions & 3 deletions crates/consensus/beacon/src/engine/hooks/prune.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
use futures::FutureExt;
use metrics::Counter;
use reth_db::database::Database;
use reth_interfaces::RethError;
use reth_interfaces::{RethError, RethResult};
use reth_primitives::BlockNumber;
use reth_prune::{Pruner, PrunerError, PrunerWithResult};
use reth_tasks::TaskSpawner;
Expand Down Expand Up @@ -43,7 +43,7 @@ impl<DB: Database + 'static> PruneHook<DB> {
fn poll_pruner(
&mut self,
cx: &mut Context<'_>,
) -> Poll<reth_interfaces::Result<(EngineHookEvent, Option<EngineHookAction>)>> {
) -> Poll<RethResult<(EngineHookEvent, Option<EngineHookAction>)>> {
let result = match self.pruner_state {
PrunerState::Idle(_) => return Poll::Pending,
PrunerState::Running(ref mut fut) => {
Expand Down Expand Up @@ -125,7 +125,7 @@ impl<DB: Database + 'static> EngineHook for PruneHook<DB> {
&mut self,
cx: &mut Context<'_>,
ctx: EngineContext,
) -> Poll<reth_interfaces::Result<(EngineHookEvent, Option<EngineHookAction>)>> {
) -> Poll<RethResult<(EngineHookEvent, Option<EngineHookAction>)>> {
// Try to spawn a pruner
match self.try_spawn_pruner(ctx.tip_block_number) {
Some((EngineHookEvent::NotReady, _)) => return Poll::Pending,
Expand Down
15 changes: 6 additions & 9 deletions crates/consensus/beacon/src/engine/hooks/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
};
use futures::FutureExt;
use reth_db::database::Database;
use reth_interfaces::{RethError, RethResult};
use reth_primitives::BlockNumber;
use reth_snapshot::{Snapshotter, SnapshotterError, SnapshotterWithResult};
use reth_tasks::TaskSpawner;
Expand Down Expand Up @@ -36,7 +37,7 @@ impl<DB: Database + 'static> SnapshotHook<DB> {
fn poll_snapshotter(
&mut self,
cx: &mut Context<'_>,
) -> Poll<reth_interfaces::Result<(EngineHookEvent, Option<EngineHookAction>)>> {
) -> Poll<RethResult<(EngineHookEvent, Option<EngineHookAction>)>> {
let result = match self.state {
SnapshotterState::Idle(_) => return Poll::Pending,
SnapshotterState::Running(ref mut fut) => {
Expand All @@ -55,12 +56,8 @@ impl<DB: Database + 'static> SnapshotHook<DB> {
EngineHookError::Internal(Box::new(err))
}
SnapshotterError::Interface(err) => err.into(),
SnapshotterError::Database(err) => {
reth_interfaces::Error::Database(err).into()
}
SnapshotterError::Provider(err) => {
reth_interfaces::Error::Provider(err).into()
}
SnapshotterError::Database(err) => RethError::Database(err).into(),
SnapshotterError::Provider(err) => RethError::Provider(err).into(),
})),
}
}
Expand All @@ -86,7 +83,7 @@ impl<DB: Database + 'static> SnapshotHook<DB> {
fn try_spawn_snapshotter(
&mut self,
finalized_block_number: BlockNumber,
) -> reth_interfaces::Result<Option<(EngineHookEvent, Option<EngineHookAction>)>> {
) -> RethResult<Option<(EngineHookEvent, Option<EngineHookAction>)>> {
Ok(match &mut self.state {
SnapshotterState::Idle(snapshotter) => {
let Some(mut snapshotter) = snapshotter.take() else { return Ok(None) };
Expand Down Expand Up @@ -125,7 +122,7 @@ impl<DB: Database + 'static> EngineHook for SnapshotHook<DB> {
&mut self,
cx: &mut Context<'_>,
ctx: EngineContext,
) -> Poll<reth_interfaces::Result<(EngineHookEvent, Option<EngineHookAction>)>> {
) -> Poll<RethResult<(EngineHookEvent, Option<EngineHookAction>)>> {
let Some(finalized_block_number) = ctx.finalized_block_number else {
return Poll::Ready(Ok((EngineHookEvent::NotReady, None)))
};
Expand Down
3 changes: 2 additions & 1 deletion crates/snapshot/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use reth_db::DatabaseError;
use reth_interfaces::RethError;
use reth_provider::ProviderError;
use thiserror::Error;

Expand All @@ -8,7 +9,7 @@ pub enum SnapshotterError {
InconsistentData(&'static str),

#[error("An interface error occurred.")]
Interface(#[from] reth_interfaces::Error),
Interface(#[from] RethError),

#[error(transparent)]
Database(#[from] DatabaseError),
Expand Down
19 changes: 10 additions & 9 deletions crates/snapshot/src/snapshotter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use crate::SnapshotterError;
use reth_db::database::Database;
use reth_interfaces::{RethError, RethResult};
use reth_primitives::{BlockNumber, ChainSpec, TxNumber};
use reth_provider::{BlockReader, ProviderFactory};
use std::{ops::RangeInclusive, sync::Arc};
Expand Down Expand Up @@ -96,7 +97,7 @@ impl<DB: Database> Snapshotter<DB> {
pub fn get_snapshot_targets(
&self,
finalized_block_number: BlockNumber,
) -> reth_interfaces::Result<SnapshotTargets> {
) -> RethResult<SnapshotTargets> {
let provider = self.provider_factory.provider()?;

// Round down `finalized_block_number` to a multiple of `block_interval`
Expand All @@ -107,7 +108,7 @@ impl<DB: Database> Snapshotter<DB> {

let to_tx_number = provider
.block_body_indices(to_block_number)?
.ok_or(reth_interfaces::Error::Custom(
.ok_or(RethError::Custom(
"Block body indices for current block number not found".to_string(),
))?
.last_tx_num();
Expand All @@ -128,7 +129,7 @@ impl<DB: Database> Snapshotter<DB> {
if let Some(previous_block_number) = self.highest_snapshots.receipts {
provider
.block_body_indices(previous_block_number)?
.ok_or(reth_interfaces::Error::Custom(
.ok_or(RethError::Custom(
"Block body indices for previous block number not found".to_string(),
))?
.next_tx_num()
Expand All @@ -143,7 +144,7 @@ impl<DB: Database> Snapshotter<DB> {
} else if let Some(previous_block_number) = self.highest_snapshots.transactions {
provider
.block_body_indices(previous_block_number)?
.ok_or(reth_interfaces::Error::Custom(
.ok_or(RethError::Custom(
"Block body indices for previous block number not found".to_string(),
))?
.next_tx_num()
Expand Down Expand Up @@ -179,7 +180,10 @@ impl<DB: Database> Snapshotter<DB> {
mod tests {
use crate::{snapshotter::SnapshotTargets, Snapshotter};
use assert_matches::assert_matches;
use reth_interfaces::test_utils::{generators, generators::random_block_range};
use reth_interfaces::{
test_utils::{generators, generators::random_block_range},
RethError,
};
use reth_primitives::{H256, MAINNET};
use reth_stages::test_utils::TestTransaction;

Expand All @@ -194,10 +198,7 @@ mod tests {
let mut snapshotter = Snapshotter::new(tx.inner_raw(), MAINNET.clone(), 2);

// Block body indices not found
assert_matches!(
snapshotter.get_snapshot_targets(5),
Err(reth_interfaces::Error::Custom(_))
);
assert_matches!(snapshotter.get_snapshot_targets(5), Err(RethError::Custom(_)));

// Snapshot targets has data per part up to the passed finalized block number
let targets = snapshotter.get_snapshot_targets(1).expect("get snapshot targets");
Expand Down

0 comments on commit f63dc8e

Please sign in to comment.