Skip to content
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

Format the rust-bitcoin crate #1434

Merged
merged 16 commits into from Mar 22, 2023

Conversation

tcharding
Copy link
Member

@tcharding tcharding commented Dec 2, 2022

One final push crew, 16 patches, only a few are big.

All non-trivial formatting is done in separate patches so the changes can be verified mechanically.

With this applied the whole rust-bitcoin crate will be formatted.

Big thanks to everyone for putting up with the ongoing formatting PRs, no-one likes doing these but hopefully this an improvement to the project - especially in helping us get more contributors to the project.

@tcharding
Copy link
Member Author

tcharding commented Dec 2, 2022

Oh and massive props to everyone patching the psbt module, check out the last patch of this series

commit 4ea682edf1812d77cab3ab35f2e4256c4bfacd46
Author: Tobin C. Harding <me@tobin.cc>
Date:   Fri Dec 2 14:52:46 2022 +1100
 
    psbt: Enable the formatter
    
    Remove the exclude for the `psbt` module. No formatting issues currently
    exist in the `psbt` module - WIN!
 
diff --git a/rustfmt.toml b/rustfmt.toml
index a14aa3a7..1942f2c4 100644
--- a/rustfmt.toml
+++ b/rustfmt.toml
@@ -1,6 +1,5 @@
 # Eventually this shoud be: ignore = []
 ignore = [
-       "bitcoin/src/psbt",
        "hashes",
 ]

cc @DanGould @sanket1729

@tcharding
Copy link
Member Author

Will need to be rebased on top of #1432 to pass CI

luckysori
luckysori previously approved these changes Dec 2, 2022
Copy link

@luckysori luckysori left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

No, but in all seriousness, thank you, @tcharding. As someone who has spent quite a few hours reading this codebase, I think it could be very valuable for future contributors and consumers to see consistently formatted code.

Comment on lines 1406 to 1423

use super::*;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This happens automatically? In the projects I've worked on we usually try to keep all imports together because it's hard to enforce any other convention.

Copy link
Member Author

@tcharding tcharding Dec 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely automatic, well I definitely don't remember moving it and I noticed it too. But that's why I request a second dev to do the changes with the tools - I have been known to make mistakes (cough psbt still needs doing).

Comment on lines -831 to -811
use super::{deserialize, serialize, Error, CheckedData, VarInt};
use super::{Transaction, BlockHash, FilterHash, TxMerkleNode, TxOut, TxIn};

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤩

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That one was manual :)

Comment on lines 387 to 378
0..=0xFC => 1,
0xFD..=0xFFFF => 3,
0x10000..=0xFFFFFFFF => 5,
_ => 9,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose you could annotate this with #[rustfmt::skip] if you really like the original formatting.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does anyone prefer the original? I wasn't fussed either way.


use crate::blockdata::transaction::Transaction;
use crate::consensus::{encode, Decodable, Encodable};
use crate::error::Error::{self, BlockBadProofOfWork, BlockBadTarget};
pub use crate::hash_types::BlockHash;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps for another time, but I like it when pub use statements appear in a separate import section. It makes it easier to see what is part of the public API of a module.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that is a definite pain point with the current config, I was unable to get rustfmt to behave as you suggest.

Comment on lines 423 to 425
let prevhash =
Vec::from_hex("4ddccd549d28f385ab457e98d1b11ce80bfea2c5ab93015ade4973e400000000")
.unwrap();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this kind of code would format with less noise like this:

let prevhash = "4ddccd549d28f385ab457e98d1b11ce80bfea2c5ab93015ade4973e400000000";
let prevhash = Vec::from_hex(prevhash).unwrap();

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want to migrate this to const literal implemented using the fact that str.len() is const fn. AFAIK putting it into variable would destroy it. I'm not sure what the length of that one is but hex!("...") might be short enough. If it's not I'd seriously consider changing max line length.

Comment on lines -176 to +163
pub const BITCOIN: Self = Self([111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247, 79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0]);
pub const BITCOIN: Self = Self([
111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247, 79, 147, 30, 131,
101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0,
]);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be nice to format these with 4 rows of 8 bytes and add the rustfmt::skip attribute. In a separate prior commit that is.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should be able to use hex!() once we bump MSRV to 1.48.0

