Skip to content

Commit

Permalink
fix: change syncAllowance to be injected through loopback
Browse files Browse the repository at this point in the history
  • Loading branch information
rem1niscence committed Jul 13, 2021
1 parent bddc518 commit 0a58339
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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')
}
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion src/controllers/v1.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions src/services/sync-checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -34,7 +34,7 @@ export class SyncChecker {
pocketConfiguration,
}: ConsensusFilterOptions): Promise<Node[]> {
// 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[] = []
Expand Down

0 comments on commit 0a58339

Please sign in to comment.