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

Split relative locktime error up #2433

Merged
merged 1 commit into from
Mar 11, 2024

Conversation

tcharding
Copy link
Member

The relative module has a single general error type, we are moving away from this style to specific error types.

Split the relative::Error up into three error structs.

I forget the policy on public inner fields.

@tcharding
Copy link
Member Author

tcharding commented Feb 2, 2024

I went to move the relative::Height and Time types to units and discovered the general error type. I'm not sure exactly what you are working on @Kixunil, hope I didn't overlap with you.

@github-actions github-actions bot added the C-bitcoin PRs modifying the bitcoin crate label Feb 2, 2024
@coveralls
Copy link

coveralls commented Feb 2, 2024

Pull Request Test Coverage Report for Build 8217718201

Details

  • 7 of 20 (35.0%) changed or added relevant lines in 2 files are covered.
  • 3 unchanged lines in 1 file lost coverage.
  • Overall coverage increased (+0.04%) to 84.002%

Changes Missing Coverage Covered Lines Changed/Added Lines %
bitcoin/src/blockdata/transaction.rs 1 4 25.0%
bitcoin/src/blockdata/locktime/relative.rs 6 16 37.5%
Files with Coverage Reduction New Missed Lines %
bitcoin/src/blockdata/locktime/relative.rs 3 71.79%
Totals Coverage Status
Change from base Build 8214829480: 0.04%
Covered Lines: 19517
Relevant Lines: 23234

💛 - Coveralls

@Kixunil
Copy link
Collaborator

Kixunil commented Feb 2, 2024

I was trying to pull out absolute before since you said it's hard, I assumed you're not going to do that soon. But got tangled in other things lately. Anyway, this PR doesn't conflict (yet?).

@tcharding
Copy link
Member Author

I hadn't tried to move any code out of the locktime modules, I said (at least I meant) that pow was hard. Moving Height and Time from both absolute and relative shouldn't be a problem once the errors are improved.

@tcharding
Copy link
Member Author

Only change in force push is improvement to the error message printed for two of the new errors.

@tcharding
Copy link
Member Author

Can this go in? We need this if we want to move the locktime types to units which is required to move ParseIntError yada yada yada

@tcharding tcharding added the P-high High priority label Feb 9, 2024
apoelstra
apoelstra previously approved these changes Feb 9, 2024
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 09ea073

@tcharding
Copy link
Member Author

Changes in force push:

  • Made the error fields public and used name fields instead of tuple struct
  • Did not include non_exhaustive - I believe these errors, and the functions in relative module, are stable and won't need changing.
  • rebased to pick up Fix CI new build warnings #2488

@tcharding tcharding added this to the 0.32.0 milestone Feb 21, 2024
bitcoin/src/blockdata/locktime/relative.rs Outdated Show resolved Hide resolved
IncompatibleTime(LockTime, Time),
pub struct IntegerOverflowError {
/// Time value in seconds that overflowed.
pub seconds: u32
Copy link
Collaborator

Choose a reason for hiding this comment

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

Well, there's one weird property - one can create the error with funny values like seconds: 0 which is never actually wrong and thus the error is kinda invalid.

So I don't like it much but it's probably OK.

Copy link
Member

Choose a reason for hiding this comment

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

I think we'll have to live with it because Rust doesn't have ranged types (and it is a real PITA to create and maintain your own).

Copy link
Collaborator

Choose a reason for hiding this comment

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

We can just hide the representation leaving the option to add it later. (And have a getter today.)

Copy link
Member Author

Choose a reason for hiding this comment

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

We can do this but it seems excessive for an error type:

/// Input time in seconds was too large to be encoded to a 16 bit 512 second interval.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TimeOverflowError {
    /// Time value in seconds that overflowed.
    seconds: u32                // We maintain an invariant that this value does indeed overflow.
}

impl TimeOverflowError {
    pub fn new(seconds: u32) -> Self {
        let overflows = u16::try_from((seconds + 511) / 512).is_ok();
        assert!(overflows);
        TimeOverflowError { seconds }
    }
}

I personally think the error is fine as it is. We are not here to protect every programmer from every single programming mistake they might make.

Copy link
Member

Choose a reason for hiding this comment

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

@tcharding the cool thing is that we don't need new because users should never be directly constructing our error types.

And internally of course we can just directly construct them.

Copy link
Collaborator

Choose a reason for hiding this comment

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

@tcharding I don't think so. I still see the field as pub, you didn't address @apoelstra's comment and I agree with his comment.

Copy link
Member Author

@tcharding tcharding Feb 26, 2024

Choose a reason for hiding this comment

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

Oh, damn - I removed the non_exhaustive thinking the error as stable inadvertently allowing construction, thereby forgetting this discussion thread.

Copy link
Member Author

Choose a reason for hiding this comment

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

I made the field pub(crate) and added a comment so it doesn't get un-privated.

Copy link
Member Author

Choose a reason for hiding this comment

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

Note also that the constructor has been removed - I believe this thread is resolved.

Copy link
Member

Choose a reason for hiding this comment

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

I am going to just disable the "resolve conversation" rule.

@tcharding tcharding force-pushed the 02-02-relative-errors branch 2 times, most recently from 70d65e1 to 2213cdd Compare February 23, 2024 01:55
apoelstra
apoelstra previously approved these changes Feb 24, 2024
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 2213cdd except for the pub/accessor thing, which would be OK as a followup PR that defined having accessors as part of our error policy

@tcharding
Copy link
Member Author

Please note the new error types in this PR are not hider errors, are returned from functions that are stable, and therefore have public fields and do not use non_exhaustive.

IncompatibleTime(LockTime, Time),
pub struct IntegerOverflowError {
/// Time value in seconds that overflowed.
pub seconds: u32
Copy link
Collaborator

Choose a reason for hiding this comment

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

@tcharding I don't think so. I still see the field as pub, you didn't address @apoelstra's comment and I agree with his comment.

bitcoin/src/blockdata/locktime/relative.rs Outdated Show resolved Hide resolved
bitcoin/src/blockdata/locktime/relative.rs Outdated Show resolved Hide resolved
@Kixunil
Copy link
Collaborator

Kixunil commented Feb 26, 2024

FTR, if we want to stabilize sooner maybe we should just hide all errors entirely and we can go back and uncover (parts of) them later.

@tcharding
Copy link
Member Author

tcharding commented Feb 26, 2024

FTR, if we want to stabilize sooner maybe we should just hide all errors entirely and we can go back and uncover (parts of) them later.

I made both incompatible error types non_exhaustive

This should be Height to statically represent that the lock was height.

I"m not sure about this. If we do this (and similar change for Time) then then the errors become the same shape, opening up the debate about using same errors in different functions. Secondly the input is a LockTime so I think the error should be a LockTime so debuggers are looking at exactly the same thing. I don't feel strongly about either argument though.

Your comments did uncover that I was using the errors incorrectly (for how I had meant it to be used) though which is interesting. Now is_satisfied_by_time returns IncompatibleTimeError and vice versa where as before I had them backwards.

@Kixunil
Copy link
Collaborator

Kixunil commented Feb 27, 2024

I made both incompatible error types non_exhaustive

That's not always sufficient.

If we do this (and similar change for Time) then then the errors become the same shape

Firstly, not really, they are mirrored (and we would need a third field to distinguish them). Secondly I think that being imprecise just to avoid the discussion is just lying to ourselves. If the discussion is not worth slowing down stabilization just make them opaque.

Secondly the input is a LockTime so I think the error should be a LockTime so debuggers are looking at exactly the same thing.

The concrete types imply the outer one, so IMO it still is the same thing.

Your comments did uncover that I was using the errors incorrectly

Cool!

@tcharding tcharding force-pushed the 02-02-relative-errors branch 2 times, most recently from dfe70d2 to 7c0cc02 Compare March 1, 2024 22:04
sanket1729
sanket1729 previously approved these changes Mar 6, 2024
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 7c0cc02

Kixunil
Kixunil previously approved these changes Mar 7, 2024
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 7c0cc02

bitcoin/src/blockdata/locktime/relative.rs Show resolved Hide resolved
bitcoin/src/blockdata/locktime/relative.rs Show resolved Hide resolved
@tcharding
Copy link
Member Author

Both unresolved comments are yours @Kixunil, I believe they are both resolved.

The `relative` module has a single general error type, we are moving
away from this style to specific error types.

Split the `relative::Error` up into three error structs.

Note the change of parameter `h` to `height`, and using `h` as the
pattern matched variable - this makes sense because it gives the
variable with large scope the longer name.
@tcharding
Copy link
Member Author

Rebase only, no other changes.

@tcharding tcharding mentioned this pull request Mar 11, 2024
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.

utACK 3c8edae

@apoelstra
Copy link
Member

Note that a hidden discussion links to issue #2553 about renaming local variables inside some method.

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 3c8edae

@apoelstra apoelstra merged commit 24b19d7 into rust-bitcoin:master Mar 11, 2024
187 checks passed
@tcharding tcharding deleted the 02-02-relative-errors branch March 11, 2024 22:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bitcoin PRs modifying the bitcoin crate P-high High priority
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants