diff --git a/rebalance/rebalance.py b/rebalance/rebalance.py index d7c0f31..c4098af 100755 --- a/rebalance/rebalance.py +++ b/rebalance/rebalance.py @@ -490,12 +490,23 @@ def rebalanceall(plugin: Plugin, min_amount: Millisatoshi = Millisatoshi("50000s To be economical, it tries to fix the liquidity cheaper than it can be ruined by transaction forwards. It may run for a long time (hours) in the background, but can be stopped with the rebalancestop method. """ + # some early checks before we start the async thread if plugin.mutex.locked(): return {"message": "Rebalance is already running, this may take a while. To stop it use the cli method 'rebalancestop'."} - if len(get_open_channels(plugin)) <= 1: - return {"message": "Error: Not enough open channels to balance anything"} + channels = get_open_channels(plugin) + if len(channels) <= 1: + return {"message": "Error: Not enough open channels to rebalance anything"} + our = sum(ch["to_us_msat"] for ch in channels) + total = sum(ch["total_msat"] for ch in channels) + min_amount = Millisatoshi(min_amount) + if total - our < min_amount or our < min_amount: + return {"message": "Error: Not enough liquidity to rebalance anything"} + + # param parsing ensure correct type plugin.feeratio = float(feeratio) - plugin.min_amount = Millisatoshi(min_amount) + plugin.min_amount = min_amount + + # run the job t = Thread(target=rebalanceall_thread, args=(plugin, )) t.start() return {"message": f"Rebalance started with min rebalancable amount: {plugin.min_amount}, feeratio: {plugin.feeratio}"} diff --git a/rebalance/test_rebalance.py b/rebalance/test_rebalance.py index 7967d85..bcac17f 100644 --- a/rebalance/test_rebalance.py +++ b/rebalance/test_rebalance.py @@ -85,9 +85,21 @@ def test_rebalance_all(node_factory, bitcoind): # check we get an error if theres just one channel result = l1.rpc.rebalanceall() - assert result['message'] == 'Error: Not enough open channels to balance anything' + assert result['message'] == 'Error: Not enough open channels to rebalance anything' - # now we form a circle so we can do rebalanceall + # now we add another 100% outgoing liquidity to l1 which does not help + l4 = node_factory.get_node() + l1.connect(l4) + l1.fundchannel(l4) + + # test this is still not possible + result = l1.rpc.rebalanceall() + assert result['message'] == 'Error: Not enough liquidity to rebalance anything' + + # remove l4 it does not distort further testing + l1.rpc.close(l1.get_channel_scid(l4)) + + # now we form a circle so we can do actually rebalanceall l3.connect(l1) l3.fundchannel(l1)