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

Implement TryFrom #1007

Merged
merged 5 commits into from Jun 29, 2022
Merged

Implement TryFrom #1007

merged 5 commits into from Jun 29, 2022

Conversation

tcharding
Copy link
Member

@tcharding tcharding commented May 25, 2022

Audit the whole codebase checking for any method that is of the form from_foo where foo is not an interesting identifier (like 'consensus' and 'standard'). Implement TryFrom for any such methods, deprecating the original.

Done as separate patches so any can be easily dropped if not liked.

apoelstra
apoelstra previously approved these changes May 27, 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 c898301

sanket1729
sanket1729 previously approved these changes Jun 1, 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.

LGTM c898301, but needs rebase

0x81 => AllPlusAnyoneCanPay,
0x82 => NonePlusAnyoneCanPay,
0x83 => SinglePlusAnyoneCanPay,
0xFF => Reserved,
Copy link
Member

Choose a reason for hiding this comment

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

Opened a new issue about removing the variant here

Copy link
Contributor

Choose a reason for hiding this comment

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

Linking the issue here: #1030

@tcharding tcharding dismissed stale reviews from sanket1729 and apoelstra via af94774 June 2, 2022 00:16
@tcharding tcharding force-pushed the 05-25-try-from branch 2 times, most recently from af94774 to 602c17a Compare June 2, 2022 00:18
@tcharding
Copy link
Member Author

Changes in force push:

  • rebased .. twice
  • Used to_u8 instead of deprecated into_u8

apoelstra
apoelstra previously approved these changes Jun 2, 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 602c17a

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.

Would've acked but I'm not sure about sighash.

