Skip to content

Commit

Permalink
feat(gui): Connect to preset rendezvous nodes at startup (#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
binarybaron authored Nov 14, 2024
1 parent 7ebe59b commit 163f6fa
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default function DaemonStatusAlert() {
const navigate = useNavigate();

if (contextStatus === null) {
return <Alert severity="info">The daemon is not running</Alert>;
return <LoadingSpinnerAlert severity="warning">Checking for available remote nodes</LoadingSpinnerAlert>;
}

switch (contextStatus.type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
15 changes: 15 additions & 0 deletions src-gui/src/renderer/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 7 additions & 4 deletions src-gui/src/store/middleware/storeListener.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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) => {
Expand All @@ -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(),
]);
}
},
});
Expand Down
21 changes: 13 additions & 8 deletions src-gui/src/utils/sortUtils.ts
Original file line number Diff line number Diff line change
@@ -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;
}
Expand Down

0 comments on commit 163f6fa

Please sign in to comment.