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)?


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".

As for when you should ask for a new template, immediately after the spot you quoted in the bitcoin wiki, 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.


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.

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.