From c6f8e5e2bac719dda3476c45de5c6308ef45d544 Mon Sep 17 00:00:00 2001 From: tecnovert Date: Mon, 26 May 2025 19:37:26 +0200 Subject: [PATCH] Set dleag_size per bid --- basicswap/basicswap.py | 47 +++++++++------ basicswap/db.py | 8 +++ basicswap/network/simplex.py | 73 +++++++++++++----------- basicswap/ui/app.py | 7 ++- tests/basicswap/extended/test_simplex.py | 14 ----- 5 files changed, 83 insertions(+), 66 deletions(-) diff --git a/basicswap/basicswap.py b/basicswap/basicswap.py index 9d36b7b..84f6225 100644 --- a/basicswap/basicswap.py +++ b/basicswap/basicswap.py @@ -444,15 +444,11 @@ class BasicSwap(BaseApp, UIApp): "restrict_unknown_seed_wallets", True ) self._max_check_loop_blocks = self.settings.get("max_check_loop_blocks", 100000) - self._bid_expired_leeway = 5 + self._use_direct_message_routes = False self.swaps_in_progress = dict() - self._dleag_split_size_init = 16000 - self._dleag_split_size = 17000 - self._use_direct_message_routes = False - self.SMSG_SECONDS_IN_HOUR = ( 60 * 60 ) # Note: Set smsgsregtestadjust=0 for regtest @@ -3798,20 +3794,21 @@ class BasicSwap(BaseApp, UIApp): msg_type, addr_from: str, addr_to: str, - bid_id: bytes, + xmr_swap, dleag: bytes, msg_valid: int, bid_msg_ids, cursor, ) -> None: - sent_bytes = self._dleag_split_size_init + dleag_split_size_init, dleag_split_size = xmr_swap.getMsgSplitInfo() + sent_bytes = dleag_split_size_init num_sent = 1 while sent_bytes < len(dleag): - size_to_send: int = min(self._dleag_split_size, len(dleag) - sent_bytes) + size_to_send: int = min(dleag_split_size, len(dleag) - sent_bytes) msg_buf = XmrSplitMessage( - msg_id=bid_id, + msg_id=xmr_swap.bid_id, msg_type=msg_type, sequence=num_sent, dleag=dleag[sent_bytes : sent_bytes + size_to_send], @@ -3867,8 +3864,9 @@ class BasicSwap(BaseApp, UIApp): msg_buf.pkaf = xmr_swap.pkaf msg_buf.kbvf = xmr_swap.vkbvf - if len(xmr_swap.kbsf_dleag) > self._dleag_split_size_init: - msg_buf.kbsf_dleag = xmr_swap.kbsf_dleag[: self._dleag_split_size_init] + dleag_split_size_init, _ = xmr_swap.getMsgSplitInfo() + if len(xmr_swap.kbsf_dleag) > dleag_split_size_init: + msg_buf.kbsf_dleag = xmr_swap.kbsf_dleag[:dleag_split_size_init] else: msg_buf.kbsf_dleag = xmr_swap.kbsf_dleag @@ -3902,12 +3900,14 @@ class BasicSwap(BaseApp, UIApp): ) bid_msg_ids = {} + if xmr_swap.bid_id is None: + xmr_swap.bid_id = bid_id if ci_to.curve_type() == Curves.ed25519: self.sendXmrSplitMessages( XmrSplitMsgTypes.BID, bid.bid_addr, offer.addr_from, - bid_id, + xmr_swap, xmr_swap.kbsf_dleag, msg_valid, bid_msg_ids, @@ -4111,6 +4111,7 @@ class BasicSwap(BaseApp, UIApp): xmr_swap = XmrSwap() xmr_swap.contract_count = self.getNewContractId(cursor) + self.setMsgSplitInfo(xmr_swap) bid = Bid( protocol_version=PROTOCOL_VERSION_ADAPTOR_SIG, @@ -4165,6 +4166,7 @@ class BasicSwap(BaseApp, UIApp): xmr_swap = XmrSwap() xmr_swap.contract_count = self.getNewContractId(cursor) + self.setMsgSplitInfo(xmr_swap) address_out = self.getReceiveAddressFromPool( coin_from, offer_id, TxTypes.XMR_SWAP_A_LOCK, cursor=cursor @@ -4518,9 +4520,10 @@ class BasicSwap(BaseApp, UIApp): msg_buf.pkal = xmr_swap.pkal msg_buf.kbvl = kbvl + dleag_split_size_init, _ = xmr_swap.getMsgSplitInfo() if ci_to.curve_type() == Curves.ed25519: xmr_swap.kbsl_dleag = ci_to.proveDLEAG(kbsl) - msg_buf.kbsl_dleag = xmr_swap.kbsl_dleag[: self._dleag_split_size_init] + msg_buf.kbsl_dleag = xmr_swap.kbsl_dleag[:dleag_split_size_init] elif ci_to.curve_type() == Curves.secp256k1: for i in range(10): xmr_swap.kbsl_dleag = ci_to.signRecoverable( @@ -4564,7 +4567,7 @@ class BasicSwap(BaseApp, UIApp): XmrSplitMsgTypes.BID_ACCEPT, addr_from, addr_to, - xmr_swap.bid_id, + xmr_swap, xmr_swap.kbsl_dleag, msg_valid, bid_msg_ids, @@ -4683,6 +4686,7 @@ class BasicSwap(BaseApp, UIApp): xmr_swap_1.setDLEAG(xmr_swap, ci_to, kbsf) assert xmr_swap.pkasf == ci_from.getPubkey(kbsf) + dleag_split_size_init, _ = xmr_swap.getMsgSplitInfo() msg_buf = ADSBidIntentAcceptMessage() msg_buf.bid_msg_id = bid_id msg_buf.dest_af = xmr_swap.dest_af @@ -4690,8 +4694,8 @@ class BasicSwap(BaseApp, UIApp): msg_buf.kbvf = kbvf msg_buf.kbsf_dleag = ( xmr_swap.kbsf_dleag - if len(xmr_swap.kbsf_dleag) < self._dleag_split_size_init - else xmr_swap.kbsf_dleag[: self._dleag_split_size_init] + if len(xmr_swap.kbsf_dleag) < dleag_split_size_init + else xmr_swap.kbsf_dleag[:dleag_split_size_init] ) bid_bytes = msg_buf.to_bytes() @@ -4712,7 +4716,7 @@ class BasicSwap(BaseApp, UIApp): XmrSplitMsgTypes.BID, addr_from, addr_to, - xmr_swap.bid_id, + xmr_swap, xmr_swap.kbsf_dleag, msg_valid, bid_msg_ids, @@ -8468,6 +8472,7 @@ class BasicSwap(BaseApp, UIApp): pkbvf=ci_to.getPubkey(bid_data.kbvf), kbsf_dleag=bid_data.kbsf_dleag, ) + self.setMsgSplitInfo(xmr_swap) wallet_restore_height = self.getWalletRestoreHeight(ci_to) if bid.chain_b_height_start < wallet_restore_height: bid.chain_b_height_start = wallet_restore_height @@ -9982,6 +9987,7 @@ class BasicSwap(BaseApp, UIApp): xmr_swap = XmrSwap( bid_id=bid_id, ) + self.setMsgSplitInfo(xmr_swap) wallet_restore_height = self.getWalletRestoreHeight(ci_to) if bid.chain_b_height_start < wallet_restore_height: bid.chain_b_height_start = wallet_restore_height @@ -12205,6 +12211,13 @@ class BasicSwap(BaseApp, UIApp): return rv + def setMsgSplitInfo(self, xmr_swap) -> None: + for network in self.active_networks: + if network["type"] == "simplex": + xmr_swap.msg_split_info = "9000:11000" + return + xmr_swap.msg_split_info = "16000:17000" + def setFilters(self, prefix, filters): key_str = "saved_filters_" + prefix value_str = json.dumps(filters) diff --git a/basicswap/db.py b/basicswap/db.py index f421e40..25e2e94 100644 --- a/basicswap/db.py +++ b/basicswap/db.py @@ -486,6 +486,14 @@ class XmrSwap(Table): b_lock_tx_id = Column("blob") + msg_split_info = Column("string") + + def getMsgSplitInfo(self): + if self.msg_split_info is None: + return 16000, 17000 + msg_split_info = self.msg_split_info.split(":") + return int(msg_split_info[0]), int(msg_split_info[1]) + class XmrSplitData(Table): __tablename__ = "xmr_split_data" diff --git a/basicswap/network/simplex.py b/basicswap/network/simplex.py index e1cd1f0..ca69d91 100644 --- a/basicswap/network/simplex.py +++ b/basicswap/network/simplex.py @@ -119,9 +119,9 @@ class WebSocketThread(threading.Thread): self.ws.send(cmd) return self.corrId - def wait_for_command_response(self, cmd_id): + def wait_for_command_response(self, cmd_id, num_tries: int = 200): cmd_id = str(cmd_id) - for i in range(100): + for i in range(num_tries): message = self.cmd_queue_get() if message is not None: data = json.loads(message) @@ -129,7 +129,9 @@ class WebSocketThread(threading.Thread): if data["corrId"] == cmd_id: return data self.delay_event.wait(0.5) - raise ValueError(f"waitForResponse timed-out waiting for id: {cmd_id}") + raise ValueError( + f"wait_for_command_response timed-out waiting for ID: {cmd_id}" + ) def run(self): self.ws = websocket.WebSocketApp( @@ -151,7 +153,7 @@ class WebSocketThread(threading.Thread): def waitForResponse(ws_thread, sent_id, delay_event): sent_id = str(sent_id) - for i in range(100): + for i in range(200): message = ws_thread.cmd_queue_get() if message is not None: data = json.loads(message) @@ -159,7 +161,7 @@ def waitForResponse(ws_thread, sent_id, delay_event): if data["corrId"] == sent_id: return data delay_event.wait(0.5) - raise ValueError(f"waitForResponse timed-out waiting for id: {sent_id}") + raise ValueError(f"waitForResponse timed-out waiting for ID: {sent_id}") def waitForConnected(ws_thread, delay_event): @@ -457,34 +459,41 @@ def initialiseSimplexNetwork(self, network_config) -> None: def closeSimplexChat(self, net_i, connId) -> bool: - cmd_id = net_i.send_command("/chats") - response = net_i.wait_for_command_response(cmd_id) - remote_name = None - for chat in response["resp"]["chats"]: - if ( - "chatInfo" not in chat - or "type" not in chat["chatInfo"] - or chat["chatInfo"]["type"] != "direct" - ): - continue - try: - if chat["chatInfo"]["contact"]["activeConn"]["connId"] == connId: - remote_name = chat["chatInfo"]["contact"]["localDisplayName"] - break - except Exception as e: - self.log.debug(f"Error parsing chat: {e}") + try: + cmd_id = net_i.send_command("/chats") + response = net_i.wait_for_command_response(cmd_id, num_tries=500) + remote_name = None + for chat in response["resp"]["chats"]: + if ( + "chatInfo" not in chat + or "type" not in chat["chatInfo"] + or chat["chatInfo"]["type"] != "direct" + ): + continue + try: + if chat["chatInfo"]["contact"]["activeConn"]["connId"] == connId: + remote_name = chat["chatInfo"]["contact"]["localDisplayName"] + break + except Exception as e: + self.log.debug(f"Error parsing chat: {e}") - if remote_name is None: - self.log.warning( - f"Unable to find remote name for simplex direct chat, ID: {connId}" - ) - return False + if remote_name is None: + self.log.warning( + f"Unable to find remote name for simplex direct chat, ID: {connId}" + ) + return False - self.log.debug(f"Deleting simplex chat @{remote_name}, connID {connId}") - cmd_id = net_i.send_command(f"/delete @{remote_name}") - cmd_response = net_i.wait_for_command_response(cmd_id) - if cmd_response["resp"]["type"] != "contactDeleted": - self.log.warning(f"Failed to delete simplex chat, ID: {connId}") - self.log.debug("cmd_response: {}".format(json.dumps(cmd_response, indent=4))) + self.log.debug(f"Deleting simplex chat @{remote_name}, connID {connId}") + cmd_id = net_i.send_command(f"/delete @{remote_name}") + cmd_response = net_i.wait_for_command_response(cmd_id) + + if cmd_response["resp"]["type"] != "contactDeleted": + self.log.warning(f"Failed to delete simplex chat, ID: {connId}") + self.log.debug( + "cmd_response: {}".format(json.dumps(cmd_response, indent=4)) + ) + return False + except Exception as e: + self.log.warning(f"Error deleting simplex chat, ID: {connId} - {e}") return False return True diff --git a/basicswap/ui/app.py b/basicswap/ui/app.py index c66aa9d..c78b79c 100644 --- a/basicswap/ui/app.py +++ b/basicswap/ui/app.py @@ -34,7 +34,6 @@ class UIApp: query_str: str = ( "SELECT record_id, network_id, route_data" + " FROM direct_message_routes " - + " WHERE active_ind = 1 " ) query_str += filter_query_str rows = cursor.execute(query_str, query_data).fetchall() @@ -47,10 +46,10 @@ class UIApp: raise ValueError("Unknown action") query_str: str = ( - "SELECT record_id, network_id, linked_type, linked_id, " + "SELECT record_id, network_id, active_ind, linked_type, linked_id, " + " smsg_addr_local, smsg_addr_remote, route_data, created_at" + " FROM direct_message_routes " - + " WHERE active_ind = 1 " + + " WHERE active_ind > 0 " ) query_str += filter_query_str @@ -71,6 +70,7 @@ class UIApp: ( record_id, network_id, + active_ind, linked_type, linked_id, smsg_addr_local, @@ -82,6 +82,7 @@ class UIApp: { "record_id": record_id, "network_id": network_id, + "active_ind": active_ind, "smsg_addr_local": smsg_addr_local, "smsg_addr_remote": smsg_addr_remote, "route_data": json.loads(route_data.decode("UTF-8")), diff --git a/tests/basicswap/extended/test_simplex.py b/tests/basicswap/extended/test_simplex.py index 2311e34..bc7a716 100644 --- a/tests/basicswap/extended/test_simplex.py +++ b/tests/basicswap/extended/test_simplex.py @@ -463,8 +463,6 @@ class Test(BaseTest): swap_clients = self.swap_clients for sc in swap_clients: - sc._dleag_split_size_init = 9000 - sc._dleag_split_size = 11000 sc._use_direct_messages = False assert len(swap_clients[0].active_networks) == 1 @@ -523,8 +521,6 @@ class Test(BaseTest): swap_clients = self.swap_clients for sc in swap_clients: - sc._dleag_split_size_init = 9000 - sc._dleag_split_size = 11000 sc._use_direct_messages = False assert len(swap_clients[0].active_networks) == 1 @@ -583,8 +579,6 @@ class Test(BaseTest): swap_clients = self.swap_clients for sc in swap_clients: - sc._dleag_split_size_init = 9000 - sc._dleag_split_size = 11000 sc._use_direct_message_routes = True assert len(swap_clients[0].active_networks) == 1 @@ -659,8 +653,6 @@ class Test(BaseTest): swap_clients = self.swap_clients for sc in swap_clients: - sc._dleag_split_size_init = 9000 - sc._dleag_split_size = 11000 sc._use_direct_message_routes = True assert len(swap_clients[0].active_networks) == 1 @@ -733,8 +725,6 @@ class Test(BaseTest): swap_clients = self.swap_clients for sc in swap_clients: - sc._dleag_split_size_init = 9000 - sc._dleag_split_size = 11000 sc._use_direct_message_routes = False assert len(swap_clients[0].active_networks) == 1 @@ -816,8 +806,6 @@ class Test(BaseTest): swap_clients = self.swap_clients for sc in swap_clients: - sc._dleag_split_size_init = 9000 - sc._dleag_split_size = 11000 sc._use_direct_message_routes = True assert len(swap_clients[0].active_networks) == 1 @@ -906,8 +894,6 @@ class Test(BaseTest): swap_clients = self.swap_clients for sc in swap_clients: - sc._dleag_split_size_init = 9000 - sc._dleag_split_size = 11000 sc._use_direct_message_routes = True assert len(swap_clients[0].active_networks) == 1