<p>One fundamental, expected way to make a valid template invalid: someone else finds a block, and it's time to switch.</p>

<p>You can technically keep incrementing "curtime" until it's 2 hours in the future, though my gut tells me I should try to avoid pushing "curtime" anywhere near that far into the future until after I've exhausted everything else.  I haven't spent too much time looking into GBT, but you should be able to make at least <em>some</em> changes to the coinbase data on the generation transaction (you probably won't have complete freedom, as I'm guessing there's a way for the pool to force things like /P2SH/ to show up in there for example) to get a different Merkle root.  People often call this the "<a href="https://en.bitcoin.it/wiki/Transactions#Generation" rel="nofollow" title="Transactions: Generation">extranonce</a>".</p>

<p>As for when you should ask for a new template, immediately after the spot you quoted in the <a href="https://en.bitcoin.it/wiki/Getblocktemplate#While_you.27re_mining" rel="nofollow" title="While you're mining">bitcoin wiki</a>, it says this:</p>

<blockquote>
  <p>If your template included a "longpollid" key, you can queue a request for a new template to be executed as soon as the pool decides you should change. This is the same as any other template request, except that you include the "longpollid" parameter that the pool provided in your request. If there is already a new template, the pool might respond immediately, so be sure not to depend on a delay!</p>
</blockquote>

<p>In other words, you should be able to do a "long poll" request that won't return any data until the pool server decides for you that it's time for a new template.  You definitely shouldn't rely on guessing that "desired block interval" ought to be good enough.</p>

CC BY-SA 3.0
2014-04-21T02:09:52.057Z
2014-04-21T02:09:52.057Z
Joe Amenta
<p>To add to Joe's answer:</p>

<p>"A block is mined every 10 minutes" is a misunderstanding. Blocks are found at random, and a new one could be found at any time. It could be 3 seconds after the last one, or 3 hours. 10 minutes is a target; the plan is that over a large number of blocks, the average time between them should approach 10 minutes. But this tells you  nothing about any single block. So if you don't have longpoll, you will want to request a new template much more often, perhaps every few seconds.</p>

CC BY-SA 3.0
2014-04-21T12:39:51.687Z
2014-04-21T12:39:51.687Z
Nate Eldredge
<p>I'm building a basic Bitcoin client in c# to better understand the mining protocol.
I need help understanding Getblocktemplate. I have read the Getblocktemplate wiki, but I'm still having an issue understanding the protocol. The wiki states "Since you're making all the blocks yourself, you really don't ever need to get another template... until it's invalid". When is the template considered invalid? How often do I need to call Getblocktemplate?
If a new block is mined every 10 minutes, with some coins that is every 1 minute, do I need to call Getblocktemplate every 10 minutes (1 minute depending on the coin) so that I have the latest "previousblockhash"?</p>

<p>The steps I'm using:</p>

<ol>
<li><p>Make a rpc call to bicoind server for Getblocktemplate.</p></li>
<li><p>Parse data and build a merkle root.</p></li>
<li><p>Build a block header (version, previousblockhash, merkleroot, curtime, bits, nonce).</p></li>
<li><p>Get a header hash. headerHash = Hash(version, previousblockhash, merkleroot, curtime, bits, nonce).</p></li>
<li><p>Repeat step 4 by 2^32 increments of nonce.</p></li>
<li><p>I understand I can then increment "curtime", (which doesn't have to be precise?), so increment curtime and then repeat step 5. </p></li>
</ol>

<p>Is this correct? </p>

<p>How long can I keep incrementing "curtime" before I need to call getblocktemplate again?</p>

<p>At what point do I need a new block (i.e. new previousblockhash and new merkle root)?</p>

<bitcoind><mining-software><getblocktemplate>
How often should I call getblocktemplate?
Sarah
# How often should I call getblocktemplate?

(Asked by: username_0 on 2014-01-30)

I'm building a basic Bitcoin client in c# to better understand the mining protocol.
I need help understanding Getblocktemplate. I have read the Getblocktemplate wiki, but I'm still having an issue understanding the protocol. The wiki states "Since you're making all the blocks yourself, you really don't ever need to get another template... until it's invalid". When is the template considered invalid? How often do I need to call Getblocktemplate?
If a new block is mined every 10 minutes, with some coins that is every 1 minute, do I need to call Getblocktemplate every 10 minutes (1 minute depending on the coin) so that I have the latest "previousblockhash"?

The steps I'm using:

1. Make a rpc call to bicoind server for Getblocktemplate.
2. Parse data and build a merkle root.
3. Build a block header (version, previousblockhash, merkleroot, curtime, bits, nonce).
4. Get a header hash. headerHash = Hash(version, previousblockhash, merkleroot, curtime, bits, nonce).
5. Repeat step 4 by 2^32 increments of nonce.
6. I understand I can then increment "curtime", (which doesn't have to be precise?), so increment curtime and then repeat step 5.

Is this correct?

How long can I keep incrementing "curtime" before I need to call getblocktemplate again?

At what point do I need a new block (i.e. new previousblockhash and new merkle root)?

**Tags:** bitcoind, mining-software, getblocktemplate


---

## 2 answers

### Answer by username_1 on 2014-04-21. Score: 2

One fundamental, expected way to make a valid template invalid: someone else finds a block, and it's time to switch.

You can technically keep incrementing "curtime" until it's 2 hours in the future, though my gut tells me I should try to avoid pushing "curtime" anywhere near that far into the future until after I've exhausted everything else. I haven't spent too much time looking into GBT, but you should be able to make at least *some* changes to the coinbase data on the generation transaction (you probably won't have complete freedom, as I'm guessing there's a way for the pool to force things like /P2SH/ to show up in there for example) to get a different Merkle root. People often call this the "[extranonce](https://en.bitcoin.it/wiki/Transactions#Generation "Transactions: Generation")".

As for when you should ask for a new template, immediately after the spot you quoted in the [bitcoin wiki](https://en.bitcoin.it/wiki/Getblocktemplate#While_you.27re_mining "While you're mining"), it says this:

> If your template included a "longpollid" key, you can queue a request for a new template to be executed as soon as the pool decides you should change. This is the same as any other template request, except that you include the "longpollid" parameter that the pool provided in your request. If there is already a new template, the pool might respond immediately, so be sure not to depend on a delay!

In other words, you should be able to do a "long poll" request that won't return any data until the pool server decides for you that it's time for a new template. You definitely shouldn't rely on guessing that "desired block interval" ought to be good enough.


---

### Answer by username_2 on 2014-04-21. Score: 1

To add to Joe's answer:

"A block is mined every 10 minutes" is a misunderstanding. Blocks are found at random, and a new one could be found at any time. It could be 3 seconds after the last one, or 3 hours. 10 minutes is a target; the plan is that over a large number of blocks, the average time between them should approach 10 minutes. But this tells you nothing about any single block. So if you don't have longpoll, you will want to request a new template much more often, perhaps every few seconds.


---

