P2Pool scientific basics

Difficulty adjustment

How Bitcoin, Litecoin, Dogecoin and the c2pool share chain set their difficulty

Updated November 2026. The first version of this page said that Bitcoin’s retarget uses the “block mediantime” and multiplied the difficulty by the timespan ratio. Both are wrong: the retarget uses block timestamps, and it is the target that scales with the ratio, so difficulty moves the other way. The page is rewritten from the Bitcoin Core source, and sections on Litecoin, Dogecoin and the c2pool share chain are added.

Bitcoin’s retarget

Every 2016 blocks Bitcoin sets a new target, so that blocks keep arriving about every 10 minutes (2016 blocks in 14 days, the target timespan T = 1,209,600 s). The steps, as in Bitcoin Core’s src/pow.cpp:

  1. Measure. The actual timespan is the timestamp (nTime) of the last block of the period minus the timestamp of the first block of the period, 2015 blocks earlier. These are the blocks’ own header timestamps, not their median time past.
  2. Clamp. The actual timespan is limited to the range [T/4, 4T], so one retarget moves the target by at most a factor of 4 either way.
  3. Scale the target. new target = old target × actual timespan / T. Blocks that came too fast give a ratio below 1, a smaller target and a higher difficulty.
  4. Cap and encode. The new target is capped at the chain’s largest allowed target (the “difficulty 1” target, the lowest difficulty) and stored in the next headers in the compact nBits form, which rounds it slightly.

Difficulty is the difficulty-1 target divided by the current target, so it changes by the inverse of the timespan ratio.

TARGET_TIMESPAN = 14 * 24 * 60 * 60      # 2016 blocks x 10 minutes
POW_LIMIT = 0xffff << 208                 # difficulty-1 target (nBits 0x1d00ffff)

def bits_to_target(bits):
    exponent, mantissa = bits >> 24, bits & 0x007fffff
    return mantissa << (8 * (exponent - 3)) if exponent >= 3 else mantissa >> (8 * (3 - exponent))

def next_target(last_bits, last_time, first_time):
    """last_time: nTime of the last block of the period.
    first_time: nTime of the block 2015 blocks before it."""
    actual = last_time - first_time
    actual = max(TARGET_TIMESPAN // 4, min(actual, TARGET_TIMESPAN * 4))
    new = bits_to_target(last_bits) * actual // TARGET_TIMESPAN
    return min(new, POW_LIMIT)

def difficulty(target):
    return POW_LIMIT / target

Bitcoin Core then encodes the result back to nBits (GetCompact), which keeps three bytes of mantissa. The window spans 2015 intervals rather than 2016, a well-known off-by-one from the first release that is now consensus. Example: with the timestamps of blocks 30,240 and 32,255, the function gives nBits = 0x1d00d86a for height 32,256, Bitcoin’s first difficulty rise (difficulty about 1.18).

Median time past

The median time past (MTP) of a block is the median of the timestamps of that block and the ten before it. It is computed by every node; it is not a header field. It is robust against a single wrong or manipulated timestamp, and Bitcoin uses it for two rules:

It does not enter the difficulty retarget. Here is the original page’s example, which computes the MTP of the chain tip over RPC (Bitcoin Core’s getblock also returns it directly as mediantime):

import bitcoin.rpc
def calculate_mediantime():
    proxy = bitcoin.rpc.Proxy()  # Establish connection to Bitcoin Core client
    block_count = proxy.getblockcount()
    if block_count >= 10:
        timestamps = []
        for height in range(block_count, block_count - 11, -1):
            block_hash = proxy.getblockhash(height)
            timestamp = proxy.getblockheader(block_hash).nTime
            timestamps.append(timestamp)
        sorted_timestamps = sorted(timestamps)
        mediantime = sorted_timestamps[5]  # Take the middle timestamp
        return mediantime
    return None  # Return None if there are not enough blocks to calculate mediantime
# Example usage
mediantime = calculate_mediantime()
print(mediantime)

In the code snippet above, we establish a connection to a running Bitcoin Core client using the bitcoin.rpc.Proxy class. The calculate_mediantime function first retrieves the current block count using getblockcount. If there are at least 11 blocks in the blockchain (a tip height of 10 or more), it proceeds with the calculation. It iterates over the previous 11 blocks, starting from the current block, and reads the block timestamp (nTime) from getblockheader. The timestamps are stored in the timestamps list. The timestamps list is then sorted in ascending order using the sorted function. Finally, the middle timestamp from the sorted list (at index 5) is taken as the mediantime. The mediantime is returned by the function. If there are not enough blocks to calculate the mediantime, None is returned.

Litecoin

Litecoin keeps Bitcoin’s rule with a 2.5-minute spacing: 2016 blocks in 3.5 days. One difference: after the first period, the window goes back a full 2016 blocks instead of 2015 (a fix credited to Art Forz, against difficulty manipulation). c2pool’s embedded Litecoin node computes the next target itself from the headers it has synced, with the same clamp and the same timestamps.

Dogecoin

Dogecoin, which c2pool merge-mines with Litecoin, had a 4-hour retarget window (240 one-minute blocks) until height 145,000. From that height it uses DigiShield, which retargets at every block. Merged mining (AuxPoW) began at height 371,337. c2pool’s embedded Dogecoin node follows both rules.

The share chain’s own difficulty

A P2Pool share chain has a difficulty of its own, set per share rather than per period. In c2pool, as in p2pool:

V37Target vs. difficulty, and an absolute schedule.

Difficulty and target are inverses (difficulty = max_target / target), so the retarget multiplies the target by the timespan ratio and difficulty moves the opposite way: faster blocks (a smaller ratio) raise difficulty.

A window-based retarget depends on the timestamps at the two ends of its window. An absolute schedule (ASERT-style) computes the target directly from an anchor block and the latest block in constant time. It walks no window, so timestamps inside a window cannot make the target drift, although the latest block’s own timestamp still matters. Bitcoin Cash has used one since November 2020. It is a candidate for a share chain’s own difficulty in the V37 design work; the c2pool share chains today use the per-share rule above.