diff --git a/locales/en.json b/locales/en.json index 765aa77fc..4c0ab51ba 100644 --- a/locales/en.json +++ b/locales/en.json @@ -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", diff --git a/utils/LndMobileUtils.ts b/utils/LndMobileUtils.ts index 19dfa176f..c726fcf68 100644 --- a/utils/LndMobileUtils.ts +++ b/utils/LndMobileUtils.ts @@ -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 @@ -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, @@ -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) => { @@ -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' }); @@ -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]; @@ -339,14 +401,14 @@ 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' }); @@ -354,19 +416,20 @@ export async function chooseNeutrinoPeers(isTestnet?: boolean) { } }); } - } - 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) { diff --git a/views/Intro.tsx b/views/Intro.tsx index 88dfc7e5c..0db40bd1f 100644 --- a/views/Intro.tsx +++ b/views/Intro.tsx @@ -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'; @@ -180,7 +183,7 @@ const Intro: React.FC = (props) => { setChoosingPeers(true); - await chooseNeutrinoPeers(undefined); + await optimizeNeutrinoPeers(undefined); setCreatingWallet(true); setChoosingPeers(false); diff --git a/views/IntroSplash.tsx b/views/IntroSplash.tsx index 0e73baf8d..acbe90f2f 100644 --- a/views/IntroSplash.tsx +++ b/views/IntroSplash.tsx @@ -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'; @@ -252,7 +255,7 @@ export default class IntroSplash extends React.Component< } = SettingsStore; try { - await chooseNeutrinoPeers( + await optimizeNeutrinoPeers( undefined ); } catch (e) { diff --git a/views/Settings/EmbeddedNode/Peers/NeutrinoPeers.tsx b/views/Settings/EmbeddedNode/Peers/NeutrinoPeers.tsx index f35fb5cb4..0c2e9529a 100644 --- a/views/Settings/EmbeddedNode/Peers/NeutrinoPeers.tsx +++ b/views/Settings/EmbeddedNode/Peers/NeutrinoPeers.tsx @@ -24,6 +24,7 @@ import SettingsStore, { DEFAULT_NEUTRINO_PEERS_TESTNET } from '../../../../stores/SettingsStore'; +import { optimizeNeutrinoPeers } from '../../../../utils/LndMobileUtils'; import { localeString } from '../../../../utils/LocaleUtils'; import { restartNeeded } from '../../../../utils/RestartUtils'; import { themeColor } from '../../../../utils/ThemeUtils'; @@ -46,7 +47,7 @@ interface NeutrinoPeersState { pingTime: number; pingTimeout: boolean; pingHost: string; - pinging: boolean; + loading: boolean; } @inject('SettingsStore') @@ -62,7 +63,7 @@ export default class NeutrinoPeers extends React.Component< pingTime: 0, pingTimeout: false, pingHost: '', - pinging: false + loading: false }; remove = (arrOriginal, elementToRemove) => { @@ -95,7 +96,7 @@ export default class NeutrinoPeers extends React.Component< pingTime, pingTimeout, pingHost, - pinging + loading } = this.state; const { updateSettings, embeddedLndNetwork }: any = SettingsStore; @@ -128,9 +129,9 @@ export default class NeutrinoPeers extends React.Component< navigation={navigation} /> - {pinging && } + {loading && } {!pingTimeout && - !pinging && + !loading && pingHost && pingTime <= 200 && ( )} {!pingTimeout && - !pinging && + !loading && pingHost && pingTime < NEUTRINO_PING_THRESHOLD_MS && pingTime > 200 && ( @@ -149,7 +150,7 @@ export default class NeutrinoPeers extends React.Component< /> )} {!pingTimeout && - !pinging && + !loading && pingHost && pingTime >= NEUTRINO_PING_THRESHOLD_MS && ( )} - {!pinging && pingHost && !!pingTimeout && ( + {!loading && pingHost && !!pingTimeout && ( + {embeddedLndNetwork === 'Mainnet' && ( + +