polkascan / py-substrate-interface

Python Substrate Interface
https://polkascan.github.io/py-substrate-interface/
Apache License 2.0
239 stars 111 forks source link

ContractExecutionReceipt.process_events() crashes on events from other contracts. #379

Closed h4nsu closed 4 months ago

h4nsu commented 4 months ago

As far as I understand the underlying code, it seems like ContractExecutionReceipt.process_events() filters all the events of type ContractEmitted and attempts to decode them using contract's metadata. However, if "our" contract call resulted in some cross-contract calling and "their" contracts emitted their events, we won't be able to decode them (we have only "our" metadata, not theirs). Currently this results in unhandled scalecodec exception and a crash. Maybe it's a good idea to put this part in a try-catch block?

DamianStraszak commented 4 months ago

I think even better would be to inspect the contract field of the ContractEmitted event and decode only ours. Inspect the definition of ContractEmitted variant below. We don't even want to attempt decoding events from other contracts, because we could succeed by coincidence and return some incorrect event as a result.

image

h4nsu commented 4 months ago

@DamianStraszak Good idea, I drafted a PR with the fix you described

arjanz commented 4 months ago

I think even better would be to inspect the contract field of the ContractEmitted event and decode only ours. Inspect the definition of ContractEmitted variant below. We don't even want to attempt decoding events from other contracts, because we could succeed by coincidence and return some incorrect event as a result.

@DamianStraszak I agree, currently it has the false assumption all ContractEmitted events in a block are related to one specific contract, which is of course not true. Checking if the events are emitted from that specific contract would be a good fix.

@h4nsu I'll take a look at the PR

arjanz commented 4 months ago

PR released: https://github.com/polkascan/py-substrate-interface/releases/tag/v1.7.7

h4nsu commented 4 months ago

Thanks!