<blockquote>
  <p>You make your list of transaction hashes(without the coinbase transaction, as you haven't mined the block), </p>
</blockquote>

<p>This is where the problem in your understanding lies. The coinbase transaction is generated before the block is mined, not afterwards, because it must be included in the list of transactions just like all the others. It is the first transaction in the block, and its hash is used in exactly the same way as all the others when constructing the merkle root.</p>

<p>Once the coinbase transaction is created and the merkle root calculated, <em>then</em> the nonce is varied and the block is mined.</p>

<blockquote>
  <p>Does a1+b1 mean that you perform addition of the hash values or that you just append the two hashes together?</p>
</blockquote>

<p>Append the hashes, not add them</p>

CC BY-SA 3.0
2018-03-02T17:10:47.977Z
This is correct. Benefits of this approach includes that the coinbase transaction cannot be altered after a block with a "correct" blockheaderhash is submitted, which for example could manipulate the mining reward the miner receives. Another benefit is that this allows the miner to add an extra nonce inside the coinbase transaction. This is very beneficial as the possible different combinations of values in the 4 byte nonce in the blockheader is too small for modern miners which exhaust all different combinations in less than one second (and the timestamp field in the blockheader changes).
user44404
CC BY-SA 3.0
2018-03-02T16:49:44.447Z
2018-03-02T16:49:44.447Z
meshcollider
<p>first time posting here.</p>

<p>I was interested on how mining software works down to the bytes. I came across this well written article right here:</p>

<p><a href="http://www.righto.com/2014/02/bitcoin-mining-hard-way-algorithms.html" rel="nofollow noreferrer">http://www.righto.com/2014/02/bitcoin-mining-hard-way-algorithms.html</a></p>

<p>Under "Notes and References", he provides a python program that generates the merkle root from a list of transaction hashes. What I don't understand however is, why or how the hash of the coinbase transaction is included in that list.</p>

<p>Lets say you are in the process of mining a new block. You make your list of transaction hashes(without the coinbase transaction, as you haven't mined the block), create the merkle root and guess the nonce until you create a block. Now the coinbase transaction takes place and you get your bitcoins, the coinbase transaction hash now has to be included in the list of transaction hashes. But that changes the merkle root and thus the entire block, which would make it unvalid.</p>

<p>How is this solved? Do you know the hash of the coinbase transaction in advance? Or do you not include the coinbase transaction for the new block and the python code is not correct?</p>

<p>Also a side question is, in the python code this part confuses me a bit:</p>

<pre><code> h = hashlib.sha256(hashlib.sha256(a1+b1).digest()).digest()
</code></pre>

<p>Does a1+b1 mean that you perform addition of the hash values or that you just append the two hashes together?</p>

<p>Thanks in advance,</p>

<p>Fihdi</p>

<mining-theory><hash><merkle-tree>
Is the hash of the coinbase transaction needed for the merkle root?
Fihdi
# Is the hash of the coinbase transaction needed for the merkle root?

(Asked by: username_0 on 2018-03-02)

first time posting here.

I was interested on how mining software works down to the bytes. I came across this well written article right here:

<http://www.righto.com/2014/02/bitcoin-mining-hard-way-algorithms.html>

Under "Notes and References", he provides a python program that generates the merkle root from a list of transaction hashes. What I don't understand however is, why or how the hash of the coinbase transaction is included in that list.

Lets say you are in the process of mining a new block. You make your list of transaction hashes(without the coinbase transaction, as you haven't mined the block), create the merkle root and guess the nonce until you create a block. Now the coinbase transaction takes place and you get your bitcoins, the coinbase transaction hash now has to be included in the list of transaction hashes. But that changes the merkle root and thus the entire block, which would make it unvalid.

How is this solved? Do you know the hash of the coinbase transaction in advance? Or do you not include the coinbase transaction for the new block and the python code is not correct?

Also a side question is, in the python code this part confuses me a bit:

```
 h = hashlib.sha256(hashlib.sha256(a1+b1).digest()).digest()

```

Does a1+b1 mean that you perform addition of the hash values or that you just append the two hashes together?

Thanks in advance,

username_0

**Tags:** mining-theory, hash, merkle-tree


---

## 1 answers

### Answer by username_1 on 2018-03-02. Score: 4

> You make your list of transaction hashes(without the coinbase transaction, as you haven't mined the block),

This is where the problem in your understanding lies. The coinbase transaction is generated before the block is mined, not afterwards, because it must be included in the list of transactions just like all the others. It is the first transaction in the block, and its hash is used in exactly the same way as all the others when constructing the merkle root.

Once the coinbase transaction is created and the merkle root calculated, *then* the nonce is varied and the block is mined.

> Does a1+b1 mean that you perform addition of the hash values or that you just append the two hashes together?

Append the hashes, not add them

> **Comments:**
> **username_2**: This is correct. Benefits of this approach includes that the coinbase transaction cannot be altered after a block with a "correct" blockheaderhash is submitted, which for example could manipulate the mining reward the miner receives. Another benefit is that this allows the miner to add an extra nonce inside the coinbase transaction. This is very beneficial as the possible different combinations of values in the 4 byte nonce in the blockheader is too small for modern miners which exhaust all different combinations in less than one second (and the timestamp field in the blockheader changes).  

---

