def fixup_merkleblock(d, f):
    def recurse(level_widths, level_index, node_index, hashes, flags, flag_index, tx_acc):
        idx, r = divmod(flag_index, 8)
        mask = (1 << r)
        flag_index += 1
        if flags[idx] & mask == 0:
            h = hashes.pop()
            return h, flag_index

        if level_index == len(level_widths) - 1:
            h = hashes.pop()
            tx_acc.append(h)
            return h, flag_index

        # traverse the left
        left_hash, flag_index = recurse(
            level_widths, level_index+1, node_index*2, hashes, flags, flag_index, tx_acc)

        # is there a right?
        if node_index*2+1 < level_widths[level_index+1]:
            right_hash, flag_index = recurse(
                level_widths, level_index+1, node_index*2+1, hashes, flags, flag_index, tx_acc)

            if left_hash == right_hash:
                raise ValueError("merkle hash has same left and right value at node %d" % node_index)
        else:
            right_hash = left_hash

        return double_sha256(left_hash + right_hash), flag_index

    level_widths = []
    count = d["total_transactions"]
    while count > 1:
        level_widths.append(count)
        count += 1
        count //= 2
    level_widths.append(1)
    level_widths.reverse()

    tx_acc = []
    flags = d["flags"]
    hashes = list(reversed(d["hashes"]))
    left_hash, flag_index = recurse(level_widths, 0, 0, hashes, flags, 0, tx_acc)

    if len(hashes) > 0:
        raise ValueError("extra hashes: %s" % hashes)

    idx, r = divmod(flag_index-1, 8)
    if idx != len(flags) - 1:
        raise ValueError("not enough flags consumed")

    if flags[idx] > (1 << (r+1))-1:
        raise ValueError("unconsumed 1 flag bits set")

    if left_hash != d["header"].merkle_root:
        raise ValueError(
            "merkle root %s does not match calculated hash %s" % (
                b2h_rev(d["header"].merkle_root), b2h_rev(left_hash)))

    d["tx_hashes"] = tx_acc
    return d
dataset/ETHPy150Open richardkiss/pycoinnet/pycoinnet/message.py/fixup_merkleblock/original