Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move duplicated function in separate file #458

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 3 additions & 5 deletions src/services/bundles_restorer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
8 changes: 3 additions & 5 deletions src/services/bundles_restorer_hermes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down
9 changes: 3 additions & 6 deletions src/services/data_model_engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions src/utils/getRandomInt.ts
Original file line number Diff line number Diff line change
@@ -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;
7 changes: 2 additions & 5 deletions src/workers/atlas_resolvers/atlas_challenge_resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down
7 changes: 2 additions & 5 deletions src/workers/atlas_resolvers/atlas_transfer_resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down