Skip to content

Commit

Permalink
chore(workspace): Remove Hand-rolled Display Error Impls (#312)
Browse files Browse the repository at this point in the history
### Description

When using [`thiserror::Error`][error] with `#[error(..)]` attributes, a
`Display` impl [is generated for the
error](https://github.com/dtolnay/thiserror?tab=readme-ov-file#details).

This PR removes all hand-rolled Display impls for errors, using
`thiserror::Error` with `#[error]` attributes on variants.

[error]: https://docs.rs/thiserror/latest/thiserror/derive.Error.html
  • Loading branch information
refcell authored Nov 23, 2024
1 parent 7ec5234 commit 4ce0024
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 288 deletions.
23 changes: 6 additions & 17 deletions crates/consensus/src/eip1559.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,17 @@ pub fn decode_holocene_extra_data(
/// Error type for EIP-1559 parameters
#[derive(Debug, thiserror::Error, Clone, Copy, PartialEq, Eq)]
pub enum EIP1559ParamError {
/// No EIP-1559 parameters provided
/// No EIP-1559 parameters provided.
#[error("No EIP1559 parameters provided")]
NoEIP1559Params,
/// Denominator overflow
/// Denominator overflow.
#[error("Denominator overflow")]
DenominatorOverflow,
/// Elasticity overflow
/// Elasticity overflow.
#[error("Elasticity overflow")]
ElasticityOverflow,
}

impl core::fmt::Display for EIP1559ParamError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::NoEIP1559Params => {
write!(f, "No EIP1559 parameters provided")
}
Self::DenominatorOverflow => write!(f, "Denominator overflow"),
Self::ElasticityOverflow => {
write!(f, "Elasticity overflow")
}
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
173 changes: 35 additions & 138 deletions crates/genesis/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,245 +329,142 @@ impl SystemConfig {
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum SystemConfigUpdateError {
/// An error occurred while processing the update log.
#[error("Log processing error: {0}")]
LogProcessing(LogProcessingError),
/// A batcher update error.
#[error("Batcher update error: {0}")]
Batcher(BatcherUpdateError),
/// A gas config update error.
#[error("Gas config update error: {0}")]
GasConfig(GasConfigUpdateError),
/// A gas limit update error.
#[error("Gas limit update error: {0}")]
GasLimit(GasLimitUpdateError),
/// An EIP-1559 parameter update error.
#[error("EIP-1559 parameter update error: {0}")]
Eip1559(EIP1559UpdateError),
}

impl core::fmt::Display for SystemConfigUpdateError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::LogProcessing(err) => write!(f, "Log processing error: {}", err),
Self::Batcher(err) => write!(f, "Batcher update error: {}", err),
Self::GasConfig(err) => write!(f, "Gas config update error: {}", err),
Self::GasLimit(err) => write!(f, "Gas limit update error: {}", err),
Self::Eip1559(err) => write!(f, "EIP-1559 parameter update error: {}", err),
}
}
}

/// An error occurred while processing the update log.
#[derive(Debug, thiserror::Error, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum LogProcessingError {
/// Received an incorrect number of log topics.
#[error("Invalid config update log: invalid topic length: {0}")]
InvalidTopicLen(usize),
/// The log topic is invalid.
#[error("Invalid config update log: invalid topic")]
InvalidTopic,
/// The config update log version is unsupported.
#[error("Invalid config update log: unsupported version: {0}")]
UnsupportedVersion(B256),
/// Failed to decode the update type from the config update log.
#[error("Failed to decode config update log: update type")]
UpdateTypeDecodingError,
/// An invalid system config update type.
#[error("Invalid system config update type: {0}")]
InvalidSystemConfigUpdateType(u64),
}

impl core::fmt::Display for LogProcessingError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::InvalidTopicLen(len) => {
write!(f, "Invalid config update log: invalid topic length: {}", len)
}
Self::InvalidTopic => write!(f, "Invalid config update log: invalid topic"),
Self::UnsupportedVersion(version) => {
write!(f, "Invalid config update log: unsupported version: {}", version)
}
Self::UpdateTypeDecodingError => {
write!(f, "Failed to decode config update log: update type")
}
Self::InvalidSystemConfigUpdateType(value) => {
write!(f, "Invalid system config update type: {}", value)
}
}
}
}

