I downloaded the required data and made the coinbase. Then I calculated the merkle root… However I all the time get an error message after 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:

    world merkleroot,txnlist
    txnlist=[coinbase]+[unhexlify(a['data']) for a in tmpl['transactions']]
    merklehashes=[dblsha(t)[::-1] for t in txnlist]
    whereas len(merklehashes)>1:
        if len(merklehashes)%2:
            merklehashes.append(merklehashes[-1])
        merklehashes=[dblsha(merklehashes[i]+merklehashes[i+1]) for i in vary(0,len(merklehashes),2)]
    merkleroot=merklehashes[0][::-1]
  1. I reversed the information of all transactions(embody coinbase)
  2. I calculated the hashes(bytes)
  3. I calculated the merkleroot(bytes)
  4. I reversed the merkleroot
  5. I transformed the merkleroot(bytes) to hex
    However all the time get an error: ‘bad-txnmerkleroot’, no matter whether or not I inverted it or not, or what number of occasions. Do I really want to reverse the information through the calculation? By the way in which, I examined the code in regtest. Ought to I take a look at the code on the primary community? Are there different causes for the ‘bad-txnmerkle’ error? Just like the improper coinbase. I am actually at my wits’ finish.

Jack Inexperienced is a brand new contributor to this web site. Take care in asking for clarification, commenting, and answering.
Try our Code of Conduct.

1