8000 Clean up bip158 module by tcharding · Pull Request #1180 · rust-bitcoin/rust-bitcoin · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Clean up bip158 module #1180

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

Merged
merged 10 commits into from
Aug 11, 2022
Merged

Clean up bip158 module #1180

merged 10 commits into from
Aug 11, 2022

Conversation

tcharding
Copy link
Member
@tcharding tcharding commented Aug 10, 2022

In attempt to resolve #1147 clean up the bip158 module.

I was unable to resolve the "confusing method names read and write that look like those from std:i:o::{trait}"

I find the bip158 data types and abstractions kind a funky but was unable to come up with any improvements.

Open question: Are all the public data types really meant to be public? Perhaps we should have an issue to write an example module that uses the bip158 module?

apoelstra
apoelstra previously approved these changes Aug 10, 2022
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 7673377e465242c09626e99f87e562653a44960b

The function `match_all` takes an iterator, it does not need to use the
`Seek` trait, we can just pass in the content as a slice, no need to
wrap it in a `Cursor`.
Currently in the `bip158` module we do a bunch of dynamic dispatch using
the reader/writer objects. We can improve runtime performance, at the
cost of compile time, by using generics instead of dynamic dispatch.
Conventionally we only put a single newline between types, impl blocks,
and functions.
To ease reading, put the `new` method at the top of the impl block.
Improve rustdocs while we do it.
Improve the module rustdocs for `bip158` by doing:

- Add bip references
- Use correct markdown headings
- Use caps in title
Refactor the `new_script_filter` by doing:

- Put it directly below the `new` constructor
- Improve docs
- Remove unneeded scope
- Correctly indent `where` keyword
Rust convention stipulates that acronyms are in snake case not all caps.
We should use `Gcs` for Golomb Coded Sets
Refactor the import statements for the bip158 tests module. Includes
removing `extern crate` which is unnecessary now we have edition 2018.
Make an effort to improve the rustdocs throughout the `bip158` module.
@tcharding
Copy link
Member Author

Rebase to pick up serde pinning change, 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 167ee8e

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 167ee8e

@apoelstra apoelstra merged commit d29f81a into rust-bitcoin:master Aug 11, 2022
Copy link
Collaborator
@Kixunil Kixunil left a comment

Choose a reason for hiding this comment

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

Yesterday was difficult for me, found the time for review only now. Looks OK, just a small improvement idea.

pub fn match_any(&self, block_hash: &BlockHash, query: &mut dyn Iterator<Item=&[u8]>) -> Result<bool, Error> {
pub fn match_any<'a, I>(&self, block_hash: &BlockHash, query: I) -> Result<bool, Error>
where
I: Iterator<Item = &'a [u8]>,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could be further improved to accept I: Iterator, I::Item: Borrow<[u8]>

Copy link
Member

Choose a reason for hiding this comment

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

I like this -- I wish I'd kept the PR open a bit longer. (Sorry to merge out from under you but it was a set of unoffensive cleanups and I wasn't sure how long you'd be busy.)

Copy link
Collaborator

Choose a reason for hiding this comment

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

No problem, can always be added later.

Copy link
Member Author

Choose a reason for hiding this comment

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

Why Borrow instead of AsRef @Kixunil?

If generic code merely needs to work for all types that can provide a reference to related type T, it is often better to use AsRef as more types can safely implement it.

ref: https://doc.rust-lang.org/std/borrow/trait.Borrow.html

I tried both and they both work, since AsRef is more general I'll push a PR with that, waiting on your correction though if needed. Thanks

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Collaborator

Choose a reason for hiding this comment

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

It shouldn't really matter for this one. Lately I default to Borrow due to correctness implications, so I automatically wrote that one.

Note that there is non-correctness case when Borrow works and AsRef doesn't: when you have T: Sized and want to be able to take owned T - usually in structs but also Iter::Item: Borrow<T>, T: Copy.

@tcharding tcharding added this to the 0.30.0 milestone Aug 11, 2022
@tcharding tcharding added documentation code quality Makes code easier to understand and less likely to lead to problems aggregate release notes mention labels Aug 11, 2022
@tcharding tcharding deleted the 08-08-bip158 branch August 15, 2022 04:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
aggregate release notes mention code quality Makes code easier to understand and less likely to lead to problems
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Audit/cleanup BIP158 code
4 participants
0