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

Shared: If enabled only sample remote pow nodes for autoswitching #1673

Merged
merged 2 commits into from
May 31, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 4 additions & 6 deletions src/shared/libs/iota/NodesManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import includes from 'lodash/includes';
import isArray from 'lodash/isArray';
import isFunction from 'lodash/isFunction';
import isUndefined from 'lodash/isUndefined';
import unionBy from 'lodash/unionBy';
import Errors from '../errors';
import { getRandomNodes } from './utils';
import { DEFAULT_RETRIES } from '../../config';
Expand Down Expand Up @@ -33,14 +34,11 @@ export default class NodesManager {
*/
withRetries(failureCallbacks, retryAttempts = DEFAULT_RETRIES) {
const { priorityNode, primaryNode, nodeAutoSwitch, nodes, quorum } = this.config;

let attempt = 0;
let executedCallback = false;

const randomNodes = getRandomNodes(nodes, retryAttempts, [primaryNode]);

const retryNodes = [priorityNode, ...randomNodes];

const remotePoW = store.getState().settings.remotePoW;
const randomNodes = getRandomNodes(nodes, retryAttempts, [primaryNode], remotePoW);
const retryNodes = unionBy(remotePoW === priorityNode.pow && [priorityNode], randomNodes, 'url');
// Abort retries on these errors
const cancellationErrors = [Errors.LEDGER_CANCELLED, Errors.CANNOT_TRANSITION_ADDRESSES_WITH_ZERO_BALANCE];

Expand Down
9 changes: 7 additions & 2 deletions src/shared/libs/iota/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,11 +454,16 @@ export const fetchRemoteNodes = (
* @param {array} nodes
* @param {number} [size]
* @param {array} [blacklistedNodes]
* @param {bool} Remote PoW
*
* @returns {Array}
*/
export const getRandomNodes = (nodes, size = 5, blacklistedNodes = []) => {
return sampleSize(filter(nodes, (node) => !find(blacklistedNodes, { url: node.url })), size);
export const getRandomNodes = (nodes, size = 5, blacklistedNodes = [], PoW = false) => {
let nodesToSample = nodes;
if (PoW) {
nodesToSample = filter(nodes, (node) => node.pow === true);
}
return sampleSize(filter(nodesToSample, (node) => !find(blacklistedNodes, { url: node.url })), size);
};

/**
Expand Down