aleph-im / aleph-client

Lightweight Python Client library for the Aleph.im network
MIT License
12 stars 14 forks source link

The Python client/SDK creates VM message filters from decorators. #47

Open hoh opened 2 years ago

hoh commented 2 years ago

Context

We are working on improving the launch of VMs in reaction to the publication of Aleph messages.

The current implementation of the feature relies on:

  1. The message defines filters to react to
  2. The program uses decorators to specify which functions to call

1. The message defines filters to react to

A message of type PROGRAM can define to react to Aleph messages in the following form:

    "content": {
        ...
        "on": {
            "message": [
                {
                    "sender": "0xb5F010860b0964090d5414406273E6b3A8726E96",
                    "channel": "TEST"
                },
                {
                    "content": {
                        "ref": "4d4db19afca380fdf06ba7f916153d0f740db9de9eee23ad26ba96a90d8a2920"
                    }
                }
            ]
        },

2. The program uses decorators to specify which functions to call


filters = [{
    "sender": "0xB31B787AdA86c6067701d4C0A250c89C7f1f29A5",
    "channel": "TEST"
}]

@app.event(filters=filters)
async def aleph_event(event):
    print("aleph_event", event)
    async with aiohttp.ClientSession(connector=aiohttp.TCPConnector()) as session:
        async with session.get("https://official.aleph.cloud/api/v0/info/public.json") as resp:
            print('RESP', resp)
            resp.raise_for_status()
    return {
        "result": "Good"
    }

Problem

This requires developers to write their filters twice: in the Python decorators and in the PROGRAM message definition. This may lead to an inconsistency between the two definitions.

It would be useful for app developers to have a tool that generates the values of content.on.message based on the filters used in the Python code.

MHHukiewitz commented 2 years ago

Example plz

hoh commented 2 years ago

Description updated

MHHukiewitz commented 1 year ago

So given the implementation of AlephApp in vm/app.py, I can create a function like this:

    def get_filters(self):
        filters = []
        for event_handler in self.event_handlers:
            filters.extend(event_handler.filters)
        return filters

to get all of the defined filters of the app in the format required to react on message events.

The task would now be to parse the code to be uploaded in the VM (if it is Python code), find the AlephApp object and call .get_filters(), correct? Two issues I see that might come up: