8000 Improve error handling in the `merkle_tree::block` module by tcharding · Pull Request #1390 · rust-bitcoin/rust-bitcoin · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Improve error handling in the merkle_tree::block module #1390

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

Closed

Conversation

tcharding
Copy link
Member
@tcharding tcharding commented Nov 15, 2022

PR 4 in the merkle_tree::block series, builds on top of #1388 and #1389. This PR is the last 3 patches.

Improve error handling in the merkle_tree::block module by doing

  • Patch 1: Rename MerkleBlockError -> Error. Please note I did this before moving the merkleblock module, now with the new location and the current reexports I'm not sure its exactly correct. I think its still a step in the right direction though.
  • Patch 2: Remove the nested string from an error variant and replace it with individual variants
  • Patch 3: Use errors instead of panicing

I'm still not 100% sure I understand where we are going with error handling, please feel free to school me if this is not going in the correct direction.

In preparation for moving `MerkleBlock` into the `merkle_tree` module;
create a new directory for the module and move `merkle_tree.rs` to
`merkle_tree/mod.rs`.
Move the `merkleblock` module into the `merkle_tree` module in a
submodule called `block`. In order to do the minimum amount of changes
in this patch DO NOT rename types to improved naming and reduce stutter.

Note:

- block module is private
- the three types are re-exported from `merkle_block`

This patch purposefully does the minimum amount of changes because there
a whole bunch of improvements to the old "merkleblock" module that are
coming next.
We have a test directory for holding test data, use it to for the hex
used for testing the `MerkleBlock`.
The macro has unusual indentation, fix to to be more regular.
Reading the code is arguably easier if we have seen the call site before
seeing the function, saves having to think what the function does.
Refactor import statements by doing:
- Put all the crate imports together
- Use `self` to import the error variants to make it more explicit
Code is arguably easier to read if the important stuff comes first.

Rearrange the `merkle_tree::block` module with this in mind, put the
`MerkbleBlock` struct at the top of the file, then the
`PartialMerkleTree`, and then the error at the bottom.

Refactor only, no logic changes.
Add a unit test for a `PartialMerkleTree` with node counts of 1-7
Add a private method to the `PartialMerkleTree` to calculate the height.
Enables removal of duplicate code.
By convention we name errors `Error` and use `module::Error` to
disambiguate them when needed.

Rename the `MerkleBlockError` type to `Error`.
Instead of a `String` nested in `BadFormat` add an error variant for
each type of error. Note that the `Error` is marked with
`non_exhaustive` so future variants can still be added.
We have a public method that panics if the input is invalid, this is
unnecessary we should return an error.

Add a new error variant `ArrayLengthsNotEqual`, use it and the existing
`NoTransactions` for the error paths instead of panicing.

While we are changing the function signature, take ownership of the
`header` parameter because we use it to build the returned data
structure - taking ownership makes more sense than borrowing and
copying.
@tcharding tcharding force-pushed the 11-15-merkle-block-error branch from 52156ea to dbed0c7 Compare November 15, 2022 01:27
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.

I'm not really sure about changing panics. In general invalid parameters do panic if the API would be too annoying otherwise and the caller is responsible for valid inputs.

As a rule of thumb, if an input is expected to come from an external source it's better to return error than panic. In this case the code assumes valid Block but the Block type has no validity invariants (maybe it should?). I think there's a good chance Block will come from an external source.

At the same time the API looks quite unfortunate. It forces allocations where I think they could be avoided. Things like supplying an empty slice or unequal lengths do look like programmer errors but again if they are likely to come from an external source maybe they aren't.

@tcharding
Copy link
Member Author

Closing due to uncertainty about benefits of the PR.

@tcharding tcharding closed this Nov 18, 2022
@tcharding tcharding deleted the 11-15-merkle-block-error branch November 18, 2022 01:34
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.

2 participants
0