Skip to content
This repository has been archived by the owner on Jun 7, 2023. It is now read-only.

Shared: Add fallback node list api endpoints #1811

Merged
merged 5 commits into from
Jun 20, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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
10 changes: 1 addition & 9 deletions src/shared/actions/polling.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,15 +325,7 @@ export const fetchNodeList = () => {
.then((remoteNodes) => {
// If there is a successful response, keep a union of (new nodes returned from the endpoint, default hardcoded nodes)
if (remoteNodes.length) {
nodes = unionBy(
nodes,
map(remoteNodes, (node) => ({
url: node.node,
pow: node.pow,
token: '',
})),
'url',
);
nodes = unionBy(nodes, remoteNodes, 'url');
} else {
// Otherwise, fallback to existing nodes
nodes = getNodesFromState(getState());
Expand Down
7 changes: 6 additions & 1 deletion src/shared/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ export const NODES_WITH_POW_ENABLED = [

export const DEFAULT_NODES = [...NODES_WITH_POW_DISABLED, ...NODES_WITH_POW_ENABLED];

export const NODELIST_URL = 'https://nodes.iota.works/api/ssl/live';
export const NODELIST_ENDPOINTS = [
'https://nodes.iota.works/api/ssl/live',
'https://iota-node-api.now.sh/api/ssl/live',
'https://iota.dance/api/ssl/live',
];

export const VERSIONS_URL =
'https://raw.githubusercontent.com/iotaledger/trinity-wallet/develop/src/shared/libs/versions.json';
Expand All @@ -58,6 +62,7 @@ export const MAX_REQUEST_TIMEOUT = 60 * 1000 * 2;
export const DEFAULT_NODE_REQUEST_TIMEOUT = 6000 * 2;
export const GET_NODE_INFO_REQUEST_TIMEOUT = 2500;
export const GET_BALANCES_REQUEST_TIMEOUT = 6000;
export const GET_NODELIST_REQUEST_TIMEOUT = 4000;
export const WERE_ADDRESSES_SPENT_FROM_REQUEST_TIMEOUT = 4000;
export const ATTACH_TO_TANGLE_REQUEST_TIMEOUT = 25000;
export const GET_TRANSACTIONS_TO_APPROVE_REQUEST_TIMEOUT = 40000;
Expand Down
53 changes: 37 additions & 16 deletions src/shared/libs/iota/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import URL from 'url-parse';
import { BigNumber } from 'bignumber.js';
import { iota } from './index';
import { isNodeHealthy } from './extendedApi';
import { NODELIST_URL, MAX_REQUEST_TIMEOUT } from '../../config';
import { NODELIST_ENDPOINTS, GET_NODELIST_REQUEST_TIMEOUT, MAX_REQUEST_TIMEOUT } from '../../config';
import Errors from '../errors';
import { roundDown } from '../utils';

Expand Down Expand Up @@ -425,28 +425,49 @@ export const withRetriesOnDifferentNodes = (nodes, failureCallbacks) => {
* Fetches list of IRI nodes from a server
*
* @method fetchRemoteNodes
* @param {string} [url]
* @param {object} [options]
*
* @returns {Promise<*>}
* @returns {Promise<array>}
*/
export const fetchRemoteNodes = (
url = NODELIST_URL,
options = {
export const fetchRemoteNodes = async () => {
const requestOptions = {
headers: {
Accept: 'application/json',
},
},
) =>
fetch(url, options)
.then((response) => response.json())
.then((response) => {
if (isArray(response)) {
return response.filter((node) => typeof node.node === 'string' && node.node.indexOf('https://') === 0);
};

let remoteNodes = [];

for (let index = 0; index < NODELIST_ENDPOINTS.length; index++) {
try {
const endPoint = NODELIST_ENDPOINTS[index];

const response = await Promise.race([
fetch(endPoint, requestOptions),
new Promise((_, reject) =>
setTimeout(() => reject(new Error('Fetch timeout')), GET_NODELIST_REQUEST_TIMEOUT),
),
]);
const remoteList = await response.json();

remoteNodes = remoteList.filter(
({ node, pow }) =>
typeof node === 'string' && node.indexOf('https://') === 0 && typeof pow === 'boolean',
);

if (remoteNodes.length === 0) {
throw Error('No nodes returned');
}

return [];
});
break;
} catch (err) {}
}

return remoteNodes.map(({ node, pow }) => ({
url: node,
pow,
token: '',
}));
};

/**
* Gets random nodes.
Expand Down
2 changes: 1 addition & 1 deletion src/shared/libs/migrations.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const migrateNodes = (nodes) => {
if (size(remoteNodes)) {
Node.addNodes(
map(nodes, (node) => {
const remoteNode = find(remoteNodes, { node: node.url });
const remoteNode = find(remoteNodes, { url: node.url });

if (remoteNode) {
return assign({}, node, { pow: remoteNode.pow });
Expand Down
3 changes: 2 additions & 1 deletion src/shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"Charlie Varley"
],
"scripts": {
"test": "NODE_ENV=test ./node_modules/.bin/mocha --timeout 5000 --exit --require @babel/register \"__tests__/**/*.spec.js\"",
"test": "NODE_ENV=test ./node_modules/.bin/mocha --timeout 5000 --exit --require @babel/register \"__tests__/**/*.spec.js\" --require babel-polyfill",
"posttest": "rimraf realm-object-server",
"test:watch": "yarn test --watch",
"test:coverage": "nyc --check-coverage --lines 50 --statements 50 --functions 50 --branches 50 yarn test",
Expand Down Expand Up @@ -54,6 +54,7 @@
"@babel/plugin-transform-react-jsx": "^7.0.0",
"@babel/preset-env": "^7.4.5",
"@babel/register": "^7.4.4",
"babel-polyfill": "^6.26.0",
"chai": "^4.2.0",
"figma-parser": "^0.0.2",
"mocha": "^6.1.4",
Expand Down
32 changes: 32 additions & 0 deletions src/shared/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1180,6 +1180,23 @@ axios@^0.18.0:
follow-redirects "1.5.10"
is-buffer "^2.0.2"

babel-polyfill@^6.26.0:
version "6.26.0"
resolved "https://registry.yarnpkg.com/babel-polyfill/-/babel-polyfill-6.26.0.tgz#379937abc67d7895970adc621f284cd966cf2153"
integrity sha1-N5k3q8Z9eJWXCtxiHyhM2WbPIVM=
dependencies:
babel-runtime "^6.26.0"
core-js "^2.5.0"
regenerator-runtime "^0.10.5"

babel-runtime@^6.26.0:
version "6.26.0"
resolved "https://registry.yarnpkg.com/babel-runtime/-/babel-runtime-6.26.0.tgz#965c7058668e82b55d7bfe04ff2337bc8b5647fe"
integrity sha1-llxwWGaOgrVde/4E/yM3vItWR/4=
dependencies:
core-js "^2.4.0"
regenerator-runtime "^0.11.0"

balanced-match@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.0.tgz#89b4d199ab2bee49de164ea02b89ce462d71b767"
Expand Down Expand Up @@ -1546,6 +1563,11 @@ core-js@^1.0.0:
resolved "https://registry.yarnpkg.com/core-js/-/core-js-1.2.7.tgz#652294c14651db28fa93bd2d5ff2983a4f08c636"
integrity sha1-ZSKUwUZR2yj6k70tX/KYOk8IxjY=

core-js@^2.4.0, core-js@^2.5.0:
version "2.6.9"
resolved "https://registry.yarnpkg.com/core-js/-/core-js-2.6.9.tgz#6b4b214620c834152e179323727fc19741b084f2"
integrity sha512-HOpZf6eXmnl7la+cUdMnLvUxKNqLUzJvgIziQ0DiF3JwSImNphIqdGqzj6hIKyX04MmV0poclQ7+wjWvxQyR2A==

core-js@^3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/core-js/-/core-js-3.0.1.tgz#1343182634298f7f38622f95e73f54e48ddf4738"
Expand Down Expand Up @@ -4090,6 +4112,16 @@ regenerate@^1.4.0:
resolved "https://registry.yarnpkg.com/regenerate/-/regenerate-1.4.0.tgz#4a856ec4b56e4077c557589cae85e7a4c8869a11"
integrity sha512-1G6jJVDWrt0rK99kBjvEtziZNCICAuvIPkSiUFIQxVP06RCVpq3dmDo2oi6ABpYaDYaTRr67BEhL8r1wgEZZKg==

regenerator-runtime@^0.10.5:
version "0.10.5"
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.10.5.tgz#336c3efc1220adcedda2c9fab67b5a7955a33658"
integrity sha1-M2w+/BIgrc7dosn6tntaeVWjNlg=

regenerator-runtime@^0.11.0:
version "0.11.1"
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.11.1.tgz#be05ad7f9bf7d22e056f9726cee5017fbf19e2e9"
integrity sha512-MguG95oij0fC3QV3URf4V2SDYGJhJnJGqvIIgdECeODCT98wSWDAJ94SSuVpYQUoTcGUIL6L4yNB7j1DFFHSBg==

regenerator-runtime@^0.12.0:
version "0.12.1"
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.12.1.tgz#fa1a71544764c036f8c49b13a08b2594c9f8a0de"
Expand Down