How to calculate Merkle Root? Need to reverse?

How to calculate Merkle Root? Need to reverse?

Manage alerts

Loading saved threads...

Jack Green · External communityPost link
External question — Bitcoin Stack Exchange Author: Jack Green Original post: https://bitcoin.stackexchange.com/questions/120889 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. 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 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.
Quote
Report
RedGrittyBrick · External communityPost link
External answer — Bitcoin Stack Exchange Author: RedGrittyBrick Original post: https://bitcoin.stackexchange.com/a/120894 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. 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
Quote
Report
4ss0 · External communityPost link
External answer — Bitcoin Stack Exchange Author: 4ss0 Original post: https://bitcoin.stackexchange.com/a/122706 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. 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).
Quote
Report

Post Reply

Quoted from Forex.com.bd-Editorial External question — Bitcoin Stack Exchange Author: Jack Green Source score (net votes, not local likes): 0 Original post: https://bitcoin.stackexchange.com/questions/120889 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. 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 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.

Cancel quote

Checking account access…