-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #185 from klayrHQ/development
Development
- Loading branch information
Showing
10 changed files
with
227 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
src/modules/indexer/block/post-block-commands/check-block-finality.command.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/modules/indexer/block/post-block-commands/check-validator-status.command.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs'; | ||
import { LokiLogger } from 'nestjs-loki-logger'; | ||
import { BLOCKS_TO_CHECK_BANNED_VALIDATOR } from 'src/config/constants'; | ||
import { NodeApi, NodeApiService } from 'src/modules/node-api/node-api.service'; | ||
import { AllValidators } from 'src/modules/node-api/types'; | ||
import { PrismaService } from 'src/modules/prisma/prisma.service'; | ||
import { ValidatorStatus } from '../../event/types'; | ||
|
||
export class CheckValidatorStatusCommand { | ||
constructor(public readonly blockHeight: number) {} | ||
} | ||
|
||
@CommandHandler(CheckValidatorStatusCommand) | ||
export class CheckValidatorStatusHandler implements ICommandHandler<CheckValidatorStatusCommand> { | ||
private readonly logger = new LokiLogger(CheckValidatorStatusHandler.name); | ||
|
||
constructor( | ||
private readonly prisma: PrismaService, | ||
private readonly nodeApi: NodeApiService, | ||
) {} | ||
|
||
async execute({ blockHeight }: CheckValidatorStatusCommand) { | ||
if (blockHeight % BLOCKS_TO_CHECK_BANNED_VALIDATOR !== 0) return; | ||
this.logger.debug('Checking validator status'); | ||
const validators = await this.getAllValidatorsFromNode(); | ||
if (!validators) return; | ||
|
||
await this.updateBannedValidators(validators); | ||
|
||
this.logger.debug('Validator banned status updated successfully'); | ||
} | ||
|
||
async getAllValidatorsFromNode(): Promise<AllValidators> { | ||
try { | ||
const validators = await this.nodeApi.invokeApi<AllValidators>( | ||
NodeApi.POS_GET_ALL_VALIDATORS, | ||
{}, | ||
); | ||
return validators; | ||
} catch (error) { | ||
this.logger.error('Error: Failed to fetch all validators', error); | ||
} | ||
} | ||
|
||
async updateBannedValidators({ validators }: AllValidators): Promise<void> { | ||
for (const validator of validators) { | ||
if (validator.isBanned) { | ||
await this.prisma.validator.update({ | ||
where: { address: validator.address }, | ||
data: { | ||
isBanned: true, | ||
status: ValidatorStatus.BANNED, | ||
totalStake: BigInt(validator.totalStake), | ||
selfStake: BigInt(validator.selfStake), | ||
lastGeneratedHeight: validator.lastGeneratedHeight, | ||
reportMisbehaviorHeights: JSON.stringify(validator.reportMisbehaviorHeights), | ||
consecutiveMissedBlocks: validator.consecutiveMissedBlocks, | ||
commission: validator.commission, | ||
lastCommissionIncreaseHeight: validator.lastCommissionIncreaseHeight, | ||
sharingCoefficients: JSON.stringify(validator.sharingCoefficients), | ||
punishmentPeriods: JSON.stringify(validator.punishmentPeriods), | ||
}, | ||
}); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { PrismaService } from 'src/modules/prisma/prisma.service'; | ||
import { NodeApi, NodeApiService } from 'src/modules/node-api/node-api.service'; | ||
import { LokiLogger } from 'nestjs-loki-logger'; | ||
import { CheckValidatorStatusHandler } from 'src/modules/indexer/block/post-block-commands/check-validator-status.command'; | ||
import { ValidatorStatus } from 'src/modules/pos/types'; | ||
import { AllValidators } from 'src/modules/node-api/types'; | ||
import { BLOCKS_TO_CHECK_BANNED_VALIDATOR } from 'src/config/constants'; | ||
|
||
describe('CheckValidatorStatusHandler', () => { | ||
let handler: CheckValidatorStatusHandler; | ||
let prismaService: PrismaService; | ||
let nodeApiService: NodeApiService; | ||
|
||
beforeEach(async () => { | ||
const mockValidators: AllValidators = { | ||
validators: [ | ||
{ | ||
name: 'przemer', | ||
totalStake: '103150000000000', | ||
selfStake: '9899000000000', | ||
lastGeneratedHeight: 397037, | ||
isBanned: true, | ||
reportMisbehaviorHeights: [], | ||
consecutiveMissedBlocks: 2209, | ||
commission: 9500, | ||
lastCommissionIncreaseHeight: 68231, | ||
sharingCoefficients: [ | ||
{ | ||
tokenID: '0200000000000000', | ||
coefficient: '20c157e32fecfda501718a0f', | ||
}, | ||
], | ||
address: 'klyhf3gtj3wecxaxy7csqb5k8kszugghpscvat5b4', | ||
punishmentPeriods: [], | ||
}, | ||
{ | ||
name: 'validator2', | ||
totalStake: '50000000000000', | ||
selfStake: '5000000000000', | ||
lastGeneratedHeight: 300000, | ||
isBanned: false, | ||
reportMisbehaviorHeights: [1, 2, 3], | ||
consecutiveMissedBlocks: 1000, | ||
commission: 5000, | ||
lastCommissionIncreaseHeight: 50000, | ||
sharingCoefficients: [ | ||
{ | ||
tokenID: '0100000000000000', | ||
coefficient: '10c157e32fecfda501718a0f', | ||
}, | ||
], | ||
address: 'klyhf3gtj3wecxaxy7csqb5k8kszugghpscvat5b5', | ||
punishmentPeriods: [], | ||
}, | ||
], | ||
}; | ||
|
||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
CheckValidatorStatusHandler, | ||
{ | ||
provide: PrismaService, | ||
useValue: { | ||
validator: { | ||
update: jest.fn(), | ||
}, | ||
}, | ||
}, | ||
{ | ||
provide: NodeApiService, | ||
useValue: { | ||
invokeApi: jest.fn().mockResolvedValue(mockValidators), | ||
}, | ||
}, | ||
{ | ||
provide: LokiLogger, | ||
useValue: { | ||
debug: jest.fn(), | ||
error: jest.fn(), | ||
}, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
handler = module.get<CheckValidatorStatusHandler>(CheckValidatorStatusHandler); | ||
prismaService = module.get<PrismaService>(PrismaService); | ||
nodeApiService = module.get<NodeApiService>(NodeApiService); | ||
}); | ||
|
||
it('should return early if blockHeight is not a multiple of BLOCKS_TO_CHECK_BANNED_VALIDATOR', async () => { | ||
await handler.execute({ blockHeight: BLOCKS_TO_CHECK_BANNED_VALIDATOR + 1 }); | ||
|
||
expect(nodeApiService.invokeApi).not.toHaveBeenCalled(); | ||
expect(prismaService.validator.update).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should fetch and update banned validators', async () => { | ||
await handler.execute({ blockHeight: BLOCKS_TO_CHECK_BANNED_VALIDATOR }); | ||
|
||
expect(nodeApiService.invokeApi).toHaveBeenCalledWith(NodeApi.POS_GET_ALL_VALIDATORS, {}); | ||
expect(prismaService.validator.update).toHaveBeenCalledWith({ | ||
where: { address: 'klyhf3gtj3wecxaxy7csqb5k8kszugghpscvat5b4' }, | ||
data: { | ||
isBanned: true, | ||
status: ValidatorStatus.BANNED, | ||
totalStake: BigInt('103150000000000'), | ||
selfStake: BigInt('9899000000000'), | ||
lastGeneratedHeight: 397037, | ||
reportMisbehaviorHeights: JSON.stringify([]), | ||
consecutiveMissedBlocks: 2209, | ||
commission: 9500, | ||
lastCommissionIncreaseHeight: 68231, | ||
sharingCoefficients: JSON.stringify([ | ||
{ | ||
tokenID: '0200000000000000', | ||
coefficient: '20c157e32fecfda501718a0f', | ||
}, | ||
]), | ||
punishmentPeriods: JSON.stringify([]), | ||
}, | ||
}); | ||
expect(prismaService.validator.update).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should handle errors when fetching validators', async () => { | ||
jest.spyOn(nodeApiService, 'invokeApi').mockRejectedValue(new Error('API Error')); | ||
|
||
await handler.execute({ blockHeight: BLOCKS_TO_CHECK_BANNED_VALIDATOR }); | ||
|
||
expect(nodeApiService.invokeApi).toHaveBeenCalledWith(NodeApi.POS_GET_ALL_VALIDATORS, {}); | ||
expect(prismaService.validator.update).not.toHaveBeenCalled(); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.