chore: more sane debug display impls (#2872)

This commit is contained in:
Matthias Seitz
2023-05-26 20:28:20 +02:00
committed by GitHub
parent 2711178ee9
commit 0ebc78651e
2 changed files with 53 additions and 5 deletions

View File

@ -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<InsertBlockErrorData>,
@ -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()
}
}

View File

@ -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<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
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.