Ejemplo n.º 1
0
def build_receipt_proof(burn_tx_receipt: TxReceipt,
                        burn_tx_block: BlockData) -> List[bytes]:
    """Build the burn_tx_receipt proof."""
    state_sync_tx_hash = keccak256(b"matic-bor-receipt-" +
                                   burn_tx_block["number"].to_bytes(8, "big") +
                                   burn_tx_block["hash"])
    receipts_trie = HexaryTrie({})
    receipts = (web3.eth.get_transaction_receipt(tx)
                for tx in tqdm(burn_tx_block["transactions"],
                               desc="Building receipts trie",
                               unit="receipt") if tx != state_sync_tx_hash)
    for tx_receipt in receipts:
        path = rlp.encode(tx_receipt["transactionIndex"])
        receipts_trie[path] = serialize_receipt(tx_receipt)

    key = rlp.encode(burn_tx_receipt["transactionIndex"])
    print("Building merkle proof")
    proof = receipts_trie.get_proof(key)

    assert (receipts_trie.root_hash == burn_tx_block["receiptsRoot"]
            ), "Receipts trie root is incorrect"

    return key, proof
Ejemplo n.º 2
0
    raw_tx = _Transaction(tx.nonce, tx.gasPrice, tx.gas,
                          w3.toBytes(hexstr=tx.to), tx.value,
                          w3.toBytes(hexstr=w3.toHex(hexstr=tx.input)), tx.v,
                          w3.toInt(tx.r), w3.toInt(tx.s))
    rlp_tx = rlp.encode(raw_tx)
    assert tx.hash == keccak(rlp_tx)
    txs.append(raw_tx)
    trie.set(rlp.encode(key), rlp_tx)
    # print(rlp_tx)
assert block.transactionsRoot == trie.root_hash
print(w3.toHex(trie.root_hash))

# Generate Merkle tree proof for per each node
proofs = []
for key in range(B):
    proof = trie.get_proof(rlp.encode(key))
    proofs.append(proof)

# Verify Merkle tree proof for per each node
nodes = []
for key in range(B):
    node = HexaryTrie.get_from_proof(trie.root_hash, rlp.encode(key),
                                     proofs[key])
    nodes.append(node)
    assert nodes[key] == rlp.encode(txs[key])
    print("proof of key %d:" % key, proofs[key], nodes[key])

print(True)

proof = proofs[3]
print(proof)
Ejemplo n.º 3
0

trie.set(rlp.encode(2), rlp.encode("TWO"))
print(w3.toHex(trie.root_hash))
print(w3.toHex(keccak(rlp.encode(trie.get_node(trie.root_hash)))))
assert trie.root_hash == keccak(rlp.encode(trie.get_node(trie.root_hash)))
print(trie.get_node(trie.root_hash))


trie.set(rlp.encode(3), rlp.encode("THREE"))
print(w3.toHex(trie.root_hash))
print(w3.toHex(keccak(rlp.encode(trie.get_node(trie.root_hash)))))
assert trie.root_hash == keccak(rlp.encode(trie.get_node(trie.root_hash)))
print(trie.get_node(trie.root_hash))

proof = trie.get_proof(rlp.encode(1))
assert HexaryTrie.get_from_proof(trie.root_hash, rlp.encode(1), proof) == rlp.encode("ONE")
print(proof)

proof = trie.get_proof(rlp.encode(2))
assert HexaryTrie.get_from_proof(trie.root_hash, rlp.encode(2), proof) == rlp.encode("TWO")
print(proof)

proof = trie.get_proof(rlp.encode(3))
assert HexaryTrie.get_from_proof(trie.root_hash, rlp.encode(3), proof) == rlp.encode("THREE")
print(proof)
#([b'\x10', b'\xae\xa5\xd0\x99\xdd\xe25gVx\xdcu\xd5\xecAC]w\xa7E\xcd=\xb20)\xb3\xaa\xf6Jy~\xdd'],
# [b'', [b' ', b'\x83ONE'], [b' ', b'\x83TWO'], [b' ', b'\x85THREE'], b'', b'', b'', b'', b'', b'', b'', b'', b'', b'', b'', b'', b''],
# [b' ', b'\x85THREE'])

print(proof[0])