Skip to content

Commit

Permalink
optimizeNeutrinoPeers: add three tiers of ping thresholds
Browse files Browse the repository at this point in the history
  • Loading branch information
kaloudis committed Jun 18, 2024
1 parent d9ebb7a commit aa82547
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 37 deletions.
117 changes: 90 additions & 27 deletions utils/LndMobileUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ import {

import { lnrpc } from '../proto/lightning';

export const NEUTRINO_PING_TIMEOUT_MS = 1500;

export const NEUTRINO_PING_OPTIMAL_MS = 200;
export const NEUTRINO_PING_LAX_MS = 500;
export const NEUTRINO_PING_THRESHOLD_MS = 1000;

export const LndMobileEventEmitter =
Platform.OS == 'android'
? DeviceEventEmitter
Expand Down Expand Up @@ -70,9 +76,6 @@ export function checkLndStreamErrorResponse(
return null;
}

export const NEUTRINO_PING_TIMEOUT_MS = 1500;
export const NEUTRINO_PING_THRESHOLD_MS = 1000;

const writeLndConfig = async (
isTestnet?: boolean,
rescan?: boolean,
Expand Down Expand Up @@ -289,15 +292,13 @@ export async function startLnd(
});
}

export async function chooseNeutrinoPeers(isTestnet?: boolean) {
console.log(
`Selecting Neutrino peers with ping times <${NEUTRINO_PING_THRESHOLD_MS}ms`
);
export async function optimizeNeutrinoPeers(isTestnet?: boolean) {
console.log('Optimizing Neutrino peers');
let peers = isTestnet
? DEFAULT_NEUTRINO_PEERS_TESTNET
: DEFAULT_NEUTRINO_PEERS_MAINNET;

const results: any = [];
const resultsMain: any = [];
for (let i = 0; i < peers.length; i++) {
const peer = peers[i];
await new Promise(async (resolve) => {
Expand All @@ -306,14 +307,14 @@ export async function chooseNeutrinoPeers(isTestnet?: boolean) {
timeout: NEUTRINO_PING_TIMEOUT_MS
});
console.log(`# ${peer} - ${ms}`);
results.push({
resultsMain.push({
peer,
ms
});
resolve(true);
} catch (e) {
console.log('e', e);
results.push({
resultsMain.push({
peer,
ms: 'Timed out'
});
Expand All @@ -322,14 +323,75 @@ export async function chooseNeutrinoPeers(isTestnet?: boolean) {
});
}

let filteredResults = results.filter((result: any) => {
// Optimal

const selectedPeers: string[] = [];

console.log(
`Adding Neutrino peers with ping times <${NEUTRINO_PING_OPTIMAL_MS}ms`
);

const optimalResults = resultsMain.filter((result: any) => {
return (
Number.isInteger(result.ms) &&
result.ms < NEUTRINO_PING_THRESHOLD_MS
Number.isInteger(result.ms) && result.ms < NEUTRINO_PING_OPTIMAL_MS
);
});

if (filteredResults.length < 3 && !isTestnet) {
optimalResults.forEach((result: any) => {
selectedPeers.push(result.peer);
});

console.log('Peers count:', selectedPeers.length);

// Lax

if (selectedPeers.length < 3) {
console.log(
`Adding Neutrino peers with ping times <${NEUTRINO_PING_LAX_MS}ms`
);

const laxResults = resultsMain.filter((result: any) => {
return (
Number.isInteger(result.ms) && result.ms < NEUTRINO_PING_LAX_MS
);
});

laxResults.forEach((result: any) => {
selectedPeers.push(result.peer);
});

console.log('Peers count:', selectedPeers.length);
}

// Threshold

if (selectedPeers.length < 3) {
console.log(
`Selecting Neutrino peers with ping times <${NEUTRINO_PING_THRESHOLD_MS}ms`
);

const thresholdResults = resultsMain.filter((result: any) => {
return (
Number.isInteger(result.ms) &&
result.ms < NEUTRINO_PING_THRESHOLD_MS
);
});

thresholdResults.forEach((result: any) => {
selectedPeers.push(result.peer);
});

console.log('Peers count:', selectedPeers.length);
}

// Extra external peers
const resultsSecondary: any = [];

if (selectedPeers.length < 3 && !isTestnet) {
console.log(
`Selecting Neutrino peers with ping times <${NEUTRINO_PING_THRESHOLD_MS}ms from alternate set`
);

peers = SECONDARY_NEUTRINO_PEERS_MAINNET;
for (let i = 0; i < peers.length; i++) {
const peer = peers[i];
Expand All @@ -339,34 +401,35 @@ export async function chooseNeutrinoPeers(isTestnet?: boolean) {
timeout: NEUTRINO_PING_TIMEOUT_MS
});
console.log(`# ${peer} - ${ms}`);
results.push({
resultsSecondary.push({
peer,
ms
});
resolve(true);
} catch (e) {
console.log('e', e);
results.push({
resultsSecondary.push({
peer,
ms: 'Timed out'
});
resolve(true);
}
});
}
}

filteredResults = results.filter((result: any) => {
return (
Number.isInteger(result.ms) &&
result.ms < NEUTRINO_PING_THRESHOLD_MS
);
});
const filteredResults = resultsMain.filter((result: any) => {
return (
Number.isInteger(result.ms) &&
result.ms < NEUTRINO_PING_THRESHOLD_MS
);
});

const selectedPeers: string[] = [];
filteredResults.forEach((result: any) => {
selectedPeers.push(result.peer);
});
filteredResults.forEach((result: any) => {
selectedPeers.push(result.peer);
});

console.log('Peers count:', selectedPeers.length);
}

if (selectedPeers.length > 0) {
if (isTestnet) {
Expand Down
4 changes: 2 additions & 2 deletions views/Intro.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import LoadingIndicator from '../components/LoadingIndicator';
import Screen from '../components/Screen';
import { ErrorMessage } from '../components/SuccessErrorMessage';

import { chooseNeutrinoPeers, createLndWallet } from '../utils/LndMobileUtils';
import { optimizeNeutrinoPeers, createLndWallet } from '../utils/LndMobileUtils';

Check failure on line 21 in views/Intro.tsx

View workflow job for this annotation

GitHub Actions / lint

Replace `·optimizeNeutrinoPeers,·createLndWallet·` with `⏎····optimizeNeutrinoPeers,⏎····createLndWallet⏎`
import { localeString } from '../utils/LocaleUtils';
import { themeColor } from '../utils/ThemeUtils';
import UrlUtils from '../utils/UrlUtils';
Expand Down Expand Up @@ -180,7 +180,7 @@ const Intro: React.FC<IntroProps> = (props) => {

setChoosingPeers(true);

await chooseNeutrinoPeers(undefined);
await optimizeNeutrinoPeers(undefined);

setCreatingWallet(true);
setChoosingPeers(false);
Expand Down
4 changes: 2 additions & 2 deletions views/IntroSplash.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { ErrorMessage } from '../components/SuccessErrorMessage';

import SettingsStore, { LOCALE_KEYS } from '../stores/SettingsStore';

import { chooseNeutrinoPeers, createLndWallet } from '../utils/LndMobileUtils';
import { optimizeNeutrinoPeers, createLndWallet } from '../utils/LndMobileUtils';

Check failure on line 24 in views/IntroSplash.tsx

View workflow job for this annotation

GitHub Actions / lint

Replace `·optimizeNeutrinoPeers,·createLndWallet·` with `⏎····optimizeNeutrinoPeers,⏎····createLndWallet⏎`
import { localeString } from '../utils/LocaleUtils';
import { themeColor } from '../utils/ThemeUtils';

Expand Down Expand Up @@ -252,7 +252,7 @@ export default class IntroSplash extends React.Component<
} = SettingsStore;

try {
await chooseNeutrinoPeers(
await optimizeNeutrinoPeers(
undefined
);
} catch (e) {
Expand Down
4 changes: 2 additions & 2 deletions views/Settings/EmbeddedNode/Peers/NeutrinoPeers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import SettingsStore, {
DEFAULT_NEUTRINO_PEERS_TESTNET
} from '../../../../stores/SettingsStore';

import { chooseNeutrinoPeers } from '../../../../utils/LndMobileUtils';
import { optimizeNeutrinoPeers } from '../../../../utils/LndMobileUtils';

Check warning on line 27 in views/Settings/EmbeddedNode/Peers/NeutrinoPeers.tsx

View workflow job for this annotation

GitHub Actions / lint

'/home/runner/work/zeus/zeus/utils/LndMobileUtils.ts' imported multiple times
import { localeString } from '../../../../utils/LocaleUtils';
import { restartNeeded } from '../../../../utils/RestartUtils';
import { themeColor } from '../../../../utils/ThemeUtils';
Expand Down Expand Up @@ -518,7 +518,7 @@ export default class NeutrinoPeers extends React.Component<
this.setState({
loading: true
});
await chooseNeutrinoPeers();
await optimizeNeutrinoPeers();
const { getSettings } = SettingsStore;
const settings = await getSettings();
this.setState({
Expand Down
4 changes: 2 additions & 2 deletions views/Settings/NodeConfiguration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ import AddIcon from '../../assets/images/SVG/Add.svg';

import { getPhoto } from '../../utils/PhotoUtils';
import {
chooseNeutrinoPeers,
optimizeNeutrinoPeers,
createLndWallet
} from '../../utils/LndMobileUtils';

Expand Down Expand Up @@ -600,7 +600,7 @@ export default class NodeConfiguration extends React.Component<
creatingWallet: true
});

await chooseNeutrinoPeers(network === 'Testnet');
await optimizeNeutrinoPeers(network === 'Testnet');

const response = await createLndWallet(
recoveryCipherSeed,
Expand Down
4 changes: 2 additions & 2 deletions views/Settings/SeedRecovery.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { themeColor } from '../../utils/ThemeUtils';
import { localeString } from '../../utils/LocaleUtils';

import {
chooseNeutrinoPeers,
optimizeNeutrinoPeers,
createLndWallet
} from '../../utils/LndMobileUtils';

Expand Down Expand Up @@ -880,7 +880,7 @@ export default class SeedRecovery extends React.PureComponent<
loading: true
});

await chooseNeutrinoPeers(
await optimizeNeutrinoPeers(
network === 'testnet'
);

Expand Down

0 comments on commit aa82547

Please sign in to comment.