coin moves tests: save updates to disk

If we don't save to disk, if the node restarts we'll lose them all
and the resulting balance check at the end will be incorrect.
This commit is contained in:
lisa neigut 2020-04-01 22:02:08 -05:00 committed by Rusty Russell
parent 5d58f125c5
commit 41d3471c7f

View file

@ -2,11 +2,21 @@
from pyln.client import Plugin
import json
import os.path
plugin = Plugin()
@plugin.init()
def init(configuration, options, plugin):
if os.path.exists('moves.json'):
jd = {}
with open('moves.json', 'r') as f:
jd = f.read()
plugin.coin_moves = json.loads(jd)
else:
plugin.coin_moves = []
@ -30,10 +40,21 @@ def notify_coin_movement(plugin, coin_movement, **kwargs):
plugin.coin_moves.append(coin_movement)
# we save to disk so that we don't get borked if the node restarts
# assumes notification calls are synchronous (not thread safe)
with open('moves.json', 'w') as f:
f.write(json.dumps(plugin.coin_moves))
@plugin.method('listcoinmoves_plugin')
def return_moves(plugin):
return {'coin_moves': plugin.coin_moves}
result = []
if os.path.exists('moves.json'):
jd = {}
with open('moves.json', 'r') as f:
jd = f.read()
result = json.loads(jd)
return {'coin_moves': result}
plugin.run()