Skip to content

Commit 635af32

Browse files
committed
Merge #12: fix: no Debug on Display implementations
3c00f1a feat: add error messages for CreateWithPersistError Display implementation (Luis Schwab) c799b52 fix: remove Debugs from CreateTxError (Luis Schwab) 1721b29 fix: no Debug on LoadError (Luis Schwab) 187a494 feat: implement Display for LoadMismatch (Luis Schwab) 0acd802 feat: implement Display for KeychainKind (Luis Schwab) Pull request description: <!-- You can erase any parts of this template not applicable to your Pull Request. --> ### Description This PR removes the use of debugs on `Display` implemetations. ### Changelog notice - Implement Display for KeychainKind. - Implement Display for LoadMismatch. - Add error message for CreateWithPersistError. - The `CreateTxError::LockTime` arm had `requested` and `required` inverted, now fixed. - Remove debugs. ### Checklists #### All Submissions: * [X] I've signed all my commits * [X] I followed the [contribution guidelines](https://github.com/bitcoindevkit/bdk/blob/master/CONTRIBUTING.md) * [X] I ran `cargo fmt` and `cargo clippy` before committing ACKs for top commit: notmandatory: utACK 3c00f1a Tree-SHA512: a17984113e7ae4b49c5a5f5f711e80b8ffaf811b1d2f066e9b82cf454ae3ee33b67556bbf5bb61e082932f855ecace2d77d46ce514d457f5ad24a78fd9ae5aeb
2 parents 8e2eeb1 + 3c00f1a commit 635af32

File tree

4 files changed

+112
-14
lines changed

4 files changed

+112
-14
lines changed

wallet/src/types.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use alloc::boxed::Box;
1313
use chain::{ChainPosition, ConfirmationBlockTime};
1414
use core::convert::AsRef;
15+
use core::fmt;
1516

1617
use bitcoin::transaction::{OutPoint, Sequence, TxOut};
1718
use bitcoin::{psbt, Weight};
@@ -37,6 +38,15 @@ impl KeychainKind {
3738
}
3839
}
3940

