Quantcast
Channel: Active questions tagged merkle-tree - Bitcoin Stack Exchange
Viewing all articles
Browse latest Browse all 94

How to calculate Merkle Root? Need to reverse?

$
0
0

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 hexBut 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.

Viewing all articles
Browse latest Browse all 94

Trending Articles