Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor validator performance #37

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 0 additions & 14 deletions src/controllers/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand Down
53 changes: 12 additions & 41 deletions src/workers/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
setCurrentEraValidator,
setDelegator,
setNextEraValidator,
updateBidPerformanceAndRewards
} from '@controllers/validator';
import { casperService } from '@utils';
import Bull from 'bull';
Expand All @@ -18,13 +17,16 @@ export type Bid = {
publicKey: string;
numOfDelegators: number;
delegationRate: number;
performance: number;
totalBid: number;
totalDelegated: number;
selfStake: number;
selfStakePercentage: number;
inactive: boolean;
networkPercentage: number;
rank?: number;
totalValidatorRewards: number;
totalDelegatorRewards: number;
};
type EraValidator = {
publicKey: string;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand All @@ -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) => {
Expand All @@ -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: bid?.bid?.delegators?.length / 360,
totalBid,
totalDelegated,
selfStake,
selfStakePercentage,
networkPercentage: 0,
inactive: bid.bid.inactive
inactive: bid.bid.inactive,
totalValidatorRewards,
totalDelegatorRewards
});
});

Expand Down