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:]