-
Notifications
You must be signed in to change notification settings - Fork 426
fix: no Debug on Display implementations
#2083
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
492114b to
b73016e
Compare
|
Concept ACK — this approach looks good, thanks for the changes. I ran the tests locally and they all passed. While trying to recreate the initial issue’s scenario (#1933), I found that I have a few small suggestions below. Most of these are nits, but since this PR is already addressing the area, I thought it’d be reasonable to mention them here:
|
evanlinjin
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Quality work.
Just a couple requested changes:
- Commit messages should contain a scope (as mentioned in the conventional commits spec). The scope should be the name of the package affected:
core,chain,example, etc. - Commits that break the API should be marked with
!.
And a nit:
- Error messages should be somewhat consistent. I personally don't think we should suggest actions in the message, just state what the error is.
|
@Dmenec are you able to make progress on this? |
b73016e to
532e2ec
Compare
|
@evanlinjin Pushed the updates. Let me know if everything checks out :) |
evanlinjin
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
utACK 532e2ec
I'm happy with how this is looking. Unable to test until I have access to a computer again.
789d8a3 to
d65eed0
Compare
|
Having some trouble with the no_std CI check for the esplora crate... Does it actually support no_std, or should be treated as std‑only? |
@Dmenec It's now fixed on master, a rebase should fix it. |
f0698e1 to
d65eed0
Compare
crates/file_store/src/lib.rs
Outdated
| ), | ||
| Self::Io(e) => write!(f, "io error trying to read file: {}", e), | ||
| Self::InvalidMagicBytes { got, expected } => { | ||
| write!(f, "file has invalid magic bytes: expected=0x")?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Error: could not open file store
Caused by:
file has invalid magic bytes: expected=0x62646b5f6578616d706c655f656c65637472756d got=0x62646b5f6578616d706c5f656c65637472756d01Given the format of the error output I would use invalid magic bytes without the file has.
This is a good opportunity to rewrite the others in the same spirit.
Also, stacking expected and got improves the feedback to the reader:
Error: could not open file store
Caused by:
invalid magic bytes
expected: 0x62646b5f6578616d706c655f656c65637472756d
got: 0x62646b5f6578616d706c5f656c65637472756d01There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure! I'll update the ones that are in scope for this PR and push the proposed changes shortly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Resolved in ae17df2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Error: could not open file store Caused by: file has invalid magic bytes: expected=0x62646b5f6578616d706c655f656c65637472756d got=0x62646b5f6578616d706c5f656c65637472756d01Given the format of the error output I would use
invalid magic byteswithout thefile has.This is a good opportunity to rewrite the others in the same spirit.
Also, stacking
expectedandgotimproves the feedback to the reader:Error: could not open file store Caused by: invalid magic bytes expected: 0x62646b5f6578616d706c655f656c65637472756d got: 0x62646b5f6578616d706c5f656c65637472756d01
Sorry for the back-and-forth, but my comment invalidates this. Error messages should be single line as per convention! Can use the # for "pretty print".
| impl<K: core::fmt::Debug> core::fmt::Display for InsertDescriptorError<K> { | ||
| /// Returns a shortened string representation of a descriptor. | ||
| /// Shows the first and last `edge_len` characters, separated by `...`. | ||
| fn short_descriptor(desc: &Descriptor<DescriptorPublicKey>, edge_len: usize) -> String { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've tried this with the following bin:
use bdk_chain::bitcoin::key::Secp256k1;
use bdk_chain::indexer::keychain_txout::KeychainTxOutIndex;
use bdk_chain::miniscript::Descriptor;
const DESC: &str = "...";
fn main() {
let lookahead = 10;
let use_cache = true;
let mut index = KeychainTxOutIndex::new(lookahead, use_cache);
let secp = Secp256k1::new();
let (desc, _) = Descriptor::parse_descriptor(&secp, DESC).unwrap();
index.insert_descriptor("some", desc.clone()).unwrap();
if let Err(e) = index.insert_descriptor("other", desc) {
eprintln!("{}", e);
}
}I think the output is not very clear:
descriptor 'tr([...d45f9rlk' is already in use by another keychain 'some'.I would keep only a fixed size prefix in the output, including the descriptor type and trying to capture the origin keypath if possible.
@qatkk proposed to increase the suffix to capture the checksum, but without any clear accessors to those components it is a "best guess" effort.
Maybe we should propose the implementation of them at the miniscript level, and add methods to get strings from DescriptorType for example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cACK — I’ll open a follow‑up PR with the proposal and then apply it here. Thanks for the suggestion!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nymius that is a good point. For practicality, can we remove the short_descriptor concept in this PR and just print the full descriptor for now?
Let's add it back once it is more readable (including the descriptor type, key path, checksum, etc.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, let's use full descriptors for now.
…iptorError Replace Debug-based formatting with user-friendly Display messages.
d65eed0 to
ae17df2
Compare
| writeln!(f, "{}", shown[0])?; | ||
| for op in &shown[1..] { | ||
| writeln!(f, "{}", op)?; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't executed this yet, but my guess is it will print the outpoint vertically, as you are using writeln! instead of write!.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, thought maybe it would be a bit more readable:
missing `TxOut` for input(s)
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:0
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb:1
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc:2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd suggest keeping Display single-line to follow Rust conventions. Callers typically don't expect {} to emit multiple lines, and it composes better with logging/error chains.
If we really want a pretty-printed multi-line version later, we can use the alternate flag ({:#}) for that. I don't think it's necessary for this PR (but feel free to include it).
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if f.alternate() {
// multi-line version
todo!()
} else {
// single-line version
todo!()
}
}There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need the multi-line now, can add in the future if needed. Also, what's the rationale for the leading whitespace ?
…eError Limited to 3 max shown items and added a suffix when there are additional entries.
ae17df2 to
91abd25
Compare
…rror. Format magic bytes as 0x-prefixed hex.
91abd25 to
75d1bd0
Compare
evanlinjin
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the bike-shedding. We're very close.
| write!( | ||
| f, | ||
| "attempt to re-assign keychain {keychain:?} already assigned to {existing:?}" | ||
| "keychain '{}' is already associated with another descriptor '{}'.", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As per Rust API Guidelines:
The error message given by the Display representation of an error type should be lowercase without trailing punctuation, and typically concise.
Please remove the trailing punctuation thanks!
| Self::Io(e) => write!(f, "io error while reading store file: {e}"), | ||
| Self::Bincode(e) => write!(f, "bincode error while decoding entry {e}"), | ||
| Self::InvalidMagicBytes { got, expected } => { | ||
| writeln!(f, "invalid magic bytes")?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also here, please make this single line thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to clarify the final intended format:
invalid magic bytes: expected 0x01020304, got 0x10203040
and the one mentioned in #2083 (comment)
missing `TxOut` for input(s): aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:0, ...
Does this look okay? Thank you!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Dmenec looks good thanks.
Nit: Message for the second one can be simplified to cannot calculate fee, missing output(s):. 😅 Messages for CalculateFeeError should mention that it's a calculate fee error?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Message for the second one can be simplified to
cannot calculate fee, missing output(s):. 😅 Messages forCalculateFeeErrorshould mention that it's a calculate fee error?
What do you think about cannot calculate fee, missing previous output(s)? Since we're referring to the outpoints of the inputs, I think it's clearer. Also agree on mentioning that it's a calculate fee error :)
oleonardolima
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall it looks good, it just need to address Evan's comments.
| writeln!(f, "{}", shown[0])?; | ||
| for op in &shown[1..] { | ||
| writeln!(f, "{}", op)?; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need the multi-line now, can add in the future if needed. Also, what's the rationale for the leading whitespace ?
Description
Fixes #1933, remove the usage of
DebugonDisplayimplemetations mentioned in #1881 (comment)Changelog notice
Debugusage withDisplaymessages forInsertDescriptorError,CalculateFeeErrorandStoreError.InvalidMagicBytesnow displays expected and actual bytes in hexadecimal instead of usingDebug.short_descriptorfunction to shorten descriptors for concise error output.SyncRequestDisplayfor Esplora and Electrum examples.Checklists
All Submissions:
just check,just fmtandjust testbefore committing