求救使用 Python 的 ETH 库 web3.py、py-evm 拼接 Receipt trie,遇到的问题

289 天前
 rainmanliu
def _build_tree(self, transactions, receiptsRoot):
    from rlp import encode, sedes
    from trie import HexaryTrie
    from eth.rlp import receipts, logs
    def zpad(x, l):
        return b'\x00' * max(0, l - len(x)) + x

    block_receipts = []
    for tx in transactions:
        rec = w3.eth.get_transaction_receipt(tx.hash.hex())
        block_receipts.append(rec)

    trie = HexaryTrie(db={})
    for receipt in block_receipts:
        path = receipt.transactionIndex
        logs_list = []
        for l in receipt['logs']:
            log_address = zpad(sedes.big_endian_int.serialize(int(l['address'][2:], 16)), 20)
            data = sedes.BigEndianInt().serialize(int(l['data'].hex()[2:], 16)) if l['data'] == '0x' else b''
            logs_list.append(
                logs.Log(
                    address=log_address,
                    topics=tuple([int(t.hex()[2:], 16) for t in l['topics']]),
                    data=data
                )
            )
        receipt_obj = receipts.Receipt(
            state_root=sedes.BigEndianInt().serialize(receipt['status']),
            gas_used=int(receipt['cumulativeGasUsed']),
            logs=logs_list,
            bloom=int(receipt['logsBloom'].hex()[2:], 16),
        )
        node_byte = receipt_obj.encode()

        trie.set(sedes.BigEndianInt().serialize(path), node_byte)

    if trie.root_hash.hex() != receiptsRoot[2:]:
        raise Exception("Failed to build receipts trie root.")

    return trie

有 python 区块链大哥 能懂吗 拼接的树的 root hash 和 receipts 的 hash 对不上 trie.root_hash.hex() receiptsRoot[2:]

632 次点击
所在节点    Python
1 条回复
makerbi
286 天前
有试过问 ChatGPT 不?帮你问了一下,你看看对不对。

你的代码看起来已经相当接近正确的答案了,你可能需要检查以下几点:

1. 确保你的输入数据是正确的。你的交易收据(`receipts`)是从哪里获取的?它们是从相同的区块中获取的吗?如果你的数据是错误的,或者来自不同的区块,那么构建出来的 Trie 树的根哈希值就有可能与给定的 `receiptsRoot` 不匹配。

2. `receipt.transactionIndex` 的处理可能有问题。在 Ethereum 中,交易索引应该是 RLP 编码的交易在区块中的位置。你需要使用 RLP 编码(`rlp.encode`)对索引进行编码,而不是使用 `sedes.BigEndianInt().serialize`。

3. 在处理日志(`logs`)时,你需要确保地址(`address`)、主题(`topics`)和数据(`data`)都是正确编码的。对于地址和主题,你应该使用 `bytes` 类型,而不是整数。对于数据,如果它不是 `0x`,你应该直接使用原始的 `bytes` 值,而不是将它转换为整数。

4. `state_root` 应该使用 `bytes` 类型的值,而不是整数。你可以直接使用 `bytes.fromhex(receipt['status'][2:])` 来获取这个值。

5. 确保 `trie.set` 方法的参数是正确的。第一个参数应该是交易索引的 RLP 编码,第二个参数应该是收据对象的 RLP 编码。

说明一下我没咋用过 web3.py ,不太能确定 ChatGPT 的回答对不对,你自己检查一下?

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/972495

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX