ethereum / web3.py

A python interface for interacting with the Ethereum blockchain and ecosystem.
http://web3py.readthedocs.io
MIT License
4.97k stars 1.7k forks source link

error while using web3.eth.get_proof #3352

Closed ylasgamers closed 5 months ago

ylasgamers commented 5 months ago

What happened?

ValueError: {'code': -32602, 'message': 'invalid argument 1: json: cannot unmars hal hex string of odd length into Go value of type common.Hash'} Screenshot_28

Code that produced the error

from web3 import Web3, HTTPProvider import json import time

web3 = Web3(Web3.HTTPProvider("https://eth-pokt.nodies.app"))

if web3.isConnected(): print("Successfully connected") else: print("Error connecting")

from eth_utils import ( keccak, ) import rlp from rlp.sedes import ( Binary, big_endian_int, ) from trie import ( HexaryTrie, ) from web3._utils.encoding import ( pad_bytes, )

def format_proof_nodes(proof): trie_proof = [] for rlp_node in proof: trie_proof.append(rlp.decode(bytes(rlp_node))) return trie_proof

def verify_eth_get_proof(proof, root): trie_root = Binary.fixed_length(32, allow_empty=True) hash32 = Binary.fixed_length(32)

class _Account(rlp.Serializable):
    fields = [
                ('nonce', big_endian_int),
                ('balance', big_endian_int),
                ('storage', trie_root),
                ('code_hash', hash32)
            ]
acc = _Account(
    proof.nonce, proof.balance, proof.storageHash, proof.codeHash
)
rlp_account = rlp.encode(acc)
trie_key = keccak(bytes.fromhex(proof.address[2:]))

assert rlp_account == HexaryTrie.get_from_proof(
    root, trie_key, format_proof_nodes(proof.accountProof)
), "Failed to verify account proof {}".format(proof.address)

for storage_proof in proof.storageProof:
    trie_key = keccak(pad_bytes(b'\x00', 32, storage_proof.key))
    root = proof.storageHash
    if storage_proof.value == b'\x00':
        rlp_value = b''
    else:
        rlp_value = rlp.encode(storage_proof.value)

    assert rlp_value == HexaryTrie.get_from_proof(
        root, trie_key, format_proof_nodes(storage_proof.proof)
    ), "Failed to verify storage proof {}".format(storage_proof.key)

return True

account = web3.toChecksumAddress("0x6C8f2A135f6ed072DE4503Bd7C4999a1a17F824B")
block_number = web3.eth.blockNumber block = web3.eth.get_block(block_number) proof = web3.eth.get_proof(account, [0, 1], block_number) assert verify_eth_get_proof(proof, block.stateRoot)

No response

Full error output

Traceback (most recent call last):
  File "F:\Program Files\SendTrx\ZOwn\claimtest\New folder\txhashcheck.py", line
 109, in <module>
    proof = web3.eth.get_proof(account, [0, 1], block_number)
  File "C:\Users\ZERO\AppData\Local\Programs\Python\Python37\lib\site-packages\w
eb3\module.py", line 60, in caller
    null_result_formatters)
  File "C:\Users\ZERO\AppData\Local\Programs\Python\Python37\lib\site-packages\w
eb3\manager.py", line 201, in request_blocking
    null_result_formatters)
  File "C:\Users\ZERO\AppData\Local\Programs\Python\Python37\lib\site-packages\w
eb3\manager.py", line 171, in formatted_response
    raise ValueError(response["error"])
ValueError: {'code': -32602, 'message': 'invalid argument 1: json: cannot unmars
hal hex string of odd length into Go value of type common.Hash'}

Fill this section in if you know how this could or should be fixed

No response

web3 Version

No response

Python Version

No response

Operating System

No response

Output from pip freeze

No response

fselmo commented 5 months ago

@ylasgamers It looks like you are using Python 3.7 which is no longer supported and also an old version of web3.py seeing as many of the methods you are invoking are still camelCase.

Using the latest web3.py I have no issues getting the proof from the example you just posted:

account = w3.to_checksum_address("0x6C8f2A135f6ed072DE4503Bd7C4999a1a17F824B")
block_number = w3.eth.block_number
block = w3.eth.get_block(block_number)
proof = w3.eth.get_proof(account, [0, 1], block_number)