From 894d7ccb918002fc65b4c246364eaf9ad1df2b65 Mon Sep 17 00:00:00 2001 From: qrest Date: Sat, 9 May 2020 20:26:48 +0200 Subject: [PATCH] add checks for peer and channels --- balancesharing/balancesharing.py | 62 ++++++++++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 2 deletions(-) diff --git a/balancesharing/balancesharing.py b/balancesharing/balancesharing.py index 891a55863..18568c85d 100755 --- a/balancesharing/balancesharing.py +++ b/balancesharing/balancesharing.py @@ -16,15 +16,71 @@ pass +# TODO: implement +def has_feature(feature): + """"check if XXX feature bit present""" + return True + + +def get_funds(plugin): + """"get output and channels""" + # TODO: switch to real data + # fund_list = plugin.rpc.listfunds() + fund_list = list_funds_mock() + outputs = fund_list["outputs"] + channels = fund_list["channels"] + + return outputs, channels + + +def list_funds_mock(): + """"read funds from file""" + # TODO: add dir + with open('/funds.json', 'r') as funds_file: + data = funds_file.read() + + return json.loads(data) + + +# TODO: we need to extend this, if we want to handle multiple channels per peer +def get_channel(channels, peer_id): + """"searches for ONE channel with the given peer_id""" + for ch in channels: + if ch["peer_id"] == peer_id: + return ch + + return None + + @plugin.method("foafbalance") def foafbalance(plugin): """gets the balance of our friends channels""" + flow_value = 1 + amt_to_rebalance = 10 reply = {} info = plugin.rpc.getinfo() msg = r'105b126182746121' + + outputs, channels = get_funds(plugin) + for peer in plugin.rpc.listpeers()["peers"]: - res = plugin.rpc.dev_sendcustommsg(peer["id"], msg) - plugin.log(str(res)) + # check if peer is the desired state + if not peer["connected"] or not has_feature(peer["features"]): + continue + peer_id = peer["id"] + peer_channel = get_channel(channels, peer_id) + + if peer_channel is None: + plugin.log("No channel found for {peer_id}".format(peer_id=peer_id)) + continue + else: + plugin.log("Found channel for {peer_id}: {channel}".format( + peer_id=peer_id, channel=peer_channel["short_channel_id"] + )) + + res = plugin.rpc.dev_sendcustommsg(peer_id, msg) + plugin.log("RPC response" + str(res)) + nid = info["id"] reply["id"] = nid reply["change"] = nid @@ -51,6 +107,7 @@ def send_reply_foaf_balances(peer, channels, plugin): plugin.rpc.dev_sendcustommsg(peer, "107b123412341234") return + @plugin.hook('peer_connected') def on_connected(plugin, **kwargs): plugin.log("GOT PEER CONNECTION HOOK") @@ -80,6 +137,7 @@ def on_custommsg(peer_id, message, plugin, **kwargs): plugin.log(str(type(message))) return {'result': 'continue'} + @plugin.init() def init(options, configuration, plugin): plugin.log("Plugin balancesharing.py initialized")