ethereum / web3.py

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

Events filtering not working with EIP checksum addresses (Ganache-CLI) #674

Closed jordanjambazov closed 6 years ago

jordanjambazov commented 6 years ago

What was wrong?

Maybe the issue is not exactly web3.py related, but when using EIP-55 checksum addresses event filtering is not working. My guess is that the reason is that Ganache-CLI is not producing proper addresses.

Event Filter done as follows:

certificate_created_filter = instance.eventFilter(
    'CertificateCreated',
    {
        'fromBlock': 0,
        'toBlock': 'latest',
    })
loop = asyncio.get_event_loop()
try:
    task = loop.create_task(
        self.log_loop(certificate_created_filter, 0.5))
    loop.run_until_complete(task)
finally:
    loop.close()

Checking for events

    async def log_loop(self, event_filter, poll_interval):
        event_filter.get_all_entries()
        while True:
            for event in event_filter.get_new_entries():
                self.handle_event(event)
            await asyncio.sleep(poll_interval)

Not Working:

for _, network in contract_interface['networks'].items():
    _contracts[contract_name] = w3.eth.contract(
        address=web3.Web3.toChecksumAddress(network['address']),
        abi=contract_interface['abi'])
    return _contracts[contract_name]

_Working (with monkey patched web3.utils.validation.validateaddress):

for _, network in contract_interface['networks'].items():
    _contracts[contract_name] = w3.eth.contract(
        address=network['address'],  # Address all lowercased
        abi=contract_interface['abi'])
    return _contracts[contract_name]

The monkey patch is to simply ignore the EIP checksum validation.

How can it be fixed?

Not sure what is the best way to fix it. Simply ignoring checksum validation seems not right. I'm glad to discuss.

_editor: Let's add an optional middleware that can be injected to the innermost layer that forces all hex to lowercase, similar to the geth_poa_middleware_

pipermerriam commented 6 years ago

Can you expand on what "Not Working:" means. It's not clear to me what is actually broken/failing here.

jordanjambazov commented 6 years ago

@pipermerriam What is meant there is that events filtering is not working if EIP checksum address is provided there. The filter always returns empty list on get_new_entries() invocation. With lower cased address it returns properly the events, with properly checksummed address the result is empty.

pipermerriam commented 6 years ago

Sounds like an issue with Ganache since filtering is done on the backend and all that web3.py is doing is making RPC requests to get whatever entries the backend returns.

EralpB commented 6 years ago

@jordanjambazov could you solve this? Maybe by replacing ganache with something else. This is also a very important blocker for me.

jordanjambazov commented 6 years ago

@EralpB Unfortunately currently I am using a forked monkey patched version of web3.py: https://github.com/jordanjambazov/web3.py/commit/a61b382dd5c11de1102779868d377408c7590839

carver commented 6 years ago

@EralpB @jordanjambazov can you help provide the ganache version issue and such on this issue: https://github.com/trufflesuite/ganache-cli/issues/494 ?

jordanjambazov commented 6 years ago

@carver I commented on the version of Ganache-CLI, it was reproducible in v6.1.0-beta.2

grahamturk commented 6 years ago

@jordanjambazov are you getting the same problem when using the new Contract.events.<event name>.createFilter() method?

pipermerriam commented 6 years ago

If you're dealing with this error consider speaking up in the following github issue in the ganache codebase.

https://github.com/trufflesuite/ganache-cli/issues/494

carver commented 6 years ago

Everyone who ran into this issue, please upgrade and try again. It's believed to be fixed!

Your versions should be:

Please come back and comment if it is still not working as expected.

smartcontracts commented 6 years ago

I'm still having this issue with:

The following filter does not work:

event_filter = web3.eth.filter({
    "address": contract.address
})

but this does:

event_filter = web3.eth.filter({
    "address": contract.address.lower()
})

Where contract.address is checksummed.

carver commented 6 years ago

@kfichter

This was marked resolved in ganache-core@2.1.3 and ganache-cli@6.1.4

Can you upgrade and try again?

smartcontracts commented 6 years ago

@carver Working, thank you!

bitcoin4cashqc commented 6 years ago

Me I have Ganache CLI v7.0.0-beta.0 (ganache-core: 3.0.0-beta.0) And I have empty arrays after running truffle test and trying to fetch obliviously made events

dylanjw commented 6 years ago

Filtering events in ganache from web3 is broken. I submitted a fix and am waiting for it to get merged and included in ganache -> https://github.com/MetaMask/provider-engine/pull/280