Browse Source

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.
patch-4
bitromortac 4 years ago
parent
commit
b97e51dbd8
No known key found for this signature in database GPG Key ID: 1965063FC13BEBE2
  1. 13
      electrum/submarine_swaps.py

13
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()

Loading…
Cancel
Save