Skip to content

Commit

Permalink
fix: simplify DENCUN_FORK_EPOCH validation
Browse files Browse the repository at this point in the history
Simplify validation of the `DENCUN_FORK_EPOCH` environment variable. Now
the validation is based on these two assumptions:

1. If the value of the `DENCUN_FORK_EPOCH` variable is not provided, it
is now implicitly set to the maximum integer for any unknown networks.
So, the app assumes that by default the Dencun fork is not applied to
any unknown network.

2. For the 3 officially supported networks correct values for the
`DENCUN_FORK_EPOCH` variable are set implicitly. For these 3 networks
values that the user sets for the `DENCUN_FORK_EPOCH` variable are
silently ignored.

Custom validation directive and complex transform function have been
removed.

The app now logs the actual value of the `DENCUN_FORK_EPOCH` variable
when it starts.
  • Loading branch information
AlexanderLukin committed Jan 10, 2024
1 parent 9a07dfd commit 334eb16
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 66 deletions.
1 change: 1 addition & 0 deletions src/app/app.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export class AppService implements OnModuleInit, OnApplicationBootstrap {
this.logger.log(`DRY RUN ${this.configService.get('DRY_RUN') ? 'enabled' : 'disabled'}`);
this.logger.log(`Slot time: ${this.configService.get('CHAIN_SLOT_TIME_SECONDS')} seconds`);
this.logger.log(`Epoch size: ${this.configService.get('FETCH_INTERVAL_SLOTS')} slots`);
this.logger.log(`Dencun fork epoch: ${this.configService.get('DENCUN_FORK_EPOCH')}`);
}

public async onApplicationBootstrap(): Promise<void> {
Expand Down
71 changes: 14 additions & 57 deletions src/common/config/env.validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@ import {
Min,
MinLength,
ValidateIf,
ValidationArguments,
registerDecorator,
validateSync,
} from 'class-validator';
import { Epoch } from 'common/consensus-provider/types';

import { DencunForkEpoch, Environment, LogFormat, LogLevel } from './interfaces';
import { Environment, LogFormat, LogLevel } from './interfaces';

export enum Network {
Mainnet = 1,
Expand All @@ -41,6 +39,15 @@ export enum WorkingMode {
Head = 'head',
}

const dencunForkEpoch = {
/**
* @todo This should be corrected once the particular epoch of the Dencun hard fork on Mainnet is known.
*/
'1': 300000,
'5': 231680,
'17000': 29696,
};

const toBoolean = (value: any): boolean => {
if (typeof value === 'boolean') {
return value;
Expand Down Expand Up @@ -170,9 +177,11 @@ export class EnvironmentVariables {

@IsInt()
@IsPositive()
@IsValidDencunEpoch()
@Expose()
@Transform(transformDencunEpoch)
@Transform(
({ value, obj }) =>
dencunForkEpoch[obj.ETH_NETWORK] || (value != null && value.trim() !== '' ? parseInt(value, 10) : Number.MAX_SAFE_INTEGER),
)
@ValidateIf((vars) => vars.NODE_ENV !== Environment.test)
public DENCUN_FORK_EPOCH: Epoch;

Expand Down Expand Up @@ -304,55 +313,3 @@ export function validate(config: Record<string, unknown>) {

return validatedConfig;
}

// ====================================================================================================================
// PRIVATE FUNCTIONS
// ====================================================================================================================
function IsValidDencunEpoch() {
return function (object: Object, propertyName: string) {
registerDecorator({
name: 'isValidDencunEpoch',
target: object.constructor,
propertyName: propertyName,
validator: {
validate(value: Epoch, args: ValidationArguments) {
switch ((args.object as any).ETH_NETWORK) {
case Network.Mainnet:
return value === DencunForkEpoch.Mainnet;
case Network.Holesky:
return value === DencunForkEpoch.Holesky;
case Network.Goerli:
return value === DencunForkEpoch.Goerli;
default:
return true;
}
},
},
});
};
}

function transformDencunEpoch({ value, obj }) {
if (value == null) {
value = '';
}

value = value.trim();

if (value === '') {
const chainId = parseInt(obj.ETH_NETWORK, 10);

switch (chainId) {
case Network.Mainnet:
return DencunForkEpoch.Mainnet;
case Network.Holesky:
return DencunForkEpoch.Holesky;
case Network.Goerli:
return DencunForkEpoch.Goerli;
default:
return null;
}
}

return parseInt(value, 10);
}
9 changes: 0 additions & 9 deletions src/common/config/interfaces/environment.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,3 @@ export enum LogFormat {
json = 'json',
simple = 'simple',
}

export enum DencunForkEpoch {
/**
* @todo This should be corrected once the particular epoch of the Dencun hard fork on Mainnet is known.
*/
Mainnet = 300000,
Goerli = 231680,
Holesky = 29696,
}

0 comments on commit 334eb16

Please sign in to comment.