Skip to content

Commit

Permalink
fix: moved getKeys part from sr service to controller service
Browse files Browse the repository at this point in the history
  • Loading branch information
Amuhar committed Aug 6, 2023
1 parent 1750e5e commit 16911c8
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 66 deletions.
52 changes: 44 additions & 8 deletions src/http/keys/keys.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,37 @@ import { LOGGER_PROVIDER } from '@lido-nestjs/logger';
import { KeyListResponse, KeyWithModuleAddress } from './entities';
import { StakingRouterService } from 'staking-router-modules/staking-router.service';
import { ELBlockSnapshot, KeyQuery } from 'http/common/entities';
import { KeyField } from 'staking-router-modules/interfaces/filters';
import { IsolationLevel } from '@mikro-orm/core';
import { EntityManager } from '@mikro-orm/knex';

@Injectable()
export class KeysService {
constructor(
@Inject(LOGGER_PROVIDER) protected readonly logger: LoggerService,
protected stakingRouterService: StakingRouterService,
protected readonly entityManager: EntityManager,
) {}

async get(
filters: KeyQuery,
): Promise<{ keysGenerators: AsyncGenerator<KeyWithModuleAddress>[]; meta: { elBlockSnapshot: ELBlockSnapshot } }> {
const { keysGenerators, elBlockSnapshot } = await this.stakingRouterService.getKeys(filters);
const { stakingModules, elBlockSnapshot } = await this.stakingRouterService.getStakingModulesAndMeta();
const keysGenerators: AsyncGenerator<KeyWithModuleAddress>[] = [];

for (const module of stakingModules) {
const moduleInstance = this.stakingRouterService.getStakingRouterModuleImpl(module.type);

const fields: KeyField[] = ['key', 'depositSignature', 'operatorIndex', 'used', 'moduleAddress'];
// TODO: maybe get rid of this type KeyWithModuleAddress
const keysGenerator: AsyncGenerator<KeyWithModuleAddress> = await moduleInstance.getKeysStream(
module.stakingModuleAddress,
filters,
fields,
);

keysGenerators.push(keysGenerator);
}

return {
keysGenerators,
Expand All @@ -23,15 +42,32 @@ export class KeysService {
}

async getByPubkey(pubkey: string): Promise<KeyListResponse> {
const { keys, elBlockSnapshot } = await this.stakingRouterService.getKeysByPubkeys([pubkey]);
return {
data: keys,
meta: { elBlockSnapshot },
};
return this.getByPubkeys([pubkey]);
}

async getByPubkeys(pubkeys: string[]): Promise<KeyListResponse> {
const { keys, elBlockSnapshot } = await this.stakingRouterService.getKeysByPubkeys(pubkeys);
async getByPubkeys(pubKeys: string[]): Promise<KeyListResponse> {
const { keys, elBlockSnapshot } = await this.entityManager.transactional(
async () => {
const { stakingModules, elBlockSnapshot } = await this.stakingRouterService.getStakingModulesAndMeta();
const collectedKeys: KeyWithModuleAddress[][] = [];

for (const module of stakingModules) {
const moduleInstance = this.stakingRouterService.getStakingRouterModuleImpl(module.type);
const fields: KeyField[] = ['key', 'depositSignature', 'operatorIndex', 'used', 'moduleAddress'];
const keys: KeyWithModuleAddress[] = await moduleInstance.getKeysByPubKeys(
module.stakingModuleAddress,
pubKeys,
fields,
);

collectedKeys.push(keys);
}

return { keys: collectedKeys.flat(), elBlockSnapshot };
},
{ isolationLevel: IsolationLevel.REPEATABLE_READ },
);

return {
data: keys,
meta: { elBlockSnapshot },
Expand Down
2 changes: 1 addition & 1 deletion src/staking-router-modules/staking-module-impl-config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CuratedModuleService } from './curated-module.service';
import { STAKING_MODULE_TYPE } from './constants';

type StakingModuleImpl = typeof CuratedModuleService;
export type StakingModuleImpl = typeof CuratedModuleService;

export const config: Record<STAKING_MODULE_TYPE, StakingModuleImpl> = {
[STAKING_MODULE_TYPE.CURATED_ONCHAIN_V1_TYPE]: CuratedModuleService,
Expand Down
121 changes: 64 additions & 57 deletions src/staking-router-modules/staking-router.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { ValidatorsService } from 'validators';

import { PrometheusService } from 'common/prometheus';
import { KeyField, KeysFilter } from './interfaces/filters';
import { STAKING_MODULE_TYPE } from './constants';

@Injectable()
export class StakingRouterService {
Expand Down Expand Up @@ -64,6 +65,12 @@ export class StakingRouterService {
return await this.srModulesStorage.findOneById(Number(moduleId));
}

public getStakingRouterModuleImpl(moduleType: string): StakingModuleInterface {
const impl = config[moduleType];
const moduleInstance = this.moduleRef.get<StakingModuleInterface>(impl);
return moduleInstance;
}

/**
* Update keys of staking modules
* @returns Number, hash and timestamp of execution layer block
Expand Down Expand Up @@ -214,69 +221,69 @@ export class StakingRouterService {
}

// TODO: check why we don't have a type conflict here
/**
* Return generators for keys of all modules with fields {key, depositSignature, operatorIndex, used, moduleAddress} and execution layer meta
* @param filters used, operatorIndex
* @returns List of generators for keys and execution layer meta
*/
public async getKeys(
filters: KeysFilter,
): Promise<{ keysGenerators: AsyncGenerator<KeyWithModuleAddress>[]; elBlockSnapshot: ELBlockSnapshot }> {
const { stakingModules, elBlockSnapshot } = await this.getStakingModulesAndMeta();
const keysGenerators: AsyncGenerator<KeyWithModuleAddress>[] = [];

for (const module of stakingModules) {
const impl = config[module.type];
const moduleInstance = this.moduleRef.get<StakingModuleInterface>(impl);

const fields: KeyField[] = ['key', 'depositSignature', 'operatorIndex', 'used', 'moduleAddress'];
// TODO: maybe get rid of this type KeyWithModuleAddress
const keysGenerator: AsyncGenerator<KeyWithModuleAddress> = await moduleInstance.getKeysStream(
module.stakingModuleAddress,
filters,
fields,
);

keysGenerators.push(keysGenerator);
}

return { keysGenerators, elBlockSnapshot };
}
// /**
// * Return generators for keys of all modules with fields {key, depositSignature, operatorIndex, used, moduleAddress} and execution layer meta
// * @param filters used, operatorIndex
// * @returns List of generators for keys and execution layer meta
// */
// public async getKeys(
// filters: KeysFilter,
// ): Promise<{ keysGenerators: AsyncGenerator<KeyWithModuleAddress>[]; elBlockSnapshot: ELBlockSnapshot }> {
// const { stakingModules, elBlockSnapshot } = await this.getStakingModulesAndMeta();
// const keysGenerators: AsyncGenerator<KeyWithModuleAddress>[] = [];

// for (const module of stakingModules) {
// const impl = config[module.type];
// const moduleInstance = this.moduleRef.get<StakingModuleInterface>(impl);

// const fields: KeyField[] = ['key', 'depositSignature', 'operatorIndex', 'used', 'moduleAddress'];
// // TODO: maybe get rid of this type KeyWithModuleAddress
// const keysGenerator: AsyncGenerator<KeyWithModuleAddress> = await moduleInstance.getKeysStream(
// module.stakingModuleAddress,
// filters,
// fields,
// );

// keysGenerators.push(keysGenerator);
// }

// return { keysGenerators, elBlockSnapshot };
// }

/**
* Return keys from pubKeys list with fields {key, depositSignature, operatorIndex, used, moduleAddress}
* @param pubKeys list of public keys for searching
* @returns list of keys with fields {key, depositSignature, operatorIndex, used, moduleAddress} and execution layer meta
*/
public async getKeysByPubkeys(
pubKeys: string[],
): Promise<{ keys: KeyWithModuleAddress[]; elBlockSnapshot: ELBlockSnapshot }> {
const { keys, elBlockSnapshot } = await this.entityManager.transactional(
async () => {
const { stakingModules, elBlockSnapshot } = await this.getStakingModulesAndMeta();
const collectedKeys: KeyWithModuleAddress[][] = [];

for (const module of stakingModules) {
const impl = config[module.type];
const moduleInstance = this.moduleRef.get<StakingModuleInterface>(impl);

const fields: KeyField[] = ['key', 'depositSignature', 'operatorIndex', 'used', 'moduleAddress'];
const keys: KeyWithModuleAddress[] = await moduleInstance.getKeysByPubKeys(
module.stakingModuleAddress,
pubKeys,
fields,
);

collectedKeys.push(keys);
}

return { keys: collectedKeys.flat(), elBlockSnapshot };
},
{ isolationLevel: IsolationLevel.REPEATABLE_READ },
);

return { keys, elBlockSnapshot };
}
// public async getKeysByPubkeys(
// pubKeys: string[],
// ): Promise<{ keys: KeyWithModuleAddress[]; elBlockSnapshot: ELBlockSnapshot }> {
// const { keys, elBlockSnapshot } = await this.entityManager.transactional(
// async () => {
// const { stakingModules, elBlockSnapshot } = await this.getStakingModulesAndMeta();
// const collectedKeys: KeyWithModuleAddress[][] = [];

// for (const module of stakingModules) {
// const impl = config[module.type];
// const moduleInstance = this.moduleRef.get<StakingModuleInterface>(impl);

// const fields: KeyField[] = ['key', 'depositSignature', 'operatorIndex', 'used', 'moduleAddress'];
// const keys: KeyWithModuleAddress[] = await moduleInstance.getKeysByPubKeys(
// module.stakingModuleAddress,
// pubKeys,
// fields,
// );

// collectedKeys.push(keys);
// }

// return { keys: collectedKeys.flat(), elBlockSnapshot };
// },
// { isolationLevel: IsolationLevel.REPEATABLE_READ },
// );

// return { keys, elBlockSnapshot };
// }

/**
* Return generators for keys of all modules with fields {key, depositSignature, operatorIndex, used } and execution layer meta
Expand Down

0 comments on commit 16911c8

Please sign in to comment.