From 87e60a7aa123656324e25fad40c1ab995bf5d3d6 Mon Sep 17 00:00:00 2001 From: Meylis Date: Thu, 13 Oct 2022 16:27:30 +0500 Subject: [PATCH 1/2] refactor: validator, performance --- src/controllers/validator.ts | 14 ---------- src/workers/validators.ts | 53 ++++++++---------------------------- 2 files changed, 12 insertions(+), 55 deletions(-) diff --git a/src/controllers/validator.ts b/src/controllers/validator.ts index 0067e2b..d7d6d22 100644 --- a/src/controllers/validator.ts +++ b/src/controllers/validator.ts @@ -83,20 +83,6 @@ export const setDelegator = async (delegator: any) => { throw new Error(`Could not save current era validator: ${error}`); } }; -export const updateBidPerformanceAndRewards = async ( - publicKey: string, - performance: number, - totalValidatorRewards: number, - totalDelegatorRewards: number -) => { - await Bid.findOneAndUpdate( - { publicKey }, - { performance, $inc: { totalValidatorRewards, totalDelegatorRewards } }, - { new: true, upsert: true } - ).catch((err) => { - throw new Error(err); - }); -}; export const seedBidRewards = async (req: Request, res: Response) => { const { currentEraId } = req.params; for (let i = 0; i <= Number(currentEraId); i++) { diff --git a/src/workers/validators.ts b/src/workers/validators.ts index 1896b14..522b67f 100644 --- a/src/workers/validators.ts +++ b/src/workers/validators.ts @@ -8,7 +8,6 @@ import { setCurrentEraValidator, setDelegator, setNextEraValidator, - updateBidPerformanceAndRewards } from '@controllers/validator'; import { casperService } from '@utils'; import Bull from 'bull'; @@ -18,6 +17,7 @@ export type Bid = { publicKey: string; numOfDelegators: number; delegationRate: number; + performance: number; totalBid: number; totalDelegated: number; selfStake: number; @@ -25,6 +25,8 @@ export type Bid = { inactive: boolean; networkPercentage: number; rank?: number; + totalValidatorRewards: number; + totalDelegatorRewards: number; }; type EraValidator = { publicKey: string; @@ -57,12 +59,6 @@ export const bidOrValidatorSave = new Bull('bid-or-validator-save', { port: Number(process.env.REDIS_PORT) } }); -export const bidPerformanceAndRewardsUpdate = new Bull('validator-update', { - redis: { - host: process.env.NODE_ENV == 'dev' ? 'localhost' : process.env.REDIS_HOST, - port: Number(process.env.REDIS_PORT) - } -}); export const bidDelegatorSave = new Bull('bid-delegator-save', { redis: { host: process.env.NODE_ENV == 'dev' ? 'localhost' : process.env.REDIS_HOST, @@ -150,43 +146,12 @@ export const processValidatorsInfoFetch = async () => { .catch((err) => done(new Error(err))); }); }; -export const addValidatorUpdate = async (eraId: number) => { - await bidPerformanceAndRewardsUpdate.add(eraId, { - attempts: 10, - removeOnComplete: true, - removeOnFail: 1000 - }); -}; -export const processValidatorUpdate = async () => { - bidPerformanceAndRewardsUpdate.process(50, async (job, done) => { - updateBid(job.data) - .then(() => { - done(); - }) - .catch((err) => done(new Error(err))); - }); -}; -export const updateBid = async (eraId: number) => { - const validatorPerformanceAggregation = await getBidPerformanceAggregation(eraId); - validatorPerformanceAggregation?.forEach(async (validator) => { - const totalValidatorRewards: number = - (await getBidRewards(validator._id))[0]?.totalRewards || 0; - const totalDelegatorRewards: number = - (await getBidDelegatorRewards(validator._id))[0]?.totalDelegatorRewards || 0; - validator._id && - (await updateBidPerformanceAndRewards( - validator._id, - validator.count / 360, - totalValidatorRewards, - totalDelegatorRewards - )); - }); -}; export const fetchValidatorsInfo = async () => { try { let bidValidators: Bid[] = []; let currentEraValidators: EraValidator[] = []; let nextEraValidators: EraValidator[] = []; + const validatorsInfoResult: ValidatorsInfoResult = await casperService.getValidatorsInfo(); const bids: any = validatorsInfoResult.auction_state.bids; const eraValidatorsInfo = validatorsInfoResult.auction_state.era_validators; @@ -197,7 +162,7 @@ export const fetchValidatorsInfo = async () => { }); }); bids && - bids.forEach((bid) => { + bids.forEach(async (bid) => { const selfStake = Number(ethers.utils.formatUnits(bid.bid.staked_amount, 9)); let totalDelegated = 0; bid?.bid?.delegators?.forEach((delegator) => { @@ -211,19 +176,25 @@ export const fetchValidatorsInfo = async () => { addBidDelegatorSave(_delegator); totalDelegated += Number(ethers.utils.formatUnits(delegator.staked_amount, 9)); }); + const totalBid = selfStake + totalDelegated; const selfStakePercentage = (selfStake / totalBid) * 100; + const totalValidatorRewards: number = (await getBidRewards(bid.public_key))[0]?.totalRewards || 0; + const totalDelegatorRewards: number = (await getBidDelegatorRewards(bid.public_key))[0]?.totalDelegatorRewards || 0; bidValidators.push({ publicKey: bid.public_key, numOfDelegators: bid?.bid?.delegators?.length, delegationRate: bid.bid?.delegation_rate, + performance: eraValidatorsInfo.length / 360, totalBid, totalDelegated, selfStake, selfStakePercentage, networkPercentage: 0, - inactive: bid.bid.inactive + inactive: bid.bid.inactive, + totalValidatorRewards, + totalDelegatorRewards }); }); From 527a926b1469da840a3668e12d6ad2b070efdb6b Mon Sep 17 00:00:00 2001 From: Meylis Date: Mon, 17 Oct 2022 15:20:06 +0500 Subject: [PATCH 2/2] chore: fix performance calculation --- src/workers/validators.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/workers/validators.ts b/src/workers/validators.ts index 522b67f..dc8feb1 100644 --- a/src/workers/validators.ts +++ b/src/workers/validators.ts @@ -186,7 +186,7 @@ export const fetchValidatorsInfo = async () => { publicKey: bid.public_key, numOfDelegators: bid?.bid?.delegators?.length, delegationRate: bid.bid?.delegation_rate, - performance: eraValidatorsInfo.length / 360, + performance: bid?.bid?.delegators?.length / 360, totalBid, totalDelegated, selfStake,