From 8e7bd305bd1b753dcac1f81e5fa5095c18679e87 Mon Sep 17 00:00:00 2001 From: Alex Kuznietsov Date: Thu, 25 Nov 2021 12:11:34 +0200 Subject: [PATCH] Move dublicated function in separate file --- src/services/bundles_restorer.js | 8 +++----- src/services/bundles_restorer_hermes.js | 8 +++----- src/services/data_model_engine.js | 9 +++------ src/utils/getRandomInt.ts | 12 ++++++++++++ .../atlas_resolvers/atlas_challenge_resolver.js | 7 ++----- .../atlas_resolvers/atlas_transfer_resolver.js | 7 ++----- 6 files changed, 25 insertions(+), 26 deletions(-) create mode 100644 src/utils/getRandomInt.ts diff --git a/src/services/bundles_restorer.js b/src/services/bundles_restorer.js index 8652f2a4..4aab7396 100644 --- a/src/services/bundles_restorer.js +++ b/src/services/bundles_restorer.js @@ -7,6 +7,8 @@ This Source Code Form is subject to the terms of the Mozilla Public License, v. This Source Code Form is “Incompatible With Secondary Licenses”, as defined by the Mozilla Public License, v. 2.0. */ +import getRandomInt from '../utils/getRandomInt'; + export default class BundlesRestorer { constructor(bundleStoreWrapper, shelteringWrapper, shelteringTransfersWrapper, dataModelEngine, bundleRepository, shelteredBundlesRepository, workerLogger) { this.bundleStoreWrapper = bundleStoreWrapper; @@ -45,7 +47,7 @@ export default class BundlesRestorer { const expirationTime = await this.shelteringWrapper.shelteringExpirationDate(bundle.bundleId); const donors = await this.getBundleDonors(bundle); while (donors.length > 0) { - const pos = this.getRandomInt(donors.length); + const pos = getRandomInt(donors.length); const donorId = donors[pos]; try { await this.dataModelEngine.downloadBundle(bundle.bundleId, donorId, expirationTime); @@ -68,10 +70,6 @@ export default class BundlesRestorer { } } - getRandomInt(max) { - return Math.floor(Math.random() * Math.floor(max)); - } - async getBundleDonors(bundle) { const shelterers = await this.bundleStoreWrapper.getShelterers(bundle.bundleId); let pos = shelterers.indexOf(bundle.shelterer); diff --git a/src/services/bundles_restorer_hermes.js b/src/services/bundles_restorer_hermes.js index dc924f85..0e6d55aa 100644 --- a/src/services/bundles_restorer_hermes.js +++ b/src/services/bundles_restorer_hermes.js @@ -7,6 +7,8 @@ This Source Code Form is subject to the terms of the Mozilla Public License, v. This Source Code Form is “Incompatible With Secondary Licenses”, as defined by the Mozilla Public License, v. 2.0. */ +import getRandomInt from '../utils/getRandomInt'; + export default class BundlesRestorerHermes { constructor( bundleStoreWrapper, @@ -103,7 +105,7 @@ export default class BundlesRestorerHermes { } while (donors.length > 0) { - const pos = this.getRandomInt(donors.length); + const pos = getRandomInt(donors.length); const donorId = donors[pos]; try { @@ -132,10 +134,6 @@ export default class BundlesRestorerHermes { } } - getRandomInt(max) { - return Math.floor(Math.random() * Math.floor(max)); - } - async getBundleDonors(bundle) { const shelterers = await this.bundleStoreWrapper.getShelterers(bundle.bundleId); let pos = shelterers.indexOf(bundle.shelterer); diff --git a/src/services/data_model_engine.js b/src/services/data_model_engine.js index 1e08f2e6..101c9500 100644 --- a/src/services/data_model_engine.js +++ b/src/services/data_model_engine.js @@ -13,6 +13,7 @@ import {pick, put} from '../utils/dict_utils'; import allPermissions from '../utils/all_permissions'; import BundleStatuses from '../utils/bundle_statuses'; import removeDuplicates from '../utils/sutils.js'; +import getRandomInt from '../utils/getRandomInt'; export default class DataModelEngine { constructor( @@ -347,7 +348,7 @@ export default class DataModelEngine { this.bundleBuilder.validateBundleMetadata(downloadedMetadata); try { - const bundle = await this.bundleDownloader.downloadBundleFull(nodeUrl, bundleId); + const bundle = await this.bundleDownloader.downloadBundleFull(nodeUrl, bundleId); // todo: check if bundle valid bundle.metadata = initialMetadata; @@ -430,10 +431,6 @@ export default class DataModelEngine { await this.bundleBuilder.validateStreamedBundleNoWrite(stream, bundleItemsCountLimit); // throws ValidationError } - getRandomInt(max) { - return Math.floor(Math.random() * Math.floor(max)); - } - async getBundleDonors(bundleId, nodeId = null) { const donors = await this.bundleStoreWrapper.getShelterers(bundleId); if (nodeId) { @@ -473,7 +470,7 @@ export default class DataModelEngine { const donors = await this.getBundleDonors(bundleId, this.identityManager.nodeAddress()); // console.log('donors',donors); while (donors.length > 0) { - const pos = this.getRandomInt(donors.length); + const pos = getRandomInt(donors.length); const donorId = donors[pos]; try { await this.downloadBundle(bundleId, donorId, expirationTime); diff --git a/src/utils/getRandomInt.ts b/src/utils/getRandomInt.ts new file mode 100644 index 00000000..9719960b --- /dev/null +++ b/src/utils/getRandomInt.ts @@ -0,0 +1,12 @@ +/* +Copyright: Ambrosus Inc. +Email: tech@ambrosus.io + +This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/. + +This Source Code Form is “Incompatible With Secondary Licenses”, as defined by the Mozilla Public License, v. 2.0. +*/ + +const getRandomInt = (max: number): number => Math.floor(Math.random() * Math.floor(max)); + +export default getRandomInt; diff --git a/src/workers/atlas_resolvers/atlas_challenge_resolver.js b/src/workers/atlas_resolvers/atlas_challenge_resolver.js index a96c9b99..45569ab9 100644 --- a/src/workers/atlas_resolvers/atlas_challenge_resolver.js +++ b/src/workers/atlas_resolvers/atlas_challenge_resolver.js @@ -10,6 +10,7 @@ This Source Code Form is “Incompatible With Secondary Licenses”, as defined import promClient from 'prom-client'; import BundleShelteringResolver from './bundle_sheltering_resolver'; import {atlasResolutionStatus} from './atlas_resolver'; +import getRandomInt from '../../utils/getRandomInt'; export default class AtlasChallengeResolver extends BundleShelteringResolver { constructor( @@ -49,10 +50,6 @@ export default class AtlasChallengeResolver extends BundleShelteringResolver { }); } - getRandomInt(max) { - return Math.floor(Math.random() * Math.floor(max)); - } - async tryToDownload(proposition) { const propositionExpirationTime = await this.resolutionsRepository.getExpirationTimeInMs(proposition); let metadata; @@ -63,7 +60,7 @@ export default class AtlasChallengeResolver extends BundleShelteringResolver { await this.workerLogger.logger.info(`Failed to download bundle: ${err.message || err}`, proposition, err.stack); const donors = await this.getBundleDonors(proposition); while (donors.length > 0) { - const pos = this.getRandomInt(donors.length); + const pos = getRandomInt(donors.length); const donorId = donors[pos]; try { metadata = await this.dataModelEngine.downloadBundle(proposition.bundleId, donorId, propositionExpirationTime); diff --git a/src/workers/atlas_resolvers/atlas_transfer_resolver.js b/src/workers/atlas_resolvers/atlas_transfer_resolver.js index eaccfdbb..edf22187 100644 --- a/src/workers/atlas_resolvers/atlas_transfer_resolver.js +++ b/src/workers/atlas_resolvers/atlas_transfer_resolver.js @@ -10,6 +10,7 @@ This Source Code Form is “Incompatible With Secondary Licenses”, as defined import promClient from 'prom-client'; import BundleShelteringResolver from './bundle_sheltering_resolver'; import {atlasResolutionStatus} from './atlas_resolver'; +import getRandomInt from '../../utils/getRandomInt'; export default class AtlasTransferResolver extends BundleShelteringResolver { constructor( @@ -49,10 +50,6 @@ export default class AtlasTransferResolver extends BundleShelteringResolver { }); } - getRandomInt(max) { - return Math.floor(Math.random() * Math.floor(max)); - } - async tryToDownload(proposition) { const propositionExpirationTime = await this.resolutionsRepository.getExpirationTimeInMs(proposition); let metadata; @@ -63,7 +60,7 @@ export default class AtlasTransferResolver extends BundleShelteringResolver { await this.workerLogger.logger.info(`Failed to download bundle: ${err.message || err}`, proposition, err.stack); const donors = await this.getBundleDonors(proposition); while (donors.length > 0) { - const pos = this.getRandomInt(donors.length); + const pos = getRandomInt(donors.length); const donorId = donors[pos]; try { metadata = await this.dataModelEngine.downloadBundle(proposition.bundleId, donorId, propositionExpirationTime);