From b97e51dbd832b87d0a74ff02b9737f26e6dbef70 Mon Sep 17 00:00:00 2001 From: bitromortac Date: Thu, 22 Apr 2021 08:52:12 +0200 Subject: [PATCH] swaps: fix off-by-one sanity check Tolerates discrepancies in the swap amount crosschecks. To ensure we calculate the send/receive amounts correctly we apply a check, using amount inversion. The inversion is not exact up to +-1 due to used floor and ceil functions in the methods. They are not invertible, which is why we relax the check to off-by-ones. --- electrum/submarine_swaps.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/electrum/submarine_swaps.py b/electrum/submarine_swaps.py index 129d5e9e4..e5ad617d2 100644 --- a/electrum/submarine_swaps.py +++ b/electrum/submarine_swaps.py @@ -519,10 +519,11 @@ class SwapManager(Logger): recv_amount = self._get_recv_amount(send_amount, is_reverse=is_reverse) # sanity check calculation can be inverted if recv_amount is not None: - inverted_recv_amount = self._get_send_amount(recv_amount, is_reverse=is_reverse) - if send_amount != inverted_recv_amount: + inverted_send_amount = self._get_send_amount(recv_amount, is_reverse=is_reverse) + # accept off-by ones as amt_rcv = recv_amt(send_amt(amt_rcv)) only up to +-1 + if abs(send_amount - inverted_send_amount) > 1: raise Exception(f"calc-invert-sanity-check failed. is_reverse={is_reverse}. " - f"send_amount={send_amount} -> recv_amount={recv_amount} -> inverted_recv_amount={inverted_recv_amount}") + f"send_amount={send_amount} -> recv_amount={recv_amount} -> inverted_send_amount={inverted_send_amount}") # account for on-chain claim tx fee if is_reverse and recv_amount is not None: recv_amount -= self.get_claim_fee() @@ -532,10 +533,10 @@ class SwapManager(Logger): send_amount = self._get_send_amount(recv_amount, is_reverse=is_reverse) # sanity check calculation can be inverted if send_amount is not None: - inverted_send_amount = self._get_recv_amount(send_amount, is_reverse=is_reverse) - if recv_amount != inverted_send_amount: + inverted_recv_amount = self._get_recv_amount(send_amount, is_reverse=is_reverse) + if recv_amount != inverted_recv_amount: raise Exception(f"calc-invert-sanity-check failed. is_reverse={is_reverse}. " - f"recv_amount={recv_amount} -> send_amount={send_amount} -> inverted_send_amount={inverted_send_amount}") + f"recv_amount={recv_amount} -> send_amount={send_amount} -> inverted_recv_amount={inverted_recv_amount}") # account for on-chain claim tx fee if is_reverse and send_amount is not None: send_amount += self.get_claim_fee()