From 81649dcf9b2e13e8ddb4d67fda50bfd507873577 Mon Sep 17 00:00:00 2001
From: tecnovert <tecnovert@tecnovert.net>
Date: Thu, 25 Jan 2024 17:04:49 +0200
Subject: [PATCH] Set reverse bid state to error when bidaccept fails.

---
 basicswap/basicswap.py          | 18 ++++++++-------
 tests/basicswap/test_btc_xmr.py | 41 +++++++++++++++++++++++++++++----
 2 files changed, 47 insertions(+), 12 deletions(-)

diff --git a/basicswap/basicswap.py b/basicswap/basicswap.py
index 977f9c9..bdbb2a1 100644
--- a/basicswap/basicswap.py
+++ b/basicswap/basicswap.py
@@ -2179,7 +2179,7 @@ class BasicSwap(BaseApp):
 
     def postBid(self, offer_id: bytes, amount: int, addr_send_from: str = None, extra_options={}) -> bytes:
         # Bid to send bid.amount * bid.rate of coin_to in exchange for bid.amount of coin_from
-        self.log.debug('postBid %s', offer_id.hex())
+        self.log.debug('postBid for offer: %s', offer_id.hex())
 
         offer = self.getOffer(offer_id)
         ensure(offer, 'Offer not found: {}.'.format(offer_id.hex()))
