bitcoin-dev-project / warnet

Monitor and analyze the emergent behaviors of Bitcoin networks
https://warnet.dev
MIT License
93 stars 35 forks source link

Add the hook api #665

Open mplsgrant opened 2 weeks ago

mplsgrant commented 2 weeks ago

Overview

This hooks system is intended to allow the user to extend Warnet with python code plugins which will have hooks into key points in the Warnet workflow.

For the user

This hooks API lets users write plugins that look like this:

@pre_status
def thing_i_want_to_do():
    """When the user runs `warnet status`, this function will run first."""
    print("I just want to say hi")

The various decorators available to users can be found in the hooks_api.py file which is also found in the plugins directory.

To get a plugins directory, simply run warnet init, and warnet will generate one for you.

For the Warnet developers

This new hooks system lets us decorate our internal Warnet functions with @api like this:

@click.command()
@api
def status():
    ....

Doing so causes Warnet to produce the corresponding hooks_api.py file when the user runs warnet init

Feedback

I am looking for feedback on this approach. Earlier I had considered authoring an abstract class/interface. After implementing the hooks system using decorators, I noticed that it became much easier to get new API endpoints.

bdp-DrahtBot commented 2 weeks ago

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Conflicts

Reviewers, this pull request conflicts with the following ones:

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.