Skip to content

Re-work sighash type conversion methods#796

Merged
dr-orlovsky merged 6 commits intorust-bitcoin:masterfrom
tcharding:as_u32
Mar 28, 2022
Merged

Re-work sighash type conversion methods#796
dr-orlovsky merged 6 commits intorust-bitcoin:masterfrom
tcharding:as_u32

Conversation

@tcharding
Copy link
Copy Markdown
Member

@tcharding tcharding commented Jan 18, 2022

This PR has evolved into a full blown clean up of the conversion methods for all three sighash types based on the discussion below.

Everything is split up into very small patches to aid review (and bikeshedding, this PR is almost totally just naming things).

EDIT: I'm not convinced this should be an RC-fix, the changes are too wide spread now. It started as just a single method rename. Leaving label as is for others to consider.

@sanket1729
Copy link
Copy Markdown
Member

This is also good, I also wouldn't mind nuking the inner method. Your preference.

sanket1729
sanket1729 previously approved these changes Jan 18, 2022
Copy link
Copy Markdown
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.

utACK 5efa0067344cbc9b03cc6c5f70729254e82a04c5

@tcharding
Copy link
Copy Markdown
Member Author

Let's nuke it :)

sanket1729
sanket1729 previously approved these changes Jan 18, 2022
Copy link
Copy Markdown
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 bec898e055d3b4024aa5221396dd5153b426557c

Copy link
Copy Markdown
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.

I think this deserves at least a comment but wouldn't block it.

@tcharding tcharding changed the title Use function name as_32 instead of inner Use function name to_32 instead of inner Jan 19, 2022
@RCasatta RCasatta changed the title Use function name to_32 instead of inner Use function name to_u32 instead of inner Jan 19, 2022
apoelstra
apoelstra previously approved these changes Jan 19, 2022
Copy link
Copy Markdown
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 f627f67626f3640af01c368094043a83e7d95c5c

@tcharding
Copy link
Copy Markdown
Member Author

@apoelstra please re-ack, if you agree, PR now uses to_u32_standard. Thanks.

Kixunil
Kixunil previously approved these changes Jan 27, 2022
Copy link
Copy Markdown
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.

ACK bb17582862146bdb83611e54c917e845f7a4241c

apoelstra
apoelstra previously approved these changes Jan 27, 2022
Copy link
Copy Markdown
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 bb17582862146bdb83611e54c917e845f7a4241c

I think the new name is a good idea, given our current crappy handling of non-standard flags.

@tcharding
Copy link
Copy Markdown
Member Author

ouch, I was on the wrong branch and rebased on master. Sorry fellas, no changes made. Please re-ack and give me a virtual slap.

Kixunil
Kixunil previously approved these changes Jan 31, 2022
Copy link
Copy Markdown
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.

ACK 9de614e6d9914194020861765c3739ce8e1cfcbc

apoelstra
apoelstra previously approved these changes Jan 31, 2022
Copy link
Copy Markdown
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 9de614e6d9914194020861765c3739ce8e1cfcbc

@tcharding
Copy link
Copy Markdown
Member Author

I believe this one should go in before we do the taproot release, friendly bump :)

@sanket1729
Copy link
Copy Markdown
Member

sanket1729 commented Feb 17, 2022

Thanks for the bump, I have been exclusively looking at taproot release PRs. Feel free to tag me for review.

I will look at your amazing PRs soon :) , but so far I was only tunnel visioning on 0.28 milestone. I missed the RC fix label.

Edit: Phone autocorrect "amazing PRs" to amazing places . Hopefully you did not see that :P

@sanket1729
Copy link
Copy Markdown
Member

I think the new name is a good idea, given our current crappy handling of non-standard flags.

This is not true. It is possible to have non-standard values if they are explicitly constructed from serialize::Deserialize with non-standard u32. I would rather it be called to_u32.

The purpose of PsbtSigHashType is to capture non-standard sighash types, but users would need their own parsing code to deal with it. All high level APIs like ecdsa_hash_ty and schnorr_hash_ty will fail and the user would need to parse the sighash byte separately and deal with it. No information should be lost in ser/de using psbt serialize methods.

@sanket1729 sanket1729 added this to the 0.28.0 milestone Feb 17, 2022
@apoelstra
Copy link
Copy Markdown
Member

Ok, given @sanket1729's comments I rescind my ACK

@Kixunil
Copy link
Copy Markdown
Collaborator

Kixunil commented Feb 17, 2022

It is possible to have non-standard values if they are explicitly constructed from serialize::Deserialize with non-standard u32.

This sounds massively broken. Either we should enforce it's standard in all constructors or in neither. I believe we agreed standard is better?

@tcharding tcharding dismissed stale reviews from dr-orlovsky and sanket1729 via ff811c5 March 23, 2022 01:52
@tcharding tcharding force-pushed the as_u32 branch 2 times, most recently from ff811c5 to 9b1ee35 Compare March 23, 2022 01:59
@tcharding
Copy link
Copy Markdown
Member Author