/// An error for updating the batcher address on the [SystemConfig].
#[derive(Debug, thiserror::Error, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum BatcherUpdateError {
/// Invalid data length.
#[error("Invalid config update log: invalid data length: {0}")]
InvalidDataLen(usize),
/// Failed to decode the data pointer argument from the batcher update log.
#[error("Failed to decode batcher update log: data pointer")]
PointerDecodingError,
/// The data pointer is invalid.
#[error("Invalid config update log: invalid data pointer: {0}")]
InvalidDataPointer(u64),
/// Failed to decode the data length argument from the batcher update log.
#[error("Failed to decode batcher update log: data length")]
LengthDecodingError,
/// The data length is invalid.
#[error("Invalid config update log: invalid data length: {0}")]
InvalidDataLength(u64),
/// Failed to decode the batcher address argument from the batcher update log.
#[error("Failed to decode batcher update log: batcher address")]
BatcherAddressDecodingError,
}

impl core::fmt::Display for BatcherUpdateError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::InvalidDataLen(len) => {
write!(f, "Invalid config update log: invalid data length: {}", len)
}
Self::PointerDecodingError => {
write!(f, "Failed to decode batcher update log: data pointer")
}
Self::InvalidDataPointer(pointer) => {
write!(f, "Invalid config update log: invalid data pointer: {}", pointer)
}
Self::LengthDecodingError => {
write!(f, "Failed to decode batcher update log: data length")
}
Self::InvalidDataLength(length) => {
write!(f, "Invalid config update log: invalid data length: {}", length)
}
Self::BatcherAddressDecodingError => {
write!(f, "Failed to decode batcher update log: batcher address")
}
}
}
}

/// An error for updating the gas config on the [SystemConfig].
#[derive(Debug, thiserror::Error, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum GasConfigUpdateError {
/// Invalid data length.
#[error("Invalid config update log: invalid data length: {0}")]
InvalidDataLen(usize),
/// Failed to decode the data pointer argument from the gas config update log.
#[error("Failed to decode gas config update log: data pointer")]
PointerDecodingError,
/// The data pointer is invalid.
#[error("Invalid config update log: invalid data pointer: {0}")]
InvalidDataPointer(u64),
/// Failed to decode the data length argument from the gas config update log.
#[error("Failed to decode gas config update log: data length")]
LengthDecodingError,
/// The data length is invalid.
#[error("Invalid config update log: invalid data length: {0}")]
InvalidDataLength(u64),
/// Failed to decode the overhead argument from the gas config update log.
#[error("Failed to decode gas config update log: overhead")]
OverheadDecodingError,
/// Failed to decode the scalar argument from the gas config update log.
#[error("Failed to decode gas config update log: scalar")]
ScalarDecodingError,
}

impl core::fmt::Display for GasConfigUpdateError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::InvalidDataLen(len) => {
write!(f, "Invalid config update log: invalid data length: {}", len)
}
Self::PointerDecodingError => {
write!(f, "Failed to decode gas config update log: data pointer")
}
Self::InvalidDataPointer(pointer) => {
write!(f, "Invalid config update log: invalid data pointer: {}", pointer)
}
Self::LengthDecodingError => {
write!(f, "Failed to decode gas config update log: data length")
}
Self::InvalidDataLength(length) => {
write!(f, "Invalid config update log: invalid data length: {}", length)
}
Self::OverheadDecodingError => {
write!(f, "Failed to decode gas config update log: overhead")
}
Self::ScalarDecodingError => {
write!(f, "Failed to decode gas config update log: scalar")
}
}
}
}

/// An error for updating the gas limit on the [SystemConfig].
#[derive(Debug, thiserror::Error, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum GasLimitUpdateError {
/// Invalid data length.
#[error("Invalid config update log: invalid data length: {0}")]
InvalidDataLen(usize),
/// Failed to decode the data pointer argument from the gas limit update log.
#[error("Failed to decode gas limit update log: data pointer")]
PointerDecodingError,
/// The data pointer is invalid.
#[error("Invalid config update log: invalid data pointer: {0}")]
InvalidDataPointer(u64),
/// Failed to decode the data length argument from the gas limit update log.
#[error("Failed to decode gas limit update log: data length")]
LengthDecodingError,
/// The data length is invalid.
#[error("Invalid config update log: invalid data length: {0}")]
InvalidDataLength(u64),
/// Failed to decode the gas limit argument from the gas limit update log.
#[error("Failed to decode gas limit update log: gas limit")]
GasLimitDecodingError,
}

