CityOfZion / neo-python

Python Node and SDK for the NEO 2.x blockchain. For NEO 3.x go to our successor project neo-mamba
https://neo-python.readthedocs.io/en/latest/
MIT License
313 stars 189 forks source link

fix NEP-5 query eventhub exception #1026

Closed ixje closed 4 years ago

ixje commented 4 years ago

What current issue(s) does this address, or what feature is it adding? Discord user Marcello#6271 reported an error where an exception is thrown upon contract deployment. Specifically

Traceback (most recent call last):
  File "/home/marcello/neoproject/venv/lib/python3.7/site-packages/neo/Wallets/NEP5Token.py", line 101, in Query
    engine = ApplicationEngine.Run(snapshot, sb.ToArray(), exit_on_error=True, gas=Fixed8.FromDecimal(10.0), test_mode=False)
  File "/home/marcello/neoproject/venv/lib/python3.7/site-packages/neo/SmartContract/ApplicationEngine.py", line 201, in Run
    events.emit(event.event_type, event)
  File "/home/marcello/neoproject/venv/lib/python3.7/site-packages/pymitter/init.py", line 282, in emit
    remove = [l for l in listeners if not l(args, **kwargs)]
  File "/home/marcello/neoproject/venv/lib/python3.7/site-packages/pymitter/init.py", line 282, in <listcomp>
    remove = [l for l in listeners if not l(args, kwargs)]
  File "/home/marcello/neoproject/venv/lib/python3.7/site-packages/pymitter/init.py", line 310, in call
    self.func(*args, kwargs)
  File "/home/marcello/neoproject/venv/lib/python3.7/site-packages/neo/EventHub.py", line 62, in on_sc_event
    logger.info("[%s][%s] [%s] [tx %s] %s" % (sc_event.event_type, sc_event.block_number, sc_event.contract_hash, sc_event.tx_hash.ToString(), payload.ToJson(auto_hex=False)))
AttributeError: 'NoneType' object has no attribute 'ToString'

Upon investigation I found that when a contract is deployed, neo-python tries to determine if the contract is a NEP-5 contract. It does this by running the VM and trying to query known methods of a nep-5 contract. This query mechanism runs the VM without setting up a container (Transaction) that the query is ran from. In a normal case VM execution is always triggered from a invocation transaction so this error would not occur.

How did you solve this problem? fix initialization of the VM for NEP-5 queries by using a fake/empty TX as container

How did you make sure your solution works? manually reproduced the issue before and after the fix

Are there any special changes in the code that we should be aware of?

Please check the following, if applicable:

coveralls commented 4 years ago

Coverage Status

Coverage increased (+0.001%) to 85.312% when pulling 3ea69e69624433b78d238cfb256f2da6357cf518 on ixje:fix-eventhub-error into 4106ecfa4261ed3ac0d2500c9ccb9acd1ba32c30 on CityOfZion:development.