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
Use network when calculating difficulty #2168
Use network when calculating difficulty #2168
Conversation
Copy a sentence from the `pow::Target::difficulty` function onto the `block:Header::difficulty` function.
The difficulty is a ratio of the max and current targets, since the max is network specific the difficulty calculation is also network specific. We already have network specific maximum target constants, use them when calculating the difficulty.
53f62c0
to
12d615d
Compare
Done as part of #2121 |
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 12d615d
Network::Testnet => Target::MAX_ATTAINABLE_TESTNET, | ||
Network::Signet => Target::MAX_ATTAINABLE_SIGNET, | ||
Network::Regtest => Target::MAX_ATTAINABLE_REGTEST, | ||
}; |
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.
Feels like this should be in consensus params.
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.
These are in Params::pow_limit
, I thought it better to take a Network
parameter not a Params
parameter though because its more specific. Or am I missing something?
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.
We could have this?
pub fn difficulty(&self, params: &Params) -> u128 {
let max = params.pow_limit;
let d = max.0 / self.0;
d.saturating_to_u128()
}
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 do think Network
makes sense but I was thinking Params::new(network).pow_limit
.
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.
Once we have Network: AsRef<Params>
/Network: AsParams
we should just take generic.
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 12d615d
Import code from https://github.com/rust-bitcoin/rust-bitcoin` comit: `966b190f238408e24efd768ffa2bcdb6046e0cea Merge rust-bitcoin/rust-bitcoin#2168: Use network when calculating difficulty` Also grab required code from `bitcoin/src/serde_utils.rs` and `internals/src/error.rs`. Make changes to get things building: - Use the most terse public bitcoin API (ie, don't reach down into modules when importing if there is a re-export at crate level) - Fix paths to use correct `crate::` vs `bitcoin::` - Import `bitcoin::consensus::encode` as `consensus` and add TODO comments about it.
Import code from https://github.com/rust-bitcoin/rust-bitcoin` comit: `966b190f238408e24efd768ffa2bcdb6046e0cea Merge rust-bitcoin/rust-bitcoin#2168: Use network when calculating difficulty` Also grab required code from `bitcoin/src/serde_utils.rs` and `internals/src/error.rs`. Make changes to get things building: - Use the most terse public bitcoin API (ie, don't reach down into modules when importing if there is a re-export at crate level) - Fix paths to use correct `crate::` vs `bitcoin::` - Import `bitcoin::consensus::encode` as `consensus` and add TODO comments about it.
Import code from https://github.com/rust-bitcoin/rust-bitcoin` comit: `966b190f238408e24efd768ffa2bcdb6046e0cea Merge rust-bitcoin/rust-bitcoin#2168: Use network when calculating difficulty` Also grab required code from `bitcoin/src/serde_utils.rs` and `internals/src/error.rs`. Make changes to get things building: - Use the most terse public bitcoin API (ie, don't reach down into modules when importing if there is a re-export at crate level) - Fix paths to use correct `crate::` vs `bitcoin::` - Import `bitcoin::consensus::encode` as `consensus` and add TODO comments about it.
Import code from https://github.com/rust-bitcoin/rust-bitcoin` comit: `966b190f238408e24efd768ffa2bcdb6046e0cea Merge rust-bitcoin/rust-bitcoin#2168: Use network when calculating difficulty` Also grab required code from `bitcoin/src/serde_utils.rs` and `internals/src/error.rs`. Make changes to get things building: - Use the most terse public bitcoin API (ie, don't reach down into modules when importing if there is a re-export at crate level) - Fix paths to use correct `crate::` vs `bitcoin::` - Import `bitcoin::consensus::encode` as `consensus` and add TODO comments about it.
The difficulty is a ratio of the max and current targets, since the max is network specific the difficulty calculation is also network specific.
We already have network specific maximum target constants, use them when calculating the difficulty.
Patch 1 is a trival docs improvement to
block::Header::difficulty
.