← 返回题库
初级

在区块中加入Merkle Root字段

未完成
初级参考 完整示例代码供参考,建议自己理解后重新输入
def solve(block, transactions):
    import hashlib
    import copy
    import json
    b = copy.deepcopy(block)
    if transactions:
        while len(transactions) > 1:
            if len(transactions) % 2 == 1:
                transactions.append(transactions[-1])
            transactions = [hashlib.sha256((transactions[i] + transactions[i+1]).encode()).hexdigest() for i in range(0, len(transactions), 2)]
        b['merkle_root'] = transactions[0] if transactions else ''
    print(b)

示例

输入
solve({"index": 1}, ["tx1", "tx2"])
期望输出
{'index': 1, 'merkle_root': 'b75fa4cca73a24cc129213c6e064b971533e2fa3eb0d118e3f484a7ae4a70fd3'}
Python 代码 🔒 登录后使用
🔒

登录后即可练习

注册免费账号,在浏览器中直接运行 Python 代码