Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event subscribing, listening, retrieving #1453

Merged
merged 40 commits into from
May 15, 2022

Conversation

YerimB
Copy link
Contributor

@YerimB YerimB commented Feb 24, 2022

What I did

  • Ability to subscribe to any event in a contract by setting a callback to be executed at each new occurrence.
  • Ability to listen for any event occurrence with a coroutine.
  • Ability to retrieve events from a contract that occurred between two blocks.

Related issue: #1162 #327 #1421

How I did it

general

  • Created the ContractEvents class, inheriting from the web3.py class of the same name, and implementing methods to subscribe to, listen for and retrieve events.
  • Set a ContractEvents instance ; named 'events' ; to every contract, so it can be used easily.

subscribing

  • Created the EventWatcher singleton class with a loop function running in a separate thread, watching for new events.
  • Event subscriptions can be added using the corresponding contract instance, using the Contract.events.subscribe method.
  • Whenever a new event is detected, the EventWatcher class creates new threads to run the user-defined callbacks instructions.

listening

  • Created the Contract.events.listen method, which returns a Coroutine object that can be awaited. A timeout parameter can be passed to the method.
  • The Coroutine waits for the user-defined event to occur before returning an web3.datastructures.AttributeDict object containing the event log receipt and a boolean that evaluates to True if the Coroutine has timed out.

retrieving

  • Created the Contract.events.get_sequence method, making able to retrieve events from a contract between 2 blocks.
  • If no event is specified as parameter, retrieves all contract events between the 2 blocks.

To get events, I am using the web3.py ContractEvent.createFilter and the get_all_entries or the get_new_entries method

How to verify it

Added some tests to the tests/network/test_event.py file.

Checklist

  • I have confirmed that my PR passes all linting checks
  • I have included test cases
  • I have updated the documentation
  • I have added an entry to the changelog

YerimB added 30 commits January 27, 2022 10:10
Remove personal file from .gitignore
Stops alerts on tests before assertion phase
Reducing lines at the same time
Not working yet, pushing to change PC
Events related classes moved from "brownie/network/alert.py" to "brownie/network/event.py"
+ Added a listen function to the ContractEvents class.
+ Fix : Removing event subscription after one trigger when repeat is disabled.
Completing and bug-fixing on event subscription and listening - related methods and classes.
Corrected and added tests on listening and subscribing with callback to an event.
- Added comments on the EventWatcher class
- Fixed the bug that made EventWatcher wait for the minimum 'delay' in the EventWatchData list each loop when no event is detected.
The main purpose of this is that if a callback takes too long, others aren't blocked
Replacing ThreadPool with a simple thead list.
A thread is created whenever a callback instruction needs to be executed (avoids blocking callback problem).
@YerimB YerimB mentioned this pull request Feb 24, 2022
4 tasks
EventWatchData class was renamed to _EventWatchData
Updated 2 classes:
- _EvenWatchData : it now manages all callbacks set to an event as well as removing them when they are not repeating ones. Creating threads when callback instructions are required by function call.
- EventWatcher : it now has a dict with the _EventWatchData instances instead of a list and uses that to call instructions on events. Does not creates threads by itself, delegates it to _EventWatchData.
Forgot one line.
@YerimB YerimB marked this pull request as ready for review February 25, 2022 16:59
Copy link
Member

@iamdefinitelyahuman iamdefinitelyahuman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, impressive work

@iamdefinitelyahuman iamdefinitelyahuman merged commit 36c746f into eth-brownie:master May 15, 2022
@YerimB
Copy link
Contributor Author

YerimB commented Sep 28, 2022

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants