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

Modify from_script functions in address.rs to return result #1038

Merged
merged 2 commits into from
Jun 8, 2022

Conversation

Eunoia1729
Copy link
Contributor

@Eunoia1729 Eunoia1729 commented Jun 5, 2022

Modify from_script functions to return result instead of option so that, in case of errors, there is more
information on what went wrong.

Resolves: #1022

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.

Good work, I left a few suggestions.

On commit descriptions and titles: the canonical blog post that folks always link to is: https://cbea.ms/git-commit/, IMO you should definitely have read this post once in your life. The better you do all the auxillary things around making code changes the better response you will get from reviewers and the more chance your changes will have of getting merged.

Of note, this PR is not a refactoring - it makes logic changes.

@@ -81,7 +81,9 @@ pub enum Error {
/// An uncompressed pubkey was used where it is not allowed.
UncompressedPubkey,
/// Address size more than 520 bytes is not allowed.
ExcessiveScriptSize
ExcessiveScriptSize,
/// Script which is not a p2pkh, p2sh or witness program.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
/// Script which is not a p2pkh, p2sh or witness program.
/// Script is not a p2pkh, p2sh, or witness program.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks @tcharding for your review and feedback!
The link was super helpful and applied the takeaways from it to this PR. Kindly let me know how it is right now and where it could be further improved.

@@ -98,6 +100,7 @@ impl fmt::Display for Error {
Error::InvalidSegwitV0ProgramLength(l) => write!(f, "a v0 witness program must be either of length 20 or 32 bytes: length={}", l),
Error::UncompressedPubkey => write!(f, "an uncompressed pubkey was used where it is not allowed"),
Error::ExcessiveScriptSize => write!(f, "Script size exceed 520 bytes"),
Error::UnrecognizedScript => write!(f, "Script is not a p2pk, p2sh or witness program")
Copy link
Member

Choose a reason for hiding this comment

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

Same as above. For the record, error messages should start with a lower case letter because they are often combined with other error messages e.g., "erorr one: error two: error three".

Copy link
Contributor Author

@Eunoia1729 Eunoia1729 Jun 7, 2022

Choose a reason for hiding this comment

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

Adding an extra commit to update the error message for Error::ExcessiveScriptSize.

@@ -119,7 +122,8 @@ impl std::error::Error for Error {
| InvalidWitnessProgramLength(_)
| InvalidSegwitV0ProgramLength(_)
| UncompressedPubkey
| ExcessiveScriptSize => None,
| ExcessiveScriptSize
Copy link
Member

Choose a reason for hiding this comment

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

This line has trailing whitespace. You should be able to configure what ever system you use to view diffs before pushing them (editor or shell) to highlight trailing white space. In case its useful, I personally view diffs using

git-log-patch () {
    local commit
    if (( $# < 1 ))
    then
        commit="HEAD~"
    else
        commit="$1"
    fi
    git log --color=always --patch --reverse "${commit}".. | vimpager
}

Copy link
Member

Choose a reason for hiding this comment

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

Along with a shell alias :)

Copy link
Contributor

Choose a reason for hiding this comment

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

@tcharding Hey, this is good. Thanks a lot! Though I modified it to (~/.gitconfig):

[alias]
check = "!f() { git log --color=always --patch --reverse "${1:-HEAD~}"..; }; f"

That pager seems unnecessary BTW, git log pages by default(?) when run on interactive shell.

Copy link
Member

Choose a reason for hiding this comment

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

Oh nice, I started using vimpager because I was loosing colours somewhere but seems you are correct. I can stop using it, cheers.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for this snippet @dpc and @tcharding ! TIL.

@@ -397,11 +401,11 @@ impl Payload {
Payload::ScriptHash(ScriptHash::from_inner(hash_inner))
} else if script.is_witness_program() {
Payload::WitnessProgram {
version: WitnessVersion::from_opcode(opcodes::All::from(script[0])).ok()?,
version: WitnessVersion::from_opcode(opcodes::All::from(script[0])).ok().unwrap(),
Copy link
Member

Choose a reason for hiding this comment

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

Typically we never want to use unwrap in non-test code because it panics. If the code path can never be hit use expect and explain why it cannot panic in the argument string.

(I'm intentionally not putting a suggestion for a better way to do this so you can problem solve it yourself :)

Copy link
Contributor Author

@Eunoia1729 Eunoia1729 Jun 7, 2022

Choose a reason for hiding this comment

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

Ahh, makes sense!
Kindly let me know if the current change looks good.

@@ -98,6 +100,7 @@ impl fmt::Display for Error {
Error::InvalidSegwitV0ProgramLength(l) => write!(f, "a v0 witness program must be either of length 20 or 32 bytes: length={}", l),
Error::UncompressedPubkey => write!(f, "an uncompressed pubkey was used where it is not allowed"),
Error::ExcessiveScriptSize => write!(f, "Script size exceed 520 bytes"),
Error::UnrecognizedScript => write!(f, "Script is not a p2pk, p2sh or witness program")
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe this should be p2pkh not p2pk.

Suggested change
Error::UnrecognizedScript => write!(f, "Script is not a p2pk, p2sh or witness program")
Error::UnrecognizedScript => write!(f, "Script is not a p2pkh, p2sh or witness program")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ahh you're right! Thanks for pointing it out, I need more coffee 😄

@apoelstra
Copy link
Member

ACK 0939ef4 except for all the nits that others have pointed out :)

@@ -397,11 +401,11 @@ impl Payload {
Payload::ScriptHash(ScriptHash::from_inner(hash_inner))
} else if script.is_witness_program() {
Payload::WitnessProgram {
Copy link
Contributor

Choose a reason for hiding this comment

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

From #1022 (comment), would it benefit to return an error indicating incorrect segwit v0 program length here?

Then we can remove the explicit check in Address::from_script() since it would be done here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. That way, we can reduce the number of calls to script.is_witness_program() for correct scripts.

@Eunoia1729 Eunoia1729 changed the title Refactor: make from_script functions in address.rs to return Result Modify from_script functions in address.rs to return result Jun 7, 2022
tcharding
tcharding previously approved these changes Jun 7, 2022
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 2a4f439

nlanson
nlanson previously approved these changes Jun 8, 2022
Modify from_script functions to return result instead of option so that, in case of errors, there is more
information on what went wrong.

Resolves: rust-bitcoin#1022
@Eunoia1729
Copy link
Contributor Author

Eunoia1729 commented Jun 8, 2022

Changes done in the force push:

  • Re-based the commit due to merge conflicts.
  • Added an extra commit to update the error message for Error::ExcessiveScriptSize (unrelated to the issue).

@Eunoia1729 Eunoia1729 requested a review from tcharding June 8, 2022 22:29
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 66e852c. LGTM

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 66e852c

@tcharding
Copy link
Member

Hey @Eunoia1729, just to let you know; in rust-bitcoin we only merge PRs when they have two acks and the acks include the commit hash of the tip of the PR. This means that when you force push reviewers have to re-review you work. Combined this with rebasing and it can require a little more concentration from the reviewer. Do force push when you are implementing review suggestions to the PR unless the suggestion can be done as a stand alone patch, this can be a bit hard to judge at times. Just something to keep in mind. You can ask if you want more direction on this sort of stuff. The easier you make life for reviewers the more chance your changes have of being merged.

That was long winded way of saying that you should only typically rebase a PR if there are merge conflicts :)

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 2a4f439

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 66e852c

@apoelstra apoelstra merged commit 3ed3d57 into rust-bitcoin:master Jun 8, 2022
@Eunoia1729 Eunoia1729 deleted the fix-1022 branch June 9, 2022 03:12
apoelstra added a commit that referenced this pull request Jun 15, 2022
…rogramLength and add test

24fdb53 Fix incorrect argument passed to Error::InvalidSegwitV0ProgramLength (eunoia_1729)

Pull request description:

  See also: #995, #1038

ACKs for top commit:
  sanket1729:
    utACK 24fdb53. Nice catch
  apoelstra:
    ACK 24fdb53
  tcharding:
    ACK 24fdb53

Tree-SHA512: ced78b69054ec81431399a853291c7bad5b1a49d6683b1ac153a0f1449935bb5d75a31e3d86160602064530959a2ddc3c59a2a2ca268252c42a6805253ead9d0
ChallengeDev210 pushed a commit to ChallengeDev210/rust-bitcoin that referenced this pull request Aug 1, 2022
… address.rs to return result

66e852c Update format of ExcessiveScriptSize error message (eunoia_1729)
89bd4b6 Modify from_script functions in address.rs to return result (eunoia_1729)

Pull request description:

  Modify from_script functions to return result instead of option so that, in case of errors, there is more
  information on what went wrong.

  Resolves: #1022

ACKs for top commit:
  sanket1729:
    ACK 66e852c. LGTM
  tcharding:
    ACK 66e852c
  apoelstra:
    ACK 66e852c

Tree-SHA512: 0d9529aee0a5459351bed2cc8b2c5571736d3293e2931c43d98f53330e9ac5f3d998a19da2b4575af0a3c1c4dcfd5a24c8813390bf6f5492a689c36ebb9cb426
@Kixunil Kixunil added the API break This PR requires a version bump for the next release label Aug 1, 2022
@Kixunil Kixunil added this to the 0.29.0 milestone Aug 1, 2022
sander2 added a commit to sander2/polkabtc-clients that referenced this pull request Oct 6, 2022
sander2 added a commit to sander2/polkabtc-clients that referenced this pull request Oct 6, 2022
sander2 added a commit to sander2/polkabtc-clients that referenced this pull request Oct 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API break This PR requires a version bump for the next release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Address::from_script to return a Result
7 participants