Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Settings: Neutrino: add optimize peers button #2243

Merged
merged 2 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,7 @@
"views.Settings.EmbeddedNode.NeutrinoPeers.allowingOtherPeers": "Allowing connections to other peers.",
"views.Settings.EmbeddedNode.NeutrinoPeers.notAllowingOtherPeers": "Not allowing connections to other peers.",
"views.Settings.EmbeddedNode.NeutrinoPeers.timedOut": "Ping timed out",
"views.Settings.EmbeddedNode.NeutrinoPeers.optimize": "Optimize peers selection",
"views.Settings.EmbeddedNode.ZeroConfPeers.title": "Zero conf Peers",
"views.Settings.EmbeddedNode.ZeroConfPeers.subtitle": "Set the peers you would like to accept zero conf lightning channels from, other than the LSP.",
"views.Settings.EmbeddedNode.ExpressGraphSync.title": "Express Graph Sync",
Expand Down
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
7 changes: 5 additions & 2 deletions views/Intro.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ 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';
import { localeString } from '../utils/LocaleUtils';
import { themeColor } from '../utils/ThemeUtils';
import UrlUtils from '../utils/UrlUtils';
Expand Down Expand Up @@ -180,7 +183,7 @@ const Intro: React.FC<IntroProps> = (props) => {

setChoosingPeers(true);

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

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

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

import { chooseNeutrinoPeers, createLndWallet } from '../utils/LndMobileUtils';
import {
optimizeNeutrinoPeers,
createLndWallet
} from '../utils/LndMobileUtils';
import { localeString } from '../utils/LocaleUtils';
import { themeColor } from '../utils/ThemeUtils';

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

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

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';
import {
NEUTRINO_PING_THRESHOLD_MS,
NEUTRINO_PING_TIMEOUT_MS
} from '../../../../utils/LndMobileUtils';

Check warning on line 34 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 Stopwatch from '../../../../assets/images/SVG/Stopwatch.svg';

Expand All @@ -46,7 +47,7 @@
pingTime: number;
pingTimeout: boolean;
pingHost: string;
pinging: boolean;
loading: boolean;
}

@inject('SettingsStore')
Expand All @@ -62,7 +63,7 @@
pingTime: 0,
pingTimeout: false,
pingHost: '',
pinging: false
loading: false
};

remove = (arrOriginal, elementToRemove) => {
Expand Down Expand Up @@ -95,7 +96,7 @@
pingTime,
pingTimeout,
pingHost,
pinging
loading
} = this.state;
const { updateSettings, embeddedLndNetwork }: any = SettingsStore;

Expand Down Expand Up @@ -128,9 +129,9 @@
navigation={navigation}
/>
<View style={{ flex: 1 }}>
{pinging && <LoadingIndicator />}
{loading && <LoadingIndicator />}
{!pingTimeout &&
!pinging &&
!loading &&
pingHost &&
pingTime <= 200 && (
<SuccessMessage
Expand All @@ -139,7 +140,7 @@
/>
)}
{!pingTimeout &&
!pinging &&
!loading &&
pingHost &&
pingTime < NEUTRINO_PING_THRESHOLD_MS &&
pingTime > 200 && (
Expand All @@ -149,15 +150,15 @@
/>
)}
{!pingTimeout &&
!pinging &&
!loading &&
pingHost &&
pingTime >= NEUTRINO_PING_THRESHOLD_MS && (
<ErrorMessage
message={pingTimeMsg}
dismissable
/>
)}
{!pinging && pingHost && !!pingTimeout && (
{!loading && pingHost && !!pingTimeout && (
<ErrorMessage
message={`${pingHost}: ${localeString(
'views.Settings.EmbeddedNode.NeutrinoPeers.timedOut'
Expand Down Expand Up @@ -272,7 +273,7 @@
pingTime: 0,
pingTimeout: false,
pingHost: addPeer,
pinging: true
loading: true
});

const ms =
Expand All @@ -285,12 +286,12 @@
);
this.setState({
pingTime: ms,
pinging: false
loading: false
});
} catch (e) {
this.setState({
pingTimeout: true,
pinging: false
loading: false
});
}
}}
Expand Down Expand Up @@ -386,7 +387,7 @@
false,
pingHost:
item,
pinging:
loading:
true
}
);
Expand All @@ -403,7 +404,7 @@
{
pingTime:
ms,
pinging:
loading:
false
}
);
Expand All @@ -412,7 +413,7 @@
{
pingTimeout:
true,
pinging:
loading:
false
}
);
Expand Down Expand Up @@ -507,6 +508,31 @@
</View>
</ScrollView>
</View>
{embeddedLndNetwork === 'Mainnet' && (
<View style={{ marginBottom: 10, marginTop: 10 }}>
<Button
title={localeString(
'views.Settings.EmbeddedNode.NeutrinoPeers.optimize'
)}
onPress={async () => {
this.setState({
loading: true
});
await optimizeNeutrinoPeers();
const { getSettings } = SettingsStore;
const settings = await getSettings();
this.setState({
loading: false,
neutrinoPeers:
settings.neutrinoPeersMainnet
});

restartNeeded();
}}
tertiary
/>
</View>
)}
{(dontAllowOtherPeers ||
mainnetPeersChanged ||
testnetPeersChanged) && (
Expand Down
Loading
Loading