Skip to content

Commit

Permalink
chore: shrink ProviderError size (#5482)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes authored Nov 17, 2023
1 parent 7162228 commit 6ae86d5
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 26 deletions.
58 changes: 38 additions & 20 deletions crates/interfaces/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
use crate::{
blockchain_tree::error::{BlockchainTreeError, CanonicalError},
consensus::ConsensusError,
db::DatabaseError,
executor::BlockExecutionError,
provider::ProviderError,
};
use reth_network_api::NetworkError;
use reth_primitives::fs::FsPathError;

/// Result alias for [`RethError`].
pub type RethResult<T> = Result<T, RethError>;

Expand All @@ -6,47 +16,55 @@ pub type RethResult<T> = Result<T, RethError>;
#[allow(missing_docs)]
pub enum RethError {
#[error(transparent)]
Execution(#[from] crate::executor::BlockExecutionError),
Execution(#[from] BlockExecutionError),

#[error(transparent)]
Consensus(#[from] crate::consensus::ConsensusError),
Consensus(#[from] ConsensusError),

#[error(transparent)]
Database(#[from] crate::db::DatabaseError),
Database(#[from] DatabaseError),

#[error(transparent)]
Provider(#[from] crate::provider::ProviderError),
Provider(#[from] ProviderError),

#[error(transparent)]
Network(#[from] reth_network_api::NetworkError),
Network(#[from] NetworkError),

#[error(transparent)]
Canonical(#[from] crate::blockchain_tree::error::CanonicalError),
Canonical(#[from] CanonicalError),

#[error("{0}")]
Custom(String),
}

impl From<crate::blockchain_tree::error::BlockchainTreeError> for RethError {
fn from(error: crate::blockchain_tree::error::BlockchainTreeError) -> Self {
RethError::Canonical(error.into())
impl From<BlockchainTreeError> for RethError {
fn from(error: BlockchainTreeError) -> Self {
RethError::Canonical(CanonicalError::BlockchainTree(error))
}
}

impl From<reth_primitives::fs::FsPathError> for RethError {
fn from(err: reth_primitives::fs::FsPathError) -> Self {
impl From<FsPathError> for RethError {
fn from(err: FsPathError) -> Self {
RethError::Custom(err.to_string())
}
}

// We don't want these types to be too large because they're used in a lot of places.
const _SIZE_ASSERTIONS: () = {
// Main error.
let _: [(); 64] = [(); std::mem::size_of::<RethError>()];
// Some types are used a lot. Make sure they don't unintentionally get bigger.
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
mod size_asserts {
use super::*;

// Biggest variant.
let _: [(); 64] = [(); std::mem::size_of::<crate::provider::ProviderError>()];
macro_rules! static_assert_size {
($t:ty, $sz:expr) => {
const _: [(); $sz] = [(); std::mem::size_of::<$t>()];
};
}

// Other common types.
let _: [(); 16] = [(); std::mem::size_of::<crate::db::DatabaseError>()];
};
static_assert_size!(RethError, 56);
static_assert_size!(BlockExecutionError, 48);
static_assert_size!(ConsensusError, 48);
static_assert_size!(DatabaseError, 16);
static_assert_size!(ProviderError, 48);
static_assert_size!(NetworkError, 0);
static_assert_size!(CanonicalError, 48);
}
10 changes: 6 additions & 4 deletions crates/interfaces/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,21 @@ pub enum ProviderError {
/// A block body is missing.
#[error("block meta not found for block #{0}")]
BlockBodyIndicesNotFound(BlockNumber),
/// The transition id was found for the given address and storage key, but the changeset was
/// The transition ID was found for the given address and storage key, but the changeset was
/// not found.
#[error("storage ChangeSet address: ({address} key: {storage_key:?}) for block #{block_number} does not exist")]
#[error("storage change set for address {address} and key {storage_key} at block #{block_number} does not exist")]
StorageChangesetNotFound {
/// The block number found for the address and storage key.
block_number: BlockNumber,
/// The account address.
address: Address,
/// The storage key.
storage_key: B256,
// NOTE: This is a Box only because otherwise this variant is 16 bytes larger than the
// second largest (which uses `BlockHashOrNumber`).
storage_key: Box<B256>,
},
/// The block number was found for the given address, but the changeset was not found.
#[error("account {address} ChangeSet for block #{block_number} does not exist")]
#[error("account change set for address {address} at block #{block_number} does not exist")]
AccountChangesetNotFound {
/// Block number found for the address.
block_number: BlockNumber,
Expand Down
4 changes: 2 additions & 2 deletions crates/storage/provider/src/providers/state/historical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,10 @@ impl<'b, TX: DbTx> StateProvider for HistoricalStateProviderRef<'b, TX> {
.cursor_dup_read::<tables::StorageChangeSet>()?
.seek_by_key_subkey((changeset_block_number, address).into(), storage_key)?
.filter(|entry| entry.key == storage_key)
.ok_or(ProviderError::StorageChangesetNotFound {
.ok_or_else(|| ProviderError::StorageChangesetNotFound {
block_number: changeset_block_number,
address,
storage_key,
storage_key: Box::new(storage_key),
})?
.value,
)),
Expand Down

0 comments on commit 6ae86d5

Please sign in to comment.