core-lightning/tests/plugins/dblog.py
Christian Decker 9d4148ce68 pylightning: Warn users of plugins that may break due to extra args
We recently noticed that the way we unpack the call arguments for hooks and
notifications in pylightning breaks pretty quickly once you start changing the
hook and notification params. If you add params they will not get mapped
correctly causing the plugin to error out.

This can be fixed by adding a `VAR_KEYWORD` argument to the calbacks, i.e., by
adding a single `**kwargs` argument at the end of the signature. This commit
adds a check that such a catch-all argument exists, and emits a warning if it
doesn't.

It also fixes up the plugins that we ship ourselves.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-08-05 00:19:48 +00:00

40 lines
1.1 KiB
Python
Executable File

#!/usr/bin/env python3
"""This plugin is used to check that db_write calls are working correctly.
"""
from lightning import Plugin, RpcError
import sqlite3
plugin = Plugin()
plugin.sqlite_pre_init_cmds = []
plugin.initted = False
@plugin.init()
def init(configuration, options, plugin):
if not plugin.get_option('dblog-file'):
raise RpcError("No dblog-file specified")
plugin.conn = sqlite3.connect(plugin.get_option('dblog-file'),
isolation_level=None)
plugin.log("replaying pre-init data:")
for c in plugin.sqlite_pre_init_cmds:
plugin.conn.execute(c)
plugin.log("{}".format(c))
plugin.initted = True
plugin.log("initialized")
@plugin.hook('db_write')
def db_write(plugin, writes, **kwargs):
if not plugin.initted:
plugin.log("deferring {} commands".format(len(writes)))
plugin.sqlite_pre_init_cmds += writes
else:
for c in writes:
plugin.conn.execute(c)
plugin.log("{}".format(c))
return True
plugin.add_option('dblog-file', None, 'The db file to create.')
plugin.run()