<p>The for-loop you describe is for a given block only. </p>

<p>So yes, when mining the next block at the next block-height, the nonce can be reset to zero, because the the header hash <strong>pre-image</strong> 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.</p>

<p>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.</p>

CC BY-SA 4.0
2019-01-22T02:13:32.273Z
> 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?
0xgoku
CC BY-SA 4.0
2019-01-22T07:45:29.730Z
@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
CC BY-SA 4.0
2019-01-21T13:05:32.537Z
2019-01-21T13:05:32.537Z
James C.
<p>I'm reading the example of the simple proof of work algorithm from here: <a href="https://www.oreilly.com/library/view/mastering-bitcoin/9781491902639/ch08.html#pow_example_outputs" rel="nofollow noreferrer">https://www.oreilly.com/library/view/mastering-bitcoin/9781491902639/ch08.html#pow_example_outputs</a></p>

<p>In that example, the <code>nonce</code> always starts from ZERO and incrementally going to the <code>max_nonce</code>:</p>

<pre><code>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) &lt; target:
        print "Success with nonce %d" % nonce
        print "Hash is %s" % hash_result
        return (hash_result,nonce)
</code></pre>

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

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

<mining-pools><mining-theory>
Do you start from the LAST NONCE to find the correct HASH to solve Hash Puzzle in Bitcoin Mining?
0xgoku
# Do you start from the LAST NONCE to find the correct HASH to solve Hash Puzzle in Bitcoin Mining?

(Asked by: username_0 on 2019-01-21)

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

**Tags:** mining-pools, mining-theory


---

## 1 answers

### Answer by username_1 on 2019-01-21. Score: 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.

> **Comments:**
> **username_0**: > 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?  
> **username_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.  

---

