From 1c97642c3c8157c53c4175c6dfcd1635e4e48698 Mon Sep 17 00:00:00 2001 From: Shallom Micah Bawa Date: Tue, 10 Mar 2026 03:24:50 +0100 Subject: [PATCH] chore: implement Display for error types Implement Display trait for error types that were missing it, enabling integration with thiserror and #[error(transparent)]. Error types updated: MemoryDatabaseError, HeaderExtError, FlatChainstoreError, BlockchainBuilderError, LeafErrorKind, slip132::Error. --- crates/floresta-chain/src/extensions.rs | 13 ++++++++++++ .../src/pruned_utreexo/chain_state_builder.rs | 14 +++++++++++++ .../src/pruned_utreexo/flat_chain_store.rs | 21 +++++++++++++++++++ .../src/pruned_utreexo/udata.rs | 10 +++++++++ crates/floresta-node/src/slip132.rs | 20 ++++++++++++++++++ .../src/memory_database.rs | 12 +++++++++++ 6 files changed, 90 insertions(+) diff --git a/crates/floresta-chain/src/extensions.rs b/crates/floresta-chain/src/extensions.rs index 29fb77b9b..9c0ffba0c 100644 --- a/crates/floresta-chain/src/extensions.rs +++ b/crates/floresta-chain/src/extensions.rs @@ -2,6 +2,9 @@ use core::cmp::min; use core::error::Error; +use core::fmt; +use core::fmt::Display; +use core::fmt::Formatter; use core::ops::Add; use bitcoin::block::Header; @@ -85,6 +88,16 @@ pub enum HeaderExtError { ChainWorkOverflow, } +impl Display for HeaderExtError { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + match self { + HeaderExtError::Chain(e) => write!(f, "Chain error: {e}"), + HeaderExtError::BlockNotFound => write!(f, "Block not found"), + HeaderExtError::ChainWorkOverflow => write!(f, "Chain work overflow"), + } + } +} + impl HeaderExt for Header { fn calculate_median_time_past( &self, diff --git a/crates/floresta-chain/src/pruned_utreexo/chain_state_builder.rs b/crates/floresta-chain/src/pruned_utreexo/chain_state_builder.rs index 91827b7d9..aeef68ad1 100644 --- a/crates/floresta-chain/src/pruned_utreexo/chain_state_builder.rs +++ b/crates/floresta-chain/src/pruned_utreexo/chain_state_builder.rs @@ -10,6 +10,9 @@ //! - Assumed valid blocks for validation optimization //! - UTREEXO accumulator state //! - Current chain tip and header +use core::fmt; +use core::fmt::Display; +use core::fmt::Formatter; use bitcoin::block::Header as BlockHeader; use bitcoin::BlockHash; @@ -42,6 +45,17 @@ pub enum BlockchainBuilderError { Database(Box), } +impl Display for BlockchainBuilderError { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + match self { + BlockchainBuilderError::MissingChainstore => write!(f, "Missing chainstore"), + BlockchainBuilderError::MissingChainParams => write!(f, "Missing chain parameters"), + BlockchainBuilderError::IncompleteTip => write!(f, "Incomplete tip"), + BlockchainBuilderError::Database(e) => write!(f, "Database error: {e:?}"), + } + } +} + #[derive(Clone, Debug, Default)] /// A builder for configuring and creating a `ChainState`. /// diff --git a/crates/floresta-chain/src/pruned_utreexo/flat_chain_store.rs b/crates/floresta-chain/src/pruned_utreexo/flat_chain_store.rs index 306e7d05b..3e134153b 100644 --- a/crates/floresta-chain/src/pruned_utreexo/flat_chain_store.rs +++ b/crates/floresta-chain/src/pruned_utreexo/flat_chain_store.rs @@ -71,6 +71,9 @@ extern crate std; +use core::fmt; +use core::fmt::Display; +use core::fmt::Formatter; use core::mem::size_of; use core::num::NonZeroUsize; use std::fs::DirBuilder; @@ -384,6 +387,24 @@ pub enum FlatChainstoreError { InvalidValidationIndex, } +impl Display for FlatChainstoreError { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + match self { + FlatChainstoreError::Io(e) => write!(f, "I/O error: {e}"), + FlatChainstoreError::BlockNotFound => write!(f, "Block not found"), + FlatChainstoreError::IndexIsFull => write!(f, "Index is full"), + FlatChainstoreError::DbTooNew(v) => write!(f, "Database too new: {v}"), + FlatChainstoreError::Poisoned => write!(f, "Poisoned"), + FlatChainstoreError::InvalidMagic(m) => write!(f, "Invalid magic: {m}"), + FlatChainstoreError::AccumulatorTooBig => write!(f, "Accumulator too big"), + FlatChainstoreError::IndexTooBig => write!(f, "Index too big"), + FlatChainstoreError::InvalidMetadataPointer => write!(f, "Invalid metadata pointer"), + FlatChainstoreError::DbCorrupted => write!(f, "Database corrupted"), + FlatChainstoreError::InvalidValidationIndex => write!(f, "Invalid validation index"), + } + } +} + /// Need this to use [FlatChainstoreError] as a [DatabaseError] in [ChainStore] impl DatabaseError for FlatChainstoreError {} diff --git a/crates/floresta-chain/src/pruned_utreexo/udata.rs b/crates/floresta-chain/src/pruned_utreexo/udata.rs index 19eac40a2..ab6f62cf6 100644 --- a/crates/floresta-chain/src/pruned_utreexo/udata.rs +++ b/crates/floresta-chain/src/pruned_utreexo/udata.rs @@ -229,6 +229,16 @@ pub mod proof_util { NotPushBytes, } + impl Display for LeafErrorKind { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + match self { + LeafErrorKind::EmptyStack => write!(f, "Empty stack"), + LeafErrorKind::InvalidInstruction(e) => write!(f, "Invalid instruction: {e}"), + LeafErrorKind::NotPushBytes => write!(f, "Not push bytes"), + } + } + } + /// Error while reconstructing a leaf's scriptPubKey, returned by `process_proof`. /// /// This error is triggered if the input lacks the hashed data required by the diff --git a/crates/floresta-node/src/slip132.rs b/crates/floresta-node/src/slip132.rs index a3beafd15..1cd98ae1c 100644 --- a/crates/floresta-node/src/slip132.rs +++ b/crates/floresta-node/src/slip132.rs @@ -9,7 +9,10 @@ //! Bitcoin SLIP-132 standard implementation for parsing custom xpub/xpriv key //! formats +use core::fmt; use core::fmt::Debug; +use core::fmt::Display; +use core::fmt::Formatter; use bitcoin::base58; use bitcoin::bip32; @@ -110,6 +113,23 @@ pub enum Error { InternalFailure, } +impl Display for Error { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + match self { + Error::Base58(e) => write!(f, "Base58 error: {e}"), + Error::Hex(e) => write!(f, "Hex error: {e}"), + Error::CannotDeriveFromHardenedKey => write!(f, "Cannot derive from hardened key"), + Error::InvalidChildNumber(n) => write!(f, "Invalid child number: {n}"), + Error::InvalidChildNumberFormat => write!(f, "Invalid child number format"), + Error::InvalidDerivationPathFormat => write!(f, "Invalid derivation path format"), + Error::UnknownVersion(v) => write!(f, "Unknown version: {v:?}"), + Error::WrongExtendedKeyLength(l) => write!(f, "Wrong extended key length: {l}"), + Error::UnknownSlip32Prefix => write!(f, "Unknown SLIP32 prefix"), + Error::InternalFailure => write!(f, "Internal failure"), + } + } +} + impl From for Error { fn from(err: bip32::Error) -> Self { match err { diff --git a/crates/floresta-watch-only/src/memory_database.rs b/crates/floresta-watch-only/src/memory_database.rs index 5b4e9acbc..92d3ddaaa 100644 --- a/crates/floresta-watch-only/src/memory_database.rs +++ b/crates/floresta-watch-only/src/memory_database.rs @@ -5,6 +5,10 @@ //! It's not meant to use in production, but for the integrated testing framework //! //! For actual databases that can be used for production code, see [KvDatabase](crate::kv_database::KvDatabase). +use core::fmt; +use core::fmt::Display; +use core::fmt::Formatter; + use bitcoin::hashes::sha256; use bitcoin::Txid; use floresta_common::prelude::sync::RwLock; @@ -34,6 +38,14 @@ pub struct MemoryDatabase { type Result = floresta_common::prelude::Result; +impl Display for MemoryDatabaseError { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + match self { + MemoryDatabaseError::PoisonedLock => write!(f, "Poisoned lock"), + } + } +} + impl MemoryDatabase { fn get_inner(&self) -> Result> { self.inner