From db817f5626b09f694b91757f037411fe1149040f Mon Sep 17 00:00:00 2001 From: Arya Date: Fri, 17 May 2024 15:25:03 -0400 Subject: [PATCH] Adds comments --- zebra-chain/src/parameters/network/testnet.rs | 10 ++++++++++ zebra-chain/src/work/difficulty.rs | 5 +++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/zebra-chain/src/parameters/network/testnet.rs b/zebra-chain/src/parameters/network/testnet.rs index 3f128b04a47..58d810e5871 100644 --- a/zebra-chain/src/parameters/network/testnet.rs +++ b/zebra-chain/src/parameters/network/testnet.rs @@ -105,6 +105,15 @@ impl Default for ParametersBuilder { .parse() .expect("hard-coded hash parses"), slow_start_interval: SLOW_START_INTERVAL, + // Testnet PoWLimit is defined as `2^251 - 1` on page 73 of the protocol specification: + // + // + // `zcashd` converts the PoWLimit into a compact representation before + // using it to perform difficulty filter checks. + // + // The Zcash specification converts to compact for the default difficulty + // filter, but not for testnet minimum difficulty blocks. (ZIP 205 and + // ZIP 208 don't specify this conversion either.) See #1277 for details. target_difficulty_limit: ExpandedDifficulty::from((U256::one() << 251) - 1) .to_compact() .to_expanded() @@ -359,6 +368,7 @@ impl Parameters { network_name: "Regtest".to_string(), ..Self::build() .with_genesis_hash(REGTEST_GENESIS_HASH) + // This value is chosen to match zcashd, see: .with_target_difficulty_limit(U256::from_big_endian(&[0x0f; 32])) .with_disable_pow(true) .with_slow_start_interval(Height::MIN) diff --git a/zebra-chain/src/work/difficulty.rs b/zebra-chain/src/work/difficulty.rs index 0069601809a..8388cd7fbc5 100644 --- a/zebra-chain/src/work/difficulty.rs +++ b/zebra-chain/src/work/difficulty.rs @@ -696,9 +696,10 @@ impl ParameterDifficulty for Network { /// See [`ParameterDifficulty::target_difficulty_limit`] fn target_difficulty_limit(&self) -> ExpandedDifficulty { let limit: U256 = match self { - /* 2^243 - 1 */ + // Mainnet PoWLimit is defined as `2^243 - 1` on page 73 of the protocol specification: + // Network::Mainnet => (U256::one() << 243) - 1, - /* 2^251 - 1 for the default testnet */ + // 2^251 - 1 for the default testnet, see `testnet::ParametersBuilder::default`() Network::Testnet(params) => return params.target_difficulty_limit(), };