From 6919f53a45ea954b56b83a3c0164846bc7f19ab1 Mon Sep 17 00:00:00 2001 From: Maxim Shishmarev Date: Tue, 7 Jul 2020 11:05:04 +1000 Subject: [PATCH] Move constants to preload.js --- preload.js | 2 ++ ts/session/onions/index.ts | 21 +++++++++------------ 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/preload.js b/preload.js index 377b757e2..a1c1a2bda 100644 --- a/preload.js +++ b/preload.js @@ -99,6 +99,8 @@ window.CONSTANTS = new (function() { // https://loki.network/2020/03/25/loki-name-system-the-facts/ this.LNS_REGEX = `^[a-zA-Z0-9_]([a-zA-Z0-9_-]{0,${this.LNS_MAX_LENGTH - 2}}[a-zA-Z0-9_]){0,1}$`; + this.MIN_GUARD_COUNT = 2; + this.DESIRED_GUARD_COUNT = 3; })(); window.versionInfo = { diff --git a/ts/session/onions/index.ts b/ts/session/onions/index.ts index 9fb8568e3..5f875fae7 100644 --- a/ts/session/onions/index.ts +++ b/ts/session/onions/index.ts @@ -6,8 +6,6 @@ import fetch from 'node-fetch'; type Snode = SnodePool.Snode; -const MIN_GUARD_COUNT = 2; - interface SnodePath { path: Array; bad: boolean; @@ -32,12 +30,12 @@ class OnionPaths { public async getOnionPath(toExclude?: { pubkey_ed25519: string; }): Promise> { - const { log } = window; + const { log, CONSTANTS } = window; let goodPaths = this.onionPaths.filter(x => !x.bad); let attemptNumber = 0; - while (goodPaths.length < MIN_GUARD_COUNT) { + while (goodPaths.length < CONSTANTS.MIN_GUARD_COUNT) { log.error( `Must have at least 2 good onion paths, actual: ${goodPaths.length}, attempt #${attemptNumber} fetching more...` ); @@ -172,12 +170,11 @@ class OnionPaths { } private async selectGuardNodes(): Promise> { - const { log } = window; + const { CONSTANTS, log } = window; - const DESIRED_GUARD_COUNT = 3; // `getRandomSnodePool` is expected to refresh itself on low nodes const nodePool = await SnodePool.getRandomSnodePool(); - if (nodePool.length < DESIRED_GUARD_COUNT) { + if (nodePool.length < CONSTANTS.DESIRED_GUARD_COUNT) { log.error( 'Could not select guard nodes. Not enough nodes in the pool: ', nodePool.length @@ -193,12 +190,12 @@ class OnionPaths { // we only want to repeat if the await fails // eslint-disable-next-line-no-await-in-loop while (guardNodes.length < 3) { - if (shuffled.length < DESIRED_GUARD_COUNT) { + if (shuffled.length < CONSTANTS.DESIRED_GUARD_COUNT) { log.error('Not enought nodes in the pool'); break; } - const candidateNodes = shuffled.splice(0, DESIRED_GUARD_COUNT); + const candidateNodes = shuffled.splice(0, CONSTANTS.DESIRED_GUARD_COUNT); // Test all three nodes at once // eslint-disable-next-line no-await-in-loop @@ -213,7 +210,7 @@ class OnionPaths { guardNodes = _.concat(guardNodes, goodNodes); } - if (guardNodes.length < DESIRED_GUARD_COUNT) { + if (guardNodes.length < CONSTANTS.DESIRED_GUARD_COUNT) { log.error( `COULD NOT get enough guard nodes, only have: ${guardNodes.length}` ); @@ -229,7 +226,7 @@ class OnionPaths { } private async buildNewOnionPathsWorker() { - const { log } = window; + const { CONSTANTS, log } = window; log.info('LokiSnodeAPI::buildNewOnionPaths - building new onion paths'); @@ -258,7 +255,7 @@ class OnionPaths { } // If guard nodes is still empty (the old nodes are now invalid), select new ones: - if (this.guardNodes.length < MIN_GUARD_COUNT) { + if (this.guardNodes.length < CONSTANTS.MIN_GUARD_COUNT) { // TODO: don't throw away potentially good guard nodes this.guardNodes = await this.selectGuardNodes(); }