Skip to content
CLEARSATSSubscribe

Module 7 of 19 · Part 2: How it works

The difficulty adjustment

Whether miners add machines or switch them off, Bitcoin keeps blocks coming about every 10 minutes. The difficulty adjustment is the thermostat that makes that happen. This module shows exactly how and when it changes, straight from the code, including the edge cases experts still talk about.

⏱ About 51 minutes7 lessons5-question quiz

By the end you will

  • Explain why Bitcoin targets 10-minute blocks
  • Say how often difficulty changes and how it is calculated
  • Name the limits on how far it can move
  • Explain the time rules that stop miners faking the clock
  • Describe the off-by-one quirk and the proposed timewarp fix
Your progress0/7 lessons · 0%

Saved in this browser only. Nothing is sent to us.

Lessons

  1. 1Lesson 1 of 7A target of one block every 10 minutesBasics6 min

    Bitcoin Core sets a target spacing of 10 minutes between blocks (Bitcoin Core chainparams.cpp). Blocks don't arrive like a train timetable. Finding one is like rolling dice over and over until a rare number comes up, so some come after one minute and some after an hour. Over many blocks, the average lands near 10 minutes.

    Why so slow? Every computer on the network needs time to hear about a new block before the next one appears. A steady pace also keeps the issuance schedule from Module 6 on track.

    1. Open mempool.space and look at the times between the last ten blocks.
    2. Notice how uneven they are.
    3. Remember: 10 minutes is an average, not a promise.
  2. 2Lesson 2 of 7Retarget every 2,016 blocksBasics8 min

    Every 2,016 blocks, every node recalculates the difficulty. 2,016 blocks at 10 minutes each is two weeks, which is the target timespan in the code (chainparams.cpp; pow.cpp).

    If the last period took less than two weeks, blocks came too fast, so difficulty goes up. If it took longer, difficulty goes down. Nobody votes; each node does the same math and gets the same answer. The difficulty adjustment page tracks it live.

    1. Multiply 2,016 by 10 minutes and convert to days.
    2. Open the difficulty adjustment page and find how many blocks remain in this period.
    3. Predict whether difficulty will rise or fall, then check after the change.
  3. 3Lesson 3 of 7The 4x limit in either directionGo deeper · optional6 min

    The code limits how far one change can go. The measured time is clamped to between one quarter and four times the two-week target, so difficulty can't jump more than 4x up or drop more than 4x down in one step (Bitcoin Core pow.cpp). This keeps a single strange period from wrecking the next one.

    1. Find `nPowTargetTimespan/4` and `nPowTargetTimespan*4` in pow.cpp.
    2. Explain what would happen without a limit if half the miners switched off overnight.
    3. Write down the biggest possible single change: 4x.
  4. 4Lesson 4 of 7Median time past and the 2-hour limitGo deeper · optional8 min

    Miners write the time into each block, so what stops them lying? Two rules.

    Together these keep block times roughly honest without needing any official clock.

    1. Find MAX_FUTURE_BLOCK_TIME in chain.h.
    2. Explain why a median of 11 blocks is harder to fake than the last block alone.
    3. Look at block times on mempool.space and spot one that is earlier than the block before it. It is allowed, within these rules.
  5. 5Lesson 5 of 7Testnet's 20-minute ruleGo deeper · optional5 min

    Testnet is a practice network with worthless coins. On testnet, if no block arrives within 20 minutes, a block at the lowest difficulty is allowed (Bitcoin Core pow.cpp). This keeps the practice network moving when few people mine on it. The rule does not exist on the real network.

    1. Find `fPowAllowMinDifficultyBlocks` in pow.cpp.
    2. Write down why a practice network needs this and the real one doesn't.
    3. Never send real bitcoin to a testnet address. Testnet addresses look different, for example starting with tb1.
  6. 6Lesson 6 of 7The off-by-one: 2,015 intervalsAdvanced · optional8 min

    The retarget measures from the first block of the period to the last. That spans 2,015 gaps between blocks, not 2,016, because the code looks back from block N to block N minus 2,015 (Bitcoin Core pow.cpp). It is a small quirk from Bitcoin's earliest code, and changing it would change consensus rules, so it stays.

    1. Count the gaps between 5 fence posts: 4.
    2. Apply the same logic to 2,016 blocks.
    3. Note how this ties into the next lesson.
  7. 7Lesson 7 of 7The timewarp weakness and its proposed fixAdvanced · optional10 min

    Because periods don't overlap, miners with enough power could set the last block of each period far in the future and the first block of the next period back in the past. Done again and again, that could trick the math into lowering difficulty and speed up blocks. This is called the timewarp attack.

    BIP 54, the Consensus Cleanup, proposes a fix: the first block of each period may be no more than 7,200 seconds (two hours) earlier than the block before it (BIP 54). BIP means Bitcoin Improvement Proposal, a numbered public document describing a change. It is a proposed soft fork; check its page for its current status.

    1. Open BIP 54 and find the timewarp section.
    2. Write the fix in one sentence.
    3. Explain why the median-time-past rule alone does not stop timewarp.

⚠ Common mistakes

  • Expecting a block every 10 minutes like clockwork
  • Thinking miners or developers vote on difficulty
  • Assuming difficulty changes every day
  • Treating BIP 54 as active without checking its status

Quick check: 5 questions

  1. 1. How often does difficulty change?

  2. 2. Blocks came faster than 10 minutes on average last period. What happens?

  3. 3. What is the most difficulty can change in one step?

  4. 4. How far ahead of a node's clock can a block's time be?

  5. 5. How many gaps between blocks does each retarget measure?

Next on the trail · available now

Module 8: Mining: hardware, pools and protocols

Follow a hash from an ASIC to a pool payout.

Start Module 8 →

Educational only. Nobody from ClearSats will ever ask for your seed words.

Sources