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
Add chain hash type #872
Conversation
We should use fully qualified paths in macros otherwise it puts a burden on users of the macro to import types/modules.
@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.
a536433
to
6320caa
Compare
This comment was marked as resolved.
This comment was marked as resolved.
There was a problem hiding this 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!
There was a problem hiding this 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?
It describes the network but is not really to do with networking, right? |
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 |
There was a problem hiding this 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()) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
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. |
Ha! I'm adding const hex strings and that makes |
#878 is an alternative implementation with const hex strings. |
Please do not merge while #878 is open. |
Closing in favour of #878 |
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