0

I'm reading the example of the simple proof of work algorithm from here: https://www.oreilly.com/library/view/mastering-bitcoin/9781491902639/ch08.html#pow_example_outputs

In that example, the nonce always starts from ZERO and incrementally going to the max_nonce:

for nonce in xrange(max_nonce):
    hash_result = hashlib.sha256(str(header)+str(nonce)).hexdigest()

    # check if this is a valid result, below the target
    if long(hash_result, 16) < target:
        print "Success with nonce %d" % nonce
        print "Hash is %s" % hash_result
        return (hash_result,nonce)

In the real world of bitcoin mining, do you always start from the LAST NONCE and incrementally go up to find the correct HASH?

Because I don't see it makes sense if you start from ZERO again to solve the next math puzzle

1

The for-loop you describe is for a given block only.

So yes, when mining the next block at the next block-height, the nonce can be reset to zero, because the the header hash pre-image is now a new block template (new transactions, new header, potentially new difficulty). A zero nonce will not hash to the same header hash as the previous block with the same nonce, since the rest of the pre-image has changed.

Also, note that the nonce field no longer provides sufficient range for the difficulty on mainnet, meaning the for-loop can potentially complete without ever finding a valid header hash. In this case coinbase input script data can be iterated to "expand" the nonce-range.

  • > In this case coinbase input script data can be iterated to "expand" the nonce-range. So coinbase input script data is also similar vaclue to nonce? – Grady Jan 22 at 2:13
  • 2
    @Grady everything is similar to the nonce. Every combination of transactions, mining payouts, nonce, timestamp, ... is an individual and independent attempt at solving the block. The nonce is simply the easiest thing to change to get a new try, but it is by no means the only one. – Pieter Wuille Jan 22 at 7:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.