From 1f9ba7fee7ef1ac1a5bc7bdbc700cc85cf67a839 Mon Sep 17 00:00:00 2001 From: Brian Faust Date: Mon, 18 May 2020 18:51:26 +0300 Subject: [PATCH] fix: use local seed list if peer discovery fails (#1924) --- src/renderer/store/modules/peer/devnet.json | 21 ++ src/renderer/store/modules/peer/fallback.js | 4 + .../store/modules/{peer.js => peer/index.js} | 79 ++++-- src/renderer/store/modules/peer/mainnet.json | 257 ++++++++++++++++++ 4 files changed, 339 insertions(+), 22 deletions(-) create mode 100644 src/renderer/store/modules/peer/devnet.json create mode 100644 src/renderer/store/modules/peer/fallback.js rename src/renderer/store/modules/{peer.js => peer/index.js} (89%) create mode 100644 src/renderer/store/modules/peer/mainnet.json diff --git a/src/renderer/store/modules/peer/devnet.json b/src/renderer/store/modules/peer/devnet.json new file mode 100644 index 0000000000..4a11cd8552 --- /dev/null +++ b/src/renderer/store/modules/peer/devnet.json @@ -0,0 +1,21 @@ +[{ + "ip": "167.114.29.51", + "port": 4002 + }, + { + "ip": "167.114.29.52", + "port": 4002 + }, + { + "ip": "167.114.29.53", + "port": 4002 + }, + { + "ip": "167.114.29.54", + "port": 4002 + }, + { + "ip": "167.114.29.55", + "port": 4002 + } +] diff --git a/src/renderer/store/modules/peer/fallback.js b/src/renderer/store/modules/peer/fallback.js new file mode 100644 index 0000000000..b2920b3c25 --- /dev/null +++ b/src/renderer/store/modules/peer/fallback.js @@ -0,0 +1,4 @@ +export const fallbackSeeds = { + 'ark.mainnet': require('./mainnet.json'), + 'ark.devnet': require('./devnet.json') +} diff --git a/src/renderer/store/modules/peer.js b/src/renderer/store/modules/peer/index.js similarity index 89% rename from src/renderer/store/modules/peer.js rename to src/renderer/store/modules/peer/index.js index 4a7cf3e77c..49b3d47de0 100644 --- a/src/renderer/store/modules/peer.js +++ b/src/renderer/store/modules/peer/index.js @@ -6,6 +6,7 @@ import i18n from '@/i18n' import { isEmpty } from '@/utils' import PeerModel from '@/models/peer' import Vue from 'vue' +import { fallbackSeeds } from './fallback' const getBaseUrl = (peer) => { const scheme = peer.isHttps ? 'https://' : 'http://' @@ -13,6 +14,29 @@ const getBaseUrl = (peer) => { return `${scheme}${peer.ip}:${peer.port}` } +const discoverPeers = async (peerDiscovery) => { + peerDiscovery.withLatency(300).sortBy('latency') + + const peers = await peerDiscovery.findPeersWithPlugin('core-api', { + additional: [ + 'height', + 'latency' + ] + }) + + if (peers && peers.length) { + return peers + } + + return peerDiscovery.findPeersWithPlugin('core-wallet-api', { + additional: [ + 'height', + 'latency', + 'version' + ] + }) +} + export default { namespaced: true, @@ -323,35 +347,46 @@ export default { * Refresh peer list. * @return {void} */ - async refresh ({ dispatch }, network = null) { + async refresh ({ dispatch, rootGetters }, network = null) { let peers = [] try { - const peerDiscovery = await dispatch('getPeerDiscovery', network) + peers = await discoverPeers(await dispatch('getPeerDiscovery', network)) - peerDiscovery.withLatency(300) - .sortBy('latency') + throw new Error('xxx') + } catch (error) { + if (!network) { + network = rootGetters['session/network'] + } - peers = await peerDiscovery - .findPeersWithPlugin('core-api', { - additional: [ - 'height', - 'latency' - ] - }) + const networkLookup = { + 'ark.mainnet': 'mainnet', + 'ark.devnet': 'devnet' + } + + if (networkLookup[network.id]) { + console.log('Could not refresh peer list. Using fallback seeds: ', error) + + let peerDiscoveryFailed = true + + while (peerDiscoveryFailed) { + try { + const seeds = fallbackSeeds[network.id] + const seed = seeds[Math.floor(Math.random() * seeds.length)] + const peerDiscovery = await PeerDiscovery.new({ networkOrHost: `http://${seed.ip}:4003/api/peers` }) + + peers = await discoverPeers(peerDiscovery) - if (!peers.length) { - peers = await peerDiscovery - .findPeersWithPlugin('core-wallet-api', { - additional: [ - 'height', - 'latency', - 'version' - ] - }) + peerDiscoveryFailed = false + } catch (error) { + peerDiscoveryFailed = true + + console.error('Could not refresh peer list:', error) + } + } + } else { + console.error('Could not refresh peer list:', error) } - } catch (error) { - console.error('Could not refresh peer list:', error) } if (!peers.length) { diff --git a/src/renderer/store/modules/peer/mainnet.json b/src/renderer/store/modules/peer/mainnet.json new file mode 100644 index 0000000000..9bbfe7e751 --- /dev/null +++ b/src/renderer/store/modules/peer/mainnet.json @@ -0,0 +1,257 @@ +[{ + "ip": "5.196.105.32", + "port": 4001 + }, + { + "ip": "5.196.105.33", + "port": 4001 + }, + { + "ip": "5.196.105.34", + "port": 4001 + }, + { + "ip": "5.196.105.35", + "port": 4001 + }, + { + "ip": "5.196.105.36", + "port": 4001 + }, + { + "ip": "5.196.105.37", + "port": 4001 + }, + { + "ip": "5.196.105.38", + "port": 4001 + }, + { + "ip": "5.196.105.39", + "port": 4001 + }, + { + "ip": "178.32.65.136", + "port": 4001 + }, + { + "ip": "178.32.65.137", + "port": 4001 + }, + { + "ip": "178.32.65.138", + "port": 4001 + }, + { + "ip": "178.32.65.139", + "port": 4001 + }, + { + "ip": "178.32.65.140", + "port": 4001 + }, + { + "ip": "178.32.65.141", + "port": 4001 + }, + { + "ip": "178.32.65.142", + "port": 4001 + }, + { + "ip": "178.32.65.143", + "port": 4001 + }, + { + "ip": "5.196.105.40", + "port": 4001 + }, + { + "ip": "5.196.105.41", + "port": 4001 + }, + { + "ip": "5.196.105.42", + "port": 4001 + }, + { + "ip": "5.196.105.43", + "port": 4001 + }, + { + "ip": "5.196.105.44", + "port": 4001 + }, + { + "ip": "5.196.105.45", + "port": 4001 + }, + { + "ip": "5.196.105.46", + "port": 4001 + }, + { + "ip": "5.196.105.47", + "port": 4001 + }, + { + "ip": "54.38.120.32", + "port": 4001 + }, + { + "ip": "54.38.120.33", + "port": 4001 + }, + { + "ip": "54.38.120.34", + "port": 4001 + }, + { + "ip": "54.38.120.35", + "port": 4001 + }, + { + "ip": "54.38.120.36", + "port": 4001 + }, + { + "ip": "54.38.120.37", + "port": 4001 + }, + { + "ip": "54.38.120.38", + "port": 4001 + }, + { + "ip": "54.38.120.39", + "port": 4001 + }, + { + "ip": "151.80.125.32", + "port": 4001 + }, + { + "ip": "151.80.125.33", + "port": 4001 + }, + { + "ip": "151.80.125.34", + "port": 4001 + }, + { + "ip": "151.80.125.35", + "port": 4001 + }, + { + "ip": "151.80.125.36", + "port": 4001 + }, + { + "ip": "151.80.125.37", + "port": 4001 + }, + { + "ip": "151.80.125.38", + "port": 4001 + }, + { + "ip": "151.80.125.39", + "port": 4001 + }, + { + "ip": "213.32.41.104", + "port": 4001 + }, + { + "ip": "213.32.41.105", + "port": 4001 + }, + { + "ip": "213.32.41.106", + "port": 4001 + }, + { + "ip": "213.32.41.107", + "port": 4001 + }, + { + "ip": "213.32.41.108", + "port": 4001 + }, + { + "ip": "213.32.41.109", + "port": 4001 + }, + { + "ip": "213.32.41.110", + "port": 4001 + }, + { + "ip": "213.32.41.111", + "port": 4001 + }, + { + "ip": "5.135.22.92", + "port": 4001 + }, + { + "ip": "5.135.22.93", + "port": 4001 + }, + { + "ip": "5.135.22.94", + "port": 4001 + }, + { + "ip": "5.135.22.95", + "port": 4001 + }, + { + "ip": "5.135.52.96", + "port": 4001 + }, + { + "ip": "5.135.52.97", + "port": 4001 + }, + { + "ip": "5.135.52.98", + "port": 4001 + }, + { + "ip": "5.135.52.99", + "port": 4001 + }, + { + "ip": "51.255.105.52", + "port": 4001 + }, + { + "ip": "51.255.105.53", + "port": 4001 + }, + { + "ip": "51.255.105.54", + "port": 4001 + }, + { + "ip": "51.255.105.55", + "port": 4001 + }, + { + "ip": "46.105.160.104", + "port": 4001 + }, + { + "ip": "46.105.160.105", + "port": 4001 + }, + { + "ip": "46.105.160.106", + "port": 4001 + }, + { + "ip": "46.105.160.107", + "port": 4001 + } +]