Skip to content

Commit

Permalink
Merge pull request #179 from klayrHQ/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
Theezr authored Nov 24, 2024
2 parents 03ae441 + f8b8746 commit 214b39c
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 75 deletions.
103 changes: 39 additions & 64 deletions src/modules/indexer/event/commands/process-validator.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,82 +45,57 @@ export class ProcessValidatorHandler implements ICommandHandler<ProcessValidator
}

private async processValidatorEvent(validatorAddress: string) {
const [validatorInfo, expectedRewards, validatorExists] = await Promise.all([
const [validatorInfo, expectedRewards] = await Promise.all([
this.getValidatorPosInfo(validatorAddress),
this.getExpectedValidatorRewards(validatorAddress),
this.prisma.validator.findUnique({
where: {
address: validatorAddress,
},
}),
]);

if (!validatorExists) {
await this.createValidator(validatorInfo);
}

if (validatorExists) {
await this.prisma.validator.update({
where: { address: validatorAddress },
data: {
totalStake: BigInt(validatorInfo.totalStake),
selfStake: BigInt(validatorInfo.selfStake),
validatorWeight: this.calcValidatorWeight(
validatorInfo.totalStake,
validatorInfo.selfStake,
),
isBanned: validatorInfo.isBanned,
lastCommissionIncreaseHeight: validatorInfo.lastCommissionIncreaseHeight,
commission: validatorInfo.commission,
consecutiveMissedBlocks: validatorInfo.consecutiveMissedBlocks,
reportMisbehaviorHeights: JSON.stringify(validatorInfo.reportMisbehaviorHeights),
punishmentPeriods: JSON.stringify(validatorInfo.punishmentPeriods),
blockReward: BigInt(expectedRewards.blockReward),
},
});
}
await this.upsertValidator(validatorInfo, expectedRewards);
}

private async createValidator(val: ValidatorInfo) {
await this.upsertAccount(val.address, val.name);

const keys = await this.getValidatorKeys(val.address);

await this.prisma.validator.create({
data: {
address: val.address,
totalStake: BigInt(val.totalStake),
selfStake: BigInt(val.selfStake),
validatorWeight: this.calcValidatorWeight(val.totalStake, val.selfStake),
lastCommissionIncreaseHeight: val.lastCommissionIncreaseHeight,
commission: val.commission,
consecutiveMissedBlocks: val.consecutiveMissedBlocks,
isBanned: val.isBanned,
lastGeneratedHeight: val.lastGeneratedHeight,
reportMisbehaviorHeights: JSON.stringify(val.reportMisbehaviorHeights),
punishmentPeriods: JSON.stringify(val.punishmentPeriods),
sharingCoefficients: JSON.stringify(val.sharingCoefficients),
generatorKey: keys.generatorKey,
blsKey: keys.blsKey,
},
private async upsertValidator(
validatorInfo: ValidatorInfo,
expectedRewards: ExpectedValidatorRewards,
) {
await this.upsertAccount(validatorInfo.address, validatorInfo.name);

const updateData = {
totalStake: BigInt(validatorInfo.totalStake),
selfStake: BigInt(validatorInfo.selfStake),
validatorWeight: this.calcValidatorWeight(validatorInfo.totalStake, validatorInfo.selfStake),
isBanned: validatorInfo.isBanned,
lastCommissionIncreaseHeight: validatorInfo.lastCommissionIncreaseHeight,
commission: validatorInfo.commission,
consecutiveMissedBlocks: validatorInfo.consecutiveMissedBlocks,
reportMisbehaviorHeights: JSON.stringify(validatorInfo.reportMisbehaviorHeights),
punishmentPeriods: JSON.stringify(validatorInfo.punishmentPeriods),
sharingCoefficients: JSON.stringify(validatorInfo.sharingCoefficients),
blockReward: BigInt(expectedRewards.blockReward),
};

const keys = await this.getValidatorKeys(validatorInfo.address);

const createData = {
...updateData,
address: validatorInfo.address,
lastGeneratedHeight: validatorInfo.lastGeneratedHeight,
generatorKey: keys.generatorKey,
blsKey: keys.blsKey,
};

await this.prisma.validator.upsert({
where: { address: validatorInfo.address },
update: updateData,
create: createData,
});
}

private async upsertAccount(address: string, name: string) {
const account = await this.prisma.account.findUnique({
await this.prisma.account.upsert({
where: { address },
update: { name },
create: { address, name },
});

if (!account) {
await this.prisma.account.create({
data: { address, name },
});
} else {
await this.prisma.account.update({
where: { address },
data: { name },
});
}
}

private async getValidatorKeys(address: string): Promise<ValidatorKeys> {
Expand Down
1 change: 1 addition & 0 deletions src/modules/indexer/event/gateways/pos.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export class ValidatorStaked extends EventGateway {
address: senderAddress,
});

// TODO: find solution so this is not needed
for await (const stake of stakes.stakes) {
await this.commandBus.execute(new ProcessValidatorCommand(stake.validatorAddress));
}
Expand Down
30 changes: 19 additions & 11 deletions src/modules/pos/pos.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,31 @@ export class PosController {
@UsePipes(new ValidationPipe({ transform: true, whitelist: true, forbidNonWhitelisted: true }))
@ApiResponse(getStakesRes)
async getStakes(@Query() query: StakesQueryDto): Promise<GetStakesResDto> {
const { account, response } = await this.getAccountAndStakes(query, 'staker');
const { account, stakes } = await this.getAccountAndStakes(query, 'staker');

const response = stakes.map((stake) => {
return {
address: stake.validatorAddress,
amount: stake.amount.toString(),
...(stake.account.name ? { name: stake.account.name } : {}),
};
});
return new GatewayResponse({ stakes: response }, { staker: account });
}

@Get('stakers')
@UsePipes(new ValidationPipe({ transform: true, whitelist: true, forbidNonWhitelisted: true }))
@ApiResponse(getStakersRes)
async getStakers(@Query() query: StakesQueryDto): Promise<GetStakersResDto> {
const { account, response } = await this.getAccountAndStakes(query, 'validatorAddress');
const { account, stakes } = await this.getAccountAndStakes(query, 'validatorAddress');

const response = stakes.map((stake) => {
return {
address: stake.staker,
amount: stake.amount.toString(),
...(stake.account.name ? { name: stake.account.name } : {}),
};
});
return new GatewayResponse({ stakers: response }, { validator: account });
}

Expand Down Expand Up @@ -240,15 +256,7 @@ export class PosController {
include: { account: true },
});

const response = stakes.map((stake) => {
return {
address: stake.staker,
amount: stake.amount.toString(),
...(stake.account.name ? { name: stake.account.name } : {}),
};
});

return { account, response };
return { account, stakes };
}

private async findAccount(
Expand Down

0 comments on commit 214b39c

Please sign in to comment.