diff --git a/utils/LndMobileUtils.ts b/utils/LndMobileUtils.ts index 19dfa176f2..c726fcf689 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 88dfc7e5cf..4a04ae472b 100644 --- a/views/Intro.tsx +++ b/views/Intro.tsx @@ -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'; import { localeString } from '../utils/LocaleUtils'; import { themeColor } from '../utils/ThemeUtils'; import UrlUtils from '../utils/UrlUtils'; @@ -180,7 +180,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 0e73baf8d4..901f28c8db 100644 --- a/views/IntroSplash.tsx +++ b/views/IntroSplash.tsx @@ -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'; import { localeString } from '../utils/LocaleUtils'; import { themeColor } from '../utils/ThemeUtils'; @@ -252,7 +252,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 5c8b40b57b..0c2e9529ad 100644 --- a/views/Settings/EmbeddedNode/Peers/NeutrinoPeers.tsx +++ b/views/Settings/EmbeddedNode/Peers/NeutrinoPeers.tsx @@ -24,7 +24,7 @@ import SettingsStore, { DEFAULT_NEUTRINO_PEERS_TESTNET } from '../../../../stores/SettingsStore'; -import { chooseNeutrinoPeers } from '../../../../utils/LndMobileUtils'; +import { optimizeNeutrinoPeers } from '../../../../utils/LndMobileUtils'; import { localeString } from '../../../../utils/LocaleUtils'; import { restartNeeded } from '../../../../utils/RestartUtils'; import { themeColor } from '../../../../utils/ThemeUtils'; @@ -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({ diff --git a/views/Settings/NodeConfiguration.tsx b/views/Settings/NodeConfiguration.tsx index d8ac02cfed..fb6c478082 100644 --- a/views/Settings/NodeConfiguration.tsx +++ b/views/Settings/NodeConfiguration.tsx @@ -52,7 +52,7 @@ import AddIcon from '../../assets/images/SVG/Add.svg'; import { getPhoto } from '../../utils/PhotoUtils'; import { - chooseNeutrinoPeers, + optimizeNeutrinoPeers, createLndWallet } from '../../utils/LndMobileUtils'; @@ -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, diff --git a/views/Settings/SeedRecovery.tsx b/views/Settings/SeedRecovery.tsx index 7f80207c51..4b61607a6f 100644 --- a/views/Settings/SeedRecovery.tsx +++ b/views/Settings/SeedRecovery.tsx @@ -22,7 +22,7 @@ import { themeColor } from '../../utils/ThemeUtils'; import { localeString } from '../../utils/LocaleUtils'; import { - chooseNeutrinoPeers, + optimizeNeutrinoPeers, createLndWallet } from '../../utils/LndMobileUtils'; @@ -880,7 +880,7 @@ export default class SeedRecovery extends React.PureComponent< loading: true }); - await chooseNeutrinoPeers( + await optimizeNeutrinoPeers( network === 'testnet' );