From 0a58339cd6a68683e062eb15b8054a8fc371e6ab Mon Sep 17 00:00:00 2001 From: Roniel Valdez Date: Tue, 13 Jul 2021 14:30:33 -0400 Subject: [PATCH] fix: change syncAllowance to be injected through loopback --- src/application.ts | 5 +++++ src/controllers/v1.controller.ts | 3 ++- src/services/sync-checker.ts | 8 ++++---- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/application.ts b/src/application.ts index 2d55481e..19787f04 100644 --- a/src/application.ts +++ b/src/application.ts @@ -56,6 +56,7 @@ export class PocketGatewayApplication extends BootMixin(ServiceMixin(RepositoryM const pocketBlockTime: string = process.env.POCKET_BLOCK_TIME || '' const relayRetries: string = process.env.POCKET_RELAY_RETRIES || '' const databaseEncryptionKey: string = process.env.DATABASE_ENCRYPTION_KEY || '' + const defaultSyncAllowance: number = parseInt(process.env.DEFAULT_SYNC_ALLOWANCE) || -1 const aatPlan = process.env.AAT_PLAN || AatPlans.PREMIUM if (!dispatchURL) { @@ -79,6 +80,9 @@ export class PocketGatewayApplication extends BootMixin(ServiceMixin(RepositoryM if (!databaseEncryptionKey) { throw new HttpErrors.InternalServerError('DATABASE_ENCRYPTION_KEY required in ENV') } + if (defaultSyncAllowance < 0) { + throw new HttpErrors.InternalServerError('DEFAULT_SYNC_ALLOWANCE required in ENV') + } if (aatPlan !== AatPlans.PREMIUM && !AatPlans.values.includes(aatPlan)) { throw new HttpErrors.InternalServerError('Unrecognized AAT Plan') } @@ -116,6 +120,7 @@ export class PocketGatewayApplication extends BootMixin(ServiceMixin(RepositoryM this.bind('relayRetries').to(parseInt(relayRetries)) this.bind('altruists').to(altruists) this.bind('logger').to(logger) + this.bind('defaultSyncAllowance').to(defaultSyncAllowance) // Unlock primary client account for relay signing try { diff --git a/src/controllers/v1.controller.ts b/src/controllers/v1.controller.ts index e9ab38d7..52be14cd 100644 --- a/src/controllers/v1.controller.ts +++ b/src/controllers/v1.controller.ts @@ -38,6 +38,7 @@ export class V1Controller { @inject('processUID') private processUID: string, @inject('altruists') private altruists: string, @inject('requestID') private requestID: string, + @inject('defaultSyncAllowance') private defaultSyncAllowance: number, @inject('aatPlan') private aatPlan: string, @repository(ApplicationsRepository) public applicationsRepository: ApplicationsRepository, @@ -56,7 +57,7 @@ export class V1Controller { cherryPicker: this.cherryPicker, processUID: this.processUID, }) - this.syncChecker = new SyncChecker(this.redis, this.metricsRecorder) + this.syncChecker = new SyncChecker(this.redis, this.metricsRecorder, this.defaultSyncAllowance) this.chainChecker = new ChainChecker(this.redis, this.metricsRecorder) this.pocketRelayer = new PocketRelayer({ host: this.host, diff --git a/src/services/sync-checker.ts b/src/services/sync-checker.ts index d2974e8a..bacc5852 100644 --- a/src/services/sync-checker.ts +++ b/src/services/sync-checker.ts @@ -8,15 +8,15 @@ const logger = require('../services/logger') import axios from 'axios' -const DEFAULT_SYNC_ALLOWANCE: number = parseInt(process.env.DEFAULT_SYNC_ALLOWANCE) || 5 - export class SyncChecker { redis: Redis metricsRecorder: MetricsRecorder + defaultSyncAllowance: number - constructor(redis: Redis, metricsRecorder: MetricsRecorder) { + constructor(redis: Redis, metricsRecorder: MetricsRecorder, defaultSyncAllowance: number) { this.redis = redis this.metricsRecorder = metricsRecorder + this.defaultSyncAllowance = defaultSyncAllowance } async consensusFilter({ @@ -34,7 +34,7 @@ export class SyncChecker { pocketConfiguration, }: ConsensusFilterOptions): Promise { // Blockchain records passed in with 0 sync allowance are missing the 'syncAllowance' field in MongoDB - syncAllowance = syncAllowance <= 0 ? syncAllowance : DEFAULT_SYNC_ALLOWANCE + syncAllowance = syncAllowance <= 0 ? syncAllowance : this.defaultSyncAllowance const syncedNodes: Node[] = [] let syncedNodesList: string[] = []