@@ -2557,7 +2557,7 @@ class BasicSwap(BaseApp):
             bid_rate: int = extra_options.get('bid_rate', offer.rate)
             amount_to: int = int((int(amount) * bid_rate) // ci_from.COIN())
 
-            bid_created_at = self.getTime()
+            bid_created_at: int = self.getTime()
             if offer.swap_type != SwapTypes.XMR_SWAP:
                 raise ValueError('TODO: Unknown swap type ' + offer.swap_type.name)
 
@@ -2567,8 +2567,8 @@ class BasicSwap(BaseApp):
 
             self.checkCoinsReady(coin_from, coin_to)
 
-            balance_to = ci_to.getSpendableBalance()
-            ensure(balance_to > amount_to, '{} spendable balance is too low: {}'.format(ci_to.coin_name(), ci_to.format_amount(balance_to)))
+            balance_to: int = ci_to.getSpendableBalance()
+            ensure(balance_to > amount_to, '{} spendable balance is too low: {} < {}'.format(ci_to.coin_name(), ci_to.format_amount(balance_to), ci_to.format_amount(amount_to)))
 
             reverse_bid: bool = self.is_reverse_ads_bid(coin_from)
             if reverse_bid:
@@ -4361,9 +4361,11 @@ class BasicSwap(BaseApp):
 
                         # If delaying with no (further) queued actions reset state
                         if self.countQueuedActions(session, bid_id, None) < 2:
-                            bid = self.getBid(bid_id, session)
-                            if bid and bid.state == BidStates.SWAP_DELAYING:
-                                bid.setState(BidStates.BID_RECEIVED)
+                            bid, offer = self.getBidAndOffer(bid_id)
+                            last_state = getLastBidState(bid.states)
+                            if bid and bid.state == BidStates.SWAP_DELAYING and last_state == BidStates.BID_RECEIVED:
+                                new_state = BidStates.BID_ERROR if offer.bid_reversed else BidStates.BID_RECEIVED
+                                bid.setState(new_state)
                                 self.saveBidInSession(bid_id, bid, session)
                     else:
                         bid = self.getBid(bid_id, session)
@@ -6063,7 +6065,7 @@ class BasicSwap(BaseApp):
                 self.processADSBidReversedAccept(msg)
 
         except InactiveCoin as ex:
-            self.log.info('Ignoring message involving inactive coin {}, type {}'.format(Coins(ex.coinid).name, MessageTypes(msg_type).name))
+            self.log.debug('Ignoring message involving inactive coin {}, type {}'.format(Coins(ex.coinid).name, MessageTypes(msg_type).name))
         except Exception as ex:
             self.log.error('processMsg %s', str(ex))
             if self.debug:
diff --git a/tests/basicswap/test_btc_xmr.py b/tests/basicswap/test_btc_xmr.py
index ba25d84..6b54477 100644
--- a/tests/basicswap/test_btc_xmr.py
+++ b/tests/basicswap/test_btc_xmr.py
@@ -1101,7 +1101,7 @@ class BasicSwapTest(TestFunctions):
         coin_from = self.test_coin_from
         coin_to = Coins.XMR
 
-        self.prepare_balance(self.test_coin_from, 10.0, 1802, 1800)
+        self.prepare_balance(coin_from, 10.0, 1802, 1800)
 
         id_offerer: int = self.node_c_id
         id_bidder: int = self.node_b_id
@@ -1110,10 +1110,10 @@ class BasicSwapTest(TestFunctions):
         ci_from = swap_clients[id_offerer].ci(coin_from)
         ci_to = swap_clients[id_bidder].ci(coin_to)
 
-        js_0 = read_json_api(1800 + id_offerer, 'wallets')
-        node0_from_before: float = self.getBalance(js_0, coin_from)
+        jsw = read_json_api(1800 + id_offerer, 'wallets')
+        balance_from_before: float = self.getBalance(jsw, coin_from)
 
-        amt_swap: int = ci_from.make_int(node0_from_before, r=1)
+        amt_swap: int = ci_from.make_int(balance_from_before, r=1)
         rate_swap: int = ci_to.make_int(2.0, r=1)
         offer_id = swap_clients[id_offerer].postOffer(coin_from, coin_to, amt_swap, rate_swap, amt_swap, SwapTypes.XMR_SWAP, auto_accept_bids=True)
         wait_for_offer(test_delay_event, swap_clients[id_bidder], offer_id)
@@ -1125,6 +1125,39 @@ class BasicSwapTest(TestFunctions):
 
         wait_for_bid(test_delay_event, swap_clients[id_offerer], bid_id, BidStates.BID_RECEIVED, wait_for=20)
 
+    def test_08_insufficient_funds_rev(self):
+        tla_from = self.test_coin_from.name
+        logging.info('---------- Test {} Insufficient Funds (reverse)'.format(tla_from))
+        swap_clients = self.swap_clients
+        coin_from = Coins.XMR
+        coin_to = self.test_coin_from
+
+        self.prepare_balance(coin_to, 10.0, 1802, 1800)
+
+        id_offerer: int = self.node_b_id
+        id_bidder: int = self.node_c_id
+
+        swap_clients = self.swap_clients
+        ci_from = swap_clients[id_offerer].ci(coin_from)
+        ci_to = swap_clients[id_bidder].ci(coin_to)
+
+        jsw = read_json_api(1800 + id_bidder, 'wallets')
+        balance_to_before: float = self.getBalance(jsw, coin_to)
+
+        amt_swap: int = ci_from.make_int(balance_to_before, r=1)
+        rate_swap: int = ci_to.make_int(1.0, r=1)
+
+        amt_swap -= 1
+        offer_id = swap_clients[id_offerer].postOffer(coin_from, coin_to, amt_swap, rate_swap, amt_swap, SwapTypes.XMR_SWAP, auto_accept_bids=True)
+        wait_for_offer(test_delay_event, swap_clients[id_bidder], offer_id)
+
+        bid_id = swap_clients[id_bidder].postXmrBid(offer_id, amt_swap)
+
+        event = wait_for_event(test_delay_event, swap_clients[id_bidder], Concepts.BID, bid_id, event_type=EventLogTypes.ERROR, wait_for=60)
+        assert ('Insufficient funds' in event.event_msg)
+
+        wait_for_bid(test_delay_event, swap_clients[id_bidder], bid_id, BidStates.BID_ERROR, sent=True, wait_for=20)
+
 
 class TestBTC(BasicSwapTest):
     __test__ = True