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

TaprootMerkleBranch improvements #2263

Merged

Conversation

Kixunil
Copy link
Collaborator

@Kixunil Kixunil commented Dec 7, 2023

This contains several improvements to TaprootMerkleBranch that make the API more idiomatic.

@Kixunil Kixunil added enhancement API break This PR requires a version bump for the next release code quality Makes code easier to understand and less likely to lead to problems 1.0 Issues and PRs required or helping to stabilize the API API hole Important API missing - significantly degrades usability or idiomatic usage labels Dec 7, 2023
@Kixunil Kixunil force-pushed the taproot-merkle-branch-improvements branch 7 times, most recently from d3665af to 20a65c7 Compare December 8, 2023 11:59
While `clippy` now allows `TBD` to be used in `since` parameter of
`deprecated` attribute it is only available in the newest, nightly,
version. Switch `clippy` version to nightly to enable the `TBD` value.
These names are more descriptive.
@Kixunil Kixunil force-pushed the taproot-merkle-branch-improvements branch from 20a65c7 to 83f00d9 Compare December 8, 2023 12:23
self.0.iter()
}
}

Copy link
Member

@tcharding tcharding Dec 8, 2023

Choose a reason for hiding this comment

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

If we have DerefMut is there a reason we don't have IntoIterator for &'a mut TaprootMerkleBranch?

I see we do get iter_mut (from having Deref to a slice I believe).

(I'm trying to deepen my understanding of all all these stdlib traits.)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oh, I forgot about that one!

tcharding
tcharding previously approved these changes Dec 8, 2023
Copy link
Member

@tcharding tcharding left a comment

Choose a reason for hiding this comment

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

ACK 83f00d9

My comments are questions for my personal education, if you have the time and inclination to answer them please.

The type is naturally a collection of hashes so make it behave that way
by implementing `Deref`, `AsRef`, `Borrow` and their mutable versions as
well as `IntoIterator` for its reference. `IntoIterator` for itself is
not yet implemented because it's a bit more complicated.
Since the iterator created by `IntoIterator` should be called `IntoIter`
we move the whole `TaprootMerkleBranch` to its own module which contains
the type to avoid confusion. This has an additional benefit of reducing
the scope where the invariant could be broken. This already uncovered
that our internal code was abusing access to the private field (although
the code was correct).

To implement the iterator we simply delegate to `vec::IntoIter`,
including overriding the default method which are likely to be
implemented by `Vec` more optimally. We avoid exposing `vec::IntoIter`
directly since we may want to change the representation (e.g. to
`ArrayVec`).
These functions either delegate to functions with identical signature or
contain condition that may be optimized-out after inlining.
@Kixunil
Copy link
Collaborator Author

Kixunil commented Dec 8, 2023

Added the missing IntoIterator for &mut TaprootMerkleBranch and a bunch of #[inline] attributes.

Copy link
Member

@tcharding tcharding left a comment

Choose a reason for hiding this comment

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

ACK e1cc989

///
/// # Errors
/// If inner proof length is more than [`TAPROOT_CONTROL_MAX_NODE_COUNT`] (128).
#[inline]
Copy link
Member

Choose a reason for hiding this comment

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

What is the benefit of inlining a private function?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Not thinking when I add attributes. :D But it's harmless so I suggest let's keep it unless anyone objects or the PR has to change anyway.

Copy link
Member

Choose a reason for hiding this comment

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

Good with me.

@apoelstra
Copy link
Member

LGTM! Note that we're almost guaranteed to get breakages from using a nightly clippy.

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 e1cc989

@apoelstra apoelstra merged commit e0886e6 into rust-bitcoin:master Dec 11, 2023
29 checks passed
@Kixunil Kixunil deleted the taproot-merkle-branch-improvements branch December 11, 2023 16:15
@Kixunil
Copy link
Collaborator Author

Kixunil commented Dec 11, 2023

If we want to use TBD we don't have any choice. But it will be fine once the changes reach a release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1.0 Issues and PRs required or helping to stabilize the API API break This PR requires a version bump for the next release API hole Important API missing - significantly degrades usability or idiomatic usage code quality Makes code easier to understand and less likely to lead to problems enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants