Merge rust-bitcoin/rust-bitcoin#2168: Use network when calculating difficulty
12d615d900
Use network when calculating difficulty (Tobin C. Harding)62af5b54f3
Improve difficulty rustdocs (Tobin C. Harding) Pull request description: 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`. ACKs for top commit: Kixunil: ACK12d615d900
apoelstra: ACK12d615d900
Tree-SHA512: 8b414c975306667309b0918109b3e5e8774496fc4c0f3413709e95ad7499bebf1a017def4c180a2bb5f1750c69bb505d94c738a28525b7ccc8b36e5e42514000
This commit is contained in:
commit
966b190f23
|
@ -21,7 +21,7 @@ use crate::hash_types::{TxMerkleNode, WitnessCommitment, WitnessMerkleNode, Wtxi
|
||||||
use crate::internal_macros::impl_consensus_encoding;
|
use crate::internal_macros::impl_consensus_encoding;
|
||||||
use crate::pow::{CompactTarget, Target, Work};
|
use crate::pow::{CompactTarget, Target, Work};
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use crate::{io, merkle_tree, VarInt};
|
use crate::{io, merkle_tree, Network, VarInt};
|
||||||
|
|
||||||
/// Bitcoin block header.
|
/// Bitcoin block header.
|
||||||
///
|
///
|
||||||
|
@ -69,7 +69,10 @@ impl Header {
|
||||||
pub fn target(&self) -> Target { self.bits.into() }
|
pub fn target(&self) -> Target { self.bits.into() }
|
||||||
|
|
||||||
/// Computes the popular "difficulty" measure for mining.
|
/// Computes the popular "difficulty" measure for mining.
|
||||||
pub fn difficulty(&self) -> u128 { self.target().difficulty() }
|
///
|
||||||
|
/// Difficulty represents how difficult the current target makes it to find a block, relative to
|
||||||
|
/// how difficult it would be at the highest possible target (highest target == lowest difficulty).
|
||||||
|
pub fn difficulty(&self, network: Network) -> u128 { self.target().difficulty(network) }
|
||||||
|
|
||||||
/// Computes the popular "difficulty" measure for mining and returns a float value of f64.
|
/// Computes the popular "difficulty" measure for mining and returns a float value of f64.
|
||||||
pub fn difficulty_float(&self) -> f64 { self.target().difficulty_float() }
|
pub fn difficulty_float(&self) -> f64 { self.target().difficulty_float() }
|
||||||
|
@ -481,6 +484,7 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn block_test() {
|
fn block_test() {
|
||||||
|
let network = Network::Bitcoin;
|
||||||
// Mainnet block 00000000b0c5a240b2a61d2e75692224efd4cbecdf6eaf4cc2cf477ca7c270e7
|
// Mainnet block 00000000b0c5a240b2a61d2e75692224efd4cbecdf6eaf4cc2cf477ca7c270e7
|
||||||
let some_block = hex!("010000004ddccd549d28f385ab457e98d1b11ce80bfea2c5ab93015ade4973e400000000bf4473e53794beae34e64fccc471dace6ae544180816f89591894e0f417a914cd74d6e49ffff001d323b3a7b0201000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0804ffff001d026e04ffffffff0100f2052a0100000043410446ef0102d1ec5240f0d061a4246c1bdef63fc3dbab7733052fbbf0ecd8f41fc26bf049ebb4f9527f374280259e7cfa99c48b0e3f39c51347a19a5819651503a5ac00000000010000000321f75f3139a013f50f315b23b0c9a2b6eac31e2bec98e5891c924664889942260000000049483045022100cb2c6b346a978ab8c61b18b5e9397755cbd17d6eb2fe0083ef32e067fa6c785a02206ce44e613f31d9a6b0517e46f3db1576e9812cc98d159bfdaf759a5014081b5c01ffffffff79cda0945903627c3da1f85fc95d0b8ee3e76ae0cfdc9a65d09744b1f8fc85430000000049483045022047957cdd957cfd0becd642f6b84d82f49b6cb4c51a91f49246908af7c3cfdf4a022100e96b46621f1bffcf5ea5982f88cef651e9354f5791602369bf5a82a6cd61a62501fffffffffe09f5fe3ffbf5ee97a54eb5e5069e9da6b4856ee86fc52938c2f979b0f38e82000000004847304402204165be9a4cbab8049e1af9723b96199bfd3e85f44c6b4c0177e3962686b26073022028f638da23fc003760861ad481ead4099312c60030d4cb57820ce4d33812a5ce01ffffffff01009d966b01000000434104ea1feff861b51fe3f5f8a3b12d0f4712db80e919548a80839fc47c6a21e66d957e9c5d8cd108c7a2d2324bad71f9904ac0ae7336507d785b17a2c115e427a32fac00000000");
|
let some_block = hex!("010000004ddccd549d28f385ab457e98d1b11ce80bfea2c5ab93015ade4973e400000000bf4473e53794beae34e64fccc471dace6ae544180816f89591894e0f417a914cd74d6e49ffff001d323b3a7b0201000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0804ffff001d026e04ffffffff0100f2052a0100000043410446ef0102d1ec5240f0d061a4246c1bdef63fc3dbab7733052fbbf0ecd8f41fc26bf049ebb4f9527f374280259e7cfa99c48b0e3f39c51347a19a5819651503a5ac00000000010000000321f75f3139a013f50f315b23b0c9a2b6eac31e2bec98e5891c924664889942260000000049483045022100cb2c6b346a978ab8c61b18b5e9397755cbd17d6eb2fe0083ef32e067fa6c785a02206ce44e613f31d9a6b0517e46f3db1576e9812cc98d159bfdaf759a5014081b5c01ffffffff79cda0945903627c3da1f85fc95d0b8ee3e76ae0cfdc9a65d09744b1f8fc85430000000049483045022047957cdd957cfd0becd642f6b84d82f49b6cb4c51a91f49246908af7c3cfdf4a022100e96b46621f1bffcf5ea5982f88cef651e9354f5791602369bf5a82a6cd61a62501fffffffffe09f5fe3ffbf5ee97a54eb5e5069e9da6b4856ee86fc52938c2f979b0f38e82000000004847304402204165be9a4cbab8049e1af9723b96199bfd3e85f44c6b4c0177e3962686b26073022028f638da23fc003760861ad481ead4099312c60030d4cb57820ce4d33812a5ce01ffffffff01009d966b01000000434104ea1feff861b51fe3f5f8a3b12d0f4712db80e919548a80839fc47c6a21e66d957e9c5d8cd108c7a2d2324bad71f9904ac0ae7336507d785b17a2c115e427a32fac00000000");
|
||||||
let cutoff_block = hex!("010000004ddccd549d28f385ab457e98d1b11ce80bfea2c5ab93015ade4973e400000000bf4473e53794beae34e64fccc471dace6ae544180816f89591894e0f417a914cd74d6e49ffff001d323b3a7b0201000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0804ffff001d026e04ffffffff0100f2052a0100000043410446ef0102d1ec5240f0d061a4246c1bdef63fc3dbab7733052fbbf0ecd8f41fc26bf049ebb4f9527f374280259e7cfa99c48b0e3f39c51347a19a5819651503a5ac00000000010000000321f75f3139a013f50f315b23b0c9a2b6eac31e2bec98e5891c924664889942260000000049483045022100cb2c6b346a978ab8c61b18b5e9397755cbd17d6eb2fe0083ef32e067fa6c785a02206ce44e613f31d9a6b0517e46f3db1576e9812cc98d159bfdaf759a5014081b5c01ffffffff79cda0945903627c3da1f85fc95d0b8ee3e76ae0cfdc9a65d09744b1f8fc85430000000049483045022047957cdd957cfd0becd642f6b84d82f49b6cb4c51a91f49246908af7c3cfdf4a022100e96b46621f1bffcf5ea5982f88cef651e9354f5791602369bf5a82a6cd61a62501fffffffffe09f5fe3ffbf5ee97a54eb5e5069e9da6b4856ee86fc52938c2f979b0f38e82000000004847304402204165be9a4cbab8049e1af9723b96199bfd3e85f44c6b4c0177e3962686b26073022028f638da23fc003760861ad481ead4099312c60030d4cb57820ce4d33812a5ce01ffffffff01009d966b01000000434104ea1feff861b51fe3f5f8a3b12d0f4712db80e919548a80839fc47c6a21e66d957e9c5d8cd108c7a2d2324bad71f9904ac0ae7336507d785b17a2c115e427a32fac");
|
let cutoff_block = hex!("010000004ddccd549d28f385ab457e98d1b11ce80bfea2c5ab93015ade4973e400000000bf4473e53794beae34e64fccc471dace6ae544180816f89591894e0f417a914cd74d6e49ffff001d323b3a7b0201000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0804ffff001d026e04ffffffff0100f2052a0100000043410446ef0102d1ec5240f0d061a4246c1bdef63fc3dbab7733052fbbf0ecd8f41fc26bf049ebb4f9527f374280259e7cfa99c48b0e3f39c51347a19a5819651503a5ac00000000010000000321f75f3139a013f50f315b23b0c9a2b6eac31e2bec98e5891c924664889942260000000049483045022100cb2c6b346a978ab8c61b18b5e9397755cbd17d6eb2fe0083ef32e067fa6c785a02206ce44e613f31d9a6b0517e46f3db1576e9812cc98d159bfdaf759a5014081b5c01ffffffff79cda0945903627c3da1f85fc95d0b8ee3e76ae0cfdc9a65d09744b1f8fc85430000000049483045022047957cdd957cfd0becd642f6b84d82f49b6cb4c51a91f49246908af7c3cfdf4a022100e96b46621f1bffcf5ea5982f88cef651e9354f5791602369bf5a82a6cd61a62501fffffffffe09f5fe3ffbf5ee97a54eb5e5069e9da6b4856ee86fc52938c2f979b0f38e82000000004847304402204165be9a4cbab8049e1af9723b96199bfd3e85f44c6b4c0177e3962686b26073022028f638da23fc003760861ad481ead4099312c60030d4cb57820ce4d33812a5ce01ffffffff01009d966b01000000434104ea1feff861b51fe3f5f8a3b12d0f4712db80e919548a80839fc47c6a21e66d957e9c5d8cd108c7a2d2324bad71f9904ac0ae7336507d785b17a2c115e427a32fac");
|
||||||
|
@ -507,7 +511,7 @@ mod tests {
|
||||||
real_decode.header.validate_pow(real_decode.header.target()).unwrap(),
|
real_decode.header.validate_pow(real_decode.header.target()).unwrap(),
|
||||||
real_decode.block_hash()
|
real_decode.block_hash()
|
||||||
);
|
);
|
||||||
assert_eq!(real_decode.header.difficulty(), 1);
|
assert_eq!(real_decode.header.difficulty(network), 1);
|
||||||
assert_eq!(real_decode.header.difficulty_float(), 1.0);
|
assert_eq!(real_decode.header.difficulty_float(), 1.0);
|
||||||
// [test] TODO: check the transaction data
|
// [test] TODO: check the transaction data
|
||||||
|
|
||||||
|
@ -527,6 +531,7 @@ mod tests {
|
||||||
// Check testnet block 000000000000045e0b1660b6445b5e5c5ab63c9a4f956be7e1e69be04fa4497b
|
// Check testnet block 000000000000045e0b1660b6445b5e5c5ab63c9a4f956be7e1e69be04fa4497b
|
||||||
#[test]
|
#[test]
|
||||||
fn segwit_block_test() {
|
fn segwit_block_test() {
|
||||||
|
let network = Network::Testnet;
|
||||||
let segwit_block = include_bytes!("../../tests/data/testnet_block_000000000000045e0b1660b6445b5e5c5ab63c9a4f956be7e1e69be04fa4497b.raw").to_vec();
|
let segwit_block = include_bytes!("../../tests/data/testnet_block_000000000000045e0b1660b6445b5e5c5ab63c9a4f956be7e1e69be04fa4497b.raw").to_vec();
|
||||||
|
|
||||||
let decode: Result<Block, _> = deserialize(&segwit_block);
|
let decode: Result<Block, _> = deserialize(&segwit_block);
|
||||||
|
@ -549,7 +554,7 @@ mod tests {
|
||||||
real_decode.header.validate_pow(real_decode.header.target()).unwrap(),
|
real_decode.header.validate_pow(real_decode.header.target()).unwrap(),
|
||||||
real_decode.block_hash()
|
real_decode.block_hash()
|
||||||
);
|
);
|
||||||
assert_eq!(real_decode.header.difficulty(), 2456598);
|
assert_eq!(real_decode.header.difficulty(network), 2456598);
|
||||||
assert_eq!(real_decode.header.difficulty_float(), 2456598.4399242126);
|
assert_eq!(real_decode.header.difficulty_float(), 2456598.4399242126);
|
||||||
// [test] TODO: check the transaction data
|
// [test] TODO: check the transaction data
|
||||||
|
|
||||||
|
|
|
@ -19,6 +19,7 @@ use crate::hash_types::BlockHash;
|
||||||
use crate::io::{self, Read, Write};
|
use crate::io::{self, Read, Write};
|
||||||
use crate::prelude::String;
|
use crate::prelude::String;
|
||||||
use crate::string::FromHexStr;
|
use crate::string::FromHexStr;
|
||||||
|
use crate::Network;
|
||||||
|
|
||||||
/// Implement traits and methods shared by `Target` and `Work`.
|
/// Implement traits and methods shared by `Target` and `Work`.
|
||||||
macro_rules! do_impl {
|
macro_rules! do_impl {
|
||||||
|
@ -223,8 +224,14 @@ impl Target {
|
||||||
/// [max]: Target::max
|
/// [max]: Target::max
|
||||||
/// [target]: crate::blockdata::block::Header::target
|
/// [target]: crate::blockdata::block::Header::target
|
||||||
#[cfg_attr(all(test, mutate), mutate)]
|
#[cfg_attr(all(test, mutate), mutate)]
|
||||||
pub fn difficulty(&self) -> u128 {
|
pub fn difficulty(&self, network: Network) -> u128 {
|
||||||
let d = Target::MAX.0 / self.0;
|
let max = match network {
|
||||||
|
Network::Bitcoin => Target::MAX_ATTAINABLE_MAINNET,
|
||||||
|
Network::Testnet => Target::MAX_ATTAINABLE_TESTNET,
|
||||||
|
Network::Signet => Target::MAX_ATTAINABLE_SIGNET,
|
||||||
|
Network::Regtest => Target::MAX_ATTAINABLE_REGTEST,
|
||||||
|
};
|
||||||
|
let d = max.0 / self.0;
|
||||||
d.saturating_to_u128()
|
d.saturating_to_u128()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue