Skip to content

Commit a4c83a6

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 a4c83a6

File tree

6 files changed

+95
-0
lines changed

6 files changed

+95
-0
lines changed

crates/floresta-chain/src/extensions.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
use core::cmp::min;
2+
use core::fmt;
3+
use core::fmt::Display;
4+
use core::fmt::Formatter;
25
use core::ops::Add;
36

47
use bitcoin::block::Header;
@@ -83,6 +86,16 @@ pub enum HeaderExtError {
8386
ChainWorkOverflow,
8487
}
8588

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

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
//! - Assumed valid blocks for validation optimization
99
//! - UTREEXO accumulator state
1010
//! - Current chain tip and header
11+
use core::fmt;
12+
use core::fmt::Display;
13+
use core::fmt::Formatter;
14+
1115
use bitcoin::block::Header as BlockHeader;
1216
use bitcoin::BlockHash;
1317
use bitcoin::Network;
@@ -39,6 +43,17 @@ pub enum BlockchainBuilderError {
3943
Database(Box<dyn DatabaseError>),
4044
}
4145

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

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

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

72+
use core::fmt;
73+
use core::fmt::Display;
74+
use core::fmt::Formatter;
7275
use core::mem::size_of;
7376
use core::num::NonZeroUsize;
7477
use std::fs::DirBuilder;
@@ -382,6 +385,24 @@ pub enum FlatChainstoreError {
382385
InvalidValidationIndex,
383386
}
384387

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

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,10 @@ impl Encodable for ScriptPubKeyKind {
179179
/// to consume a block (delete transactions included in it from the mempool);
180180
/// or to validate a block.
181181
pub mod proof_util {
182+
use core::fmt;
183+
use core::fmt::Display;
184+
use core::fmt::Formatter;
185+
182186
use bitcoin::blockdata::script;
183187
use bitcoin::blockdata::script::Instruction;
184188
use bitcoin::consensus::Encodable;
@@ -223,6 +227,16 @@ pub mod proof_util {
223227
NotPushBytes,
224228
}
225229

230+
impl Display for LeafErrorKind {
231+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
232+
match self {
233+
LeafErrorKind::EmptyStack => write!(f, "Empty stack"),
234+
LeafErrorKind::InvalidInstruction(e) => write!(f, "Invalid instruction: {e}"),
235+
LeafErrorKind::NotPushBytes => write!(f, "Not push bytes"),
236+
}
237+
}
238+
}
239+
226240
/// Error while reconstructing a leaf's scriptPubKey, returned by `process_proof`.
227241
///
228242
/// 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
@@ -7,6 +7,9 @@
77
//! Bitcoin SLIP-132 standard implementation for parsing custom xpub/xpriv key
88
//! formats
99
10+
use core::fmt;
11+
use core::fmt::Display;
12+
use core::fmt::Formatter;
1013
use std::fmt::Debug;
1114

1215
use bitcoin::base58;
@@ -108,6 +111,23 @@ pub enum Error {
108111
InternalFailure,
109112
}
110113

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

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

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

3337
type Result<T> = floresta_common::prelude::Result<T, MemoryDatabaseError>;
3438

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

0 commit comments

Comments
 (0)