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

Public short_id module #10

Merged
merged 2 commits into from
Nov 14, 2021
Merged

Public short_id module #10

merged 2 commits into from
Nov 14, 2021

Conversation

xn3cr0nx
Copy link
Contributor

As described in BP-WG/bp-node#4 this PR exports short_id module in order to be used in bp-node.

The PR also includes some basic unit tests. My initial goal was to test the code in order to understand the implementation. I would like to implement a complete test coverage of the package in order to make the implementation easy to understand. Tests can be really helpful, documenting the expected behavior of the code. Specific test cases can be defined and properly commented in order to describe the protocol representation.

Copy link
Member

@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.

Thank you, especially for the doc comments. I have just two nits

src/short_id.rs Outdated
@@ -20,14 +20,21 @@ use bitcoin::{BlockHash, Txid};

#[derive(Copy, Clone, Debug, Display, PartialEq, Eq)]
#[display(Debug)]
Copy link
Member

Choose a reason for hiding this comment

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

we need here

Suggested change
#[display(Debug)]
#[display(doc_comments)]

since now we have meaningful comments

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great thanks for pointing that out 🙌

src/short_id.rs Outdated
pub enum Error {
/// block height invalid error
Copy link
Member

Choose a reason for hiding this comment

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

here and below we can remove error from the end, since it will be displayed as Error: <message>

Suggested change
/// block height invalid error
/// invalid block height in bitcoin ShortId

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense, thanks for suggestion, still collecting some Rust best practices 🙂

src/short_id.rs Outdated
@@ -123,6 +159,8 @@ impl Default for Descriptor {
}

impl Descriptor {
/// verifies if Descriptor type has valid properties otherwise returns
Copy link
Member

Choose a reason for hiding this comment

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

If possible pls uppercase the first letters of doc comments (except error variants)

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 clarifying that, should be ok now 👍

Copy link
Member

@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.

One small test case nit, but I will merge anyway and if you like you can make a follow-up PR

}

#[test]
#[should_panic(expected = "attempt to subtract with overflow")]
Copy link
Member

Choose a reason for hiding this comment

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

This will test just a single case. To test multiple one needs to use assert_eq with Err - or expect_err

Copy link
Contributor Author

@xn3cr0nx xn3cr0nx Nov 14, 2021

Choose a reason for hiding this comment

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

Good point, it doesn't make sense to have multiple test cases here, but also cannot check the error with assert_eq or expect_error because get_block_height returns an Option, no error is raised. The method panics with such input, so the only way to test it is to capture the panic with the directive.

I refactored for a single panic case in order to check panic is properly captured.

Copy link
Member

@dr-orlovsky dr-orlovsky Nov 14, 2021

Choose a reason for hiding this comment

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

Hm, I think the fact that you can construct incorrect ShortId and have a method call on it which will panic is an API bug. But I assume we can address it in the separate PR.

Copy link
Member

Choose a reason for hiding this comment

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

Did an issue for that: #11

Copy link
Contributor Author

Choose a reason for hiding this comment

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

lol yes code panicking definitely sounds as a bug, thanks for creating the issue, makes sense as followup 👍

@dr-orlovsky
Copy link
Member

Oops, sorry, CI fails: test coverage uses rust stable, and it can't iterate over array. You have to use .iter() in for loop inside test cases (or, better, prefix with &)

@xn3cr0nx
Copy link
Contributor Author

xn3cr0nx commented Nov 14, 2021

Oops, sorry, CI fails: test coverage uses rust stable, and it can't iterate over array. You have to use .iter() in for loop inside test cases (or, better, prefix with &)

Oh I see, the issue is related to the used toolchain 1.47.0, as also shown in the CI preview. I didn't get this error cause I'm using the current stable 1.56.0. Any particular reason for keeping this toolchain for CI? Btw, I reproduced locally and refactored the tests in order to make it work with 1.47.0

@dr-orlovsky dr-orlovsky merged commit 6bc15e2 into BP-WG:master Nov 14, 2021
@dr-orlovsky
Copy link
Member

Well, there should be a reason to increase MSRV, not the otherwise :) In general, rust bitcoin ecosystem tries to achieve as low MSRV as possible, to support non-mainstream rust compilers. You may see a lot of debates in rust-bitcoin on this matter - and we just follow their policy with the aim to eventually get to the same MSRV with them.

@dr-orlovsky dr-orlovsky mentioned this pull request Nov 14, 2021
@xn3cr0nx
Copy link
Contributor Author

Well, there should be a reason to increase MSRV, not the otherwise :) In general, rust bitcoin ecosystem tries to achieve as low MSRV as possible, to support non-mainstream rust compilers. You may see a lot of debates in rust-bitcoin on this matter - and we just follow their policy with the aim to eventually get to the same MSRV with them.

Thanks for clarifying! Sounds good, I will stick with the proper toolchain locally 👍

@xn3cr0nx xn3cr0nx deleted the pub-short-id branch November 14, 2021 11:24
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