fn find_next_difficulty_in_chain(
    network: &Network,
    store: &impl HeaderStore,
    prev_header: &StoredHeader,
) -> u32 {
    // This is the maximum difficulty target for the network
    let pow_limit_bits = pow_limit_bits(network);
    match network {
        Network::Testnet | Network::Regtest => {
            let mut current_header = prev_header;
            let initial_header_hash = store.get_initial_hash();

            // Keep traversing the blockchain backwards from the recent block to initial
            // header hash.
            while current_header.header.block_hash() != initial_header_hash {
                if current_header.header.bits != pow_limit_bits
                    || current_header.height % DIFFICULTY_ADJUSTMENT_INTERVAL == 0
                {
                    return current_header.header.bits;
                }

                // Traverse to the previous header
                current_header = store
                    .get_header(&current_header.header.prev_blockhash)
                    .unwrap();
            }
            pow_limit_bits
        }
        Network::Bitcoin | Network::Signet => pow_limit_bits,
    }
}
/// This method is only valid when used for testnet and regtest networks.
/// As per "https://en.bitcoin.it/wiki/Testnet",
/// "If no block has been found in 20 minutes, the difficulty automatically
/// resets back to the minimum for a single block, after which it
/// returns to its previous value." This function is used to compute the
/// difficulty target in case the block has been found within 20
/// minutes.
This method is only valid when used for testnet and regtest networks.
This method is only valid when used for testnet and regtest networks .
fn find_next_difficulty_in_chain(
    network: &Network,
    store: &impl HeaderStore,
    prev_header: &StoredHeader,
) -> u32 {
    // This is the maximum difficulty target for the network
    let pow_limit_bits = pow_limit_bits(network);
    match network {
        Network::Testnet | Network::Regtest => {
            let mut current_header = prev_header;
            let initial_header_hash = store.get_initial_hash();
            // Keep traversing the blockchain backwards from the recent block to initial
            // header hash.
            while current_header.header.block_hash() != initial_header_hash {
                if current_header.header.bits != pow_limit_bits
                    || current_header.height % DIFFICULTY_ADJUSTMENT_INTERVAL == 0
                {
                    return current_header.header.bits;
                }
                // Traverse to the previous header
                current_header = store
                    .get_header(&current_header.header.prev_blockhash)
                    .unwrap();
            }
            pow_limit_bits
        }
        Network::Bitcoin | Network::Signet => pow_limit_bits,
    }
}
fn find_next_difficulty_in_chain ( network : & Network , store : & impl HeaderStore , prev_header : & StoredHeader , ) -> u32 { // This is the maximum difficulty target for the network let pow_limit_bits = pow_limit_bits ( network ) ; match network { Network :: Testnet | Network :: Regtest => { let mut current_header = prev_header ; let initial_header_hash = store . get_initial_hash ( ) ; // Keep traversing the blockchain backwards from the recent block to initial // header hash. while current_header . header . block_hash ( ) != initial_header_hash { if current_header . header . bits != pow_limit_bits || current_header . height % DIFFICULTY_ADJUSTMENT_INTERVAL == 0 { return current_header . header . bits ; } // Traverse to the previous header current_header = store . get_header ( & current_header . header . prev_blockhash ) . unwrap ( ) ; } pow_limit_bits } Network :: Bitcoin | Network :: Signet => pow_limit_bits , } }
This method is only valid when used for testnet and regtest networks.
This method is only valid when used for testnet and regtest networks .
network
&Network
store
&impl HeaderStore
prev_header
&StoredHeader
network
&Network
store
&impl HeaderStore
prev_header
&StoredHeader