<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://en.bitcoin.it/w/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Pagent</id>
	<title>Bitcoin Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://en.bitcoin.it/w/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Pagent"/>
	<link rel="alternate" type="text/html" href="https://en.bitcoin.it/wiki/Special:Contributions/Pagent"/>
	<updated>2026-04-11T01:32:35Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.43.8</generator>
	<entry>
		<id>https://en.bitcoin.it/w/index.php?title=PHP_developer_intro&amp;diff=54479</id>
		<title>PHP developer intro</title>
		<link rel="alternate" type="text/html" href="https://en.bitcoin.it/w/index.php?title=PHP_developer_intro&amp;diff=54479"/>
		<updated>2015-02-20T15:15:03Z</updated>

		<summary type="html">&lt;p&gt;Pagent: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;L&#039;&#039;&#039;inux &#039;&#039;&#039;A&#039;&#039;&#039;pache &#039;&#039;&#039;M&#039;&#039;&#039;ySQL &#039;&#039;&#039;P&#039;&#039;&#039;HP + Bitcoin tutorial.&lt;br /&gt;
&lt;br /&gt;
For this introduction we assume that you have GNU/Linux server with Apache and PHP and that you wish to interact with the Bitcoin network from a web application. We assume some knowledge of Bitcoin and experience in PHP.&lt;br /&gt;
&lt;br /&gt;
While this is written for PHP, the same principles apply for other languages. See the associated [[API reference (JSON-RPC)|API reference]] pages for info on other languages.&lt;br /&gt;
&lt;br /&gt;
The easiest way to get started is to run Bitcoin in daemon mode with which PHP communicates via local HTTP requests. A library called [http://jsonrpcphp.org/ JSON-RPC] is used to call the various functions of bitcoind, which will respond back with a [http://en.wikipedia.org/wiki/Json JSON object].&lt;br /&gt;
&lt;br /&gt;
It is however recommended to use one of the [[#Alternative_Libs_For_RPC|Alternative Libraries]] listed below instead, since they are more sophisticated.&lt;br /&gt;
&lt;br /&gt;
== Setting up Bitcoin ==&lt;br /&gt;
&lt;br /&gt;
You can download the Bitcoin daemon from the [[Main_Page|homepage]] and run one of the included binaries or compile your own from the included source code. See [[Running Bitcoin]] for details on configuring bitcoind.&lt;br /&gt;
&lt;br /&gt;
Before running bitcoind you will need to create a configuration file in the Bitcoin data directory (~/.bitcoin/bitcoin.conf on Linux):&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
rpcuser=user&lt;br /&gt;
rpcpassword={you MUST pick a unique password to be secure}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
If you miss this step, bitcoind will remind you.&lt;br /&gt;
&lt;br /&gt;
Now run bitcoind:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$ ./bitcoind&lt;br /&gt;
# wait a few seconds for it to start up&lt;br /&gt;
$ ./bitcoind getinfo&lt;br /&gt;
# various information will be shown. If you get an error, try again until you see some useful output.&lt;br /&gt;
$ ./bitcoind help&lt;br /&gt;
# get help on commands, note no dash before help&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Bitcoin will begin synchronizing with the network and downloading a complete copy of the block chain. As of August 2012, more than 2gb of data must be downloaded and verified during this process. It may take two or more hours to complete. You will know when it&#039;s done when the block count reaches the [http://blockexplorer.com/q/getblockcount current count].&lt;br /&gt;
&lt;br /&gt;
== Getinfo (Bitcoind&#039;s version of Hello World) ==&lt;br /&gt;
&lt;br /&gt;
Assuming Bitcoin has finished the initialisation process; download the file jsonRPCClient.php from [http://jsonrpcphp.org/ JSON-RPC PHP] and place it in a web-accessible location.&lt;br /&gt;
&lt;br /&gt;
Second, create a PHP file with the following and visit it with your browser to test.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
  require_once &#039;jsonRPCClient.php&#039;;&lt;br /&gt;
  &lt;br /&gt;
  $bitcoin = new jsonRPCClient(&#039;http://user:password@127.0.0.1:8332/&#039;);&lt;br /&gt;
   &lt;br /&gt;
  echo &amp;quot;&amp;lt;pre&amp;gt;\n&amp;quot;;&lt;br /&gt;
  print_r($bitcoin-&amp;gt;getinfo());&lt;br /&gt;
  echo &amp;quot;&amp;lt;/pre&amp;gt;&amp;quot;;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Note:&#039;&#039;&#039; The jsonRPCClient library uses fopen() and will throw an exception saying &amp;quot;Unable to connect&amp;quot; if it receives a 404 or 500 error from bitcoind. This prevents you from being able to see error messages generated by bitcoind (as they are sent with status 404 or 500). The [[#Alternative_Libs_For_RPC|Alternative Libraries]] listed below are similar in function to JSON-RPC PHP but do not have this issue.&lt;br /&gt;
&lt;br /&gt;
== Precision ==&lt;br /&gt;
&lt;br /&gt;
Bitcoin amounts can range from 1 Satoshi (0.00000001 BTC) to nearly 2,100,000,000,000,000 (21,000,000 BTC).  To avoid rounding errors, you must make sure your PHP implementation supports the full range of Bitcoin values without losing precision.  Most PHP implementations use IEEE 64-bit double-precision floating point numbers with 53 bits of precision, which is enough to correctly represent the full range of bitcoin values.&lt;br /&gt;
&lt;br /&gt;
See [[Proper Money Handling (JSON-RPC)]] for more information.&lt;br /&gt;
&lt;br /&gt;
If your PHP implementation does not support 64-bit numbers (again, this is very rare), you must use a version of bitcoind that sends values as strings (genjix maintains a fork at http://github.com/genjix/bitcoin) and use the  [http://php.net/manual/en/ref.gmp.php GMP] and [http://php.net/manual/en/ref.bc.php BC Math] libraries for all calculations involving bitcoin amounts.&lt;br /&gt;
&lt;br /&gt;
== Accounts ==&lt;br /&gt;
&lt;br /&gt;
In Bitcoin, money is sent to addresses and many addresses can be held by one wallet. The balance shown by default in bitcoind is the sum of the bitcoins in all the addresses in the wallet.&lt;br /&gt;
&lt;br /&gt;
Bitcoin goes another step. You can have [[Accounts explained|accounts]]. Each account holds multiple addresses and acts like a mini-bitcoind. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$ ./bitcoind listaccounts&lt;br /&gt;
# show list of accounts and various info for each one&lt;br /&gt;
$ ./bitcoind getaccountaddress user889&lt;br /&gt;
# get an address to receive money to that is unique for the account user889&lt;br /&gt;
$ ./bitcoind getbalance user889&lt;br /&gt;
# get the sum of all the money in the addresses owned by the account user889&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In your application, each user should have a unique username. You may then query bitcoind for a unique address using $bitcoin-&amp;gt;getaccountaddress(&amp;quot;user889&amp;quot;); [gets the first address for user889] or $bitcoin-&amp;gt;getnewaddress(&amp;quot;user889&amp;quot;); [creates a new address for user889].&lt;br /&gt;
&lt;br /&gt;
The customer then deposits to this address.&lt;br /&gt;
&lt;br /&gt;
You can check the funds for that customer by doing $bitcoin-&amp;gt;getbalance(&amp;quot;user889&amp;quot;, 4);. The 4 indicates the minimum number of confirmations we will accept before assuming this payment is valid.&lt;br /&gt;
&lt;br /&gt;
If you will be using accounts for multiple deposits and withdrawals long-term, you may want to consider tracking user balances in your own database. This simplifies transfers between your application&#039;s accounts and decouples your accounts from the Bitcoin wallet.&lt;br /&gt;
&lt;br /&gt;
=== getnewaddress vs getaccountaddress ===&lt;br /&gt;
&lt;br /&gt;
Using getnewaddress helps increase maintain anonymity of your users by making it hard for a malicious agent to track payments flowing through your application. Running getnewaddress too often, however, will cause your wallet to become filled with many empty addresses.&lt;br /&gt;
&lt;br /&gt;
It is therefore recommended to in some way limit the number of unfunded addresses each user can request. Here is an example using sessions:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
    require_once(&#039;jsonRPCClient.php&#039;);&lt;br /&gt;
    $bitcoin = new jsonRPCClient(&#039;http://root:root@127.0.0.1:8332/&#039;); &lt;br /&gt;
    # now check for appropriate funds in user account&lt;br /&gt;
    try {&lt;br /&gt;
        $username = ...&lt;br /&gt;
        if(isset($_SESSION[&#039;sendaddress&#039;]))&lt;br /&gt;
            $sendaddress = $_SESSION[&#039;sendaddress&#039;];&lt;br /&gt;
        else {&lt;br /&gt;
            $sendaddress = $bitcoin-&amp;gt;getnewaddress($username);&lt;br /&gt;
            $_SESSION[&#039;sendaddress&#039;] = $sendaddress;&lt;br /&gt;
        }&lt;br /&gt;
        $balance = $bitcoin-&amp;gt;getbalance($username);&lt;br /&gt;
    }&lt;br /&gt;
    catch (Exception $e) {&lt;br /&gt;
        die(&amp;quot;&amp;lt;p&amp;gt;Server error! Please contact the admin.&amp;lt;/p&amp;gt;&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This creates a new address at the beginning of every new session, and stores it in the session variable.&lt;br /&gt;
&lt;br /&gt;
==Alternative Libs For RPC==&lt;br /&gt;
There are alternative PHP libraries for connecting to the bitcoind RPC which are recommended over using the plain jsonRPCClient.php class.&lt;br /&gt;
They do not rely on magic __call, use cURL instead of fopen and have better error handling (and can be installed using composer).&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/nbobtc/bitcoind-php NboBTC Bitcoind-PHP]&lt;br /&gt;
* [https://github.com/aceat64/EasyBitcoin-PHP EasyBitcoin-PHP]&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
* [[API reference (JSON-RPC)]]&lt;br /&gt;
* [[Lazy_API]]&lt;br /&gt;
* [https://github.com/cryptoapi/Payment-Gateway Bitcoin-PHP Payment library]&lt;br /&gt;
* [[Merchant Howto]]&lt;br /&gt;
* [[https://github.com/Bit-Wasp/bitcoin-lib-php Bitcoin-Lib-PHP - PHP Lib implementing signing of transactions, BIP32, etc]]&lt;br /&gt;
&lt;br /&gt;
[[es:Introducción para desarrolladores de PHP]]&lt;br /&gt;
[[de:Einführung_für_PHP-Entwickler]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Developer]]&lt;/div&gt;</summary>
		<author><name>Pagent</name></author>
	</entry>
	<entry>
		<id>https://en.bitcoin.it/w/index.php?title=Getblocktemplate&amp;diff=35841</id>
		<title>Getblocktemplate</title>
		<link rel="alternate" type="text/html" href="https://en.bitcoin.it/w/index.php?title=Getblocktemplate&amp;diff=35841"/>
		<updated>2013-03-02T13:13:24Z</updated>

		<summary type="html">&lt;p&gt;Pagent: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;getblocktemplate is a decentralized Bitcoin mining protocol openly developed by the Bitcoin community in 2012.&lt;br /&gt;
getblocktemplate supercedes the [[getwork]] mining protocol.&lt;br /&gt;
&lt;br /&gt;
{{TOC limit|3}}&lt;br /&gt;
&lt;br /&gt;
==Why change something that works?==&lt;br /&gt;
&lt;br /&gt;
===Decentralization===&lt;br /&gt;
The original getwork mining protocol simply issues block headers for a miner to solve.&lt;br /&gt;
The miner is kept in the dark as to what is actually in this block, and has no influence over it.&lt;br /&gt;
In effect, the authority of the miner to decide which transactions are accepted, etc, is all transferred blindly to the pool operator.&lt;br /&gt;
A corrupt (or compromised) pool operator could use the combined hash power of all the miners to execute double spend attacks or other similar attacks.&lt;br /&gt;
&lt;br /&gt;
getblocktemplate moves block creation to the miner, while giving pools a way to set down the rules for participation.&lt;br /&gt;
While pools can do just as much as they could before by expressing it in these rules, miners can not be kept in the dark and are enabled to freely choose what they participate in mining.&lt;br /&gt;
This improves the security of the Bitcoin network by making blocks decentralized again.&lt;br /&gt;
&lt;br /&gt;
===ASICs===&lt;br /&gt;
The original getwork protocol only provides a single block header, which is sufficient for a total of about 4 GH of mining.&lt;br /&gt;
With the &amp;quot;rollntime&amp;quot; extension, this can be extended to 4 GH *per second*, but even that is far from sufficient for the next generation of mining equipment (ASICs) which are capable of 1000 GH/s on the high end.&lt;br /&gt;
&lt;br /&gt;
By moving block creation to the miners, they are enabled to create as much work as they need locally, thus overcoming this limitation.&lt;br /&gt;
&lt;br /&gt;
===Scalability===&lt;br /&gt;
Due to scalability problems, bitcoind&#039;s JSON-RPC stack has not been able to keep up with the hashrates needed for solo mining today.&lt;br /&gt;
Since getblocktemplate drastically reduces the load required to a single request per new block on the network, direct solo mining on bitcoind is again possible.&lt;br /&gt;
Poolservers likewise benefit from having to meet much lower demands of miners who can make their own blocks.&lt;br /&gt;
&lt;br /&gt;
===Extensible===&lt;br /&gt;
The original getwork protocol was designed in a way that was very incompatible with extensions.&lt;br /&gt;
As a result, as new functionality was needed, extensions were &amp;quot;hacked in&amp;quot; out-of-band using HTTP headers.&lt;br /&gt;
getblocktemplate is designed from the start to be flexible for future extensions, and the [[BIP 0023|BIP 23]] specification already covers how the established getwork extensions can be implemented cleanly, regardless of transport protocol.&lt;br /&gt;
&lt;br /&gt;
==How to use it==&lt;br /&gt;
===For miners===&lt;br /&gt;
Mining software with GBT support:&lt;br /&gt;
* [https://bitcointalk.org/?topic=78192.msg870395#msg870395 BFGMiner 2.8+]&lt;br /&gt;
* [https://bitcointalk.org/?topic=28402.msg357369#msg357369 cgminer 2.9+]&lt;br /&gt;
* [http://ufasoft.com/open/bitcoin/ Ufasoft Bitcoin Miner 0.39+]&lt;br /&gt;
&lt;br /&gt;
The following miners have confirmed future support:&lt;br /&gt;
* [http://www.butterflylabs.com/drivers/ Butterfly Labs&#039;s EasyMiner]&lt;br /&gt;
* [https://bitcointalk.org/index.php?topic=1334.0 poclbm]&lt;br /&gt;
&lt;br /&gt;
To take advantage of getblocktemplate, you also need a compatible pool:&lt;br /&gt;
* [https://www.bitarena.net BitArena.net]&lt;br /&gt;
* [https://bitminter.com BitMinter]&lt;br /&gt;
* [https://eclipsemc.com/ EclipseMC]&lt;br /&gt;
* [http://eligius.st Eligius]&lt;br /&gt;
* [https://pool.itzod.ru/ itzod.ru]&lt;br /&gt;
* [http://polmine.pl PolMine]&lt;br /&gt;
* [https://www.triplemining.com/ TripleMining]&lt;br /&gt;
* [https://www.pool.mkalinin.ru/ pool.mkalinin.ru]&lt;br /&gt;
* [http://mmpool.bitparking.com/pool Bitparking merged mining pool] (planned)&lt;br /&gt;
* [https://50btc.com/ 50BTC] (planned)&lt;br /&gt;
* Encourage your favourite pool(s) to support decentralized mining!&lt;br /&gt;
&lt;br /&gt;
===For pool operators===&lt;br /&gt;
See also: [[Poolservers]]&lt;br /&gt;
&lt;br /&gt;
If you are implementing your own pool server, see the [[#Poolserver software|section for poolserver development]].&lt;br /&gt;
&lt;br /&gt;
===For developers===&lt;br /&gt;
====Mining software====&lt;br /&gt;
=====Using libblkmaker (C library)=====&lt;br /&gt;
If your miner can include C libraries, you can harness [http://gitorious.org/bitcoin/libblkmaker libblkmaker] to do all the GBT interpretation for you:&lt;br /&gt;
all your miner needs to do then is handle the networking (libblkmaker can prepare the JSON for you) and ask libblkmaker for data (block headers to search).&lt;br /&gt;
Note that libblkmaker does not provide a SHA256 implementation, and your miner needs to provide one for it to work.&lt;br /&gt;
libblkmaker currently only supports the Jansson JSON library, but was designed such that it can easily be ported to others;&lt;br /&gt;
[[User:Luke-jr|Luke Dashjr]] is willing to do this porting to other JSON libraries free of charge on request.&lt;br /&gt;
&lt;br /&gt;
=====Using python-blkmaker=====&lt;br /&gt;
If your miner is written in Python, you can harness [http://gitorious.org/bitcoin/python-blkmaker python-blkmaker], a native Python port of libblkmaker, to do all the GBT interpretation for you:&lt;br /&gt;
all your miner needs to do then is handle the networking (python-blkmaker can prepare the JSON for you) and ask the blkmaker module for data (block headers to search).&lt;br /&gt;
A short example Python script is included that finds a prepared share using CPU mining.&lt;br /&gt;
&lt;br /&gt;
=====From scratch=====&lt;br /&gt;
======Miner requests block template======&lt;br /&gt;
&lt;br /&gt;
To start participating, the miner contacts the pool and requests an initial template:&lt;br /&gt;
 {&amp;quot;id&amp;quot;: 0, &amp;quot;method&amp;quot;: &amp;quot;getblocktemplate&amp;quot;, &amp;quot;params&amp;quot;: [{&amp;quot;capabilities&amp;quot;: [&amp;quot;coinbasetxn&amp;quot;, &amp;quot;workid&amp;quot;, &amp;quot;coinbase/append&amp;quot;]}]}&lt;br /&gt;
&lt;br /&gt;
The server will respond with the full details needed to immediately begin mining blocks:&lt;br /&gt;
 {&lt;br /&gt;
  &amp;quot;error&amp;quot;: null,&lt;br /&gt;
  &amp;quot;result&amp;quot;: {&lt;br /&gt;
    &amp;quot;coinbasetxn&amp;quot;: {&lt;br /&gt;
      &amp;quot;data&amp;quot;: &amp;quot;0100000001000000000000000000000000000000000000000000000000000000&lt;br /&gt;
 0000000000ffffffff1302955d0f00456c6967697573005047dc66085fffffffff02fff1052a01&lt;br /&gt;
 0000001976a9144ebeb1cd26d6227635828d60d3e0ed7d0da248fb88ac01000000000000001976&lt;br /&gt;
 a9147c866aee1fa2f3b3d5effad576df3dbf1f07475588ac00000000&amp;quot;&lt;br /&gt;
    },&lt;br /&gt;
    &amp;quot;previousblockhash&amp;quot;: &amp;quot;000000004d424dec1c660a68456b8271d09628a80cc62583e5904f5894a2483c&amp;quot;,&lt;br /&gt;
    &amp;quot;transactions&amp;quot;: [],&lt;br /&gt;
    &amp;quot;expires&amp;quot;: 120,&lt;br /&gt;
    &amp;quot;target&amp;quot;: &amp;quot;00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff&amp;quot;,&lt;br /&gt;
    &amp;quot;longpollid&amp;quot;: &amp;quot;some gibberish&amp;quot;,&lt;br /&gt;
    &amp;quot;height&amp;quot;: 23957,&lt;br /&gt;
    &amp;quot;version&amp;quot;: 2,&lt;br /&gt;
    &amp;quot;curtime&amp;quot;: 1346886758,&lt;br /&gt;
    &amp;quot;mutable&amp;quot;: [&amp;quot;coinbase/append&amp;quot;],&lt;br /&gt;
    &amp;quot;bits&amp;quot;: &amp;quot;ffff001d&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
  &amp;quot;id&amp;quot;: 0&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
======How to build coinbase transaction======&lt;br /&gt;
If the pool allows the &amp;quot;coinbase/append&amp;quot; mutation by including it in the &amp;quot;mutable&amp;quot; key, you can rebuild the coinbase transaction to append any data your miner wants, such as an extranonce - you can use as much space as you need so long as the coinbase data does not overflow the 100 byte hard limit.&lt;br /&gt;
If the server does *not* allow &amp;quot;coinbase/append&amp;quot;, or you don&#039;t care to change it, you can skip this step entirely :)&lt;br /&gt;
&lt;br /&gt;
The coinbase data always begins after exactly 42 bytes of the coinbase transaction.&lt;br /&gt;
The 42nd byte (that is, the byte immediately before the data) is the length of the data.&lt;br /&gt;
So when/if you want to add on to the coinbase data, simply insert it 42+DataLen bytes into the transaction, and increment the 42nd byte by the length of your inserted data.&lt;br /&gt;
&lt;br /&gt;
Python example:&lt;br /&gt;
 import binascii&lt;br /&gt;
 coinbase = binascii.a2b_hex(template[&#039;coinbasetxn&#039;][&#039;data&#039;])&lt;br /&gt;
 extradata = b&#039;my block&#039;&lt;br /&gt;
 # The following can be done better in both Python 2 and Python 3, but this way works with both&lt;br /&gt;
 origLen = ord(coinbase[41:42])&lt;br /&gt;
 newLen = origLen + len(extradata)&lt;br /&gt;
 coinbase = coinbase[0:41] + chr(newLen).encode(&#039;ascii&#039;) + coinbase[42:42 + origLen] + extradata + coinbase[42 + origLen:]&lt;br /&gt;
&lt;br /&gt;
======How to build merkle root======&lt;br /&gt;
Collect your coinbase transaction (modified or not) at the front of the &amp;quot;transactions&amp;quot; list provided by the server.&lt;br /&gt;
Apply a double-SHA256 hash to each transaction in the list.&lt;br /&gt;
&lt;br /&gt;
Now, as long as the list has more than 1 hash remaining, go through each pair and hash them together.&lt;br /&gt;
That is, concatenate the first two, double-SHA256 that, repeat for the next two, and so on.&lt;br /&gt;
If you encounter an odd pair (that is, the hash list ends with a single item and no pairing), concatenate it with itself and hash that.&lt;br /&gt;
Continue to do that until there is only one hash left:&lt;br /&gt;
that is your merkle root.&lt;br /&gt;
&lt;br /&gt;
Python example:&lt;br /&gt;
 import hashlib&lt;br /&gt;
 def dblsha(data):&lt;br /&gt;
  	return hashlib.sha256(hashlib.sha256(data).digest()).digest()&lt;br /&gt;
 &lt;br /&gt;
 txnlist = [coinbase] + [binascii.a2b_hex(a[&#039;data&#039;]) for a in template[&#039;transactions&#039;]]&lt;br /&gt;
 merklehashes = [dblsha(t) for t in txnlist]&lt;br /&gt;
 while len(merklehashes) &amp;gt; 1:&lt;br /&gt;
 	 if len(merklehashes) % 2:&lt;br /&gt;
 	  	merklehashes.append(merklehashes[-1])&lt;br /&gt;
  	merklehashes = [dblsha(merklehashes[i] + merklehashes[i + 1]) for i in range(0, len(merklehashes), 2)]&lt;br /&gt;
 merkleroot = merklehashes[0]&lt;br /&gt;
&lt;br /&gt;
======How to build block header======&lt;br /&gt;
Assemble the block header as laid out in the Bitcoin [[block hashing algorithm]], using the data provided in the block template along with your very own merkle root.&lt;br /&gt;
Note that miners are expected to check the &amp;quot;version&amp;quot; number, and should not create blocks with versions they do not understand unless the server instructs them to do so with the &amp;quot;version/force&amp;quot; or &amp;quot;version/reduce&amp;quot; mutations - you don&#039;t need to support those, but if you don&#039;t support the version the server has provided, understand that the server may reject submissions if they don&#039;t meet some unknown future rules.&lt;br /&gt;
As of this time, the current Bitcoin block version is 2, so it is safe to build blocks with version 1 or 2.&lt;br /&gt;
&lt;br /&gt;
Python example:&lt;br /&gt;
 import struct&lt;br /&gt;
 blkheader = struct.pack(&#039;&amp;lt;I&#039;, template[&#039;version&#039;]) + \&lt;br /&gt;
             binascii.a2b_hex(template[&#039;previousblockhash&#039;]) + \&lt;br /&gt;
             merkleroot + \&lt;br /&gt;
             struct.pack(&#039;&amp;lt;I&#039;, template[&#039;curtime&#039;]) + \&lt;br /&gt;
             binascii.a2b_hex(template[&#039;bits&#039;]) + \&lt;br /&gt;
             b&#039;NONC&#039;&lt;br /&gt;
&lt;br /&gt;
======While you&#039;re mining======&lt;br /&gt;
Since you&#039;re making all the blocks yourself, you really don&#039;t ever need to get another template... until it&#039;s invalid.&lt;br /&gt;
Generally, it&#039;s a good idea to refresh more often anyway to get more transactions, but it&#039;s better to let the pool decide when to do that, since it knows what&#039;s changed in the meantime already.&lt;br /&gt;
&lt;br /&gt;
If your template included a &amp;quot;longpollid&amp;quot; key, you can queue a request for a new template to be executed as soon as the pool decides you should change.&lt;br /&gt;
This is the same as any other template request, except that you include the &amp;quot;longpollid&amp;quot; parameter that the pool provided in your request.&lt;br /&gt;
If there is already a new template, the pool might respond immediately, so be sure not to depend on a delay!&lt;br /&gt;
&lt;br /&gt;
So to amend the template request we used initially, now we use:&lt;br /&gt;
 {&amp;quot;id&amp;quot;: 0, &amp;quot;method&amp;quot;: &amp;quot;getblocktemplate&amp;quot;, &amp;quot;params&amp;quot;: [{&lt;br /&gt;
     &amp;quot;capabilities&amp;quot;: [&amp;quot;coinbasetxn&amp;quot;, &amp;quot;workid&amp;quot;, &amp;quot;coinbase/append&amp;quot;],&lt;br /&gt;
     &amp;quot;longpollid&amp;quot;: &amp;quot;some gibberish&amp;quot;,&lt;br /&gt;
 }]}&lt;br /&gt;
&lt;br /&gt;
======Submitting shares======&lt;br /&gt;
When miner find the job which meets requested difficulty, it can submit the block to the server as a share:&lt;br /&gt;
 {&amp;quot;id&amp;quot;: 0, &amp;quot;method&amp;quot;: &amp;quot;submitblock&amp;quot;, &amp;quot;params&amp;quot;: [&lt;br /&gt;
      &amp;quot;020000003c48a294584f90e58325c60ca82896d071826b45680a661cec4d424d00000000&lt;br /&gt;
 de6433d46c0c7f50d84a05aec77be0199176cdd47f77e344b6f50c84380fddba66dc47501d00ff&lt;br /&gt;
 ff0000010001010000000100000000000000000000000000000000000000000000000000000000&lt;br /&gt;
 00000000ffffffff1302955d0f00456c6967697573005047dc66085fffffffff02fff1052a0100&lt;br /&gt;
 00001976a9144ebeb1cd26d6227635828d60d3e0ed7d0da248fb88ac01000000000000001976a9&lt;br /&gt;
 147c866aee1fa2f3b3d5effad576df3dbf1f07475588ac00000000&amp;quot;&lt;br /&gt;
 ]}&lt;br /&gt;
&lt;br /&gt;
To assemble the block data, simply concatenate your block header, number of transactions encoded in [[Protocol specification#Variable length integer|Bitcoin varint format]], followed by each of the transactions in your block (beginning with the coinbase).&lt;br /&gt;
If the server has listed &amp;quot;submit/coinbase&amp;quot; in its &amp;quot;mutable&amp;quot; key, you may opt to omit the transactions after the coinbase.&lt;br /&gt;
&lt;br /&gt;
Python example:&lt;br /&gt;
 def varintEncode(n):&lt;br /&gt;
   if n &amp;lt; 0xfd:&lt;br /&gt;
     return struct.pack(&#039;&amp;lt;B&#039;, n)&lt;br /&gt;
   # NOTE: Technically, there are more encodings for numbers bigger than&lt;br /&gt;
   # 16-bit, but transaction counts can&#039;t be that high with version 2 Bitcoin&lt;br /&gt;
   # blocks&lt;br /&gt;
   return b&#039;\xfd&#039; + struct.pack(&#039;&amp;lt;H&#039;, n)&lt;br /&gt;
 blkdata = blkheader + varintEncode(len(txnlist)) + coinbase&lt;br /&gt;
 if &#039;submit/coinbase&#039; not in template.get(&#039;mutable&#039;, ()):&lt;br /&gt;
   for txn in txnlist[1:]:&lt;br /&gt;
     blkdata += txn&lt;br /&gt;
&lt;br /&gt;
====Poolserver software====&lt;br /&gt;
Recommended standards to start with (in order of importance):&lt;br /&gt;
* [[BIP 0022|BIP 22 (non-optional sections)]]&lt;br /&gt;
* [[BIP 0022#Optional: Long Polling|BIP 22 Long Polling]]&lt;br /&gt;
* [[BIP 0023#Basic Pool Extensions|BIP 23 Basic Pool Extensions]]&lt;br /&gt;
* [[BIP 0023#Mutations|BIP 23 Mutation &amp;quot;coinbase/append&amp;quot;]] (required for miner extranonce rolling)&lt;br /&gt;
* [[BIP 0023#Submission Abbreviation|BIP 23 Submission Abbreviation &amp;quot;submit/coinbase&amp;quot;]]&lt;br /&gt;
* [[BIP 0023#Mutations|BIP 23 Mutation &amp;quot;time/increment&amp;quot;]] (be sure to specify &amp;quot;maxtime&amp;quot; or &amp;quot;maxtimeoff&amp;quot;!)&lt;br /&gt;
&lt;br /&gt;
==Technical specifications==&lt;br /&gt;
* [[BIP 0022|BIP 22: getblocktemplate - Fundamentals]]&lt;br /&gt;
* [[BIP 0023|BIP 23: getblocktemplate - Pooled Mining]]&lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
&lt;br /&gt;
Getblocktemplate&#039;s origins trace back to forrestv&#039;s getmemorypool JSON-RPC method for bitcoind.&lt;br /&gt;
He created it so that his pool ([[P2Pool]]) could piggy-back on bitcoind so as to avoid harming the Bitcoin network (up to this point, it mined only empty blocks that never confirmed transactions).&lt;br /&gt;
Having been fighting with scalability problems in pushpool/bitcoind for months on his pool ([[Eligius]]), Luke-Jr set out to implement a fast pool server using getmemorypool to do its own block production (this became [[Eloipool]], this first open source makes-its-own-blocks poolserver).&lt;br /&gt;
Other poolservers also implemented block creation using getmemorypool over the months following.&lt;br /&gt;
&lt;br /&gt;
At about the same time, interest in decentralizing pooled mining became a hot topic.&lt;br /&gt;
While [[BitPenny]] had initially released its own decentralized mining proxy months prior, [[P2Pool]]&#039;s implementation became rapidly popular.&lt;br /&gt;
Anyone involved in Bitcoin mining protocols could see the need to move control of block creation back into the hands of the miners.&lt;br /&gt;
Unfortunately, both [[BitPenny]] and [[P2Pool]] had used very pool-specific proprietary protocols to implement their decentralization.&lt;br /&gt;
On the other hand, getmemorypool was *almost* a perfect fit for the task - it just lacked support for the now-ubiquitous pooled mining extensions that had developed around the getwork protocol over time.&lt;br /&gt;
&lt;br /&gt;
In February of 2012, Luke implemented and [https://bitcointalk.org/?topic=23768.msg774497#msg774497 deployed] a first draft of getmemorypool mining support in [[Eloipool]] (and on [[Eligius]]) along with a proof-of-concept getwork proxy (now known as gmp-proxy), adding revisions as needed to function as a general-purpose decentralized mining protocol.&lt;br /&gt;
After he had confirmed it was working, he documented and proposed it on the [[Bitcoin development mailing list]] for review on February 28th, where discussion began on what was missing and what needed to be changed or clarified.&lt;br /&gt;
During the following few months, a number of others, both developers and testers, provided constructive criticism and suggestions, which were integrated into the standard.&lt;br /&gt;
Luke also actively encouraged participation in the development of the standard among pool operators and poolserver authors, especially as it became necessary to move forward into the ASIC &amp;quot;mining generation&amp;quot;.&lt;br /&gt;
Eventually, it was decided it would be best to rename it to the more appropriate &amp;quot;getblocktemplate&amp;quot; name and drop backward compatibility with getmemorypool for simplicity.&lt;br /&gt;
The standard was split into two pieces and the technical specification can be found in [[BIP 0022|BIP 22]] and [[BIP 0023|BIP 23]].&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
* [https://bitcointalk.org/?topic=108854 BitcoinTalk forum thread]&lt;br /&gt;
* [[BIP 0022|BIP 22: getblocktemplate - Fundamentals]] (technical)&lt;br /&gt;
* [[BIP 0023|BIP 23: getblocktemplate - Pooled Mining]] (technical)&lt;/div&gt;</summary>
		<author><name>Pagent</name></author>
	</entry>
	<entry>
		<id>https://en.bitcoin.it/w/index.php?title=Getblocktemplate&amp;diff=35840</id>
		<title>Getblocktemplate</title>
		<link rel="alternate" type="text/html" href="https://en.bitcoin.it/w/index.php?title=Getblocktemplate&amp;diff=35840"/>
		<updated>2013-03-02T13:11:46Z</updated>

		<summary type="html">&lt;p&gt;Pagent: Removed speculation that getblocktemplate will supercede other getwork replacements (stratum)&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;getblocktemplate is a decentralized Bitcoin mining protocol openly developed by the Bitcoin community in 2012.&lt;br /&gt;
getblocktemplate supercedes the old getwork mining protocol.&lt;br /&gt;
&lt;br /&gt;
{{TOC limit|3}}&lt;br /&gt;
&lt;br /&gt;
==Why change something that works?==&lt;br /&gt;
&lt;br /&gt;
===Decentralization===&lt;br /&gt;
The original getwork mining protocol simply issues block headers for a miner to solve.&lt;br /&gt;
The miner is kept in the dark as to what is actually in this block, and has no influence over it.&lt;br /&gt;
In effect, the authority of the miner to decide which transactions are accepted, etc, is all transferred blindly to the pool operator.&lt;br /&gt;
A corrupt (or compromised) pool operator could use the combined hash power of all the miners to execute double spend attacks or other similar attacks.&lt;br /&gt;
&lt;br /&gt;
getblocktemplate moves block creation to the miner, while giving pools a way to set down the rules for participation.&lt;br /&gt;
While pools can do just as much as they could before by expressing it in these rules, miners can not be kept in the dark and are enabled to freely choose what they participate in mining.&lt;br /&gt;
This improves the security of the Bitcoin network by making blocks decentralized again.&lt;br /&gt;
&lt;br /&gt;
===ASICs===&lt;br /&gt;
The original getwork protocol only provides a single block header, which is sufficient for a total of about 4 GH of mining.&lt;br /&gt;
With the &amp;quot;rollntime&amp;quot; extension, this can be extended to 4 GH *per second*, but even that is far from sufficient for the next generation of mining equipment (ASICs) which are capable of 1000 GH/s on the high end.&lt;br /&gt;
&lt;br /&gt;
By moving block creation to the miners, they are enabled to create as much work as they need locally, thus overcoming this limitation.&lt;br /&gt;
&lt;br /&gt;
===Scalability===&lt;br /&gt;
Due to scalability problems, bitcoind&#039;s JSON-RPC stack has not been able to keep up with the hashrates needed for solo mining today.&lt;br /&gt;
Since getblocktemplate drastically reduces the load required to a single request per new block on the network, direct solo mining on bitcoind is again possible.&lt;br /&gt;
Poolservers likewise benefit from having to meet much lower demands of miners who can make their own blocks.&lt;br /&gt;
&lt;br /&gt;
===Extensible===&lt;br /&gt;
The original getwork protocol was designed in a way that was very incompatible with extensions.&lt;br /&gt;
As a result, as new functionality was needed, extensions were &amp;quot;hacked in&amp;quot; out-of-band using HTTP headers.&lt;br /&gt;
getblocktemplate is designed from the start to be flexible for future extensions, and the [[BIP 0023|BIP 23]] specification already covers how the established getwork extensions can be implemented cleanly, regardless of transport protocol.&lt;br /&gt;
&lt;br /&gt;
==How to use it==&lt;br /&gt;
===For miners===&lt;br /&gt;
Mining software with GBT support:&lt;br /&gt;
* [https://bitcointalk.org/?topic=78192.msg870395#msg870395 BFGMiner 2.8+]&lt;br /&gt;
* [https://bitcointalk.org/?topic=28402.msg357369#msg357369 cgminer 2.9+]&lt;br /&gt;
* [http://ufasoft.com/open/bitcoin/ Ufasoft Bitcoin Miner 0.39+]&lt;br /&gt;
&lt;br /&gt;
The following miners have confirmed future support:&lt;br /&gt;
* [http://www.butterflylabs.com/drivers/ Butterfly Labs&#039;s EasyMiner]&lt;br /&gt;
* [https://bitcointalk.org/index.php?topic=1334.0 poclbm]&lt;br /&gt;
&lt;br /&gt;
To take advantage of getblocktemplate, you also need a compatible pool:&lt;br /&gt;
* [https://www.bitarena.net BitArena.net]&lt;br /&gt;
* [https://bitminter.com BitMinter]&lt;br /&gt;
* [https://eclipsemc.com/ EclipseMC]&lt;br /&gt;
* [http://eligius.st Eligius]&lt;br /&gt;
* [https://pool.itzod.ru/ itzod.ru]&lt;br /&gt;
* [http://polmine.pl PolMine]&lt;br /&gt;
* [https://www.triplemining.com/ TripleMining]&lt;br /&gt;
* [https://www.pool.mkalinin.ru/ pool.mkalinin.ru]&lt;br /&gt;
* [http://mmpool.bitparking.com/pool Bitparking merged mining pool] (planned)&lt;br /&gt;
* [https://50btc.com/ 50BTC] (planned)&lt;br /&gt;
* Encourage your favourite pool(s) to support decentralized mining!&lt;br /&gt;
&lt;br /&gt;
===For pool operators===&lt;br /&gt;
See also: [[Poolservers]]&lt;br /&gt;
&lt;br /&gt;
If you are implementing your own pool server, see the [[#Poolserver software|section for poolserver development]].&lt;br /&gt;
&lt;br /&gt;
===For developers===&lt;br /&gt;
====Mining software====&lt;br /&gt;
=====Using libblkmaker (C library)=====&lt;br /&gt;
If your miner can include C libraries, you can harness [http://gitorious.org/bitcoin/libblkmaker libblkmaker] to do all the GBT interpretation for you:&lt;br /&gt;
all your miner needs to do then is handle the networking (libblkmaker can prepare the JSON for you) and ask libblkmaker for data (block headers to search).&lt;br /&gt;
Note that libblkmaker does not provide a SHA256 implementation, and your miner needs to provide one for it to work.&lt;br /&gt;
libblkmaker currently only supports the Jansson JSON library, but was designed such that it can easily be ported to others;&lt;br /&gt;
[[User:Luke-jr|Luke Dashjr]] is willing to do this porting to other JSON libraries free of charge on request.&lt;br /&gt;
&lt;br /&gt;
=====Using python-blkmaker=====&lt;br /&gt;
If your miner is written in Python, you can harness [http://gitorious.org/bitcoin/python-blkmaker python-blkmaker], a native Python port of libblkmaker, to do all the GBT interpretation for you:&lt;br /&gt;
all your miner needs to do then is handle the networking (python-blkmaker can prepare the JSON for you) and ask the blkmaker module for data (block headers to search).&lt;br /&gt;
A short example Python script is included that finds a prepared share using CPU mining.&lt;br /&gt;
&lt;br /&gt;
=====From scratch=====&lt;br /&gt;
======Miner requests block template======&lt;br /&gt;
&lt;br /&gt;
To start participating, the miner contacts the pool and requests an initial template:&lt;br /&gt;
 {&amp;quot;id&amp;quot;: 0, &amp;quot;method&amp;quot;: &amp;quot;getblocktemplate&amp;quot;, &amp;quot;params&amp;quot;: [{&amp;quot;capabilities&amp;quot;: [&amp;quot;coinbasetxn&amp;quot;, &amp;quot;workid&amp;quot;, &amp;quot;coinbase/append&amp;quot;]}]}&lt;br /&gt;
&lt;br /&gt;
The server will respond with the full details needed to immediately begin mining blocks:&lt;br /&gt;
 {&lt;br /&gt;
  &amp;quot;error&amp;quot;: null,&lt;br /&gt;
  &amp;quot;result&amp;quot;: {&lt;br /&gt;
    &amp;quot;coinbasetxn&amp;quot;: {&lt;br /&gt;
      &amp;quot;data&amp;quot;: &amp;quot;0100000001000000000000000000000000000000000000000000000000000000&lt;br /&gt;
 0000000000ffffffff1302955d0f00456c6967697573005047dc66085fffffffff02fff1052a01&lt;br /&gt;
 0000001976a9144ebeb1cd26d6227635828d60d3e0ed7d0da248fb88ac01000000000000001976&lt;br /&gt;
 a9147c866aee1fa2f3b3d5effad576df3dbf1f07475588ac00000000&amp;quot;&lt;br /&gt;
    },&lt;br /&gt;
    &amp;quot;previousblockhash&amp;quot;: &amp;quot;000000004d424dec1c660a68456b8271d09628a80cc62583e5904f5894a2483c&amp;quot;,&lt;br /&gt;
    &amp;quot;transactions&amp;quot;: [],&lt;br /&gt;
    &amp;quot;expires&amp;quot;: 120,&lt;br /&gt;
    &amp;quot;target&amp;quot;: &amp;quot;00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff&amp;quot;,&lt;br /&gt;
    &amp;quot;longpollid&amp;quot;: &amp;quot;some gibberish&amp;quot;,&lt;br /&gt;
    &amp;quot;height&amp;quot;: 23957,&lt;br /&gt;
    &amp;quot;version&amp;quot;: 2,&lt;br /&gt;
    &amp;quot;curtime&amp;quot;: 1346886758,&lt;br /&gt;
    &amp;quot;mutable&amp;quot;: [&amp;quot;coinbase/append&amp;quot;],&lt;br /&gt;
    &amp;quot;bits&amp;quot;: &amp;quot;ffff001d&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
  &amp;quot;id&amp;quot;: 0&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
======How to build coinbase transaction======&lt;br /&gt;
If the pool allows the &amp;quot;coinbase/append&amp;quot; mutation by including it in the &amp;quot;mutable&amp;quot; key, you can rebuild the coinbase transaction to append any data your miner wants, such as an extranonce - you can use as much space as you need so long as the coinbase data does not overflow the 100 byte hard limit.&lt;br /&gt;
If the server does *not* allow &amp;quot;coinbase/append&amp;quot;, or you don&#039;t care to change it, you can skip this step entirely :)&lt;br /&gt;
&lt;br /&gt;
The coinbase data always begins after exactly 42 bytes of the coinbase transaction.&lt;br /&gt;
The 42nd byte (that is, the byte immediately before the data) is the length of the data.&lt;br /&gt;
So when/if you want to add on to the coinbase data, simply insert it 42+DataLen bytes into the transaction, and increment the 42nd byte by the length of your inserted data.&lt;br /&gt;
&lt;br /&gt;
Python example:&lt;br /&gt;
 import binascii&lt;br /&gt;
 coinbase = binascii.a2b_hex(template[&#039;coinbasetxn&#039;][&#039;data&#039;])&lt;br /&gt;
 extradata = b&#039;my block&#039;&lt;br /&gt;
 # The following can be done better in both Python 2 and Python 3, but this way works with both&lt;br /&gt;
 origLen = ord(coinbase[41:42])&lt;br /&gt;
 newLen = origLen + len(extradata)&lt;br /&gt;
 coinbase = coinbase[0:41] + chr(newLen).encode(&#039;ascii&#039;) + coinbase[42:42 + origLen] + extradata + coinbase[42 + origLen:]&lt;br /&gt;
&lt;br /&gt;
======How to build merkle root======&lt;br /&gt;
Collect your coinbase transaction (modified or not) at the front of the &amp;quot;transactions&amp;quot; list provided by the server.&lt;br /&gt;
Apply a double-SHA256 hash to each transaction in the list.&lt;br /&gt;
&lt;br /&gt;
Now, as long as the list has more than 1 hash remaining, go through each pair and hash them together.&lt;br /&gt;
That is, concatenate the first two, double-SHA256 that, repeat for the next two, and so on.&lt;br /&gt;
If you encounter an odd pair (that is, the hash list ends with a single item and no pairing), concatenate it with itself and hash that.&lt;br /&gt;
Continue to do that until there is only one hash left:&lt;br /&gt;
that is your merkle root.&lt;br /&gt;
&lt;br /&gt;
Python example:&lt;br /&gt;
 import hashlib&lt;br /&gt;
 def dblsha(data):&lt;br /&gt;
  	return hashlib.sha256(hashlib.sha256(data).digest()).digest()&lt;br /&gt;
 &lt;br /&gt;
 txnlist = [coinbase] + [binascii.a2b_hex(a[&#039;data&#039;]) for a in template[&#039;transactions&#039;]]&lt;br /&gt;
 merklehashes = [dblsha(t) for t in txnlist]&lt;br /&gt;
 while len(merklehashes) &amp;gt; 1:&lt;br /&gt;
 	 if len(merklehashes) % 2:&lt;br /&gt;
 	  	merklehashes.append(merklehashes[-1])&lt;br /&gt;
  	merklehashes = [dblsha(merklehashes[i] + merklehashes[i + 1]) for i in range(0, len(merklehashes), 2)]&lt;br /&gt;
 merkleroot = merklehashes[0]&lt;br /&gt;
&lt;br /&gt;
======How to build block header======&lt;br /&gt;
Assemble the block header as laid out in the Bitcoin [[block hashing algorithm]], using the data provided in the block template along with your very own merkle root.&lt;br /&gt;
Note that miners are expected to check the &amp;quot;version&amp;quot; number, and should not create blocks with versions they do not understand unless the server instructs them to do so with the &amp;quot;version/force&amp;quot; or &amp;quot;version/reduce&amp;quot; mutations - you don&#039;t need to support those, but if you don&#039;t support the version the server has provided, understand that the server may reject submissions if they don&#039;t meet some unknown future rules.&lt;br /&gt;
As of this time, the current Bitcoin block version is 2, so it is safe to build blocks with version 1 or 2.&lt;br /&gt;
&lt;br /&gt;
Python example:&lt;br /&gt;
 import struct&lt;br /&gt;
 blkheader = struct.pack(&#039;&amp;lt;I&#039;, template[&#039;version&#039;]) + \&lt;br /&gt;
             binascii.a2b_hex(template[&#039;previousblockhash&#039;]) + \&lt;br /&gt;
             merkleroot + \&lt;br /&gt;
             struct.pack(&#039;&amp;lt;I&#039;, template[&#039;curtime&#039;]) + \&lt;br /&gt;
             binascii.a2b_hex(template[&#039;bits&#039;]) + \&lt;br /&gt;
             b&#039;NONC&#039;&lt;br /&gt;
&lt;br /&gt;
======While you&#039;re mining======&lt;br /&gt;
Since you&#039;re making all the blocks yourself, you really don&#039;t ever need to get another template... until it&#039;s invalid.&lt;br /&gt;
Generally, it&#039;s a good idea to refresh more often anyway to get more transactions, but it&#039;s better to let the pool decide when to do that, since it knows what&#039;s changed in the meantime already.&lt;br /&gt;
&lt;br /&gt;
If your template included a &amp;quot;longpollid&amp;quot; key, you can queue a request for a new template to be executed as soon as the pool decides you should change.&lt;br /&gt;
This is the same as any other template request, except that you include the &amp;quot;longpollid&amp;quot; parameter that the pool provided in your request.&lt;br /&gt;
If there is already a new template, the pool might respond immediately, so be sure not to depend on a delay!&lt;br /&gt;
&lt;br /&gt;
So to amend the template request we used initially, now we use:&lt;br /&gt;
 {&amp;quot;id&amp;quot;: 0, &amp;quot;method&amp;quot;: &amp;quot;getblocktemplate&amp;quot;, &amp;quot;params&amp;quot;: [{&lt;br /&gt;
     &amp;quot;capabilities&amp;quot;: [&amp;quot;coinbasetxn&amp;quot;, &amp;quot;workid&amp;quot;, &amp;quot;coinbase/append&amp;quot;],&lt;br /&gt;
     &amp;quot;longpollid&amp;quot;: &amp;quot;some gibberish&amp;quot;,&lt;br /&gt;
 }]}&lt;br /&gt;
&lt;br /&gt;
======Submitting shares======&lt;br /&gt;
When miner find the job which meets requested difficulty, it can submit the block to the server as a share:&lt;br /&gt;
 {&amp;quot;id&amp;quot;: 0, &amp;quot;method&amp;quot;: &amp;quot;submitblock&amp;quot;, &amp;quot;params&amp;quot;: [&lt;br /&gt;
      &amp;quot;020000003c48a294584f90e58325c60ca82896d071826b45680a661cec4d424d00000000&lt;br /&gt;
 de6433d46c0c7f50d84a05aec77be0199176cdd47f77e344b6f50c84380fddba66dc47501d00ff&lt;br /&gt;
 ff0000010001010000000100000000000000000000000000000000000000000000000000000000&lt;br /&gt;
 00000000ffffffff1302955d0f00456c6967697573005047dc66085fffffffff02fff1052a0100&lt;br /&gt;
 00001976a9144ebeb1cd26d6227635828d60d3e0ed7d0da248fb88ac01000000000000001976a9&lt;br /&gt;
 147c866aee1fa2f3b3d5effad576df3dbf1f07475588ac00000000&amp;quot;&lt;br /&gt;
 ]}&lt;br /&gt;
&lt;br /&gt;
To assemble the block data, simply concatenate your block header, number of transactions encoded in [[Protocol specification#Variable length integer|Bitcoin varint format]], followed by each of the transactions in your block (beginning with the coinbase).&lt;br /&gt;
If the server has listed &amp;quot;submit/coinbase&amp;quot; in its &amp;quot;mutable&amp;quot; key, you may opt to omit the transactions after the coinbase.&lt;br /&gt;
&lt;br /&gt;
Python example:&lt;br /&gt;
 def varintEncode(n):&lt;br /&gt;
   if n &amp;lt; 0xfd:&lt;br /&gt;
     return struct.pack(&#039;&amp;lt;B&#039;, n)&lt;br /&gt;
   # NOTE: Technically, there are more encodings for numbers bigger than&lt;br /&gt;
   # 16-bit, but transaction counts can&#039;t be that high with version 2 Bitcoin&lt;br /&gt;
   # blocks&lt;br /&gt;
   return b&#039;\xfd&#039; + struct.pack(&#039;&amp;lt;H&#039;, n)&lt;br /&gt;
 blkdata = blkheader + varintEncode(len(txnlist)) + coinbase&lt;br /&gt;
 if &#039;submit/coinbase&#039; not in template.get(&#039;mutable&#039;, ()):&lt;br /&gt;
   for txn in txnlist[1:]:&lt;br /&gt;
     blkdata += txn&lt;br /&gt;
&lt;br /&gt;
====Poolserver software====&lt;br /&gt;
Recommended standards to start with (in order of importance):&lt;br /&gt;
* [[BIP 0022|BIP 22 (non-optional sections)]]&lt;br /&gt;
* [[BIP 0022#Optional: Long Polling|BIP 22 Long Polling]]&lt;br /&gt;
* [[BIP 0023#Basic Pool Extensions|BIP 23 Basic Pool Extensions]]&lt;br /&gt;
* [[BIP 0023#Mutations|BIP 23 Mutation &amp;quot;coinbase/append&amp;quot;]] (required for miner extranonce rolling)&lt;br /&gt;
* [[BIP 0023#Submission Abbreviation|BIP 23 Submission Abbreviation &amp;quot;submit/coinbase&amp;quot;]]&lt;br /&gt;
* [[BIP 0023#Mutations|BIP 23 Mutation &amp;quot;time/increment&amp;quot;]] (be sure to specify &amp;quot;maxtime&amp;quot; or &amp;quot;maxtimeoff&amp;quot;!)&lt;br /&gt;
&lt;br /&gt;
==Technical specifications==&lt;br /&gt;
* [[BIP 0022|BIP 22: getblocktemplate - Fundamentals]]&lt;br /&gt;
* [[BIP 0023|BIP 23: getblocktemplate - Pooled Mining]]&lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
&lt;br /&gt;
Getblocktemplate&#039;s origins trace back to forrestv&#039;s getmemorypool JSON-RPC method for bitcoind.&lt;br /&gt;
He created it so that his pool ([[P2Pool]]) could piggy-back on bitcoind so as to avoid harming the Bitcoin network (up to this point, it mined only empty blocks that never confirmed transactions).&lt;br /&gt;
Having been fighting with scalability problems in pushpool/bitcoind for months on his pool ([[Eligius]]), Luke-Jr set out to implement a fast pool server using getmemorypool to do its own block production (this became [[Eloipool]], this first open source makes-its-own-blocks poolserver).&lt;br /&gt;
Other poolservers also implemented block creation using getmemorypool over the months following.&lt;br /&gt;
&lt;br /&gt;
At about the same time, interest in decentralizing pooled mining became a hot topic.&lt;br /&gt;
While [[BitPenny]] had initially released its own decentralized mining proxy months prior, [[P2Pool]]&#039;s implementation became rapidly popular.&lt;br /&gt;
Anyone involved in Bitcoin mining protocols could see the need to move control of block creation back into the hands of the miners.&lt;br /&gt;
Unfortunately, both [[BitPenny]] and [[P2Pool]] had used very pool-specific proprietary protocols to implement their decentralization.&lt;br /&gt;
On the other hand, getmemorypool was *almost* a perfect fit for the task - it just lacked support for the now-ubiquitous pooled mining extensions that had developed around the getwork protocol over time.&lt;br /&gt;
&lt;br /&gt;
In February of 2012, Luke implemented and [https://bitcointalk.org/?topic=23768.msg774497#msg774497 deployed] a first draft of getmemorypool mining support in [[Eloipool]] (and on [[Eligius]]) along with a proof-of-concept getwork proxy (now known as gmp-proxy), adding revisions as needed to function as a general-purpose decentralized mining protocol.&lt;br /&gt;
After he had confirmed it was working, he documented and proposed it on the [[Bitcoin development mailing list]] for review on February 28th, where discussion began on what was missing and what needed to be changed or clarified.&lt;br /&gt;
During the following few months, a number of others, both developers and testers, provided constructive criticism and suggestions, which were integrated into the standard.&lt;br /&gt;
Luke also actively encouraged participation in the development of the standard among pool operators and poolserver authors, especially as it became necessary to move forward into the ASIC &amp;quot;mining generation&amp;quot;.&lt;br /&gt;
Eventually, it was decided it would be best to rename it to the more appropriate &amp;quot;getblocktemplate&amp;quot; name and drop backward compatibility with getmemorypool for simplicity.&lt;br /&gt;
The standard was split into two pieces and the technical specification can be found in [[BIP 0022|BIP 22]] and [[BIP 0023|BIP 23]].&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
* [https://bitcointalk.org/?topic=108854 BitcoinTalk forum thread]&lt;br /&gt;
* [[BIP 0022|BIP 22: getblocktemplate - Fundamentals]] (technical)&lt;br /&gt;
* [[BIP 0023|BIP 23: getblocktemplate - Pooled Mining]] (technical)&lt;/div&gt;</summary>
		<author><name>Pagent</name></author>
	</entry>
</feed>