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

Add chain hash type #872

Closed
wants to merge 2 commits into from

Conversation

tcharding
Copy link
Member

@tcharding tcharding commented Mar 10, 2022

The Lightning network defines a type called 'chain hash' that is used to uniquely represent the various Bitcoin networks as a 32 byte hash value. It is calculated by hashing the genesis block for the respective network. Chain hash is now being used by the DLC folks, as such it is useful to have it implemented in rust-bitcoin.

Add a ChainHash type that can be used to get the unique identifier of each of the 4 Bitcoin networks we support.

Question: Are new files required to include a copyright notice like many of the other files?

Closes: #481

We should use fully qualified paths in macros otherwise it puts a burden
on users of the macro to import types/modules.
@tcharding
Copy link
Member Author

@dr-orlovsky, I thought I'd take a crack at this one, hope you don't mind. Is this what you had in mind?

The Lightning network defines a type called 'chain hash' that is used to
uniquely represent the various Bitcoin networks as a 32 byte hash value.
It is calculated by hashing the genesis block for the respective
network. Chain hash is now being used by the DLC folks, as such it is
useful to have it implemented in `rust-bitcoin`.

Add a `ChainHash` type that can be used to get the unique identifier of
each of the 4 Bitcoin networks we support.
@Kixunil

This comment was marked as resolved.

Copy link
Collaborator

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

ACK 6320caa

Thank you!

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 6320caa. Reviewed the value from bolt0. I am not too sure whether it should be located at src/chain_hash.rs.

Somewhere in src/network might more sense?

@tcharding
Copy link
Member Author

It describes the network but is not really to do with networking, right?

@sanket1729
Copy link
Member

Maybe, I just need to get over my thinking that relatively unimportant things don't deserve to live at the top of the directory hierarchy. Like src/chain_hash.rs.

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.

Just one question, looks good otherwise.

/// Returns the chain hash for `network`.
pub fn for_network(network: Network) -> Self {
let genesis = constants::genesis_block(network);
ChainHash(genesis.block_hash().into_inner())
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we really want to rely on the compiler const-folding sha256?

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 don't know what you mean by 'const-folding' but do you mean you'd prefer to see hard coded strings of the hashes for this constructor. Then we could have a unit test that checks the const string against the hash of the genesis block?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, the compiler may optimize sha256 to replace it with the constant but I'm uncomfortable with relying on it in this case involving heavy computation. Test is a good idea.

@tcharding
Copy link
Member Author

... relatively unimportant things don't deserve to live at the top of the directory hierarchy

I tend to work under the principle that unless there is a really intuitive place something should be then it goes at the top level until a really intuitive place turns up. I'm commenting so others can jump in and school me if they have better guiding principles.

@tcharding
Copy link
Member Author

tcharding commented Mar 16, 2022

Ha! I'm adding const hex strings and that makes blockdata/constants a 'really intuitive' place to put this :)

@tcharding
Copy link
Member Author

#878 is an alternative implementation with const hex strings.

@tcharding
Copy link
Member Author

Please do not merge while #878 is open.

@tcharding
Copy link
Member Author

Closing in favour of #878

@tcharding tcharding closed this Mar 20, 2022
@tcharding tcharding deleted the add-chain-hash-type branch March 23, 2022 02:13
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.

ChainHash type: best location
4 participants