<p>The stratum protocol gives you some hashes of specific branches in the merkle tree. They may not be at the same depth in the tree (and I don't think they are). They are only provided so that the miner can change the coinbase transaction by incrementing the extranonce and then incorporating it into the merkle root. The Code 1 merkle root should be the correct one since that is the method that the Stratum protocol docs tells you to do. The other code is for creating the merkle root from a list of transactions, not a list of branch hashes as Stratum provides.</p>

CC BY-SA 3.0
2017-09-20T21:45:57.810Z
So the merkle_root obtained from Code1 "7cbd55a748aa8e778e0795e33d5bd5a07c9f465cd3de1fef3bf36ea3ac4d56f9" will be used directly in Block Header or it is in display format same as https://blockchain.info/ and i will need to reverse the bytes to put it in Block Header.
Santan
CC BY-SA 3.0
2017-09-21T04:09:19.673Z
It should be usable directly in the block header.
Ava Chow
CC BY-SA 3.0
2017-09-20T19:48:21.660Z
2017-09-20T19:48:21.660Z
Ava Chow
<p>While trying to calculate the <strong>merkle root</strong> in python from the data i got from the mining pool.... i found there are 2 different codes.</p>

<p><strong>code 1:</strong> </p>

<pre><code>def build_merkle_root(coinbase_hash_bin, merkle_branch):
    merkle_root = coinbase_hash_bin
    for h in merkle_branch:
        merkle_root = hashlib.sha256(hashlib.sha256(merkle_root + binascii.unhexlify(h)).digest()).digest()
    return binascii.hexlify(merkle_root).decode('utf-8')
</code></pre>

<p>This is explained in shlushpool stratum document <a href="https://slushpool.com/help/manual/stratum-protocol" rel="nofollow noreferrer"><strong>here</strong></a> and also in an altcoin sia mining protocol <a href="https://siamining.com/stratum" rel="nofollow noreferrer"><strong>here</strong></a> . <strong>So i think it should be correct.</strong>
But there is another piece of code below, </p>

<p><strong>code 2:</strong></p>

<pre><code>def build_merkle_root(hash_list):
if len(hash_list) &lt; 2:
    return hash_list[0]
new_hash_list = []

# Process pairs. For odd length, the last is skipped
for i in range(0, len(hash_list) - 1, 2):
    new_hash_list.append(hash2(hash_list[i], hash_list[i + 1]))

# odd, hash last item twice
if len(hash_list) % 2 == 1:
    new_hash_list.append(hash2(hash_list[-1], hash_list[-1]))

return build_merkle_root(new_hash_list)
#==========================================================================
def hash2(a, b):
     # Reverse inputs before and after hashing due to big-endian / little-endian nonsense
     a1 = binascii.unhexlify(a)[::-1]
     b1 = binascii.unhexlify(b)[::-1]
     h = hashlib.sha256(hashlib.sha256(a1 + b1).digest()).digest()

     return binascii.hexlify(h[::-1])
</code></pre>

<p><strong>This one looks more logical</strong> from the merkle tree structure point of view which is hashing each pair separately.</p>

<p>For testing them, Taking below as an example of the job obtained from a pool....</p>

<pre><code>[{'id': None,
  'method': 'mining.notify',
  'params': ['59bc8dfc00003829',
   'ceeefd1381f491d9a0ba90b26a495a225e8bfc9f00ecc4250000000000000000',
   '01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff3503226b07000455abc259040bf12d090c',
   '0a636b706f6f6c112f736f6c6f2e636b706f6f6c2e6f72672fffffffff035976a953000000001976a914fc6123f4bfd3a840b4387ab90e9801e98fb17cf888ac8756d800000000001976a914f4cbe6c6bb3a8535c963169c22963d3a20e7686988ac0000000000000000266a24aa21a9edc963e012cb380138e3d16283a094454782879557714d3b0d73fecfe612d01f0600000000',
   ['b32abe89497354ceaa2dff35cc41995d58c19893d517e92e30b4ead75a66970c',
    'ca93a597a29860178f4cd01326f43b080f516f3f81ba6b86b0ff35f5cb206b7f',
    '213f7e41a5b228ec554c1a2e3fe18b1f3e1c23821b1b2b3aa1319d3d616044ca',
    '0941052c2e5864e57a18bfc82d1e7ab15740d4ed7b852cda92bffd3ff622e59d',
    '7ad5af30a3daa89c1244016227cde139d5d54232a673cbf7954f6a1aa26bbc05',
    'e24a96489d5782c6bcde209952a12826cd225b97b9a3ad3142c87dd062136d0b',
    '383a433dc503c8ab4b7beb9dc4a3e07811a1cb9681e2da019cec16966fc727be',
    'f10804628f630bcead6cd1b667d4352cdfb93aea382662456c2caadda3e1b0c9',
    '03e9bf5b6053fe86d272dac8c2a32f08ace6d045799ae499ed7087af21c737c6',
    '3cf338c75d7ef47b0c029d16b5b02340a62ce0a7846910175b021d919dcccc6f',
    '40319623d80cae8a5b1a3e319a962110a08ff40daf40ebf421b9454f20e54dcc',
    'c29e3826153dfc1d10e9e173281d21ad2103bba86bc2930440e7258afca2b957'],
   '20000000',
   '1800ff18',
   '59c2ab55',
   False]}]
</code></pre>

<p>Code 1 gives me merkle root as <code>7cbd55a748aa8e778e0795e33d5bd5a07c9f465cd3de1fef3bf36ea3ac4d56f9</code> while
 Code 2 gives as <code>6681ae1409685d71a51ced80ae0c7eacb78d5facb401ece2287543b6cc17e4cf</code>
Since both are not same, <strong>so which one is wrong</strong> ? I needed merkle root for block header.</p>

<p>In both the above cases, I took extranonce2 all zeros as <code>0000000000000000</code> and build coinbase as <code>01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff3503226b07000455abc259040bf12d090cff47c35900000000000000000a636b706f6f6c112f736f6c6f2e636b706f6f6c2e6f72672fffffffff035976a953000000001976a914fc6123f4bfd3a840b4387ab90e9801e98fb17cf888ac8756d800000000001976a914f4cbe6c6bb3a8535c963169c22963d3a20e7686988ac0000000000000000266a24aa21a9edc963e012cb380138e3d16283a094454782879557714d3b0d73fecfe612d01f0600000000</code> which gives a coinbase hash of <code>20d0b9a72f1e940e29b064e428f5e037f948c9ede27dfb7e77b1df7d289d1861</code></p>

<mining-pools><python><block-header><merkle-tree><block-hash>
merkle root 2 codes
Santan
# merkle root 2 codes

(Asked by: username_0 on 2017-09-20)

While trying to calculate the merkle root in python from the data i got from the mining pool.... i found there are 2 different codes.
code 1: 
def build_merkle_root(coinbase_hash_bin, merkle_branch):
    merkle_root = coinbase_hash_bin
    for h in merkle_branch:
        merkle_root = hashlib.sha256(hashlib.sha256(merkle_root + binascii.unhexlify(h)).digest()).digest()
    return binascii.hexlify(merkle_root).decode('utf-8')

This is explained in shlushpool stratum document here and also in an altcoin sia mining protocol here . So i think it should be correct.
But there is another piece of code below, 
code 2:
def build_merkle_root(hash_list):
if len(hash_list) < 2:
    return hash_list[0]
new_hash_list = []

# Process pairs. For odd length, the last is skipped
for i in range(0, len(hash_list) - 1, 2):
    new_hash_list.append(hash2(hash_list[i], hash_list[i + 1]))

# odd, hash last item twice
if len(hash_list) % 2 == 1:
    new_hash_list.append(hash2(hash_list[-1], hash_list[-1]))

return build_merkle_root(new_hash_list)
#==========================================================================
def hash2(a, b):
     # Reverse inputs before and after hashing due to big-endian / little-endian nonsense
     a1 = binascii.unhexlify(a)[::-1]
     b1 = binascii.unhexlify(b)[::-1]
     h = hashlib.sha256(hashlib.sha256(a1 + b1).digest()).digest()

     return binascii.hexlify(h[::-1])

This one looks more logical from the merkle tree structure point of view which is hashing each pair separately.
For testing them, Taking below as an example of the job obtained from a pool....
[{'id': None,
  'method': 'mining.notify',
  'params': ['59bc8dfc00003829',
   'ceeefd1381f491d9a0ba90b26a495a225e8bfc9f00ecc4250000000000000000',
   '01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff3503226b07000455abc259040bf12d090c',
   '0a636b706f6f6c112f736f6c6f2e636b706f6f6c2e6f72672fffffffff035976a953000000001976a914fc6123f4bfd3a840b4387ab90e9801e98fb17cf888ac8756d800000000001976a914f4cbe6c6bb3a8535c963169c22963d3a20e7686988ac0000000000000000266a24aa21a9edc963e012cb380138e3d16283a094454782879557714d3b0d73fecfe612d01f0600000000',
   ['b32abe89497354ceaa2dff35cc41995d58c19893d517e92e30b4ead75a66970c',
    'ca93a597a29860178f4cd01326f43b080f516f3f81ba6b86b0ff35f5cb206b7f',
    '213f7e41a5b228ec554c1a2e3fe18b1f3e1c23821b1b2b3aa1319d3d616044ca',
    '0941052c2e5864e57a18bfc82d1e7ab15740d4ed7b852cda92bffd3ff622e59d',
    '7ad5af30a3daa89c1244016227cde139d5d54232a673cbf7954f6a1aa26bbc05',
    'e24a96489d5782c6bcde209952a12826cd225b97b9a3ad3142c87dd062136d0b',
    '383a433dc503c8ab4b7beb9dc4a3e07811a1cb9681e2da019cec16966fc727be',
    'f10804628f630bcead6cd1b667d4352cdfb93aea382662456c2caadda3e1b0c9',
    '03e9bf5b6053fe86d272dac8c2a32f08ace6d045799ae499ed7087af21c737c6',
    '3cf338c75d7ef47b0c029d16b5b02340a62ce0a7846910175b021d919dcccc6f',
    '40319623d80cae8a5b1a3e319a962110a08ff40daf40ebf421b9454f20e54dcc',
    'c29e3826153dfc1d10e9e173281d21ad2103bba86bc2930440e7258afca2b957'],
   '20000000',
   '1800ff18',
   '59c2ab55',
   False]}]

Code 1 gives me merkle root as 7cbd55a748aa8e778e0795e33d5bd5a07c9f465cd3de1fef3bf36ea3ac4d56f9 while
 Code 2 gives as 6681ae1409685d71a51ced80ae0c7eacb78d5facb401ece2287543b6cc17e4cf
Since both are not same, so which one is wrong ? I needed merkle root for block header.
In both the above cases, I took extranonce2 all zeros as 0000000000000000 and build coinbase as 01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff3503226b07000455abc259040bf12d090cff47c35900000000000000000a636b706f6f6c112f736f6c6f2e636b706f6f6c2e6f72672fffffffff035976a953000000001976a914fc6123f4bfd3a840b4387ab90e9801e98fb17cf888ac8756d800000000001976a914f4cbe6c6bb3a8535c963169c22963d3a20e7686988ac0000000000000000266a24aa21a9edc963e012cb380138e3d16283a094454782879557714d3b0d73fecfe612d01f0600000000 which gives a coinbase hash of 20d0b9a72f1e940e29b064e428f5e037f948c9ede27dfb7e77b1df7d289d1861

**Tags:** mining-pools, python, block-header, merkle-tree, block-hash


---

## 1 answers

### Answer by username_1 on 2017-09-20. Score: 0 | Accepted answer

The stratum protocol gives you some hashes of specific branches in the merkle tree. They may not be at the same depth in the tree (and I don't think they are). They are only provided so that the miner can change the coinbase transaction by incrementing the extranonce and then incorporating it into the merkle root. The Code 1 merkle root should be the correct one since that is the method that the Stratum protocol docs tells you to do. The other code is for creating the merkle root from a list of transactions, not a list of branch hashes as Stratum provides.

> **Comments:**
> **username_0**: So the merkle_root obtained from Code1 "7cbd55a748aa8e778e0795e33d5bd5a07c9f465cd3de1fef3bf36ea3ac4d56f9" will be used directly in Block Header or it is in display format same as https://blockchain.info/ and i will need to reverse the bytes to put it in Block Header.  
> **username_1**: It should be usable directly in the block header.  

---

