From 7ae25feb1f38a9dcb2cd9b2ff9e24b9ac0d123d0 Mon Sep 17 00:00:00 2001 From: Michael Schmoock Date: Sun, 22 Nov 2020 17:52:58 +0100 Subject: [PATCH] drain: fix Millisatoshi from float bug This was compatible in the past but pyln changed and doing e.g. `10000msat * 0.01 * percentage` became unreliable on rounding errors. --- drain/drain.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/drain/drain.py b/drain/drain.py index 9869dd3..c726e59 100755 --- a/drain/drain.py +++ b/drain/drain.py @@ -140,7 +140,7 @@ def test_or_set_chunks(plugin, payload): cmd = payload['command'] spendable, receivable = spendable_from_scid(plugin, payload) total = spendable + receivable - amount = total * 0.01 * payload['percentage'] + amount = Millisatoshi(int(int(total) * (0.01 * payload['percentage']))) # if capacity exceeds, limit amount to full or empty channel if cmd == "drain" and amount > spendable: @@ -345,7 +345,7 @@ def read_params(command: str, scid: str, percentage: float, if command == 'setbalance': spendable, receivable = spendable_from_scid(plugin, payload) total = spendable + receivable - target = total * 0.01 * payload['percentage'] + target = Millisatoshi(int(int(total) * (0.01 * payload['percentage']))) if target == spendable: raise RpcError(payload['command'], payload, {'message': 'target already reached, nothing to do.'}) if spendable > target: @@ -373,7 +373,7 @@ def execute(payload: dict): # as fees from previous chunks affect reserves spendable, receivable = spendable_from_scid(plugin, payload) total = spendable + receivable - amount = total * 0.01 * payload['percentage'] / payload['chunks'] + amount = Millisatoshi(int(int(total) * (0.01 * payload['percentage'] / payload['chunks']))) if amount == Millisatoshi(0): raise RpcError(payload['command'], payload, {'message': 'Cannot process chunk. Amount would be 0msat.'})