# Is the hash of the coinbase transaction needed for the merkle root?

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?

Fihdi

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

• 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). – Viktor T. Mar 2 '18 at 17:10