fn try_from(opcode: opcodes::All) -> Result<Self, Self::Error> {
match opcode.to_u8() {
0 => Ok(WitnessVersion::V0),
version if version >= opcodes::all::OP_PUSHNUM_1.to_u8() && version <= opcodes::all::OP_PUSHNUM_16.to_u8() =>
Copy link
Collaborator

Choose a reason for hiding this comment

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

Doesn't clippy complain about this? I don't like the lint much anyway, just surprised.

Copy link
Member Author

Choose a reason for hiding this comment

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

Super bizarre, it seems that to trigger the 'manual range' lint there has to be an = sign. This does not trigger the lint

        let version = 0u8;
        if version > 1 && version < 10 {
            in_range = true;
        }

But this does (added =):

        let version = 0u8;
        if version >= 1 && version < 10 {
            in_range = true;
        }

src/util/taproot.rs Show resolved Hide resolved
src/util/sighash.rs Show resolved Hide resolved
@tcharding
Copy link
Member Author

Changes in force push:

  • Added the additional TryFrom<&[sha256::Hash]> version as suggested
  • rebased

Remaining open question: what to do about sighash? I do think this PR is a move in the correct direction, possibly we could tackle the naming of try_from vs from_consensus at a later date? @Kixunil do you think we can find consensus quickly/easily or is it a thorny one?

///
/// If inner proof length is more than [`TAPROOT_CONTROL_MAX_NODE_COUNT`] (128).
fn try_from(v: &[sha256::Hash]) -> Result<Self, Self::Error> {
let v = v.to_vec();
Copy link
Collaborator

Choose a reason for hiding this comment

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

This wasn't my point, the caller can already do this. :) My point was to check the length here so that allocation is avoided in the error case. If you want to avoid duplicating checking logic I have a trick for that:

// non-public
fn from_collection<T: AsRef<[sha256::Hash]> + Into<Vec<sha256::Hash>>>(collection: T) -> Result<Self, TaprootError> {
    if collection.as_ref().len() > TAPROOT_CONTROL_MAX_NODE_COUNT {
        Err(TaprootError::InvalidMerkleTreeDepth(collection.as_ref().len()))
    } else {
        Ok(TaprootMerkleBranch(collection.into()))
    }
}

// just directly call `Self::from_collection(v)` in all `TryFrom` impls

You could also use macros for TryFrom and also add Box<[sha256::Hash]> as well.

Copy link
Member Author

Choose a reason for hiding this comment

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

Nice trick :) Implemented as suggested, thanks.

@tcharding
Copy link
Member Author

tcharding commented Jun 19, 2022

Changes in force push: Amend the final commit adding a macro as suggested.

@tcharding
Copy link
Member Author

Weird, the CI run is not showing?

apoelstra
apoelstra previously approved these changes Jun 20, 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 6ef1ecc

@tcharding
Copy link
Member Author

Changes in force push:

  • rebased on master
  • Removed TryFrom<u8> for SchnorrSighashType patch, replaced with "rename from_u8 -> from_consensus_u8" patch
  • Re-ordered patches so the TryFrom ones were all together and the re-name was last.

@tcharding tcharding added this to the 0.29.0 milestone Jun 24, 2022
@apoelstra
Copy link
Member

Intermediate commits seem not to compile now.

We have a bunch of 'from' methods that are fallible; `TryFrom` became
available in Rust 1.34 so we can use it now we have bumped our MSRV.

Implement the various `WitnessVersion` from methods using `TryFrom` and
deprecate the originals.
TryFrom` became available in Rust 1.34 so we can use it now we have
bumped our MSRV.

Implement `TryFrom<TaprootBuilder>` for `TapTree` and deprecate the
`from_builder` method.
TryFrom` became available in Rust 1.34 so we can use it now we have
bumped our MSRV.

Implement `TryFrom<Key>` for `ProprietaryKey` and deprecate the
`from_key` method.
TryFrom` became available in Rust 1.34 so we can use it now we have
bumped our MSRV.

Add a macro for implementing `TryFrom` for various lists of
`sha256::Hash` types. Use the macro to for vec, slice, and boxed slice.
The `u8` parameter in the `SchnorrSighashType` constructor is a
consensus valid `u8`. Re-name the constructor to make this explicit.

Deprecate `from_u8` as is typical.
@tcharding
Copy link
Member Author

Force push is rebase to pick up clippy fix from #1063, no changes.

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 b29ff9b

(Having local compilation errors but just due to rust-bitcoin/rust-secp256k1#466 )

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.

ACK b29ff9b

@apoelstra apoelstra merged commit 022ab0b into rust-bitcoin:master Jun 29, 2022
@tcharding tcharding deleted the 05-25-try-from branch June 29, 2022 23:53
@Kixunil Kixunil added API break This PR requires a version bump for the next release release notes mention and removed API break This PR requires a version bump for the next release labels Jun 30, 2022
ChallengeDev210 pushed a commit to ChallengeDev210/rust-bitcoin that referenced this pull request Aug 1, 2022
b29ff9b Rename SchnorrSighashType::from_u8 -> from_consensus_u8 (Tobin C. Harding)
af16286 Implement TryFrom sha256::Hash for TaprootMerkleBranch (Tobin C. Harding)
6b7b440 Implement TryFrom<Key> for ProprietaryKey (Tobin C. Harding)
5c49fe7 Implement TryFrom<TaprootBuilder> for TapTree (Tobin C. Harding)
632a5db Implement TryFrom for WitnessVersion (Tobin C. Harding)

Pull request description:

  Audit the whole codebase checking for any method that is of the form `from_foo` where foo is not an interesting identifier (like 'consensus' and 'standard'). Implement `TryFrom` for any such methods, deprecating the original.

  Done as separate patches so any can be easily dropped if not liked.

ACKs for top commit:
  apoelstra:
    ACK b29ff9b
  Kixunil:
    ACK b29ff9b

Tree-SHA512: 40f1d96b505891080df1f7a9b3507979b0279a9e0f9d7cd32598bdc16c866785e6b13d5cb1face5ba50e3bc8484a5cd9c7f430d7abc86db9609962476dacd467
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants