Skip to content

Commit

Permalink
refactor scorekeeper to separate files
Browse files Browse the repository at this point in the history
  • Loading branch information
will pankiewicz authored and will pankiewicz committed Feb 13, 2024
1 parent 878ae2f commit ffac567
Show file tree
Hide file tree
Showing 15 changed files with 1,003 additions and 874 deletions.
2 changes: 2 additions & 0 deletions packages/common/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import path from "path";
import { ClaimerConfig } from "./types";

type CandidateConfig = {
id: number;
name: string;
stash: string;
riotHandle: string;
kusamaStash?: string;
skipSelfStake?: boolean;
kyc: boolean;
};

export type NominatorConfig = {
Expand Down
115 changes: 4 additions & 111 deletions packages/common/src/constraints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ export class OTV implements Constraints {
});
// Scale the location value to between the 10th and 95th percentile
const scaledLocation =
scaledDefined(candidateLocation, locationValues, 0.15, 0.9) || 0;
scaledDefined(candidateLocation, locationValues, 0, 1) || 0;
const locationScore = bannedProvider
? 0
: candidate.location == "No Location"
Expand All @@ -626,7 +626,7 @@ export class OTV implements Constraints {
});
// Scale the value to between the 10th and 95th percentile
const scaledRegion =
scaledDefined(candidateRegion, regionValues, 0.15, 0.9) || 0;
scaledDefined(candidateRegion, regionValues, 0, 1) || 0;
const regionScore = bannedProvider
? 0
: candidate.location == "No Location"
Expand All @@ -645,7 +645,7 @@ export class OTV implements Constraints {
});
// Scale the value to between the 10th and 95th percentile
const scaledCountry =
scaledDefined(candidateCountry, countryValues, 0.2, 0.8) || 0;
scaledDefined(candidateCountry, countryValues, 0, 1) || 0;
const countryScore = bannedProvider
? 0
: candidate.location == "No Location"
Expand All @@ -664,7 +664,7 @@ export class OTV implements Constraints {
});
// Scale the value to between the 10th and 95th percentile
const scaledProvider =
scaledDefined(candidateProvider, providerValues, 0.2, 0.8) || 0;
scaledDefined(candidateProvider, providerValues, 0, 1) || 0;
const providerScore = bannedProvider
? 0
: candidate.location == "No Location"
Expand Down Expand Up @@ -703,23 +703,6 @@ export class OTV implements Constraints {
) || 0;
const nominatorStakeScore = scaledNominatorStake * this.NOMINATIONS_WEIGHT;

// const delegations = await getDelegations(candidate.stash);
// let totalDelegations = 0;
// if (
// delegations != undefined &&
// delegations?.totalBalance &&
// delegations?.delegators
// ) {
// const { totalBalance, delegators } = delegations;
//
// for (const delegator of delegators) {
// totalDelegations += Math.sqrt(delegator.effectiveBalance);
// }
// }
// const scaledDelegations =
// scaledDefined(totalDelegations, delegationStats.values, 0.1, 0.95) || 0;
// const delegationScore = scaledDelegations * this.DELEGATIONS_WEIGHT;

let openGovDelegationScore = 0;
const isDelegationsUpdating = await queries.getUpdatingDelegations();
if (!isDelegationsUpdating) {
Expand All @@ -745,38 +728,6 @@ export class OTV implements Constraints {
openGovDelegationScore = score?.openGovDelegations || 0;
}

// Score the council backing weight based on what percentage of their staking bond it is
// const denom = await this.chaindata.getDenom();
// const formattedBonded = candidate.bonded / denom;
// const councilStakeScore =
// candidate.councilStake == 0
// ? 0
// : candidate.councilStake >= 0.75 * formattedBonded
// ? this.COUNCIL_WEIGHT
// : candidate.councilStake >= 0.5 * formattedBonded
// ? 0.75 * this.COUNCIL_WEIGHT
// : candidate.councilStake >= 0.25 * formattedBonded
// ? 0.5 * this.COUNCIL_WEIGHT
// : candidate.councilStake < 0.25 * formattedBonded
// ? 0.25 * this.COUNCIL_WEIGHT
// : 0;

// const lastReferendum = (await getLastReferenda())[0]?.referendumIndex;
// // Score democracy based on how many proposals have been voted on
// const candidateVotes = candidate?.democracyVotes
// ? candidate.democracyVotes
// : [];
// const {
// baseDemocracyScore,
// totalDemocracyScore,
// totalConsistencyMultiplier,
// lastConsistencyMultiplier,
// } = scoreDemocracyVotes(candidateVotes, lastReferendum);
// const democracyValues = democracyStats.values;
// const scaledDemocracyScore =
// scaled(totalDemocracyScore, democracyStats.values) *
// this.DEMOCRACY_WEIGHT;

const lastOpenGovReferendum = (await getLastOpenGovReferenda())[0]?.index;
// Score democracy based on how many proposals have been voted on
const candidateConvictionVotes = candidate?.convictionVotes
Expand Down Expand Up @@ -1200,31 +1151,6 @@ export const getNominatorStakeValues = async (
return { ownNominatorAddresses, nominatorStakeValues, nominatorStakeStats };
};

// export const getDelegationValues = async (
// validCandidates: Types.CandidateData[]
// ) => {
// const delegationValues = [];
// for (const candidate of validCandidates) {
// const delegations = await getDelegations(candidate.stash);
// if (delegations != undefined && delegations?.delegators) {
// try {
// const { totalBalance, delegators } = delegations;
//
// let total = 0;
// for (const delegator of delegators) {
// total += Math.sqrt(delegator.effectiveBalance);
// }
// delegationValues.push(total);
// } catch (e) {
// logger.info(`{delegations} Can't find delegation values`);
// logger.info(JSON.stringify(delegations));
// }
// }
// }
// const delegationStats = getStats(delegationValues);
// return { delegationValues, delegationStats };
// };

export const getOpenGovDelegationValues = async (
validCandidates: Types.CandidateData[],
) => {
Expand All @@ -1247,39 +1173,6 @@ export const getOpenGovDelegationValues = async (
return { delegationValues, delegationStats };
};

// export const getCouncilStakeValues = (
// validCandidates: Types.CandidateData[]
// ) => {
// const councilStakeValues = validCandidates.map((candidate) => {
// return candidate.councilStake ? Number(candidate.councilStake) : 0;
// });
// const councilStakeStats = getStats(councilStakeValues);
// return { councilStakeValues, councilStakeStats };
// };

// export const getDemocracyValues = async (
// validCandidates: Types.CandidateData[]
// ) => {
// const lastReferendum = (await getLastReferenda())[0]?.referendumIndex;
//
// // Democracy
//
// const democracyValues = validCandidates.map((candidate) => {
// const democracyVotes = candidate.democracyVotes
// ? candidate.democracyVotes
// : [];
// const {
// baseDemocracyScore,
// totalDemocracyScore,
// totalConsistencyMultiplier,
// lastConsistencyMultiplier,
// } = scoreDemocracyVotes(democracyVotes, lastReferendum);
// return totalDemocracyScore || 0;
// });
// const democracyStats = getStats(democracyValues);
// return { lastReferendum, democracyValues, democracyStats };
// };

export const getOpenGovValues = async (
validCandidates: Types.CandidateData[],
) => {
Expand Down
42 changes: 42 additions & 0 deletions packages/common/src/db/queries/Candidate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1605,3 +1605,45 @@ export const setValid = async (
},
).exec();
};

export const getUniqueNameSet = async (): Promise<any> => {
const nameSet = new Set();
const allNodes = await allCandidates();
for (const node of allNodes) {
nameSet.add(node.name);
}
return Array.from(nameSet);
};

export const getDuplicatesByName = async (): Promise<any> => {
const duplicates = [];
const names = await getUniqueNameSet();
for (const name of names) {
const candidates = await CandidateModel.find({ name: name }).exec();
if (candidates.length > 1) {
duplicates.push({ name: name, num: candidates.length });
}
}
return duplicates;
};

export const getDuplicatesByStash = async (): Promise<any> => {
const duplicates = [];
const stashes = await getUniqueStashSet();
for (const stash of stashes) {
const candidates = await CandidateModel.find({ stash: stash }).exec();
if (candidates.length > 1) {
duplicates.push({ stash: stash, num: candidates.length });
}
}
return duplicates;
};

export const getUniqueStashSet = async (): Promise<any> => {
const stashSet = new Set();
const allNodes = await allCandidates();
for (const node of allNodes) {
stashSet.add(node.stash);
}
return Array.from(stashSet);
};
35 changes: 35 additions & 0 deletions packages/common/src/utils/candidatesFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import fs from "fs";

const writeCandidatesMatrix = (path: any, network: any) => {
try {
const conf = fs.readFileSync(path, { encoding: "utf-8" });
const candidates = JSON.parse(conf).candidates;

const filePath = `${network}-matrix.txt`;

fs.writeFileSync(filePath, "", "utf8");

const matrixHandles: (string | any[])[] = [];

candidates.forEach((candidate: { riotHandle: string | any[] }) => {
if (typeof candidate.riotHandle == "string") {
if (!matrixHandles.includes(candidate.riotHandle)) {
matrixHandles.push(candidate.riotHandle);
}
} else if (Array.isArray(candidate.riotHandle)) {
candidate.riotHandle.forEach((handle) => {
if (!matrixHandles.includes(handle)) {
matrixHandles.push(handle);
}
});
}
});

matrixHandles.forEach((handle) => {
fs.appendFileSync(filePath, handle + "\n", "utf8");
});
} catch (e) {
console.log("Invalid JSON!");
process.exit(1);
}
};
12 changes: 12 additions & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,25 @@ export const clean = async (scorekeeper) => {
await queries.clearCandidates();
await queries.deleteOldValidatorScores();

await findDuplicates();

logger.info(`Cleaning finished`, winstonLabel);
} catch (e) {
logger.error(e.toString());
process.exit(1);
}
};

export const findDuplicates = async () => {
const nameDuplicates = await queries.getDuplicatesByName();
logger.warn("Found Duplicates with multiple names", winstonLabel);
logger.warn(JSON.stringify(nameDuplicates), winstonLabel);

const stashDuplicates = await queries.getDuplicatesByStash();
logger.warn("Found Duplicates with multiple stashes", winstonLabel);
logger.warn(JSON.stringify(stashDuplicates), winstonLabel);
};

export const addRewardClaimer = async (config, scorekeeper) => {
try {
if (config.scorekeeper.claimer) {
Expand Down
Loading

0 comments on commit ffac567

Please sign in to comment.