求救使用 Python 的 ETH 库 web3.py、py-evm 拼接 Receipt trie,遇到的问题 - V2EX
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 2023-09-10 18:05:21 +08:00 997 次点击
    这是一个创建于 837 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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  
       2023-09-13 14:08:21 +08:00
    有试过问 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     Solana     955 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 32ms UTC 19:22 PVG 03:22 LAX 11:22 JFK 14:22
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86