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

feat: compare chainId and compare app version #171

Merged
merged 1 commit into from
Jan 18, 2024
Merged
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
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@chainsafe/ssz": "^0.9.2",
"@ethersproject/providers": "^5.4.5",
"@lido-nestjs/fetch": "^1.3.1",
"@lido-nestjs/key-validation": "^7.4.0",
"@lido-nestjs/middleware": "^1.1.1",
"@lido-sdk/constants": "^3.2.1",
"@nestjs/common": "^8.0.0",
Expand All @@ -41,24 +42,24 @@
"@nestjs/platform-express": "^8.1.1",
"@nestjs/schedule": "^2.1.0",
"@nestjs/terminus": "^8.0.1",
"@lido-nestjs/key-validation": "^7.4.0",
"@willsoto/nestjs-prometheus": "^4.0.1",
"app-root-path": "^3.0.0",
"cache-manager": "^3.6.3",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.0",
"compare-versions": "^6.1.0",
"ethers": "^5.4.7",
"glob": "^7.1.2",
"kafkajs": "^1.15.0",
"lru-cache": "^9.1.1",
"nest-winston": "^1.6.1",
"node-abort-controller": "^3.0.1",
"prom-client": "^14.0.0",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"rxjs": "^7.2.0",
"winston": "^3.3.3",
"ws": "^8.10.0",
"lru-cache": "^9.1.1"
"ws": "^8.10.0"
},
"devDependencies": {
"@nestjs/cli": "^8.2.5",
Expand Down
1 change: 1 addition & 0 deletions src/guardian/guardian.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ import { CronExpression } from '@nestjs/schedule';
export const GUARDIAN_DEPOSIT_RESIGNING_BLOCKS = 10;
export const GUARDIAN_DEPOSIT_JOB_NAME = 'guardian-deposit-job';
export const GUARDIAN_DEPOSIT_JOB_DURATION = CronExpression.EVERY_5_SECONDS;
export const MIN_KAPI_VERSION = '0.10.2';
2 changes: 2 additions & 0 deletions src/guardian/guardian.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { BlockGuardModule } from './block-guard/block-guard.module';
import { StakingModuleGuardModule } from './staking-module-guard';
import { GuardianMessageModule } from './guardian-message';
import { GuardianMetricsModule } from './guardian-metrics';
import { KeysApiModule } from 'keys-api/keys-api.module';

@Module({
imports: [
Expand All @@ -23,6 +24,7 @@ import { GuardianMetricsModule } from './guardian-metrics';
StakingModuleGuardModule,
GuardianMessageModule,
GuardianMetricsModule,
KeysApiModule,
],
providers: [GuardianService],
exports: [GuardianService],
Expand Down
30 changes: 30 additions & 0 deletions src/guardian/guardian.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
LoggerService,
OnModuleInit,
} from '@nestjs/common';
import { compare } from 'compare-versions';
import { WINSTON_MODULE_NEST_PROVIDER } from 'nest-winston';
import { SchedulerRegistry } from '@nestjs/schedule';
import { CronJob } from 'cron';
Expand All @@ -22,6 +23,9 @@ import { StakingModuleGuardService } from './staking-module-guard';
import { GuardianMessageService } from './guardian-message';
import { GuardianMetricsService } from './guardian-metrics';
import { StakingModuleData } from './interfaces';
import { ProviderService } from 'provider';
import { KeysApiService } from 'keys-api/keys-api.service';
import { MIN_KAPI_VERSION } from './guardian.constants';

@Injectable()
export class GuardianService implements OnModuleInit {
Expand All @@ -42,6 +46,9 @@ export class GuardianService implements OnModuleInit {
private stakingModuleGuardService: StakingModuleGuardService,
private guardianMessageService: GuardianMessageService,
private guardianMetricsService: GuardianMetricsService,

private providerService: ProviderService,
private keysApiService: KeysApiService,
) {}

public async onModuleInit(): Promise<void> {
Expand All @@ -57,6 +64,29 @@ export class GuardianService implements OnModuleInit {
this.securityService.initialize({ blockHash }),
]);

const chainId = await this.providerService.getChainId();
const keysApiStatus = await this.keysApiService.getKeysApiStatus();

if (chainId !== keysApiStatus.chainId) {
this.logger.warn('Wrong KAPI chainId', {
chainId,
keysApiChainId: keysApiStatus.chainId,
});
throw new Error(
'The ChainId in KeysAPI must match the ChainId in EL Node',
);
}

if (!compare(keysApiStatus.appVersion, MIN_KAPI_VERSION, '>=')) {
this.logger.warn('Wrong KAPI version', {
minKAPIVersion: MIN_KAPI_VERSION,
keysApiVersion: keysApiStatus.appVersion,
});
throw new Error(
`The KAPI version must be greater than or equal to ${MIN_KAPI_VERSION}`,
);
}

// The event cache is stored with an N block lag to avoid caching data from uncle blocks
// so we don't worry about blockHash here
await this.depositService.updateEventsCache();
Expand Down
6 changes: 6 additions & 0 deletions src/keys-api/interfaces/Status.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export type Status = {
/** @description App version */
appVersion: string;
/** @description Chain id */
chainId: number;
};
1 change: 1 addition & 0 deletions src/keys-api/interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export type { SRModule } from './SRModule';
export type { KeyListResponse } from './KeyListResponse';
export type { Status } from './Status';
12 changes: 11 additions & 1 deletion src/keys-api/keys-api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Injectable, LoggerService, Inject } from '@nestjs/common';
import { FetchService, RequestInit } from '@lido-nestjs/fetch';
import { AbortController } from 'node-abort-controller';
import { FETCH_REQUEST_TIMEOUT } from './keys-api.constants';
import { KeyListResponse } from './interfaces';
import { KeyListResponse, Status } from './interfaces';
import { WINSTON_MODULE_NEST_PROVIDER } from 'nest-winston';
import { Configuration } from 'common/config';
import { GroupedByModuleOperatorListResponse } from './interfaces/GroupedByModuleOperatorListResponse';
Expand Down Expand Up @@ -68,4 +68,14 @@ export class KeysApiService {
);
return result;
}

/**
*
* @param The /v1/status API endpoint returns chainId, appVersion, El and Cl meta
* @returns
*/
public async getKeysApiStatus(): Promise<Status> {
const result = await this.fetch<Status>(`/v1/status`);
return result;
}
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2909,6 +2909,11 @@ commander@^2.20.0:
resolved "https://registry.yarnpkg.com/commander/-/commander-2.20.3.tgz#fd485e84c03eb4881c20722ba48035e8531aeb33"
integrity sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==

compare-versions@^6.1.0:
version "6.1.0"
resolved "https://registry.yarnpkg.com/compare-versions/-/compare-versions-6.1.0.tgz#3f2131e3ae93577df111dba133e6db876ffe127a"
integrity sha512-LNZQXhqUvqUTotpZ00qLSaify3b4VFD588aRr8MKFw4CMUr98ytzCW5wDH5qx/DEY5kCDXcbcRuCqL0szEf2tg==

component-emitter@^1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/component-emitter/-/component-emitter-1.3.0.tgz#16e4070fba8ae29b679f2215853ee181ab2eabc0"
Expand Down