2

Based on https://en.bitcoin.it/wiki/Getblocktemplate :

Collect your coinbase transaction (modified or not) at the front of the "transactions" list provided by the server. Apply a double-SHA256 hash to each transaction in the list. Now, as long as the list has more than 1 hash remaining, go through each pair and hash them together. That is, concatenate the first two, double-SHA256 that, repeat for the next two, and so on. 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. Continue to do that until there is only one hash left: that is your merkle root.

But what if the block is empty? Since there is no other transaction other than the coinbase transaction, it means there is only one hash.

Is the merkle root of an empty block the double hash of the coinbase transaction? Or do I still need to do something other than double hashing the coinbase?

Murch
  • 75,206
  • 34
  • 186
  • 622
user6668201
  • 77
  • 10

1 Answers1

3

If the coinbase transaction is the only transaction in a block, then the block's transaction Merkle root is equal to the coinbase transaction's txid.

Pieter Wuille
  • 105,497
  • 9
  • 194
  • 308
  • what is txid ? i cant find it on the getblocktemplate wiki . And do i need to hash the coinbase transaction txid or just straight up merkleroot = txid without hash ? – user6668201 Sep 27 '21 at 14:20
  • The txid is the double-SHA256 hash of the serialized transaction. The merkle root is identical to the txid, without any further hashing. – Pieter Wuille Sep 27 '21 at 14:38
  • Oh thank you ! but i found this on google https://bitcoin.stackexchange.com/questions/97162/merkle-root-with-one-transaction , it says merkle root is equal to the double sha256 of txid0 + txid0 , which one is correct , your answer or the link i just give ? i'm confused – user6668201 Sep 27 '21 at 14:40
  • For the third time: the Merkle root IS the txid, without further hashing. Your link is about the case where you have an odd number of elements in a level, but larger than 1, in order to compute the next level. When a level has only 1 element left, that is the Merkle root. If the coinbase is the only transaction, you already reached the end. – Pieter Wuille Sep 27 '21 at 14:42
  • So Merkle root = sha256d(txid0 + txid0) is incorrect ? – user6668201 Sep 27 '21 at 14:43
  • No, it is not incorrect, but it doesn't apply here. That is for computing the next level of the Merkle tree when there is an odd number of elements. In your case, there is no next level as you're already done. – Pieter Wuille Sep 27 '21 at 14:44
  • I'm not trolling , that's literally the accepted answer on other question titled "Merkle root with one transaction" , i'm sorry forgive me i guess i got too confused – user6668201 Sep 27 '21 at 14:45
  • Oh ! that clear it all up ! thank you ! – user6668201 Sep 27 '21 at 14:46
  • My apologies, I reacted too quickly. That answer is indeed wrong; it only applies when there is an odd number of elements, but more than 1. – Pieter Wuille Sep 27 '21 at 14:46