impl core::fmt::Display for GasLimitUpdateError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::InvalidDataLen(len) => {
write!(f, "Invalid config update log: invalid data length: {}", len)
}
Self::PointerDecodingError => {
write!(f, "Failed to decode gas limit update log: data pointer")
}
Self::InvalidDataPointer(pointer) => {
write!(f, "Invalid config update log: invalid data pointer: {}", pointer)
}
Self::LengthDecodingError => {
write!(f, "Failed to decode gas limit update log: data length")
}
Self::InvalidDataLength(length) => {
write!(f, "Invalid config update log: invalid data length: {}", length)
}
Self::GasLimitDecodingError => {
write!(f, "Failed to decode gas limit update log: gas limit")
}
}
}
}

/// An error for updating the EIP-1559 parameters on the [SystemConfig].
#[derive(Debug, thiserror::Error, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum EIP1559UpdateError {
/// Invalid data length.
#[error("Invalid config update log: invalid data length: {0}")]
InvalidDataLen(usize),
/// Failed to decode the data pointer argument from the eip 1559 update log.
#[error("Failed to decode eip1559 parameter update log: data pointer")]
PointerDecodingError,
/// The data pointer is invalid.
#[error("Invalid config update log: invalid data pointer: {0}")]
InvalidDataPointer(u64),
/// Failed to decode the data length argument from the eip 1559 update log.
#[error("Failed to decode eip1559 parameter update log: data length")]
LengthDecodingError,
/// The data length is invalid.
#[error("Invalid config update log: invalid data length: {0}")]
InvalidDataLength(u64),
/// Failed to decode the eip1559 params argument from the eip 1559 update log.
#[error("Failed to decode eip1559 parameter update log: eip1559 parameters")]
EIP1559DecodingError,
}

impl core::fmt::Display for EIP1559UpdateError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::InvalidDataLen(len) => {
write!(f, "Invalid config update log: invalid data length: {}", len)
}
Self::PointerDecodingError => {
write!(f, "Failed to decode eip1559 parameter update log: data pointer")
}
Self::InvalidDataPointer(pointer) => {
write!(f, "Invalid config update log: invalid data pointer: {}", pointer)
}
Self::LengthDecodingError => {
write!(f, "Failed to decode eip1559 parameter update log: data length")
}
Self::InvalidDataLength(length) => {
write!(f, "Invalid config update log: invalid data length: {}", length)
}
Self::EIP1559DecodingError => {
write!(
f,
"Failed to decode eip1559 parameter update log: eip1559 parameters invalid"
)
}
}
}
}

/// System accounts
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
Expand Down
19 changes: 5 additions & 14 deletions crates/protocol/src/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,31 +143,22 @@ pub const MAX_RLP_BYTES_PER_CHANNEL: u64 = 10_000_000;
pub const FJORD_MAX_RLP_BYTES_PER_CHANNEL: u64 = 100_000_000;

/// An error returned when adding a frame to a channel.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Debug, thiserror::Error, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ChannelError {
/// The frame id does not match the channel id.
#[error("Frame id does not match channel id")]
FrameIdMismatch,
/// The channel is closed.
#[error("Channel is closed")]
ChannelClosed,
/// The frame number is already in the channel.
#[error("Frame number {0} already exists")]
FrameNumberExists(usize),
/// The frame number is beyond the end frame.
#[error("Frame number {0} is beyond end frame")]
FrameBeyondEndFrame(usize),
}

impl core::fmt::Display for ChannelError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::FrameIdMismatch => write!(f, "Frame id does not match channel id"),
Self::ChannelClosed => write!(f, "Channel is closed"),
Self::FrameNumberExists(n) => write!(f, "Frame number {} already exists", n),
Self::FrameBeyondEndFrame(n) => {
write!(f, "Frame number {} is beyond end frame", n)
}
}
}
}

/// A Channel is a set of batches that are split into at least one, but possibly multiple frames.
///
/// Frames are allowed to be ingested out of order.
Expand Down
Loading

0 comments on commit 4ce0024

Please sign in to comment.