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>
This commit is contained in:
Rusty Russell
2021-07-23 12:30:00 +09:30
parent 424770449a
commit 1f6f701bf1
4 changed files with 184 additions and 0 deletions

45
datastore/datastore.py Executable file
View File

@@ -0,0 +1,45 @@
#!/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()