rebalance: rebalanceall with maxhops 6 and msatfactor 1

This commit is contained in:
Michael Schmoock
2021-06-07 17:16:58 +02:00
parent 039e972cdf
commit 309a75b03d

View File

@@ -130,7 +130,9 @@ def calc_optimal_amount(out_ours, out_total, in_ours, in_total, payload):
@plugin.method("rebalance") @plugin.method("rebalance")
def rebalance(plugin, outgoing_scid, incoming_scid, msatoshi: Millisatoshi = None, def rebalance(plugin, outgoing_scid, incoming_scid, msatoshi: Millisatoshi = None,
maxfeepercent: float = 0.5, retry_for: int = 60, exemptfee: Millisatoshi = Millisatoshi(5000)): maxfeepercent: float = 0.5, retry_for: int = 60,
exemptfee: Millisatoshi = Millisatoshi(5000),
maxhops: int = None, msatfactor: float = None):
"""Rebalancing channel liquidity with circular payments. """Rebalancing channel liquidity with circular payments.
This tool helps to move some msatoshis between your channels. This tool helps to move some msatoshis between your channels.
@@ -139,6 +141,10 @@ def rebalance(plugin, outgoing_scid, incoming_scid, msatoshi: Millisatoshi = Non
msatoshi = Millisatoshi(msatoshi) msatoshi = Millisatoshi(msatoshi)
maxfeepercent = float(maxfeepercent) maxfeepercent = float(maxfeepercent)
retry_for = int(retry_for) retry_for = int(retry_for)
if maxhops is None:
maxhops = plugin.maxhops
if msatfactor is None:
msatfactor = plugin.msatfactor
exemptfee = Millisatoshi(exemptfee) exemptfee = Millisatoshi(exemptfee)
payload = { payload = {
"outgoing_scid": outgoing_scid, "outgoing_scid": outgoing_scid,
@@ -178,13 +184,14 @@ def rebalance(plugin, outgoing_scid, incoming_scid, msatoshi: Millisatoshi = Non
rpc_result = None rpc_result = None
excludes = [my_node_id] # excude all own channels to prevent shortcuts excludes = [my_node_id] # excude all own channels to prevent shortcuts
nodes = {} # here we store erring node counts nodes = {} # here we store erring node counts
maxhops = 1 # start with short routes and increase _maxhops = 1 # start with short routes and increase
msatfactor = plugin.msatfactor # start with high msatoshi factor to reduce _msatfactor = msatfactor # start with high msatoshi factor to reduce
# WIRE_TEMPORARY failures because of imbalances # WIRE_TEMPORARY failures because of imbalances
# 'disable' maxhops filter if set to <= 0 # 'disable' maxhops filter if set to <= 0
# I know this is ugly, but we don't ruin the rest of the code this way
if plugin.maxhops <= 0: if plugin.maxhops <= 0:
maxhops = 20 _maxhops = 20
# trace stats # trace stats
count = 0 count = 0
@@ -200,22 +207,22 @@ def rebalance(plugin, outgoing_scid, incoming_scid, msatoshi: Millisatoshi = Non
r = plugin.rpc.getroute(incoming_node_id, r = plugin.rpc.getroute(incoming_node_id,
fromid=outgoing_node_id, fromid=outgoing_node_id,
exclude=excludes, exclude=excludes,
msatoshi=msatoshi * msatfactor, msatoshi=msatoshi * _msatfactor,
maxhops=maxhops, maxhops=_maxhops,
riskfactor=10, cltv=9) riskfactor=10, cltv=9)
time_getroute += time.time() - time_start time_getroute += time.time() - time_start
except RpcError as e: except RpcError as e:
# could not find route -> change params and restart loop # could not find route -> change params and restart loop
if e.method == "getroute" and e.error.get('code') == 205: if e.method == "getroute" and e.error.get('code') == 205:
# reduce msatfactor to look for smaller channels now # reduce _msatfactor to look for smaller channels now
msatfactor -= 1 _msatfactor -= 1
if msatfactor < 1: if _msatfactor < 1:
# when we reached neutral msat factor: # when we reached neutral msat factor:
# increase maxhops and restart with msatfactor # increase _maxhops and restart with msatfactor
maxhops = maxhops + 1 _maxhops = _maxhops + 1
msatfactor = plugin.msatfactor _msatfactor = msatfactor
# abort if we reached maxhop limit # abort if we reached maxhop limit
if plugin.maxhops > 0 and maxhops > plugin.maxhops: if maxhops > 0 and _maxhops > maxhops:
rpc_result = {'status': 'error', 'message': 'No suitable routes found'} rpc_result = {'status': 'error', 'message': 'No suitable routes found'}
return cleanup(plugin, label, payload, rpc_result) return cleanup(plugin, label, payload, rpc_result)
continue continue
@@ -256,7 +263,7 @@ def rebalance(plugin, outgoing_scid, incoming_scid, msatoshi: Millisatoshi = Non
except RpcError as e: except RpcError as e:
time_sendpay += time.time() - time_start time_sendpay += time.time() - time_start
plugin.log(f"maxhops:{maxhops} msatfactor:{msatfactor} running_for:{int(time.time()) - start_ts} count_getroute:{count} time_getroute:{time_getroute} time_getroute_avg:{time_getroute / count} count_sendpay:{count_sendpay} time_sendpay:{time_sendpay} time_sendpay_avg:{time_sendpay / count_sendpay}", 'debug') plugin.log(f"maxhops:{_maxhops} msatfactor:{_msatfactor} running_for:{int(time.time()) - start_ts} count_getroute:{count} time_getroute:{time_getroute} time_getroute_avg:{time_getroute / count} count_sendpay:{count_sendpay} time_sendpay:{time_sendpay} time_sendpay_avg:{time_sendpay / count_sendpay}", 'debug')
#plugin.log(f"RpcError: {str(e)}", 'debug') #plugin.log(f"RpcError: {str(e)}", 'debug')
# check if we ran into the `rpc.waitsendpay` timeout # check if we ran into the `rpc.waitsendpay` timeout
if e.method == "waitsendpay" and e.error.get('code') == 200: if e.method == "waitsendpay" and e.error.get('code') == 200:
@@ -483,7 +490,9 @@ def maybe_rebalance_pairs(plugin: Plugin, ch1, ch2, failed_channels: list):
plugin.log(f"Try to rebalance: {scid1} -> {scid2}; amount={amount}; maxfee={maxfee}") plugin.log(f"Try to rebalance: {scid1} -> {scid2}; amount={amount}; maxfee={maxfee}")
start_ts = time.time() start_ts = time.time()
try: try:
res = rebalance(plugin, outgoing_scid=scid1, incoming_scid=scid2, msatoshi=amount, maxfeepercent=0, retry_for=1200, exemptfee=maxfee) res = rebalance(plugin, outgoing_scid=scid1, incoming_scid=scid2,
msatoshi=amount, maxfeepercent=0, retry_for=1200,
exemptfee=maxfee, maxhops=6, msatfactor=1)
if not res.get('status') == 'complete': if not res.get('status') == 'complete':
raise Exception # fall into exception handler below raise Exception # fall into exception handler below
except Exception: except Exception: