V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
rainmanliu
V2EX  ›  Python

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

  •  
  •   rainmanliu · 230 天前 · 592 次点击
    这是一个创建于 230 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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:]

    1 条回复    2023-09-13 14:08:21 +08:00
    makerbi
        1
    makerbi  
       227 天前
    有试过问 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 的回答对不对,你自己检查一下?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1438 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 17:09 · PVG 01:09 · LAX 10:09 · JFK 13:09
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.