From 163f6fa94d27796c49d1d7fe90861caaef042d46 Mon Sep 17 00:00:00 2001 From: binarybaron <86064887+binarybaron@users.noreply.github.com> Date: Thu, 14 Nov 2024 17:04:27 +0100 Subject: [PATCH] feat(gui): Connect to preset rendezvous nodes at startup (#162) --- .../components/alert/DaemonStatusAlert.tsx | 2 +- .../modal/listSellers/ListSellersDialog.tsx | 7 +------ src-gui/src/renderer/rpc.ts | 15 +++++++++++++ src-gui/src/store/middleware/storeListener.ts | 11 ++++++---- src-gui/src/utils/sortUtils.ts | 21 ++++++++++++------- 5 files changed, 37 insertions(+), 19 deletions(-) diff --git a/src-gui/src/renderer/components/alert/DaemonStatusAlert.tsx b/src-gui/src/renderer/components/alert/DaemonStatusAlert.tsx index 1cbc5db1f..35d82a1ff 100644 --- a/src-gui/src/renderer/components/alert/DaemonStatusAlert.tsx +++ b/src-gui/src/renderer/components/alert/DaemonStatusAlert.tsx @@ -11,7 +11,7 @@ export default function DaemonStatusAlert() { const navigate = useNavigate(); if (contextStatus === null) { - return The daemon is not running; + return Checking for available remote nodes; } switch (contextStatus.type) { diff --git a/src-gui/src/renderer/components/modal/listSellers/ListSellersDialog.tsx b/src-gui/src/renderer/components/modal/listSellers/ListSellersDialog.tsx index b338b870d..0cbe0da8d 100644 --- a/src-gui/src/renderer/components/modal/listSellers/ListSellersDialog.tsx +++ b/src-gui/src/renderer/components/modal/listSellers/ListSellersDialog.tsx @@ -16,16 +16,11 @@ import { useSnackbar } from "notistack"; import { ChangeEvent, useState } from "react"; import TruncatedText from "renderer/components/other/TruncatedText"; import PromiseInvokeButton from "renderer/components/PromiseInvokeButton"; -import { listSellersAtRendezvousPoint } from "renderer/rpc"; +import { listSellersAtRendezvousPoint, PRESET_RENDEZVOUS_POINTS } from "renderer/rpc"; import { discoveredProvidersByRendezvous } from "store/features/providersSlice"; import { useAppDispatch } from "store/hooks"; import { isValidMultiAddressWithPeerId } from "utils/parseUtils"; -const PRESET_RENDEZVOUS_POINTS = [ - "/dns4/discover.unstoppableswap.net/tcp/8888/p2p/12D3KooWA6cnqJpVnreBVnoro8midDL9Lpzmg8oJPoAGi7YYaamE", - "/dns4/eratosthen.es/tcp/7798/p2p/12D3KooWAh7EXXa2ZyegzLGdjvj1W4G3EXrTGrf6trraoT1MEobs", -]; - const useStyles = makeStyles((theme: Theme) => ({ chipOuter: { display: "flex", diff --git a/src-gui/src/renderer/rpc.ts b/src-gui/src/renderer/rpc.ts index ca30765ca..02960942a 100644 --- a/src-gui/src/renderer/rpc.ts +++ b/src-gui/src/renderer/rpc.ts @@ -48,6 +48,21 @@ import logger from "utils/logger"; import { getNetwork, getNetworkName, isTestnet } from "store/config"; import { Blockchain } from "store/features/settingsSlice"; import { setStatus } from "store/features/nodesSlice"; +import { discoveredProvidersByRendezvous } from "store/features/providersSlice"; + +export const PRESET_RENDEZVOUS_POINTS = [ + "/dns4/discover.unstoppableswap.net/tcp/8888/p2p/12D3KooWA6cnqJpVnreBVnoro8midDL9Lpzmg8oJPoAGi7YYaamE", +]; + +export async function fetchSellersAtPresetRendezvousPoints() { + await Promise.all(PRESET_RENDEZVOUS_POINTS.map(async (rendezvousPoint) => { + const response = await listSellersAtRendezvousPoint(rendezvousPoint); + store.dispatch(discoveredProvidersByRendezvous(response.sellers)); + + console.log(`Discovered ${response.sellers.length} sellers at rendezvous point ${rendezvousPoint} during startup fetch`); + }), + ); +} export async function initEventListeners() { // This operation is in-expensive diff --git a/src-gui/src/store/middleware/storeListener.ts b/src-gui/src/store/middleware/storeListener.ts index d648ff8cd..1108f6f96 100644 --- a/src-gui/src/store/middleware/storeListener.ts +++ b/src-gui/src/store/middleware/storeListener.ts @@ -1,5 +1,5 @@ import { createListenerMiddleware } from "@reduxjs/toolkit"; -import { getAllSwapInfos, checkBitcoinBalance, updateAllNodeStatuses } from "renderer/rpc"; +import { getAllSwapInfos, checkBitcoinBalance, updateAllNodeStatuses, fetchSellersAtPresetRendezvousPoints } from "renderer/rpc"; import logger from "utils/logger"; import { contextStatusEventReceived } from "store/features/rpcSlice"; import { addNode, setFetchFiatPrices, setFiatCurrency } from "store/features/settingsSlice"; @@ -10,7 +10,7 @@ export function createMainListeners() { const listener = createListenerMiddleware(); // Listener for when the Context becomes available - // When the context becomes available, we check the bitcoin balance and fetch all swap infos + // When the context becomes available, we check the bitcoin balance, fetch all swap infos and connect to the rendezvous point listener.startListening({ actionCreator: contextStatusEventReceived, effect: async (action) => { @@ -21,8 +21,11 @@ export function createMainListeners() { logger.debug( "Context is available, checking bitcoin balance and history", ); - await checkBitcoinBalance(); - await getAllSwapInfos(); + await Promise.allSettled([ + checkBitcoinBalance(), + getAllSwapInfos(), + fetchSellersAtPresetRendezvousPoints(), + ]); } }, }); diff --git a/src-gui/src/utils/sortUtils.ts b/src-gui/src/utils/sortUtils.ts index 0df96046f..e70223323 100644 --- a/src-gui/src/utils/sortUtils.ts +++ b/src-gui/src/utils/sortUtils.ts @@ -1,20 +1,25 @@ import { ExtendedProviderStatus } from "models/apiModel"; -import { isProviderCompatible } from "./multiAddrUtils"; +import { isProviderCompatible, isProviderOutdated } from "./multiAddrUtils"; export function sortProviderList(list: ExtendedProviderStatus[]) { return list .filter(isProviderCompatible) .concat() .sort((firstEl, secondEl) => { - // If neither of them have a relevancy score, sort by max swap amount - if (firstEl.relevancy === undefined && secondEl.relevancy === undefined) { - if (firstEl.maxSwapAmount > secondEl.maxSwapAmount) { - return -1; - } + // If either provider is outdated, prioritize the one that isn't + if (isProviderOutdated(firstEl) && !isProviderOutdated(secondEl)) return 1; + if (!isProviderOutdated(firstEl) && isProviderOutdated(secondEl)) return -1; + + // If neither of them have a relevancy score, sort by price + if (firstEl.relevancy == null && secondEl.relevancy == null) { + return firstEl.price - secondEl.price; } + // If only on of the two don't have a relevancy score, prioritize the one that does - if (firstEl.relevancy === undefined) return 1; - if (secondEl.relevancy === undefined) return -1; + if (firstEl.relevancy == null) return 1; + if (secondEl.relevancy == null) return -1; + + // Otherwise, sort by relevancy score if (firstEl.relevancy > secondEl.relevancy) { return -1; }