<p>The Stratum server is delivering a partially hashed merkle branch in the response… for which the miner needs to provide, ‘A’ (the generation/coinbase transaction) concatenated with the ‘first’ transaction (B). The remaining elements of the array are ‘pre-computed’ portions of the merkle tree. As indicated on slush’s bitcoin.cz site (<a href="http://mining.bitcoin.cz/stratum-mining" rel="noreferrer">http://mining.bitcoin.cz/stratum-mining</a>) “merkle_branch - List of hashes, will be used for calculation of merkle root. This is not a list of all transactions, it only contains prepared hashes of steps of merkle tree algorithm.”</p>

<p>The items returned by the Stratum server for the merkle branch are essentially [b,h(c,d)…] pre-computed to reduce the number of calculations required later to complete the merkle root.</p>

CC BY-SA 3.0
2014-01-21T06:20:01.640Z
2014-01-21T06:20:01.640Z
user12503
<p>From page <a href="https://en.bitcoin.it/wiki/Protocol_specification" rel="nofollow">https://en.bitcoin.it/wiki/Protocol_specification</a> , I understand the merkle tree works like</p>

<blockquote>
  <p>merkle([a,b,c,d]) =  h( h(a,b), h(c,d) )</p>
</blockquote>

<p>But when I do the stratum protocol <a href="http://mining.bitcoin.cz/stratum-mining/" rel="nofollow">http://mining.bitcoin.cz/stratum-mining/</a> , it works something like this </p>

<blockquote>
  <p>merkle([a,b,c,d]) = h(h(h(a,b),c),d)</p>
</blockquote>

<p>And here is the code</p>

<pre><code>def build_merkle_root(self, merkle_branch, coinbase_hash_bin):
    merkle_root = coinbase_hash_bin
    for h in self.merkle_branch:
        merkle_root = doublesha(merkle_root + binascii.unhexlify(h))
    return binascii.hexlify(merkle_root)
</code></pre>

<p>Do I miss anything?</p>

<merkle-tree>
Get confused on the merkle tree algorithm
user11567
# Get confused on the merkle tree algorithm

(Asked by: username_0 on 2013-12-30)

From page <https://en.bitcoin.it/wiki/Protocol_specification> , I understand the merkle tree works like

> merkle([a,b,c,d]) = h( h(a,b), h(c,d) )

But when I do the stratum protocol <http://mining.bitcoin.cz/stratum-mining/> , it works something like this

> merkle([a,b,c,d]) = h(h(h(a,b),c),d)

And here is the code

```
def build_merkle_root(self, merkle_branch, coinbase_hash_bin):
    merkle_root = coinbase_hash_bin
    for h in self.merkle_branch:
        merkle_root = doublesha(merkle_root + binascii.unhexlify(h))
    return binascii.hexlify(merkle_root)

```

Do I miss anything?

**Tags:** merkle-tree


---

## 1 answers

### Answer by username_1 on 2014-01-21. Score: 5 | Accepted answer

The Stratum server is delivering a partially hashed merkle branch in the response… for which the miner needs to provide, ‘A’ (the generation/coinbase transaction) concatenated with the ‘first’ transaction (B). The remaining elements of the array are ‘pre-computed’ portions of the merkle tree. As indicated on slush’s bitcoin.cz site (<http://mining.bitcoin.cz/stratum-mining>) “merkle\_branch - List of hashes, will be used for calculation of merkle root. This is not a list of all transactions, it only contains prepared hashes of steps of merkle tree algorithm.”

The items returned by the Stratum server for the merkle branch are essentially [b,h(c,d)…] pre-computed to reduce the number of calculations required later to complete the merkle root.


---

