Skip to content

Commit

Permalink
fix: error updatimg operators and keys
Browse files Browse the repository at this point in the history
  • Loading branch information
Amuhar committed Jul 27, 2023
1 parent 41753ae commit 5472cb6
Show file tree
Hide file tree
Showing 7 changed files with 241 additions and 249 deletions.
4 changes: 2 additions & 2 deletions src/common/registry/main/abstract-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export abstract class AbstractRegistryService {
await entityManager
.createQueryBuilder(RegistryKey)
.insert(keysChunk)
.onConflict(['index', 'operator_index'])
.onConflict(['index', 'operator_index', 'module_address'])
.merge()
.execute();
}),
Expand Down Expand Up @@ -184,7 +184,7 @@ export abstract class AbstractRegistryService {
await entityManager
.createQueryBuilder(RegistryOperator)
.insert(operatorsChunk)
.onConflict('index')
.onConflict(['index', 'module_address'])
.merge()
.execute();
}),
Expand Down
2 changes: 1 addition & 1 deletion src/http/sr-modules-validators/entities/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ApiProperty } from '@nestjs/swagger';
import { IsInt, IsOptional, Min } from 'class-validator';
import { Type } from 'class-transformer';

export class Query {
export class ValidatorsQuery {
@ApiProperty({
required: false,
description: 'Number of validators to exit. If validators number less than amount, return all validators.',
Expand Down
142 changes: 71 additions & 71 deletions src/http/sr-modules-validators/sr-modules-validators.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
} from '@nestjs/swagger';
import { SRModulesValidatorsService } from './sr-modules-validators.service';
import { ModuleId } from 'http/common/entities/';
import { Query as ValidatorsQuery } from './entities/query';
import { ValidatorsQuery } from './entities/query';
import { ExitPresignMessageListResponse, ExitValidatorListResponse } from './entities';
import { OperatorIdParam } from 'http/common/entities/operator-id-param';
import { TooEarlyResponse } from 'http/common/entities/http-exceptions';
Expand All @@ -28,75 +28,75 @@ import { TooEarlyResponse } from 'http/common/entities/http-exceptions';
export class SRModulesValidatorsController {
constructor(protected readonly validatorsService: SRModulesValidatorsService) {}

// @Version('1')
// @Get(':module_id/validators/validator-exits-to-prepare/:operator_id')
// @ApiOperation({ summary: 'Get list of N oldest lido validators' })
// @ApiResponse({
// status: 200,
// description: 'N oldest lido validators for operator.',
// type: ExitValidatorListResponse,
// })
// @ApiResponse({
// status: 425,
// description: "Meta is null, maybe data hasn't been written in db yet",
// type: TooEarlyResponse,
// })
// @ApiNotFoundResponse({
// status: HttpStatus.NOT_FOUND,
// description: 'Provided module or operator are not supported',
// type: NotFoundException,
// })
// @ApiInternalServerErrorResponse({
// status: HttpStatus.INTERNAL_SERVER_ERROR,
// description: 'Disabled endpoint/ Last Execution Layer block number in our database older than last Consensus Layer',
// type: InternalServerErrorException,
// })
// @ApiParam({
// name: 'module_id',
// example: '0x55032650b14df07b85bF18A3a3eC8E0Af2e028d5',
// description: 'Staking router module_id or contract address.',
// })
// getOldestValidators(
// @Param('module_id') moduleId: ModuleId,
// @Param() operator: OperatorIdParam,
// @Query() query: ValidatorsQuery,
// ) {
// return this.validatorsService.getOldestLidoValidators(moduleId, operator.operator_id, query);
// }
@Version('1')
@Get(':module_id/validators/validator-exits-to-prepare/:operator_id')
@ApiOperation({ summary: 'Get list of N oldest lido validators' })
@ApiResponse({
status: 200,
description: 'N oldest lido validators for operator.',
type: ExitValidatorListResponse,
})
@ApiResponse({
status: 425,
description: "Meta is null, maybe data hasn't been written in db yet",
type: TooEarlyResponse,
})
@ApiNotFoundResponse({
status: HttpStatus.NOT_FOUND,
description: 'Provided module or operator are not supported',
type: NotFoundException,
})
@ApiInternalServerErrorResponse({
status: HttpStatus.INTERNAL_SERVER_ERROR,
description: 'Disabled endpoint/ Last Execution Layer block number in our database older than last Consensus Layer',
type: InternalServerErrorException,
})
@ApiParam({
name: 'module_id',
example: '0x55032650b14df07b85bF18A3a3eC8E0Af2e028d5',
description: 'Staking router module_id or contract address.',
})
getOldestValidators(
@Param('module_id') moduleId: ModuleId,
@Param() operator: OperatorIdParam,
@Query() query: ValidatorsQuery,
) {
return this.validatorsService.getOldestLidoValidators(moduleId, operator.operator_id, query);
}

// @Version('1')
// @Get(':module_id/validators/generate-unsigned-exit-messages/:operator_id')
// @ApiOperation({ summary: 'Get list of exit messages for N oldest lido validators' })
// @ApiResponse({
// status: 200,
// description: 'Exit messages for N oldest lido validators of operator',
// type: ExitPresignMessageListResponse,
// })
// @ApiResponse({
// status: 425,
// description: "Meta is null, maybe data hasn't been written in db yet",
// type: TooEarlyResponse,
// })
// @ApiNotFoundResponse({
// status: HttpStatus.NOT_FOUND,
// description: 'Provided module or operator are not supported',
// type: NotFoundException,
// })
// @ApiInternalServerErrorResponse({
// status: HttpStatus.INTERNAL_SERVER_ERROR,
// description: 'Disabled endpoint/ Last Execution Layer block number in our database older than last Consensus Layer',
// type: InternalServerErrorException,
// })
// @ApiParam({
// name: 'module_id',
// example: '0x55032650b14df07b85bF18A3a3eC8E0Af2e028d5',
// description: 'Staking router module_id or contract address.',
// })
// getMessagesForOldestValidators(
// @Param('module_id') moduleId: ModuleId,
// @Param() operator: OperatorIdParam,
// @Query() query: ValidatorsQuery,
// ) {
// return this.validatorsService.getVoluntaryExitMessages(moduleId, operator.operator_id, query);
// }
@Version('1')
@Get(':module_id/validators/generate-unsigned-exit-messages/:operator_id')
@ApiOperation({ summary: 'Get list of exit messages for N oldest lido validators' })
@ApiResponse({
status: 200,
description: 'Exit messages for N oldest lido validators of operator',
type: ExitPresignMessageListResponse,
})
@ApiResponse({
status: 425,
description: "Meta is null, maybe data hasn't been written in db yet",
type: TooEarlyResponse,
})
@ApiNotFoundResponse({
status: HttpStatus.NOT_FOUND,
description: 'Provided module or operator are not supported',
type: NotFoundException,
})
@ApiInternalServerErrorResponse({
status: HttpStatus.INTERNAL_SERVER_ERROR,
description: 'Disabled endpoint/ Last Execution Layer block number in our database older than last Consensus Layer',
type: InternalServerErrorException,
})
@ApiParam({
name: 'module_id',
example: '0x55032650b14df07b85bF18A3a3eC8E0Af2e028d5',
description: 'Staking router module_id or contract address.',
})
getMessagesForOldestValidators(
@Param('module_id') moduleId: ModuleId,
@Param() operator: OperatorIdParam,
@Query() query: ValidatorsQuery,
) {
return this.validatorsService.getVoluntaryExitMessages(moduleId, operator.operator_id, query);
}
}
202 changes: 64 additions & 138 deletions src/http/sr-modules-validators/sr-modules-validators.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
ExitValidator,
ExitPresignMessageListResponse,
ExitPresignMessage,
Query as ValidatorsQuery,
ValidatorsQuery,
} from './entities';
import { CLBlockSnapshot, ModuleId } from 'http/common/entities/';
import { LOGGER_PROVIDER } from '@lido-nestjs/logger';
Expand All @@ -28,141 +28,67 @@ export class SRModulesValidatorsService {
protected stakingRouterService: StakingRouterService,
) {}

// async getOldestLidoValidators(
// moduleId: ModuleId,
// operatorId: number,
// filters: ValidatorsQuery,
// ): Promise<ExitValidatorListResponse> {
// if (this.disabledRegistry()) {
// this.logger.warn('ValidatorsRegistry is disabled in API');
// throw new InternalServerErrorException(VALIDATORS_REGISTRY_DISABLED_ERROR);
// }

// const stakingModule = await this.stakingRouterService.getStakingModule(moduleId);

// if (!stakingModule) {
// throw new NotFoundException(`Module with moduleId ${moduleId} is not supported`);
// }

// // We suppose if module in list, Keys API knows how to work with it
// // it is also important to have consistent module info and meta

// if (stakingModule.type === STAKING_MODULE_TYPE.CURATED_ONCHAIN_V1_TYPE) {
// const { validators, meta: clMeta } = await this.getOperatorOldestValidators(operatorId, filters);
// const data = this.createExitValidatorList(validators);
// const clBlockSnapshot = new CLBlockSnapshot(clMeta);

// return {
// data,
// meta: {
// clBlockSnapshot: clBlockSnapshot,
// },
// };
// }

// throw new NotFoundException(`Modules with other types are not supported`);
// }

// async getVoluntaryExitMessages(
// moduleId: ModuleId,
// operatorId: number,
// filters: ValidatorsQuery,
// ): Promise<ExitPresignMessageListResponse> {
// if (this.disabledRegistry()) {
// this.logger.warn('ValidatorsRegistry is disabled in API');
// throw new InternalServerErrorException(VALIDATORS_REGISTRY_DISABLED_ERROR);
// }

// const stakingModule = await this.stakingRouterService.getStakingModule(moduleId);

// if (!stakingModule) {
// throw new NotFoundException(`Module with moduleId ${moduleId} is not supported`);
// }

// // We suppose if module in list, Keys API knows how to work with it
// // it is also important to have consistent module info and meta

// if (stakingModule.type === STAKING_MODULE_TYPE.CURATED_ONCHAIN_V1_TYPE) {
// const { validators, meta: clMeta } = await this.getOperatorOldestValidators(operatorId, filters);
// const data = this.createExitPresignMessageList(validators, clMeta);
// const clBlockSnapshot = new CLBlockSnapshot(clMeta);

// return {
// data,
// meta: {
// clBlockSnapshot: clBlockSnapshot,
// },
// };
// }

// throw new NotFoundException(`Modules with other types are not supported`);
// }

// private async getOperatorOldestValidators(
// operatorId: number,
// filters: ValidatorsQuery,
// ): Promise<{ validators: Validator[]; meta: ConsensusMeta }> {
// // get used keys for operator
// const { keys, meta: elMeta } = await this.curatedService.getKeysWithMeta({
// used: true,
// operatorIndex: operatorId,
// });

// // check if elMeta is not null
// // if it is null, it means keys db is empty and Updating Keys Job is not finished yet
// if (!elMeta) {
// this.logger.warn(`EL meta is empty, maybe first Updating Keys Job is not finished yet.`);
// throw httpExceptionTooEarlyResp();
// }

// const pubkeys = keys.map((pubkey) => pubkey.key);
// const percent =
// filters?.max_amount == undefined && filters?.percent == undefined ? DEFAULT_EXIT_PERCENT : filters?.percent;

// const result = await this.validatorsService.getOldestValidators({
// pubkeys,
// statuses: VALIDATORS_STATUSES_FOR_EXIT,
// max_amount: filters?.max_amount,
// percent: percent,
// });

// if (!result) {
// // if result of this method is null it means Validators Registry is disabled
// throw new InternalServerErrorException(VALIDATORS_REGISTRY_DISABLED_ERROR);
// }

// const { validators, meta: clMeta } = result;

// // check if clMeta is not null
// // if it is null, it means keys db is empty and Updating Validators Job is not finished yet
// if (!clMeta) {
// this.logger.warn(`CL meta is empty, maybe first Updating Validators Job is not finished yet.`);
// throw httpExceptionTooEarlyResp();
// }

// // We need EL meta always be actual
// if (elMeta.blockNumber < clMeta.blockNumber) {
// this.logger.warn('Last Execution Layer block number in our database older than last Consensus Layer');
// // add metric or alert on breaking el > cl condition
// // TODO: what answer will be better here?
// // TODO: describe in doc
// throw new InternalServerErrorException(
// 'Last Execution Layer block number in our database older than last Consensus Layer',
// );
// }

// return { validators, meta: clMeta };
// }

// private createExitValidatorList(validators: Validator[]): ExitValidator[] {
// return validators.map((v) => ({ validatorIndex: v.index, key: v.pubkey }));
// }

// private createExitPresignMessageList(validators: Validator[], clMeta: ConsensusMeta): ExitPresignMessage[] {
// return validators.map((v) => ({ validator_index: String(v.index), epoch: String(clMeta.epoch) }));
// }

// private disabledRegistry() {
// return !this.configService.get('VALIDATOR_REGISTRY_ENABLE');
// }
async getOldestLidoValidators(
moduleId: ModuleId,
operatorId: number,
filters: ValidatorsQuery,
): Promise<ExitValidatorListResponse> {
if (this.disabledRegistry()) {
this.logger.warn('ValidatorsRegistry is disabled in API');
throw new InternalServerErrorException(VALIDATORS_REGISTRY_DISABLED_ERROR);
}

const { validators, meta } = await this.stakingRouterService.getOperatorOldestValidators(
moduleId,
operatorId,
filters,
);
const data = this.createExitValidatorList(validators);
const clBlockSnapshot = new CLBlockSnapshot(meta);

return {
data,
meta: {
clBlockSnapshot: clBlockSnapshot,
},
};
}

async getVoluntaryExitMessages(
moduleId: ModuleId,
operatorId: number,
filters: ValidatorsQuery,
): Promise<ExitPresignMessageListResponse> {
if (this.disabledRegistry()) {
this.logger.warn('ValidatorsRegistry is disabled in API');
throw new InternalServerErrorException(VALIDATORS_REGISTRY_DISABLED_ERROR);
}

const { validators, meta } = await this.stakingRouterService.getOperatorOldestValidators(
moduleId,
operatorId,
filters,
);
const data = this.createExitPresignMessageList(validators, meta);
const clBlockSnapshot = new CLBlockSnapshot(meta);

return {
data,
meta: {
clBlockSnapshot: clBlockSnapshot,
},
};
}

private createExitValidatorList(validators: Validator[]): ExitValidator[] {
return validators.map((v) => ({ validatorIndex: v.index, key: v.pubkey }));
}

private createExitPresignMessageList(validators: Validator[], clMeta: ConsensusMeta): ExitPresignMessage[] {
return validators.map((v) => ({ validator_index: String(v.index), epoch: String(clMeta.epoch) }));
}

private disabledRegistry() {
return !this.configService.get('VALIDATOR_REGISTRY_ENABLE');
}
}
Loading

0 comments on commit 5472cb6

Please sign in to comment.