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

Neutrino: auto-select peers on wallet creation #2226

Merged
merged 1 commit into from
Jun 10, 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 @@ -497,6 +497,7 @@
"views.Intro.lightningLiquidity": "Learn about lightning liquidity",
"views.Intro.advancedSetUp": "Advanced set-up",
"views.Intro.creatingWallet": "Zeus is creating your wallet.",
"views.Intro.choosingPeers": "ZEUS is choosing your peers.",
"views.Intro.carousel1.title": "Payments you can trust",
"views.Intro.carousel1.text": "ZEUS runs a Bitcoin and Lightning node to verify and keep your transactions private.",
"views.Intro.carousel2.title": "On-chain transfers",
Expand Down
6 changes: 6 additions & 0 deletions stores/SettingsStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,12 @@ export const DEFAULT_NEUTRINO_PEERS_MAINNET = [
'sg.lnolymp.us'
];

export const SECONDARY_NEUTRINO_PEERS_MAINNET = [
'node.blixtwallet.com',
'bb1.breez.technology',
'bb2.breez.technology'
];

export const DEFAULT_NEUTRINO_PEERS_TESTNET = [
'testnet.lnolymp.us',
'btcd-testnet.lightning.computer',
Expand Down
100 changes: 100 additions & 0 deletions utils/LndMobileUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {

import { generateSecureRandom } from 'react-native-securerandom';
import NetInfo from '@react-native-community/netinfo';
import Ping from 'react-native-ping';

import Log from '../lndmobile/log';
const log = Log('utils/LndMobileUtils.ts');
Expand All @@ -22,6 +23,11 @@ import {
} from '../lndmobile/index';

import stores from '../stores/Stores';
import {
DEFAULT_NEUTRINO_PEERS_MAINNET,
SECONDARY_NEUTRINO_PEERS_MAINNET,
DEFAULT_NEUTRINO_PEERS_TESTNET
} from '../stores/SettingsStore';

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

Expand Down Expand Up @@ -280,6 +286,100 @@ export async function startLnd(
});
}

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

const results: any = [];
for (let i = 0; i < peers.length; i++) {
const peer = peers[i];
await new Promise(async (resolve) => {
try {
const ms = await Ping.start(peer, {
timeout: 1000
});
console.log(`# ${peer} - ${ms}`);
results.push({
peer,
ms
});
resolve(true);
} catch (e) {
console.log('e', e);
results.push({
peer,
ms: 'Timed out'
});
resolve(true);
}
});
}

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

if (filteredResults.length < 3 && !isTestnet) {
peers = SECONDARY_NEUTRINO_PEERS_MAINNET;
for (let i = 0; i < peers.length; i++) {
const peer = peers[i];
await new Promise(async (resolve) => {
try {
const ms = await Ping.start(peer, {
timeout: 1000
});
console.log(`# ${peer} - ${ms}`);
results.push({
peer,
ms
});
resolve(true);
} catch (e) {
console.log('e', e);
results.push({
peer,
ms: 'Timed out'
});
resolve(true);
}
});
}
}

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

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

if (selectedPeers.length > 0) {
if (isTestnet) {
await stores.settingsStore.updateSettings({
neutrinoPeersTestnet: selectedPeers,
dontAllowOtherPeers: selectedPeers.length > 2 ? true : false
});
} else {
await stores.settingsStore.updateSettings({
neutrinoPeersMainnet: selectedPeers,
dontAllowOtherPeers: selectedPeers.length > 2 ? true : false
});
}

console.log('Selected the following Neutrino peers:', selectedPeers);
} else {
// TODO allow users to manually choose peers if we can't
// pick good defaults for them
console.log('Falling back to the default Neutrino peers.');
}

return;
}

