Move constants to preload.js

pull/1221/head
Maxim Shishmarev 5 years ago
parent 12f73e23f2
commit 6919f53a45

@ -99,6 +99,8 @@ window.CONSTANTS = new (function() {
// https://loki.network/2020/03/25/loki-name-system-the-facts/ // 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 - this.LNS_REGEX = `^[a-zA-Z0-9_]([a-zA-Z0-9_-]{0,${this.LNS_MAX_LENGTH -
2}}[a-zA-Z0-9_]){0,1}$`; 2}}[a-zA-Z0-9_]){0,1}$`;
this.MIN_GUARD_COUNT = 2;
this.DESIRED_GUARD_COUNT = 3;
})(); })();
window.versionInfo = { window.versionInfo = {

@ -6,8 +6,6 @@ import fetch from 'node-fetch';
type Snode = SnodePool.Snode; type Snode = SnodePool.Snode;
const MIN_GUARD_COUNT = 2;
interface SnodePath { interface SnodePath {
path: Array<Snode>; path: Array<Snode>;
bad: boolean; bad: boolean;
@ -32,12 +30,12 @@ class OnionPaths {
public async getOnionPath(toExclude?: { public async getOnionPath(toExclude?: {
pubkey_ed25519: string; pubkey_ed25519: string;
}): Promise<Array<Snode>> { }): Promise<Array<Snode>> {
const { log } = window; const { log, CONSTANTS } = window;
let goodPaths = this.onionPaths.filter(x => !x.bad); let goodPaths = this.onionPaths.filter(x => !x.bad);
let attemptNumber = 0; let attemptNumber = 0;
while (goodPaths.length < MIN_GUARD_COUNT) { while (goodPaths.length < CONSTANTS.MIN_GUARD_COUNT) {
log.error( log.error(
`Must have at least 2 good onion paths, actual: ${goodPaths.length}, attempt #${attemptNumber} fetching more...` `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<Array<Snode>> { private async selectGuardNodes(): Promise<Array<Snode>> {
const { log } = window; const { CONSTANTS, log } = window;
const DESIRED_GUARD_COUNT = 3;
// `getRandomSnodePool` is expected to refresh itself on low nodes // `getRandomSnodePool` is expected to refresh itself on low nodes
const nodePool = await SnodePool.getRandomSnodePool(); const nodePool = await SnodePool.getRandomSnodePool();
if (nodePool.length < DESIRED_GUARD_COUNT) { if (nodePool.length < CONSTANTS.DESIRED_GUARD_COUNT) {
log.error( log.error(
'Could not select guard nodes. Not enough nodes in the pool: ', 'Could not select guard nodes. Not enough nodes in the pool: ',
nodePool.length nodePool.length
@ -193,12 +190,12 @@ class OnionPaths {
// we only want to repeat if the await fails // we only want to repeat if the await fails
// eslint-disable-next-line-no-await-in-loop // eslint-disable-next-line-no-await-in-loop
while (guardNodes.length < 3) { 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'); log.error('Not enought nodes in the pool');
break; break;
} }
const candidateNodes = shuffled.splice(0, DESIRED_GUARD_COUNT); const candidateNodes = shuffled.splice(0, CONSTANTS.DESIRED_GUARD_COUNT);
// Test all three nodes at once // Test all three nodes at once
// eslint-disable-next-line no-await-in-loop // eslint-disable-next-line no-await-in-loop
@ -213,7 +210,7 @@ class OnionPaths {
guardNodes = _.concat(guardNodes, goodNodes); guardNodes = _.concat(guardNodes, goodNodes);
} }
if (guardNodes.length < DESIRED_GUARD_COUNT) { if (guardNodes.length < CONSTANTS.DESIRED_GUARD_COUNT) {
log.error( log.error(
`COULD NOT get enough guard nodes, only have: ${guardNodes.length}` `COULD NOT get enough guard nodes, only have: ${guardNodes.length}`
); );
@ -229,7 +226,7 @@ class OnionPaths {
} }
private async buildNewOnionPathsWorker() { private async buildNewOnionPathsWorker() {
const { log } = window; const { CONSTANTS, log } = window;
log.info('LokiSnodeAPI::buildNewOnionPaths - building new onion paths'); 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 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 // TODO: don't throw away potentially good guard nodes
this.guardNodes = await this.selectGuardNodes(); this.guardNodes = await this.selectGuardNodes();
} }

Loading…
Cancel
Save