Files
plugins/datastore/datastore.py
Rusty Russell 1f6f701bf1 datastore: plugin to replicate datastore commands before next release.
See https://github.com/ElementsProject/lightning/pull/4674
(I manually tested that it passes all the tests there, too!)

When they finally upgrade the node, this automatically puts any
datastore data into the inbuilt datastore and shuts down.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2021-07-25 10:56:40 +09:30

46 lines
1.2 KiB
Python
Executable File

#!/usr/bin/env python3
from pyln.client import Plugin, RpcError
import shelve
import os
plugin = Plugin()
def unload_store(plugin):
"""When we have a real store, we transfer our contents into it"""
try:
datastore = shelve.open('datastore.dat', 'r')
except:
return
plugin.log("Emptying store into main store!", level='unusual')
for k, d in datastore.items():
try:
plugin.rpc.datastore(k, d.hex())
except RpcError as e:
plugin.log("Failed to put {} into store: {}".format(k, e),
level='broken')
datastore.close()
plugin.log("Erasing our store", level='unusual')
os.unlink('datastore.dat')
@plugin.init()
def init(options, configuration, plugin):
# If we have real datastore commands, don't load plugin.
try:
plugin.rpc.help('datastore')
unload_store(plugin)
return {'disable': 'there is a real datastore command'}
except RpcError:
pass
# Start up real plugin now
plugin.rpc.plugin_start(os.path.join(os.path.dirname(__file__),
"datastore-plugin.py"))
return {'disable': 'no builtin-datastore: plugin loaded'}
plugin.run()