Post
Topic
Board Development & Technical Discussion
Re: Bitcoin Mining Technical Details. Help
by
xDilettante
on 13/10/2023, 12:57:30 UTC
Could you please tell me what exactly I'm doing wrong with getting the Merkle root? Here is my Python code, but I am not getting the result I need.

Target: "merkleroot": "19f3769778eb2019476630aafb376634cdf3f114e617a7e0a300e822265022bc"

Code:
if __name__ == "__main__":
    res = request_rpc(method="getblock", params=["00000000000000000000bd684e36789d16da2cf5b08b47deb72d1448a73eae5c"])
    tx = res.json()["result"]["tx"]
    print(tx)

    hashes = []
    for i in range(len(tx)):
        res = request_rpc(method="getrawtransaction",
                          params=[tx[i], 1, '00000000000000000000bd684e36789d16da2cf5b08b47deb72d1448a73eae5c'])
        hashes.append(res.json()["result"]["hash"])
    print(len(hashes))
    # print(hashes)

    hashes = [bytes.fromhex(x) for x in hashes]
    while len(hashes) > 1:
        if len(hashes) % 2 == 1:
            hashes.append(hashes[-1])
        parent_level = []
        for x in range(0, len(hashes), 2):
            hash = sha256(hashes[x] + hashes[x + 1]).digest()
            parent_level.append(hash)
        hashes = parent_level

    print(hashes[0].hex())