Changes in force push:

  • I added back ticks to a u32 in the docs change in 06e7b09 Rename as_u32 -> to_u32.
  • rebased

While checking over the diff before force pushing I noticed that this PR adds a From<&str> method that immediately calls to_string() on the argument. Do you want me to change this to a From<String> method @Kixunil and then call to_string at the call site? (Noticed while reviewing: #898)

@tcharding
Copy link
Copy Markdown
Member Author

This PR had a bunch of unrelated changes to the SchnorrSigHashType, these were conflicting with work being done by @dr-orlovsky in #898. I've pulled all those changes out and put them in a separate PR so we can more easily handle the conflicts.

sanket1729
sanket1729 previously approved these changes Mar 24, 2022
Copy link
Copy Markdown
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 51ac67356d6b581a99f4775546962a964c18b77b. In the interest of moving things forward, I think we should merge this after it meets the requirements(2 ACKs and no Nacks/comments for other reviewers).

We can address @Kixunil new comments in follow-up PR after the next RC or if required in the next breaking release.

@dr-orlovsky
Copy link
Copy Markdown
Collaborator

ACK 51ac67356d6b581a99f4775546962a964c18b77b

Not merging yet to give @apoelstra and @Kixunil another day to comment.

@dr-orlovsky
Copy link
Copy Markdown
Collaborator

ouh, this now requires rebase :( :( :(

@sanket1729
Copy link
Copy Markdown
Member

I think we should focus on getting this in before any other merge now :)

We have conversion functions that include suffixes `_consensus`
and `_standard` to make it explicit what guarantees are provided by the
returned `u32` value. The `From` implementation reduces the clarity of
the API.
Rust naming conventions stipulate that conversion methods from owned ->
owned for `Copy` types use the naming convention `to_`.

This change makes the function name objectively better, however it makes
no claims of being the 'best' name. We have had much discussion on using
`to_standard` vs `to_u32` but are unable to reach consensus.
`EcdsaSigHashType::from_u32` was deprecated in v0.26, since we are
working on the v0.28 release we can drop this method.
The functions `from_u32_standard` and `from_u32_consensus` smell a bit
like hungarian notation. We can look at the method definition to see
that the methods accept `u32` arguments without mentioning that in the
method names.

Remove `_u32_` from the method names. This brings the `from_*` methods
in line  with the `to_standard` method also.
Improve the `PsbtSigHashType` conversion methods by doing:

- Re-name `inner` -> `to_u32` as per Rust convention
- Add `from_u32` method

Note, we explicitly do _not_ use suffix 'consensus' because these
conversion methods make no guarantees about the validity of the
underlying `u32`.
It is possible, although not immediately obvious, that it is possible to
create a `PsbtSigHashType` with a non-standard value.

Add a unit test to show this and also catch any regressions if we
accidental change this logic.
@tcharding
Copy link
Copy Markdown
Member Author

Force push is rebase only, no other changes.

@tcharding
Copy link
Copy Markdown
Member Author

@Kixunil where have you gone man, are you ok with this one? Its not perfect but is it acceptable?

Copy link
Copy Markdown
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.

utACK 8e2422f.

Copy link
Copy Markdown
Collaborator

@dr-orlovsky dr-orlovsky left a comment

Choose a reason for hiding this comment

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

ACK 8e2422f

@dr-orlovsky
Copy link
Copy Markdown
Collaborator

dr-orlovsky commented Mar 28, 2022

Merging now: if there will be major comments from other reviewers will do a follow-up PR. We waited for more than a week for the comments already.

@dr-orlovsky dr-orlovsky merged commit 6417c37 into rust-bitcoin:master Mar 28, 2022
dr-orlovsky added a commit that referenced this pull request Mar 28, 2022
Post-merge #796 follow-up. Feel free to add other changes/nits which hadn't get into #796.
@tcharding
Copy link
Copy Markdown
Member Author

BOOM!

apoelstra added a commit that referenced this pull request Mar 28, 2022
c3d30d5 Remove deprecated method use for sighash conversion (Dr. Maxim Orlovsky)

Pull request description:

  Post-merge #796 follow-up. Feel free to add other changes/nits which hadn't get into #796.

ACKs for top commit:
  tcharding:
    ACK c3d30d5
  sanket1729:
    ACK c3d30d5
  apoelstra:
    ACK c3d30d5

Tree-SHA512: 7418f90bad9ce43b5504a3e45f06fecd636f62f2f7bd75bfc27e29faa181202595ed9b5175866e0fce01a301ea34c2b07afb16e658757215823965e7e1440c2e
@tcharding tcharding deleted the as_u32 branch April 4, 2022 22:29
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.

5 participants