From 9a0fff2571fa4e992b30e2ee5c1aa7bc8ee7b21f Mon Sep 17 00:00:00 2001 From: SomberNight Date: Thu, 12 Jan 2023 18:12:05 +0000 Subject: [PATCH] wallet.get_request_by_addr: make deterministic This makes test_invoices/test_wallet_get_request_by_addr pass without flakyness. closes https://github.com/spesmilo/electrum/issues/8113 --- electrum/wallet.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/electrum/wallet.py b/electrum/wallet.py index a4568eabe..b532a3f09 100644 --- a/electrum/wallet.py +++ b/electrum/wallet.py @@ -2355,8 +2355,13 @@ class Abstract_Wallet(ABC, Logger, EventListener): if not req.is_lightning() or self.lnworker.get_invoice_status(req) == PR_UNPAID] if not reqs: return None - # note: there typically should not be more than one relevant request for an address - return reqs[0] + # note: There typically should not be more than one relevant request for an address. + # If there's multiple, return the one created last (see #8113). Consider: + # - there is an old expired req1, and a newer unpaid req2, reusing the same addr (and same amount), + # - now req2 gets paid. however, get_invoice_status will say both req1 and req2 are PAID. (see #8061) + # - as a workaround, we return the request with the larger creation time. + reqs.sort(key=lambda req: req.get_time()) + return reqs[-1] def get_request(self, request_id: str) -> Optional[Invoice]: return self._receive_requests.get(request_id)