Comment on lines -280 to +322
Self::IntegerOverflow(val) => write!(f, "{} seconds is too large to be encoded to a 16 bit 512 second interval", val),
Self::IncompatibleHeight(lock, height) => write!(f, "tried to satisfy lock {} with height: {}", lock, height),
Self::IncompatibleTime(lock, time) => write!(f, "tried to satisfy lock {} with time: {}", lock, time),
Self::IntegerOverflow(val) => write!(
f,
"{} seconds is too large to be encoded to a 16 bit 512 second interval",
val
),
Self::IncompatibleHeight(lock, height) =>
write!(f, "tried to satisfy lock {} with height: {}", lock, height),
Self::IncompatibleTime(lock, time) =>
write!(f, "tried to satisfy lock {} with time: {}", lock, time),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original was a bit nicer, but this is pretty boring code anyway.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, formatting of Error impls is often rubbish. Apart from using shorter strings I'm not sure how to fix this. There are many instances throughout the crate of this sort of horrible formatting.

I tried importing Self::* just now but the lines are still too long.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is better

        use Error::*;

        match *self {
            IntegerOverflow(val) =>
                write!(f, "{} seconds does not fit in a 16 bit 512 second interval", val),
            IncompatibleHeight(lock, height) =>
                write!(f, "tried to satisfy lock {} with height: {}", lock, height),
            IncompatibleTime(lock, time) =>
                write!(f, "tried to satisfy lock {} with time: {}", lock, time),
        }

I've added "audit code base for stupid formatting" to my todo list (that should cover your array suggestion above also)

