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

Improve timelock code #414

Merged
merged 7 commits into from May 19, 2022
Merged

Conversation

tcharding
Copy link
Member

Done while working on a timelock module. This is all the initial patches (except one) from #408 (which is a PR displaying usage of the new timelock module).

Note, does not do the 'make TimelockInfo fields pub crate' change - I was unsure if this was correct.

@tcharding tcharding marked this pull request as ready for review May 19, 2022 01:18
apoelstra
apoelstra previously approved these changes May 19, 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 e870519

sanket1729
sanket1729 previously approved these changes May 19, 2022
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 e870519. Thanks for looking into this. Apart from the refactor here, there is also great value in someone looking at this code again.

@@ -89,7 +89,7 @@ impl TimeLockInfo {
|mut timelock_info, sub_timelock| {
// If more than one branch may be taken, and some other branch has a requirement
// that conflicts with this one, set `contains_combination`
if k >= 2 {
if k > 1 {
Copy link
Member

Choose a reason for hiding this comment

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

This one is an improvement and I will ACK this change. But generally speaking, the symbols > >= should represent the way to think about the code rather than the conversion.

If while explaining some concept, we usually say "Two or more" rather than "more than one", it makes sense to write >=2.

Copy link
Member Author

Choose a reason for hiding this comment

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

Interesting, thanks for your thoughts. I tried to explain this if statement out loud to myself and I was unsure if I would say "if there are more than 1 ..." or "if there are 2 or more". I think I'd say "more than one" but I think that's just from reading > statements many times, so might be circular reasoning :) Amusingly I then opened the editor at this line and there is the following code comment

                // If more than one branch may be taken, and some other branch has a requirement
                // that conflicts with this one, set `contains_combination`.

@sanket1729
Copy link
Member

This would now has conflicts because of changes in #340. Would need rebase

Bitcoin Core uses the identifier `LOCKTIME_THRESHOLD` but we are using
`HEIGHT_TIME_THRESHOLD`. Its not immediately clear that one is better
than the other so lets use the same identifier as Bitcoin Core.
It is more typical to write `if k > 1` than `if k >= 2`.
Use more descriptive names for the 'combine' methods while removing the
'timelocks' bit of the method names.
We currently use the form "time lock" and also "timelock" (implies
`TimeLock`, `Timelock`). Which we use is not important but we should be
uniform. Favour "timelock" (and `Timelock`).
Add a simple unit test showing how `combine_threshold` works. Test
asserts that multiple timelocks of the same type result in unspendable
paths if threshold is 2.
When using `fold` the first argument is the accumulator, we can use the
typical functional programming identifier `acc` with no loss of clarity.
The other argument is a timelock, this is clear because we are iterating
`timelocks`, use `t` instead of `sub_timelock` with no loss of
clarity (subjective).
@tcharding
Copy link
Member Author

Force push is rebase only.

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 d1fdbaa. Reviwed the range-diff

@sanket1729 sanket1729 merged commit d40003c into rust-bitcoin:master May 19, 2022
@tcharding tcharding deleted the 05-19-timelock branch May 19, 2022 23:57
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

3 participants