test plugins/coin_moves: clean up/tidy coin moves plugin logic

removes unused in-memory stash of coin-moves; don't write the entire set
of coinmoves out on every update.
This commit is contained in:
niftynei 2021-12-09 11:46:26 -06:00 committed by Rusty Russell
parent ec991e3af7
commit f169597a02

View file

@ -9,36 +9,23 @@ import os.path
plugin = Plugin() 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 = []
@plugin.subscribe("coin_movement") @plugin.subscribe("coin_movement")
def notify_coin_movement(plugin, coin_movement, **kwargs): def notify_coin_movement(plugin, coin_movement, **kwargs):
plugin.log("coin movement: {}".format(coin_movement)) plugin.log("coin movement: {}".format(coin_movement))
plugin.coin_moves.append(coin_movement)
# we save to disk so that we don't get borked if the node restarts # we save to disk so that we don't get borked if the node restarts
# assumes notification calls are synchronous (not thread safe) # assumes notification calls are synchronous (not thread safe)
with open('moves.json', 'w') as f: with open('moves.json', 'a') as f:
f.write(json.dumps(plugin.coin_moves)) f.write(json.dumps(coin_movement) + ',')
@plugin.method('listcoinmoves_plugin') @plugin.method('listcoinmoves_plugin')
def return_moves(plugin): def return_moves(plugin):
result = [] result = []
if os.path.exists('moves.json'): if os.path.exists('moves.json'):
jd = {}
with open('moves.json', 'r') as f: with open('moves.json', 'r') as f:
jd = f.read() jd = f.read()
result = json.loads(jd) result = json.loads('[' + jd[:-1] + ']')
return {'coin_moves': result} return {'coin_moves': result}