<blockquote>
<p>You should not need to reverse anything; hashes are never reversed in Bitcoin, except for human display purposes. So if you start from txids you get from an explorer or RPC output, then you should reverse those (to unreverse them), but other than that, none are used.</p>
</blockquote>
<hr />
<p><sub><a href="https://bitcoin.meta.stackexchange.com/a/839/13866">About stealing comments and posting them as answers</a></sub></p>

CC BY-SA 4.0
2023-12-11T16:41:06.510Z
I tried to cancel all inversions, but still got the error "ProcessNewBlock: AcceptBlock FAILED (bad-txnmrklroot, hashMerkleRoot mismatch)". I'm getting crazy with this question.
Jack Green
CC BY-SA 4.0
2023-12-11T17:03:20.947Z
Can update your question to reflect your updated code, specifically how are you submitting the result to `bitcoind`?
Pieter Wuille
CC BY-SA 4.0
2023-12-11T15:34:54.297Z
2023-12-11T15:34:54.297Z
RedGrittyBrick
<p>Don't know if you stll need this info, but:</p>
<p>If you get tx.ids from the RPC (node) than you have to reverse those.
When you create the coinbase and double sha256 it you MUST NOT reverse it.</p>
<p>So, when you have the hash256 (double sha256) of the coinbase and you reversed the tx.ids you get from RPC, you can calculate the merkle root.</p>
<p>Same result is done by creating merkle branches: when you reversed the tx.ids got from the node with RPC, you can create merkle branches (aka steps in some sources) and put the coinbase hash256 as the first input when calculate the merkle root (in some sources this the function is called &quot;with_first(coinbase, steps)&quot;). This will recreate the same merkle root as it would be generated by starting with leaves (TXIDs hash256).</p>

CC BY-SA 4.0
2024-04-19T08:15:46.990Z
2024-04-19T08:17:10.947Z
2024-04-19T08:17:10.947Z
4ss0
<p>I downloaded the necessary information and made the coinbase. Then I calculated the merkle root... But I always get an error message when I submit a block: &quot;bad-txnmerkleroot&quot;. Tere is my code:</p>
<pre><code>def dblsha(d)-&gt;bytes:

    return hashlib.sha256(hashlib.sha256(d).digest()).digest()


def makeMerkleRoot()-&gt;None:

    global merkleroot,txnlist
    txnlist=[coinbase]+[unhexlify(a['data']) for a in tmpl['transactions']]
    merklehashes=[dblsha(t)[::-1] for t in txnlist]
    while len(merklehashes)&gt;1:
        if len(merklehashes)%2:
            merklehashes.append(merklehashes[-1])
        merklehashes=[dblsha(merklehashes[i]+merklehashes[i+1]) for i in range(0,len(merklehashes),2)]
    merkleroot=merklehashes[0][::-1]
</code></pre>
<ol>
<li>I reversed the data of all transactions(include coinbase)</li>
<li>I calculated the hashes(bytes)</li>
<li>I calculated the merkleroot(bytes)</li>
<li>I reversed the merkleroot</li>
<li>I converted the merkleroot(bytes) to hex
But always get an error: 'bad-txnmerkleroot', regardless of whether I inverted it or not, or how many times. Do I really need to reverse the data during the calculation? By the way, I tested the code in regtest. Should I test the code on the main network? Are there other reasons for the 'bad-txnmerkle' error? Like the wrong coinbase. I'm really at my wits' end.</li>
</ol>

<hash><coinbase-transaction><merkle-tree>
How to calculate Merkle Root? Need to reverse?
Jack Green
# How to calculate Merkle Root? Need to reverse?

(Asked by: username_0 on 2023-12-11)

I downloaded the necessary information and made the coinbase. Then I calculated the merkle root... But I always get an error message when I submit a block: "bad-txnmerkleroot". Tere is my code:

```
def dblsha(d)->bytes:

    return hashlib.sha256(hashlib.sha256(d).digest()).digest()

def makeMerkleRoot()->None:

    global merkleroot,txnlist
    txnlist=[coinbase]+[unhexlify(a['data']) for a in tmpl['transactions']]
    merklehashes=[dblsha(t)[::-1] for t in txnlist]
    while len(merklehashes)>1:
        if len(merklehashes)%2:
            merklehashes.append(merklehashes[-1])
        merklehashes=[dblsha(merklehashes[i]+merklehashes[i+1]) for i in range(0,len(merklehashes),2)]
    merkleroot=merklehashes[0][::-1]

```

1. I reversed the data of all transactions(include coinbase)
2. I calculated the hashes(bytes)
3. I calculated the merkleroot(bytes)
4. I reversed the merkleroot
5. I converted the merkleroot(bytes) to hex
   But always get an error: 'bad-txnmerkleroot', regardless of whether I inverted it or not, or how many times. Do I really need to reverse the data during the calculation? By the way, I tested the code in regtest. Should I test the code on the main network? Are there other reasons for the 'bad-txnmerkle' error? Like the wrong coinbase. I'm really at my wits' end.

**Tags:** hash, coinbase-transaction, merkle-tree


---

## 2 answers

### Answer by username_1 on 2023-12-11. Score: 0

> You should not need to reverse anything; hashes are never reversed in Bitcoin, except for human display purposes. So if you start from txids you get from an explorer or RPC output, then you should reverse those (to unreverse them), but other than that, none are used.

---

[About stealing comments and posting them as answers](https://bitcoin.meta.stackexchange.com/a/839/13866)

> **Comments:**
> **username_0**: I tried to cancel all inversions, but still got the error "ProcessNewBlock: AcceptBlock FAILED (bad-txnmrklroot, hashMerkleRoot mismatch)". I'm getting crazy with this question.  
> **username_2**: Can update your question to reflect your updated code, specifically how are you submitting the result to `bitcoind`?  

---

### Answer by username_3 on 2024-04-19. Score: 0

Don't know if you stll need this info, but:

If you get tx.ids from the RPC (node) than you have to reverse those.
When you create the coinbase and double sha256 it you MUST NOT reverse it.

So, when you have the hash256 (double sha256) of the coinbase and you reversed the tx.ids you get from RPC, you can calculate the merkle root.

Same result is done by creating merkle branches: when you reversed the tx.ids got from the node with RPC, you can create merkle branches (aka steps in some sources) and put the coinbase hash256 as the first input when calculate the merkle root (in some sources this the function is called "with\_first(coinbase, steps)"). This will recreate the same merkle root as it would be generated by starting with leaves (TXIDs hash256).


---

