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]
- I reversed the data of all transactions(include coinbase)
- I calculated the hashes(bytes)
- I calculated the merkleroot(bytes)
- I reversed the merkleroot
- 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.