Skip to content

Commit 993dfd5

Browse files
committed
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.
1 parent 6242d2f commit 993dfd5

File tree

6 files changed

+78
-0
lines changed

6 files changed

+78
-0
lines changed

crates/floresta-chain/src/extensions.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use core::cmp::min;
2+
use core::fmt;
23
use core::ops::Add;
34

45
use bitcoin::block::Header;
@@ -83,6 +84,16 @@ pub enum HeaderExtError {
8384
ChainWorkOverflow,
8485
}
8586

87+
impl fmt::Display for HeaderExtError {
88+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
89+
match self {
90+
HeaderExtError::Chain(e) => write!(f, "Chain error: {e}"),
91+
HeaderExtError::BlockNotFound => write!(f, "Block not found"),
92+
HeaderExtError::ChainWorkOverflow => write!(f, "Chain work overflow"),
93+
}
94+
}
95+
}
96+
8697
impl HeaderExt for Header {
8798
fn calculate_median_time_past(
8899
&self,

crates/floresta-chain/src/pruned_utreexo/chain_state_builder.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
//! - Assumed valid blocks for validation optimization
99
//! - UTREEXO accumulator state
1010
//! - Current chain tip and header
11+
use core::fmt;
12+
1113
use bitcoin::block::Header as BlockHeader;
1214
use bitcoin::BlockHash;
1315
use bitcoin::Network;
@@ -39,6 +41,17 @@ pub enum BlockchainBuilderError {
3941
Database(Box<dyn DatabaseError>),
4042
}
4143

44+
impl fmt::Display for BlockchainBuilderError {
45+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46+
match self {
47+
BlockchainBuilderError::MissingChainstore => write!(f, "Missing chainstore"),
48+
BlockchainBuilderError::MissingChainParams => write!(f, "Missing chain parameters"),
49+
BlockchainBuilderError::IncompleteTip => write!(f, "Incomplete tip"),
50+
BlockchainBuilderError::Database(e) => write!(f, "Database error: {:?}", e),
51+
}
52+
}
53+
}
54+
4255
#[derive(Clone, Debug, Default)]
4356
/// A builder for configuring and creating a `ChainState`.
4457
///

crates/floresta-chain/src/pruned_utreexo/flat_chain_store.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,24 @@ pub enum FlatChainstoreError {
382382
InvalidValidationIndex,
383383
}
384384

385+
impl fmt::Display for FlatChainstoreError {
386+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
387+
match self {
388+
FlatChainstoreError::Io(e) => write!(f, "I/O error: {e}"),
389+
FlatChainstoreError::BlockNotFound => write!(f, "Block not found"),
390+
FlatChainstoreError::IndexIsFull => write!(f, "Index is full"),
391+
FlatChainstoreError::DbTooNew(v) => write!(f, "Database too new: {v}"),
392+
FlatChainstoreError::Poisoned => write!(f, "Poisoned"),
393+
FlatChainstoreError::InvalidMagic(m) => write!(f, "Invalid magic: {m}"),
394+
FlatChainstoreError::AccumulatorTooBig => write!(f, "Accumulator too big"),
395+
FlatChainstoreError::IndexTooBig => write!(f, "Index too big"),
396+
FlatChainstoreError::InvalidMetadataPointer => write!(f, "Invalid metadata pointer"),
397+
FlatChainstoreError::DbCorrupted => write!(f, "Database corrupted"),
398+
FlatChainstoreError::InvalidValidationIndex => write!(f, "Invalid validation index"),
399+
}
400+
}
401+
}
402+
385403
/// Need this to use [FlatChainstoreError] as a [DatabaseError] in [ChainStore]
386404
impl DatabaseError for FlatChainstoreError {}
387405

crates/floresta-chain/src/pruned_utreexo/udata.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,16 @@ pub mod proof_util {
223223
NotPushBytes,
224224
}
225225

226+
impl fmt::Display for LeafErrorKind {
227+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
228+
match self {
229+
LeafErrorKind::EmptyStack => write!(f, "Empty stack"),
230+
LeafErrorKind::InvalidInstruction(e) => write!(f, "Invalid instruction: {e}"),
231+
LeafErrorKind::NotPushBytes => write!(f, "Not push bytes"),
232+
}
233+
}
234+
}
235+
226236
/// Error while reconstructing a leaf's scriptPubKey, returned by `process_proof`.
227237
///
228238
/// This error is triggered if the input lacks the hashed data required by the

crates/floresta-node/src/slip132.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//! Bitcoin SLIP-132 standard implementation for parsing custom xpub/xpriv key
88
//! formats
99
10+
use core::fmt;
1011
use std::fmt::Debug;
1112

1213
use bitcoin::base58;
@@ -108,6 +109,23 @@ pub enum Error {
108109
InternalFailure,
109110
}
110111

112+
impl fmt::Display for Error {
113+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
114+
match self {
115+
Error::Base58(e) => write!(f, "Base58 error: {e}"),
116+
Error::Hex(e) => write!(f, "Hex error: {e}"),
117+
Error::CannotDeriveFromHardenedKey => write!(f, "Cannot derive from hardened key"),
118+
Error::InvalidChildNumber(n) => write!(f, "Invalid child number: {n}"),
119+
Error::InvalidChildNumberFormat => write!(f, "Invalid child number format"),
120+
Error::InvalidDerivationPathFormat => write!(f, "Invalid derivation path format"),
121+
Error::UnknownVersion(v) => write!(f, "Unknown version: {:?}", v),
122+
Error::WrongExtendedKeyLength(l) => write!(f, "Wrong extended key length: {l}"),
123+
Error::UnknownSlip32Prefix => write!(f, "Unknown SLIP32 prefix"),
124+
Error::InternalFailure => write!(f, "Internal failure"),
125+
}
126+
}
127+
}
128+
111129
impl From<bip32::Error> for Error {
112130
fn from(err: bip32::Error) -> Self {
113131
match err {

crates/floresta-watch-only/src/memory_database.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ pub struct MemoryDatabase {
3232

3333
type Result<T> = floresta_common::prelude::Result<T, MemoryDatabaseError>;
3434

35+
impl Display for MemoryDatabaseError {
36+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37+
match self {
38+
MemoryDatabaseError::PoisonedLock => write!(f, "Poisoned lock"),
39+
}
40+
}
41+
}
42+
3543
impl MemoryDatabase {
3644
fn get_inner(&self) -> Result<sync::RwLockReadGuard<'_, Inner>> {
3745
self.inner

0 commit comments

Comments
 (0)