@@ -687,6 +692,7 @@ impl All {
(OP_VERIF, _) | (OP_VERNOTIF, _) | (OP_INVALIDOPCODE, _) => Class::IllegalOp,

// 15 opcodes illegal in Legacy context
#[rustfmt::skip]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You cheated, @tcharding!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As in this probably belongs in a separate commit to 0c95bb7.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bollocks! You got me. However, review for the win. Will fix and re-spin ... and do psbt.

Comment on lines 1213 to 1200
script = script.push_int(1); comp.push(81u8); assert_eq!(&script[..], &comp[..]);
script = script.push_int(0); comp.push(0u8); assert_eq!(&script[..], &comp[..]);
script = script.push_int(4); comp.push(84u8); assert_eq!(&script[..], &comp[..]);
script = script.push_int(-1); comp.push(79u8); assert_eq!(&script[..], &comp[..]);
script = script.push_int(1);
comp.push(81u8);
assert_eq!(&script[..], &comp[..]);
script = script.push_int(0);
comp.push(0u8);
assert_eq!(&script[..], &comp[..]);
script = script.push_int(4);
comp.push(84u8);
assert_eq!(&script[..], &comp[..]);
script = script.push_int(-1);
comp.push(79u8);
assert_eq!(&script[..], &comp[..]);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be another candidate for rustfmt::skip since the original does make it easier to understand the test.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll go with you on this one, will fix in next iteration, cheers.

Comment on lines 1510 to 1500
10,
100,
255,
256,
1000,
10000,
25000,
200000,
5000000,
1000000000,
(1 << 31) - 1,
-((1 << 31) - 1),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's quite satisfying!

@sanket1729
Copy link
Member

sanket1729 commented Dec 3, 2022

@tcharding, Is this all the formatting to be done? I am getting a large diff with cargo fmt. Ideally running cargo +nightly fmt on tip head should not have any diff

sanket1729:~/rust-bitcoin$ cargo --version
cargo 1.67.0-nightly (e027c4b5d 2022-11-25)

@tcharding
Copy link
Member Author

tcharding commented Dec 3, 2022

@tcharding, Is this all the formatting to be done? I am getting a large diff with cargo fmt. Ideally running cargo +nightly fmt on tip head should not have any diff

sanket1729:~/rust-bitcoin$ cargo --version
cargo 1.67.0-nightly (e027c4b5d 2022-11-25)

WTF they are all in psbt, seems after 6 months of formatting changes I still don't know how to do it :( Somehow I convinced myself psbt had no formatting issues - face palm.

@tcharding
Copy link
Member Author

Thanks for the reviews! Sorry for the mistakes, hopefully the range-diff will not be too much work to re-review when I force push.

@tcharding
Copy link
Member Author

Seen to review suggestions. Please note while re-creating all the formatting patches after rebase I stumbled upon an issue with a code comment that gets moved incorrectly by the formatter. Fixed in a new patch 97bda3e9 Make test panic instead of using code comment.

@tcharding
Copy link
Member Author

Also, just so I don't create any more false hopes, please note the hashes crate is not yet formatted.

@tcharding
Copy link
Member Author

Rebased and redid consensus and blockdata formatting patches. Also re-rand formatter and added changes to patch 1.

@Kixunil
Copy link
Collaborator

Kixunil commented Dec 8, 2022

Suggestion for you to not have to rebase this for eternity:

  • Drop formatting and file activation patches
  • Drop any patches against files which have large PRs open against them (unsized script mainly)
  • Merge the resulting PR (should only have things like rustfmt::skip, creating temporary variable and similar)

The last change before release (which means during the freeze period) will be formatting and activation of all files.

I understand it may suck for you to read unformatted code but honestly, if I had to rebase unsized script or similarly large PR I'd be pissed off to the point of considering quitting. And rebasing this every time wastes your time and spams my notifications.

@tcharding
Copy link
Member Author

I understand it may suck for you to read unformatted code but honestly, if I had to rebase unsized script or similarly large PR I'd be pissed off to the point of considering quitting.

I'm definitely not pushing this through because of that, I stare soley at this library all day so the code layout is "normal" for me however it is - I'm pushing this because I think it improves the experience and perception of our cade base for others.

@tcharding
Copy link
Member Author

I'm not confident pushing the rustfmt skip patches without the formatting changes because I could miss new code. I can leave this until code freeze though, no worries.

@tcharding tcharding marked this pull request as draft December 8, 2022 21:18
@tcharding
Copy link
Member Author

Sorry to use your review time up @luckysori and then switch this to draft :(

@tcharding tcharding force-pushed the 12-02-format-rust-bitcoin branch 2 times, most recently from edb26b7 to ef2da64 Compare February 21, 2023 23:26
@tcharding tcharding marked this pull request as ready for review February 21, 2023 23:28
@tcharding
Copy link
Member Author

cc @luckysori in case you want to take a look. I had to redo some patches because I botched the rebase (specifically the block module) but I attempted to include all your previous suggestions.

@tcharding
Copy link
Member Author

Rebase and redo formatting patches to resolve conflicts so they maintain "mechanical only no manual changes".

@tcharding tcharding force-pushed the 12-02-format-rust-bitcoin branch 2 times, most recently from f6eb6ed to 11d968a Compare February 28, 2023 01:37
@tcharding
Copy link
Member Author

Same again: Rebase and redo formatting patches to resolve conflicts so they maintain "mechanical only no manual changes".

@tcharding
Copy link
Member Author

tcharding commented Mar 2, 2023

On ice until #1111 merges.

@tcharding tcharding marked this pull request as draft March 2, 2023 00:32
@tcharding
Copy link
Member Author

tcharding commented Mar 20, 2023

Assuming I'm correct about #1714 being the only problem with this then it would be super cool to merge this as the first PR merged after release of 0.30 before everyone starts rebasing their old PRs. All the formatting changes are in patches titled for example013c9fa4 hashes: Run the formatter so they are easy to verify and hopefully review.

@tcharding tcharding marked this pull request as ready for review March 20, 2023 07:39
@apoelstra
Copy link
Member

Sounds good! Could use a rebase to fix CI.

In preparation for running the formatter introduce a couple of local
variables to reduce the line length and inhibit function call from being
split over multiple lines.

Refactor only, no logic changes.
We already import `super::*`, these other imports are useless.
Remove the exclude for the `consensus` module. Do not run the formatter,
that will be done as a separate patch to aid review.
 Run `cargo +nightly fmt`, no other manual changes.
In preparation for formatting the `crypto` module add a couple of `skip`
attributes to keep arrays formatted 8 bytes per line.
Remove the exclude for the `crypto` module. Do not run the formatter,
that will be done as a separate patch to aid review.
Run `cargo +nightly fmt`, no other manual changes.
Currently we have a code comment that is supposed to assist devs in
maintaining the `network::constants::Network` type by failing to build
if a new variant is added. This plays havoc with the formatter because
the comment is hanging at the bottom of a match block and the formatting
thinks its for the proceeding line of code.

Instead of using a code comment add a panic so the unit test fails if a
new variant is added to `network::constants::Network`.
Remove the exclude for the `psbt` module. Do not run the formatter, that
will be done as a separate patch to aid review.
Run `cargo +nightly fmt`, no other manual changes.
Add `rustfmt::skip` attribute in a couple of places and then remove the
exclude for the `blockdata` module. Do not run the formatter, that will
be done as a separate patch to aid review.
Run `cargo +nightly fmt`, no other manual changes.
Remove the exclude for the `util` module. Do not run the formatter,
that will be done as a separate patch to aid review.
Run `cargo +nightly fmt`, no other manual changes.
The `hashes` module contains a bunch of arrays, mostly formatted with 8
hex bytes on a line; add `rustfmt::skip` to keep the formatting of
arrays as is.

Remove the exclude for the `hashes` crate. Do not run the formatter,
that will be done as a separate patch to aid review.
Run `cargo +nightly fmt`, no other manual changes.
@tcharding
Copy link
Member Author

Rebased to pick up CI fix, no other changes.

Copy link
Member

@sanket1729 sanket1729 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 913575a. Went through the workflow locally.

@apoelstra
Copy link
Member

Reviewed every change by hand. Thanks for al the work you did working around it! It still fucks up some comment alignment but nothing egregiously wrong.

Lol at that comment that you had to turn into a panic to prevent it attaching it to the wrong code ... I don't know what the hell they are thinking having a formatter that literally moves comments from one line of code to another.

Overall this is definitely an improvement, and 95% of it is stuff I'm pretty confident won't change in the future.

@tcharding
Copy link
Member Author

ACK 913575a. Went through the workflow locally.

Legend, thanks man!

@tcharding
Copy link
Member Author

Reviewed every change by hand. Thanks for al the work you did working around it! It still fucks up some comment alignment but nothing egregiously wrong.

Lol at that comment that you had to turn into a panic to prevent it attaching it to the wrong code ... I don't know what the hell they are thinking having a formatter that literally moves comments from one line of code to another.

Bit sad huh, but I"m not going to patch rustfmt so I'm not going to complain too much.

Overall this is definitely an improvement, and 95% of it is stuff I'm pretty confident won't change in the future.

I agree its definitely an improvement - hopefully over time someone else will improve rustfmt for us, "summer of rustfmt" ;)

match network {
Network::Bitcoin => {},
Network::Testnet => {},
Network::Signet => {},
Network::Regtest => {},
// Update ChainHash::using_genesis_block and chain_hash_genesis_block with new variants.
_ => panic!("Update ChainHash::using_genesis_block and chain_hash_genesis_block with new variants"),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OMG, please no. Put the comment on top of match instead. Anyway, we need a macro for this so the comment won't be needed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm quite confident this panic will be compiled away even with no optimizations, and it's only in a test ... and I'm skeptical that a macro would be easier to read/maintain.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The point is previously it was impossible to forget adding the branch now it's possible without any warning.

Single source of truth is super-important. Breaking it kicked my ass so hard so many times I don't want to ever do it again.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I now realized what it actually does. I'm OK with this for now but still think macro is superior.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because this is test code, we will get a warning -- the test failing with a panic.

I'll take your "OK for now" as an ok to merge this -- we can fix it to use a macro after the fact.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I intended to review this right after release but I guess it's fine. I will complain when I see something problematic later.

Copy link
Member

@apoelstra apoelstra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 913575a

@apoelstra apoelstra merged commit 24af58c into rust-bitcoin:master Mar 22, 2023
22 checks passed
@tcharding tcharding deleted the 12-02-format-rust-bitcoin branch March 22, 2023 02:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants