Skip to content

Commit

Permalink
fix: simple refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Amuhar committed Dec 19, 2023
1 parent 409f7ab commit 8758649
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 51 deletions.
2 changes: 1 addition & 1 deletion src/guardian/guardian.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ describe('GuardianService', () => {

it('should exit if the previous call is not completed', async () => {
jest
.spyOn(stakingRouterService, 'getVettedAndUnusedKeys')
.spyOn(stakingRouterService, 'getStakingModulesData')
.mockImplementation(async () => vettedKeysResponse);

const getBlockGuardServiceMock = jest
Expand Down
9 changes: 4 additions & 5 deletions src/guardian/guardian.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export class GuardianService implements OnModuleInit {

try {
const { blockHash, blockNumber, stakingModulesData } =
await this.stakingRouterService.getVettedAndUnusedKeys();
await this.stakingRouterService.getStakingModulesData();

await this.repositoryService.initCachedContracts({ blockHash });

Expand All @@ -118,10 +118,10 @@ export class GuardianService implements OnModuleInit {
return;
}

const stakingModulesNumber = stakingModulesData.length;
const stakingModulesCount = stakingModulesData.length;

this.logger.log('Staking modules loaded', {
modulesCount: stakingModulesNumber,
modulesCount: stakingModulesCount,
});

await this.depositService.handleNewBlock(blockNumber);
Expand All @@ -137,9 +137,8 @@ export class GuardianService implements OnModuleInit {
blockHash: blockData.blockHash,
});

// TODO: check only if one of nonce changed
const modulesIdWithDuplicateKeys: number[] =
this.stakingModuleGuardService.checkVettedKeysDuplicates(
this.stakingModuleGuardService.getModulesIdsWithDuplicatedVettedUnusedKeys(
stakingModulesData,
blockData,
);
Expand Down
26 changes: 9 additions & 17 deletions src/guardian/staking-module-guard/staking-module-guard.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class StakingModuleGuardService {
/**
* @returns List of staking modules id with duplicates
*/
public checkVettedKeysDuplicates(
public getModulesIdsWithDuplicatedVettedUnusedKeys(
stakingModulesData: StakingModuleData[],
blockData: BlockData,
): number[] {
Expand Down Expand Up @@ -95,20 +95,10 @@ export class StakingModuleGuardService {
stakingModulesData: StakingModuleData[],
modulesIdWithDuplicateKeys: number[],
): StakingModuleData[] {
// exclude from stakingModulesData stakingModulesWithDuplicates
let stakingModulesWithoutDuplicates: StakingModuleData[] =
stakingModulesData;

if (modulesIdWithDuplicateKeys.length) {
// need to filter stakingModulesWithoutDuplicates

stakingModulesWithoutDuplicates = stakingModulesWithoutDuplicates.filter(
({ stakingModuleId }) =>
!modulesIdWithDuplicateKeys.includes(stakingModuleId),
);
}

return stakingModulesWithoutDuplicates;
return stakingModulesData.filter(
({ stakingModuleId }) =>
!modulesIdWithDuplicateKeys.includes(stakingModuleId),
);
}

/**
Expand Down Expand Up @@ -273,12 +263,14 @@ export class StakingModuleGuardService {
* Filter out the used keys, and since used keys cannot be deleted,
* it is sufficient to check if the blockNumber in the new result is greater than the current blockNumber.
*/
// getUsedLidoKeysForUnused ?
private async getDuplicatedLidoUsedKeys(
keys: string[],
prevBlockNumber: number,
): Promise<RegistryKey[]> {
const { data, meta } =
await this.stakingRouterService.getKeysWithDuplicates(keys);
const { data, meta } = await this.stakingRouterService.findKeysEntires(
keys,
);

if (meta.elBlockSnapshot.blockNumber < prevBlockNumber) {
this.logger.error(
Expand Down
44 changes: 24 additions & 20 deletions src/guardian/staking-module-guard/staking-module-guard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -580,10 +580,11 @@ describe('StakingModuleGuardService', () => {
const blockData = { blockHash: 'some_hash' } as any;

it('should found duplicated keys across two module', () => {
const result = stakingModuleGuardService.checkVettedKeysDuplicates(
vettedKeysDuplicatesAcrossModules,
blockData,
);
const result =
stakingModuleGuardService.getModulesIdsWithDuplicatedVettedUnusedKeys(
vettedKeysDuplicatesAcrossModules,
blockData,
);

const addressesOfModulesWithDuplicateKeys = [100, 102];

Expand All @@ -596,10 +597,11 @@ describe('StakingModuleGuardService', () => {
});

it('should found duplicated keys across one module', () => {
const result = stakingModuleGuardService.checkVettedKeysDuplicates(
vettedKeysDuplicatesAcrossOneModule,
blockData,
);
const result =
stakingModuleGuardService.getModulesIdsWithDuplicatedVettedUnusedKeys(
vettedKeysDuplicatesAcrossOneModule,
blockData,
);

const addressesOfModulesWithDuplicateKeys = [100];
expect(result).toEqual(
Expand All @@ -609,10 +611,11 @@ describe('StakingModuleGuardService', () => {
});

it('should found duplicated keys across one module and few', () => {
const result = stakingModuleGuardService.checkVettedKeysDuplicates(
vettedKeysDuplicatesAcrossOneModuleAndFew,
blockData,
);
const result =
stakingModuleGuardService.getModulesIdsWithDuplicatedVettedUnusedKeys(
vettedKeysDuplicatesAcrossOneModuleAndFew,
blockData,
);

const addressesOfModulesWithDuplicateKeys = [100, 102];
expect(result).toEqual(
Expand All @@ -622,10 +625,11 @@ describe('StakingModuleGuardService', () => {
});

it('should return empty list if duplicated keys were not found', () => {
const result = stakingModuleGuardService.checkVettedKeysDuplicates(
vettedKeysWithoutDuplicates,
blockData,
);
const result =
stakingModuleGuardService.getModulesIdsWithDuplicatedVettedUnusedKeys(
vettedKeysWithoutDuplicates,
blockData,
);

const addressesOfModulesWithDuplicateKeys = [];

Expand All @@ -643,7 +647,7 @@ describe('StakingModuleGuardService', () => {
// function that return list from kapi that match keys in parameter
const mockSendMessageFromGuardian = jest.spyOn(
stakingRouterService,
'getKeysWithDuplicates',
'findKeysEntires',
);

const result =
Expand Down Expand Up @@ -671,7 +675,7 @@ describe('StakingModuleGuardService', () => {
];
// function that return list from kapi that match keys in parameter
const mockSendMessageFromGuardian = jest
.spyOn(stakingRouterService, 'getKeysWithDuplicates')
.spyOn(stakingRouterService, 'findKeysEntires')
.mockImplementation(async () => ({
data: [
{
Expand Down Expand Up @@ -769,7 +773,7 @@ describe('StakingModuleGuardService', () => {
];
// function that return list from kapi that match keys in parameter
const mockSendMessageFromGuardian = jest
.spyOn(stakingRouterService, 'getKeysWithDuplicates')
.spyOn(stakingRouterService, 'findKeysEntires')
.mockImplementation(async () => ({
data: [
{
Expand Down Expand Up @@ -831,7 +835,7 @@ describe('StakingModuleGuardService', () => {
];
// function that return list from kapi that match keys in parameter
const mockSendMessageFromGuardian = jest
.spyOn(stakingRouterService, 'getKeysWithDuplicates')
.spyOn(stakingRouterService, 'findKeysEntires')
.mockImplementation(async () => ({
data: [
{
Expand Down
2 changes: 1 addition & 1 deletion src/keys-api/keys-api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export class KeysApiService {
* @param The /v1/keys/find KAPI endpoint returns a key along with its duplicates
* @returns
*/
public async getKeysWithDuplicates(pubkeys: string[]) {
public async findKeysEntires(pubkeys: string[]) {
const result = await this.fetch<KeyListResponse>(`/v1/keys/find`, {
method: 'POST',
headers: {
Expand Down
14 changes: 9 additions & 5 deletions src/staking-router/staking-router.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ export class StakingRouterService {
/**
* Return staking module data and block information
*/
public async getVettedAndUnusedKeys() {
public async getStakingModulesData(): Promise<{
stakingModulesData: StakingModuleData[];
blockHash: string;
blockNumber: number;
}> {
// TODO: add cache by modules nonce
const { operatorsByModules, unusedKeys, blockHash, blockNumber } =
await this.getOperatorsAndKeysFromKAPI();
await this.getOperatorsAndUnusedKeysFromKAPI();
// all staking modules list
const stakingModulesData: StakingModuleData[] = operatorsByModules.data.map(
({ operators, module: stakingModule }) => {
Expand All @@ -49,7 +53,7 @@ export class StakingRouterService {
/**
* Request grouped by modules operators and all staking modules keys with meta from KAPI
*/
private async getOperatorsAndKeysFromKAPI() {
private async getOperatorsAndUnusedKeysFromKAPI() {
const operatorsByModules =
await this.keysApiService.getOperatorListWithModule();
const { blockHash: operatorsBlockHash, blockNumber: operatorsBlockNumber } =
Expand Down Expand Up @@ -147,8 +151,8 @@ export class StakingRouterService {
return unusedKeys.slice(0, numberOfVettedUnusedKeys);
}

public async getKeysWithDuplicates(pubkeys: string[]) {
return await this.keysApiService.getKeysWithDuplicates(pubkeys);
public async findKeysEntires(pubkeys: string[]) {
return await this.keysApiService.findKeysEntires(pubkeys);
}

public async getStakingModules() {
Expand Down
2 changes: 1 addition & 1 deletion src/staking-router/staking-router.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('StakingRouter', () => {
keysAllStakingModules,
);

const result = await stakingRouterService.getVettedAndUnusedKeys();
const result = await stakingRouterService.getStakingModulesData();

// Assertions
expect(result).toEqual({
Expand Down
2 changes: 1 addition & 1 deletion test/helpers/mockKeysApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export const mockKeysApi = (
}));

jest
.spyOn(keysApiService, 'getKeysWithDuplicates')
.spyOn(keysApiService, 'findKeysEntires')
.mockImplementation(async () => ({
data: mockedKeys,
meta: {
Expand Down

0 comments on commit 8758649

Please sign in to comment.