Skip to content

Commit dcab56a

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 68e5a1c commit dcab56a

File tree

6 files changed

+90
-0
lines changed

6 files changed

+90
-0
lines changed

crates/floresta-chain/src/extensions.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
use core::cmp::min;
44
use core::error::Error;
5+
use core::fmt;
6+
use core::fmt::Display;
7+
use core::fmt::Formatter;
58
use core::ops::Add;
69

710
use bitcoin::block::Header;
@@ -85,6 +88,16 @@ pub enum HeaderExtError {
8588
ChainWorkOverflow,
8689
}
8790

91+
impl Display for HeaderExtError {
92+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
93+
match self {
94+
HeaderExtError::Chain(e) => write!(f, "Chain error: {e}"),
95+
HeaderExtError::BlockNotFound => write!(f, "Block not found"),
96+
HeaderExtError::ChainWorkOverflow => write!(f, "Chain work overflow"),
97+
}
98+
}
99+
}
100+
88101
impl HeaderExt for Header {
89102
fn calculate_median_time_past(
90103
&self,

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
//! - Assumed valid blocks for validation optimization
1111
//! - UTREEXO accumulator state
1212
//! - Current chain tip and header
13+
use core::fmt;
14+
use core::fmt::Display;
15+
use core::fmt::Formatter;
1316

1417
use bitcoin::block::Header as BlockHeader;
1518
use bitcoin::BlockHash;
@@ -42,6 +45,17 @@ pub enum BlockchainBuilderError {
4245
Database(Box<dyn DatabaseError>),
4346
}
4447

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

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@
7171
7272
extern crate std;
7373

74+
use core::fmt;
75+
use core::fmt::Display;
76+
use core::fmt::Formatter;
7477
use core::mem::size_of;
7578
use core::num::NonZeroUsize;
7679
use std::fs::DirBuilder;
@@ -384,6 +387,24 @@ pub enum FlatChainstoreError {
384387
InvalidValidationIndex,
385388
}
386389

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

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,16 @@ pub mod proof_util {
229229
NotPushBytes,
230230
}
231231

232+
impl Display for LeafErrorKind {
233+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
234+
match self {
235+
LeafErrorKind::EmptyStack => write!(f, "Empty stack"),
236+
LeafErrorKind::InvalidInstruction(e) => write!(f, "Invalid instruction: {e}"),
237+
LeafErrorKind::NotPushBytes => write!(f, "Not push bytes"),
238+
}
239+
}
240+
}
241+
232242
/// Error while reconstructing a leaf's scriptPubKey, returned by `process_proof`.
233243
///
234244
/// This error is triggered if the input lacks the hashed data required by the

crates/floresta-node/src/slip132.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
//! Bitcoin SLIP-132 standard implementation for parsing custom xpub/xpriv key
1010
//! formats
1111
12+
use core::fmt;
1213
use core::fmt::Debug;
14+
use core::fmt::Display;
15+
use core::fmt::Formatter;
1316

1417
use bitcoin::base58;
1518
use bitcoin::bip32;
@@ -110,6 +113,23 @@ pub enum Error {
110113
InternalFailure,
111114
}
112115

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

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
//! It's not meant to use in production, but for the integrated testing framework
66
//!
77
//! For actual databases that can be used for production code, see [KvDatabase](crate::kv_database::KvDatabase).
8+
use core::fmt;
9+
use core::fmt::Display;
10+
use core::fmt::Formatter;
11+
812
use bitcoin::hashes::sha256;
913
use bitcoin::Txid;
1014
use floresta_common::prelude::sync::RwLock;
@@ -34,6 +38,14 @@ pub struct MemoryDatabase {
3438

3539
type Result<T> = floresta_common::prelude::Result<T, MemoryDatabaseError>;
3640

41+
impl Display for MemoryDatabaseError {
42+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
43+
match self {
44+
MemoryDatabaseError::PoisonedLock => write!(f, "Poisoned lock"),
45+
}
46+
}
47+
}
48+
3749
impl MemoryDatabase {
3850
fn get_inner(&self) -> Result<sync::RwLockReadGuard<'_, Inner>> {
3951
self.inner

0 commit comments

Comments
 (0)