From 0ebc78651e2c223a8cd223cda458882652605f34 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Fri, 26 May 2023 20:28:20 +0200 Subject: [PATCH] chore: more sane debug display impls (#2872) --- .../interfaces/src/blockchain_tree/error.rs | 30 +++++++++++++++---- .../rpc/rpc-types/src/eth/engine/payload.rs | 28 +++++++++++++++++ 2 files changed, 53 insertions(+), 5 deletions(-) diff --git a/crates/interfaces/src/blockchain_tree/error.rs b/crates/interfaces/src/blockchain_tree/error.rs index ed4d75c7bd..a360cf8d4e 100644 --- a/crates/interfaces/src/blockchain_tree/error.rs +++ b/crates/interfaces/src/blockchain_tree/error.rs @@ -2,7 +2,6 @@ use crate::{consensus::ConsensusError, executor::BlockExecutionError}; use reth_primitives::{BlockHash, BlockNumber, SealedBlock}; -use std::fmt::Formatter; /// Various error cases that can occur when a block violates tree assumptions. #[derive(Debug, Clone, Copy, thiserror::Error, Eq, PartialEq)] @@ -29,7 +28,7 @@ pub enum BlockchainTreeError { } /// Error thrown when inserting a block failed because the block is considered invalid. -#[derive(Debug, thiserror::Error)] +#[derive(thiserror::Error)] #[error(transparent)] pub struct InsertBlockError { inner: Box, @@ -83,15 +82,36 @@ impl InsertBlockError { } } -#[derive(Debug)] +impl std::fmt::Debug for InsertBlockError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + std::fmt::Debug::fmt(&self.inner, f) + } +} + struct InsertBlockErrorData { block: SealedBlock, kind: InsertBlockErrorKind, } impl std::fmt::Display for InsertBlockErrorData { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - write!(f, "Failed to insert block {:?}: {}", self.block.hash, self.kind) + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "Failed to insert block (hash={:?}, number={}, parent_hash={:?}): {}", + self.block.hash, self.block.number, self.block.parent_hash, self.kind + ) + } +} + +impl std::fmt::Debug for InsertBlockErrorData { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("InsertBlockError") + .field("error", &self.kind) + .field("hash", &self.block.hash) + .field("number", &self.block.number) + .field("parent_hash", &self.block.parent_hash) + .field("num_txs", &self.block.body.len()) + .finish_non_exhaustive() } } diff --git a/crates/rpc/rpc-types/src/eth/engine/payload.rs b/crates/rpc/rpc-types/src/eth/engine/payload.rs index 358a511387..30d511d97a 100644 --- a/crates/rpc/rpc-types/src/eth/engine/payload.rs +++ b/crates/rpc/rpc-types/src/eth/engine/payload.rs @@ -298,6 +298,16 @@ impl PayloadStatus { } } +impl std::fmt::Display for PayloadStatus { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "PayloadStatus {{status: {}, latestValidHash: {:?} }}", + self.status, self.latest_valid_hash + ) + } +} + impl Serialize for PayloadStatus { fn serialize(&self, serializer: S) -> Result where @@ -394,6 +404,24 @@ impl PayloadStatusEnum { } } +impl std::fmt::Display for PayloadStatusEnum { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + PayloadStatusEnum::Invalid { validation_error } => { + f.write_str(self.as_str())?; + f.write_str(": ")?; + f.write_str(validation_error.as_str()) + } + PayloadStatusEnum::InvalidBlockHash { validation_error } => { + f.write_str(self.as_str())?; + f.write_str(": ")?; + f.write_str(validation_error.as_str()) + } + _ => f.write_str(self.as_str()), + } + } +} + /// Various errors that can occur when validating a payload or forkchoice update. /// /// This is intended for the [PayloadStatusEnum::Invalid] variant.