rebalance: better feeadjuster integration

- turns off feeadjuster's automatic fee updates while rebalance running
- runs feeadjust only once, at the end
- measures time for rebalance
This commit is contained in:
Gálli Zoltán
2021-01-15 10:06:30 +01:00
committed by Michael Schmoock
parent 7c8840266d
commit b80c558ebd

View File

@@ -1,6 +1,7 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from pyln.client import Plugin, Millisatoshi, RpcError from pyln.client import Plugin, Millisatoshi, RpcError
from threading import Thread, Lock from threading import Thread, Lock
from datetime import timedelta
import time import time
import uuid import uuid
@@ -314,10 +315,11 @@ def get_ideal_ratio(channels: list, enough_liquidity: Millisatoshi):
def feeadjust_would_be_nice(plugin: Plugin): def feeadjust_would_be_nice(plugin: Plugin):
try: commands = [c for c in plugin.rpc.help().get("help") if c["command"].split()[0] == "feeadjust"]
if len(commands) == 1:
msg = plugin.rpc.feeadjust() msg = plugin.rpc.feeadjust()
plugin.log(f"Feeadjust succeeded: {msg}") plugin.log(f"Feeadjust succeeded: {msg}")
except Exception: else:
plugin.log("The feeadjuster plugin would be useful here") plugin.log("The feeadjuster plugin would be useful here")
@@ -375,6 +377,7 @@ def maybe_rebalance_pairs(plugin: Plugin, ch1, ch2, failed_pairs: list):
amount = min(amount, get_max_amount(i, plugin)) amount = min(amount, get_max_amount(i, plugin))
maxfee = get_max_fee(plugin, amount) maxfee = get_max_fee(plugin, amount)
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()
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)
except Exception: except Exception:
@@ -386,6 +389,8 @@ def maybe_rebalance_pairs(plugin: Plugin, ch1, ch2, failed_pairs: list):
if amount > get_max_amount(i, plugin): if amount > get_max_amount(i, plugin):
continue continue
return result return result
elapsed_time = timedelta(seconds=time.time() - start_ts)
res["elapsed_time"] = str(elapsed_time)[:-3]
plugin.log(f"Rebalance succeeded: {res}") plugin.log(f"Rebalance succeeded: {res}")
result["success"] = True result["success"] = True
result["fee_spent"] += res["fee"] result["fee_spent"] += res["fee"]
@@ -408,10 +413,21 @@ def maybe_rebalance_once(plugin: Plugin, failed_pairs: list):
return {"success": False, "fee_spent": Millisatoshi(0)} return {"success": False, "fee_spent": Millisatoshi(0)}
def feeadjuster_toggle(plugin: Plugin, new_value: bool):
commands = [c for c in plugin.rpc.help().get("help") if c["command"].split()[0] == "feeadjustertoggle"]
if len(commands) == 1:
msg = plugin.rpc.feeadjustertoggle(new_value)
return msg["forward_event_subscription"]["previous"]
else:
return True
def rebalanceall_thread(plugin: Plugin): def rebalanceall_thread(plugin: Plugin):
if not plugin.mutex.acquire(blocking=False): if not plugin.mutex.acquire(blocking=False):
return return
try: try:
start_ts = time.time()
feeadjuster_state = feeadjuster_toggle(plugin, False)
channels = get_our_channels(plugin) channels = get_our_channels(plugin)
plugin.enough_liquidity = get_enough_liquidity_threshold(channels) plugin.enough_liquidity = get_enough_liquidity_threshold(channels)
plugin.ideal_ratio = get_ideal_ratio(channels, plugin.enough_liquidity) plugin.ideal_ratio = get_ideal_ratio(channels, plugin.enough_liquidity)
@@ -428,8 +444,10 @@ def rebalanceall_thread(plugin: Plugin):
break break
success += 1 success += 1
fee_spent += result["fee_spent"] fee_spent += result["fee_spent"]
feeadjust_would_be_nice(plugin) feeadjust_would_be_nice(plugin)
plugin.log(f"Automatic rebalance finished: {success} successful rebalance, {fee_spent} fee spent") feeadjuster_toggle(plugin, feeadjuster_state)
elapsed_time = timedelta(seconds=time.time() - start_ts)
plugin.log(f"Automatic rebalance finished: {success} successful rebalance, {fee_spent} fee spent, it took {str(elapsed_time)[:-3]}")
finally: finally:
plugin.mutex.release() plugin.mutex.release()
@@ -447,7 +465,7 @@ def rebalanceall(plugin: Plugin, min_amount: Millisatoshi = Millisatoshi("50000s
plugin.min_amount = Millisatoshi(min_amount) plugin.min_amount = Millisatoshi(min_amount)
t = Thread(target=rebalanceall_thread, args=(plugin, )) t = Thread(target=rebalanceall_thread, args=(plugin, ))
t.start() t.start()
return {"message": "Rebalance started"} return {"message": f"Rebalance started with min rebalancable amount: {plugin.min_amount}, feeratio: {plugin.feeratio}"}
@plugin.method("rebalancestop") @plugin.method("rebalancestop")
@@ -470,7 +488,8 @@ def init(options, configuration, plugin):
plugin.fee_base = Millisatoshi(config.get("fee-base")) plugin.fee_base = Millisatoshi(config.get("fee-base"))
plugin.fee_ppm = config.get("fee-per-satoshi") plugin.fee_ppm = config.get("fee-per-satoshi")
plugin.mutex = Lock() plugin.mutex = Lock()
plugin.log("Plugin rebalance.py initialized") plugin.log(f"Plugin rebalance initialized with {plugin.fee_base} base / {plugin.fee_ppm} ppm fee, "
f"cltv_final: {plugin.cltv_final}")
plugin.run() plugin.run()