Skip to content

Commit

Permalink
Merge pull request #158 from pokt-foundation/sync-allowance-hardcode
Browse files Browse the repository at this point in the history
Change syncAllowance default value as an environment variable
  • Loading branch information
rem1niscence committed Jul 13, 2021
2 parents c1a9e73 + 0a58339 commit 9d1f01f
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 19 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ POCKET_SESSION_BLOCK_FREQUENCY=4
POCKET_BLOCK_TIME=1038000
POCKET_RELAY_RETRIES=0

DEFAULT_SYNC_ALLOWANCE = 5

# Choose your AAT plan
# values: freemium, premium
AAT_PLAN=freemium
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/production-ap-southeast-1.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ jobs:
envkey_POCKET_SESSION_BLOCK_FREQUENCY: 4
envkey_POCKET_BLOCK_TIME: 1038000
envkey_POCKET_RELAY_RETRIES: 0
envkey_DEFAULT_SYNC_ALLOWANCE: 5
envkey_AAT_PLAN: 'premium'
envkey_NODE_ENV: 'production'
file_name: .env
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/production-eu-west-1.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ jobs:
envkey_POCKET_SESSION_BLOCK_FREQUENCY: 4
envkey_POCKET_BLOCK_TIME: 1038000
envkey_POCKET_RELAY_RETRIES: 0
envkey_DEFAULT_SYNC_ALLOWANCE: 5
envkey_AAT_PLAN: 'premium'
envkey_NODE_ENV: 'production'
file_name: .env
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/production-us-east-2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ jobs:
envkey_POCKET_SESSION_BLOCK_FREQUENCY: 4
envkey_POCKET_BLOCK_TIME: 1038000
envkey_POCKET_RELAY_RETRIES: 0
envkey_DEFAULT_SYNC_ALLOWANCE: 5
envkey_AAT_PLAN: 'premium'
envkey_NODE_ENV: 'production'
file_name: .env
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/production-us-west-2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ jobs:
envkey_POCKET_SESSION_BLOCK_FREQUENCY: 4
envkey_POCKET_BLOCK_TIME: 1038000
envkey_POCKET_RELAY_RETRIES: 0
envkey_DEFAULT_SYNC_ALLOWANCE: 5
envkey_AAT_PLAN: 'premium'
envkey_NODE_ENV: 'production'
file_name: .env
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/staging-us-west-2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ jobs:
envkey_POCKET_SESSION_BLOCK_FREQUENCY: 4
envkey_POCKET_BLOCK_TIME: 1038000
envkey_POCKET_RELAY_RETRIES: 0
envkey_DEFAULT_SYNC_ALLOWANCE: 5
envkey_AAT_PLAN: 'premium'
envkey_NODE_ENV: 'production'
file_name: .env
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/testnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ jobs:
envkey_POCKET_SESSION_BLOCK_FREQUENCY: 4
envkey_POCKET_BLOCK_TIME: 900000
envkey_POCKET_RELAY_RETRIES: 5
envkey_DEFAULT_SYNC_ALLOWANCE: 5
envkey_AAT_PLAN: 'premium'
file_name: .env

Expand Down
29 changes: 17 additions & 12 deletions src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,20 @@ export class PocketGatewayApplication extends BootMixin(ServiceMixin(RepositoryM
// Requirements; for Production these are stored in GitHub repo secrets
//
// For Dev, you need to pass them in via .env file
const environment: string = process.env.NODE_ENV ?? 'production'
const environment: string = process.env.NODE_ENV || 'production'

logger.log('info', 'Environment: ' + environment)

const dispatchURL: string = process.env.DISPATCH_URL ?? ''
const altruists: string = process.env.ALTRUISTS ?? ''
const clientPrivateKey: string = process.env.GATEWAY_CLIENT_PRIVATE_KEY ?? ''
const clientPassphrase: string = process.env.GATEWAY_CLIENT_PASSPHRASE ?? ''
const pocketSessionBlockFrequency: string = process.env.POCKET_SESSION_BLOCK_FREQUENCY ?? ''
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 aatPlan = process.env.AAT_PLAN ?? AatPlans.PREMIUM
const dispatchURL: string = process.env.DISPATCH_URL || ''
const altruists: string = process.env.ALTRUISTS || ''
const clientPrivateKey: string = process.env.GATEWAY_CLIENT_PRIVATE_KEY || ''
const clientPassphrase: string = process.env.GATEWAY_CLIENT_PASSPHRASE || ''
const pocketSessionBlockFrequency: string = process.env.POCKET_SESSION_BLOCK_FREQUENCY || ''
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) {
throw new HttpErrors.InternalServerError('DISPATCH_URL required in ENV')
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 Expand Up @@ -144,8 +149,8 @@ export class PocketGatewayApplication extends BootMixin(ServiceMixin(RepositoryM
this.bind('redisInstance').to(redis)

// Load Postgres for TimescaleDB metrics
const pgConnection: string = process.env.PG_CONNECTION ?? ''
const pgCertificate: string = process.env.PG_CERTIFICATE ?? ''
const pgConnection: string = process.env.PG_CONNECTION || ''
const pgCertificate: string = process.env.PG_CERTIFICATE || ''

if (!pgConnection) {
throw new HttpErrors.InternalServerError('PG_CONNECTION required in ENV')
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
4 changes: 2 additions & 2 deletions src/services/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ interface Log {
elapsedTime: number
}

const environment: string = process.env.NODE_ENV ?? 'production'
const logzToken: string = process.env.LOGZ_TOKEN ?? ''
const environment: string = process.env.NODE_ENV || 'production'
const logzToken: string = process.env.LOGZ_TOKEN || ''

if (!logzToken && environment === 'production') {
throw new HttpErrors.InternalServerError('LOGZ_TOKEN required in ENV')
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 @@ -11,10 +11,12 @@ import axios from 'axios'
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 @@ -32,9 +34,7 @@ export class SyncChecker {
pocketConfiguration,
}: ConsensusFilterOptions): Promise<Node[]> {
// Blockchain records passed in with 0 sync allowance are missing the 'syncAllowance' field in MongoDB
if (syncAllowance <= 0) {
syncAllowance = 5
}
syncAllowance = syncAllowance <= 0 ? syncAllowance : this.defaultSyncAllowance

const syncedNodes: Node[] = []
let syncedNodesList: string[] = []
Expand Down

0 comments on commit 9d1f01f

Please sign in to comment.