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:]
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.