41+
impl fmt::Display for KeychainKind {
42+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43+
match self {
44+
KeychainKind::External => write!(f, "External"),
45+
KeychainKind::Internal => write!(f, "Internal"),
46+
}
47+
}
48+
}
49+
4050
impl AsRef<[u8]> for KeychainKind {
4151
fn as_ref(&self) -> &[u8] {
4252
match self {

wallet/src/wallet/error.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ impl fmt::Display for CreateTxError {
112112
Self::Descriptor(e) => e.fmt(f),
113113
Self::Policy(e) => e.fmt(f),
114114
CreateTxError::SpendingPolicyRequired(keychain_kind) => {
115-
write!(f, "Spending policy required: {:?}", keychain_kind)
115+
write!(f, "Spending policy required: {}", keychain_kind)
116116
}
117117
CreateTxError::Version0 => {
118118
write!(f, "Invalid version `0`")
@@ -127,12 +127,12 @@ impl fmt::Display for CreateTxError {
127127
requested,
128128
required,
129129
} => {
130-
write!(f, "TxBuilder requested timelock of `{:?}`, but at least `{:?}` is required to spend from this script", required, requested)
130+
write!(f, "TxBuilder requested timelock of `{}`, but at least `{}` is required to spend from this script", requested, required)
131131
}
132132
CreateTxError::RbfSequenceCsv { sequence, csv } => {
133133
write!(
134134
f,
135-
"Cannot enable RBF with nSequence `{:?}` given a required OP_CSV of `{:?}`",
135+
"Cannot enable RBF with nSequence `{}` given a required OP_CSV of `{}`",
136136
sequence, csv
137137
)
138138
}

wallet/src/wallet/mod.rs

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,9 @@ impl fmt::Display for LoadError {
194194
LoadError::MissingNetwork => write!(f, "loaded data is missing network type"),
195195
LoadError::MissingGenesis => write!(f, "loaded data is missing genesis hash"),
196196
LoadError::MissingDescriptor(k) => {
197-
write!(f, "loaded data is missing descriptor for keychain {k:?}")
197+
write!(f, "loaded data is missing descriptor for {k} keychain")
198198
}
199-
LoadError::Mismatch(mismatch) => write!(f, "data mismatch: {mismatch:?}"),
199+
LoadError::Mismatch(e) => write!(f, "{e}"),
200200
}
201201
}
202202
}
@@ -232,6 +232,44 @@ pub enum LoadMismatch {
232232
},
233233
}
234234

235+
impl fmt::Display for LoadMismatch {
236+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
237+
match self {
238+
LoadMismatch::Network { loaded, expected } => {
239+
write!(
240+
f,
241+
"Network mismatch: loaded {}, expected {}",
242+
loaded, expected
243+
)
244+
}
245+
LoadMismatch::Genesis { loaded, expected } => {
246+
write!(
247+
f,
248+
"Genesis hash mismatch: loaded {}, expected {}",
249+
loaded, expected
250+
)
251+
}
252+
LoadMismatch::Descriptor {
253+
keychain,
254+
loaded,
255+
expected,
256+
} => {
257+
write!(
258+
f,
259+
"Descriptor mismatch for {} keychain: loaded {}, expected {}",
260+
keychain,
261+
loaded
262+
.as_ref()
263+
.map_or("None".to_string(), |d| d.to_string()),
264+
expected
265+
.as_ref()
266+
.map_or("None".to_string(), |d| d.to_string())
267+
)
268+
}
269+
}
270+
}
271+
}
272+
235273
impl From<LoadMismatch> for LoadError {
236274
fn from(mismatch: LoadMismatch) -> Self {
237275
Self::Mismatch(mismatch)

wallet/src/wallet/persisted.rs

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@ use core::{
66
pin::Pin,
77
};
88

9-
use alloc::boxed::Box;
9+
use alloc::{boxed::Box, string::ToString};
1010
use chain::Merge;
1111

12-
use crate::{descriptor::DescriptorError, ChangeSet, CreateParams, LoadParams, Wallet};
12+
use crate::{
13+
descriptor::{calc_checksum, DescriptorError},
14+
ChangeSet, CreateParams, LoadParams, Wallet,
15+
};
1316

1417
/// Trait that persists [`PersistedWallet`].
1518
///
@@ -361,16 +364,63 @@ pub enum CreateWithPersistError<E> {
361364
impl<E: fmt::Display> fmt::Display for CreateWithPersistError<E> {
362365
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
363366
match self {
364-
Self::Persist(err) => fmt::Display::fmt(err, f),
365-
Self::DataAlreadyExists(changeset) => write!(
366-
f,
367-
"Cannot create wallet in persister which already contains wallet data: {:?}",
368-
changeset
369-
),
370-
Self::Descriptor(err) => fmt::Display::fmt(&err, f),
367+
Self::Persist(err) => write!(f, "{}", err),
368+
Self::DataAlreadyExists(changeset) => {
369+
write!(
370+
f,
371+
"Cannot create wallet in a persister which already contains data: "
372+
)?;
373+
changeset_info(f, changeset)
374+
}
375+
Self::Descriptor(err) => {
376+
write!(f, "{err}")
377+
}
371378
}
372379
}
373380
}
374381

375382
#[cfg(feature = "std")]
376383
impl<E: fmt::Debug + fmt::Display> std::error::Error for CreateWithPersistError<E> {}
384+
385+
/// Helper function to display basic information about a [`ChangeSet`].
386+
fn changeset_info(f: &mut fmt::Formatter<'_>, changeset: &ChangeSet) -> fmt::Result {
387+
let network = changeset
388+
.network
389+
.as_ref()
390+
.map_or("None".to_string(), |n| n.to_string());
391+
392+
let descriptor_checksum = changeset
393+
.descriptor
394+
.as_ref()
395+
.and_then(|d| calc_checksum(&d.to_string()).ok())
396+
.unwrap_or_else(|| "None".to_string());
397+
398+
let change_descriptor_checksum = changeset
399+
.change_descriptor
400+
.as_ref()
401+
.and_then(|d| calc_checksum(&d.to_string()).ok())
402+
.unwrap_or_else(|| "None".to_string());
403+
404+
let tx_count = changeset.tx_graph.txs.len();
405+
406+
let anchor_count = changeset.tx_graph.anchors.len();
407+
408+
let block_count = if let Some(&count) = changeset.local_chain.blocks.keys().last() {
409+
count
410+
} else {
411+
0
412+
};
413+
414+
writeln!(f, " Network: {}", network)?;
415+
writeln!(f, " Descriptor Checksum: {}", descriptor_checksum)?;
416+
writeln!(
417+
f,
418+
" Change Descriptor Checksum: {}",
419+
change_descriptor_checksum
420+
)?;
421+
writeln!(f, " Transaction Count: {}", tx_count)?;
422+
writeln!(f, " Anchor Count: {}", anchor_count)?;
423+
writeln!(f, " Block Count: {}", block_count)?;
424+
425+
Ok(())
426+
}

0 commit comments

Comments
 (0)