Skip to content

Commit

Permalink
Merge rust-bitcoin#2782: Format the pow module
Browse files Browse the repository at this point in the history
f612931 Run the formatter (Tobin C. Harding)
fa4d3d4 Add whitespace (Tobin C. Harding)

Pull request description:

  rust-bitcoin#2781 done by a human.

ACKs for top commit:
  apoelstra:
    ACK f612931 sad

Tree-SHA512: a72b507995a70ab2b5c2bb717d8bbc4b47f3190157e5d526d33e6a9fd2ad990295806a36e0bda0ac988b1e175d8356c6e6d277337d0ed0a5e0499ad3f336a930
  • Loading branch information
apoelstra committed May 20, 2024
2 parents 3d9ceb5 + f612931 commit 02ae925
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions bitcoin/src/pow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1791,49 +1791,59 @@ mod tests {

#[test]
fn compact_target_from_upwards_difficulty_adjustment_using_headers() {
use crate::{block::Version, constants::genesis_block, TxMerkleNode};
use hashes::Hash;

use crate::block::Version;
use crate::constants::genesis_block;
use crate::TxMerkleNode;
let params = Params::new(crate::Network::Signet);
let epoch_start = genesis_block(&params).header;

// Block 2015, the only information used are `bits` and `time`
let current = Header {
version: Version::ONE,
prev_blockhash: BlockHash::all_zeros(),
merkle_root: TxMerkleNode::all_zeros(),
time: 1599332177,
bits: epoch_start.bits,
nonce: epoch_start.nonce
nonce: epoch_start.nonce,
};
let adjustment = CompactTarget::from_header_difficulty_adjustment(epoch_start, current, params);
let adjustment =
CompactTarget::from_header_difficulty_adjustment(epoch_start, current, params);
let adjustment_bits = CompactTarget::from_consensus(503394215); // Block 2016 compact target
assert_eq!(adjustment, adjustment_bits);
}

#[test]
fn compact_target_from_downwards_difficulty_adjustment_using_headers() {
use crate::{block::Version, TxMerkleNode};
use hashes::Hash;

use crate::block::Version;
use crate::TxMerkleNode;
let params = Params::new(crate::Network::Signet);
let starting_bits = CompactTarget::from_consensus(503394215); // Block 2016 compact target

// Block 2016, the only information used is `time`
let epoch_start = Header {
version: Version::ONE,
prev_blockhash: BlockHash::all_zeros(),
merkle_root: TxMerkleNode::all_zeros(),
time: 1599332844,
bits: starting_bits,
nonce: 0
nonce: 0,
};

// Block 4031, the only information used are `bits` and `time`
let current = Header {
version: Version::ONE,
prev_blockhash: BlockHash::all_zeros(),
merkle_root: TxMerkleNode::all_zeros(),
time: 1600591200,
bits: starting_bits,
nonce: 0
nonce: 0,
};
let adjustment = CompactTarget::from_header_difficulty_adjustment(epoch_start, current, params);
let adjustment =
CompactTarget::from_header_difficulty_adjustment(epoch_start, current, params);
let adjustment_bits = CompactTarget::from_consensus(503397348); // Block 4032 compact target
assert_eq!(adjustment, adjustment_bits);
}
Expand All @@ -1844,29 +1854,27 @@ mod tests {
let starting_bits = CompactTarget::from_consensus(503403001);
let timespan = (0.2 * params.pow_target_timespan as f64) as u64;
let got = CompactTarget::from_next_work_required(starting_bits, timespan, params);
let want = Target::from_compact(starting_bits)
.min_transition_threshold()
.to_compact_lossy();
let want =
Target::from_compact(starting_bits).min_transition_threshold().to_compact_lossy();
assert_eq!(got, want);
}

#[test]
fn compact_target_from_minimum_downward_difficulty_adjustment() {
let params = Params::new(crate::Network::Signet);
let starting_bits = CompactTarget::from_consensus(403403001); // High difficulty for Signet
let timespan = 5 * params.pow_target_timespan; // Really slow.
let timespan = 5 * params.pow_target_timespan; // Really slow.
let got = CompactTarget::from_next_work_required(starting_bits, timespan, &params);
let want = Target::from_compact(starting_bits)
.max_transition_threshold(params)
.to_compact_lossy();
let want =
Target::from_compact(starting_bits).max_transition_threshold(params).to_compact_lossy();
assert_eq!(got, want);
}

#[test]
fn compact_target_from_adjustment_is_max_target() {
let params = Params::new(crate::Network::Signet);
let starting_bits = CompactTarget::from_consensus(503543726); // Genesis compact target on Signet
let timespan = 5 * params.pow_target_timespan; // Really slow.
let timespan = 5 * params.pow_target_timespan; // Really slow.
let got = CompactTarget::from_next_work_required(starting_bits, timespan, &params);
let want = params.max_attainable_target.to_compact_lossy();
assert_eq!(got, want);
Expand Down

0 comments on commit 02ae925

Please sign in to comment.