export async function createLndWallet(
seedMnemonic?: string,
walletPassphrase?: string,
Expand Down
24 changes: 17 additions & 7 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 { createLndWallet } from '../utils/LndMobileUtils';
import { chooseNeutrinoPeers, createLndWallet } from '../utils/LndMobileUtils';
import { localeString } from '../utils/LocaleUtils';
import { themeColor } from '../utils/ThemeUtils';
import UrlUtils from '../utils/UrlUtils';
Expand All @@ -36,6 +36,7 @@ interface IntroProps {

const Intro: React.FC<IntroProps> = (props) => {
const [creatingWallet, setCreatingWallet] = useState(false);
const [choosingPeers, setChoosingPeers] = useState(false);
const [error, setError] = useState(false);

let screenWidth: number;
Expand Down Expand Up @@ -171,12 +172,19 @@ const Intro: React.FC<IntroProps> = (props) => {
<Button
title={localeString('views.Intro.quickStart')}
onPress={async () => {
setCreatingWallet(true);
const { settingsStore } = stores;
const {
setConnectingStatus,
updateSettings
} = settingsStore;

setChoosingPeers(true);

await chooseNeutrinoPeers(undefined);

setCreatingWallet(true);
setChoosingPeers(false);

const response = await createLndWallet(
undefined
);
Expand All @@ -203,6 +211,7 @@ const Intro: React.FC<IntroProps> = (props) => {
});
} else {
setCreatingWallet(false);
setChoosingPeers(false);
setError(true);
}
}}
Expand Down Expand Up @@ -282,7 +291,7 @@ const Intro: React.FC<IntroProps> = (props) => {
);
};

if (creatingWallet) {
if (choosingPeers || creatingWallet) {
return (
<Screen>
<View
Expand Down Expand Up @@ -312,10 +321,11 @@ const Intro: React.FC<IntroProps> = (props) => {
padding: 8
}}
>
{localeString('views.Intro.creatingWallet').replace(
'Zeus',
'ZEUS'
)}
{choosingPeers
? localeString('views.Intro.choosingPeers')
: localeString(
'views.Intro.creatingWallet'
).replace('Zeus', 'ZEUS')}
</Text>
<View style={{ marginTop: 40 }}>
<LoadingIndicator />
Expand Down
36 changes: 29 additions & 7 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 { createLndWallet } from '../utils/LndMobileUtils';
import { chooseNeutrinoPeers, createLndWallet } from '../utils/LndMobileUtils';
import { localeString } from '../utils/LocaleUtils';
import { themeColor } from '../utils/ThemeUtils';

Expand All @@ -33,6 +33,7 @@ interface IntroSplashProps {
}

interface IntroSplashState {
choosingPeers: boolean;
creatingWallet: boolean;
error: boolean;
modalBlur: number;
Expand All @@ -46,6 +47,7 @@ export default class IntroSplash extends React.Component<
> {
private backPressSubscription: NativeEventSubscription;
state = {
choosingPeers: false,
creatingWallet: false,
error: false,
modalBlur: 90
Expand Down Expand Up @@ -77,6 +79,7 @@ export default class IntroSplash extends React.Component<

render() {
const { navigation, SettingsStore } = this.props;
const { creatingWallet, choosingPeers } = this.state;
const { settings } = SettingsStore;
const locale = settings.locale || '';

Expand All @@ -86,7 +89,7 @@ export default class IntroSplash extends React.Component<
let localeName;
if (localeItem[0]) localeName = localeItem[0].value;

if (this.state.creatingWallet) {
if (choosingPeers || creatingWallet) {
return (
<Screen>
<View
Expand Down Expand Up @@ -116,10 +119,11 @@ export default class IntroSplash extends React.Component<
padding: 8
}}
>
{localeString('views.Intro.creatingWallet').replace(
'Zeus',
'ZEUS'
)}
{choosingPeers
? localeString('views.Intro.choosingPeers')
: localeString(
'views.Intro.creatingWallet'
).replace('Zeus', 'ZEUS')}
</Text>
<View style={{ marginTop: 40 }}>
<LoadingIndicator />
Expand Down Expand Up @@ -241,14 +245,31 @@ export default class IntroSplash extends React.Component<
)}
onPress={async () => {
this.setState({
creatingWallet: true
choosingPeers: true
});
const { SettingsStore } = this.props;
const {
setConnectingStatus,
updateSettings
} = SettingsStore;

try {
await chooseNeutrinoPeers(
undefined
);
} catch (e) {
this.setState({
error: true,
choosingPeers: false,
creatingWallet: false
});
}

this.setState({
choosingPeers: false,
creatingWallet: true
});

let response;
try {
response = await createLndWallet(
Expand All @@ -257,6 +278,7 @@ export default class IntroSplash extends React.Component<
} catch (e) {
this.setState({
error: true,
choosingPeers: false,
creatingWallet: false
});
}
Expand Down
7 changes: 6 additions & 1 deletion views/Settings/NodeConfiguration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ import Scan from '../../assets/images/SVG/Scan.svg';
import AddIcon from '../../assets/images/SVG/Add.svg';

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

interface NodeConfigurationProps {
navigation: StackNavigationProp<any, any>;
Expand Down Expand Up @@ -597,6 +600,8 @@ export default class NodeConfiguration extends React.Component<
creatingWallet: true
});

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

const response = await createLndWallet(
recoveryCipherSeed,
undefined,
Expand Down
9 changes: 8 additions & 1 deletion views/Settings/SeedRecovery.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ import TextInput from '../../components/TextInput';
import { themeColor } from '../../utils/ThemeUtils';
import { localeString } from '../../utils/LocaleUtils';

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

import { BIP39_WORD_LIST } from '../../utils/Bip39Utils';

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

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

try {
const response = await createLndWallet(
recoveryCipherSeed,
Expand Down
Loading