Skip to content

Commit

Permalink
Randomize network selection (#457)
Browse files Browse the repository at this point in the history
* feat: Add utils functions to generate random stuff

* feat: Choose nodes and explorer randomly

* fix: reintroduce fSilent flag
  • Loading branch information
panleone authored Nov 8, 2024
1 parent f268986 commit b3520ff
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
21 changes: 12 additions & 9 deletions scripts/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { Database } from './database.js';
import { getEventEmitter } from './event_bus.js';
import countries from 'country-locale-map/countries.json';
import { getNetwork } from './network/network_manager.js';
import { getRandomElement } from './utils.js';

// --- Default Settings
/** A mode that emits verbose console info for internal MPW operations */
Expand Down Expand Up @@ -212,19 +213,21 @@ function subscribeToNetworkEvents() {
}

// --- Settings Functions
export async function setExplorer(explorer) {
export async function setExplorer(explorer, fSilent = false) {
const database = await Database.getInstance();
await database.setSettings({ explorer: explorer.url });
getNetwork().setNetwork(explorer.url, false);

// Update the selector UI
doms.domExplorerSelect.value = explorer.url;

createAlert(
'success',
tr(ALERTS.SWITCHED_EXPLORERS, [{ explorerName: explorer.name }]),
2250
);
if (!fSilent) {
createAlert(
'success',
tr(ALERTS.SWITCHED_EXPLORERS, [{ explorerName: explorer.name }]),
2250
);
}
getEventEmitter().emit('explorer_changed', explorer.url);
}

Expand Down Expand Up @@ -433,7 +436,7 @@ export async function toggleAutoSwitch() {
}

async function fillExplorerSelect() {
const firstExplorer = cChainParams.current.Explorers[0];
const firstExplorer = getRandomElement(cChainParams.current.Explorers);

while (doms.domExplorerSelect.options.length > 0) {
doms.domExplorerSelect.remove(0);
Expand All @@ -452,7 +455,7 @@ async function fillExplorerSelect() {
const database = await Database.getInstance();
const { explorer: strSettingExplorer } = await database.getSettings();

// For any that exist: load them, or use the defaults
// For any that exist: load them, or use the default
await setExplorer(
cChainParams.current.Explorers.find(
(a) => a.url === strSettingExplorer
Expand All @@ -462,7 +465,7 @@ async function fillExplorerSelect() {
}

async function fillNodeSelect() {
const firstNode = cChainParams.current.Nodes[0];
const firstNode = getRandomElement(cChainParams.current.Nodes);

while (doms.domNodeSelect.options.length > 0) {
doms.domNodeSelect.remove(0);
Expand Down
19 changes: 19 additions & 0 deletions scripts/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,22 @@ export async function startBatch(
export function sleep(ms) {
return new Promise((res, _) => setTimeout(res, ms));
}

/**
* Returns a random number in the range [0, N)
* @param {number} N
* @returns {number}
*/
function getRandomInt(N) {
return Math.floor(Math.random() * N);
}

/**
* Returns a random element of arr
* @template T
* @param {T[]} arr
* @returns T
*/
export function getRandomElement(arr) {
return arr[getRandomInt(arr.length)];
}

0 comments on commit b3520ff

Please sign in to comment.