Skip to content

Commit

Permalink
Merge pull request #2930 from w3f/staging
Browse files Browse the repository at this point in the history
Staging
  • Loading branch information
ironoa authored May 15, 2024
2 parents 4157ccf + a08a17b commit f3e30e0
Show file tree
Hide file tree
Showing 19 changed files with 50 additions and 109 deletions.
2 changes: 1 addition & 1 deletion apps/1kv-backend/templates/kusama-otv-backend.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ spec:
source:
repoURL: https://w3f.github.io/helm-charts/
chart: otv-backend
targetRevision: v3.2.1
targetRevision: v3.2.2
plugin:
env:
- name: HELM_VALUES
Expand Down
2 changes: 1 addition & 1 deletion apps/1kv-backend/templates/polkadot-otv-backend.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ spec:
source:
repoURL: https://w3f.github.io/helm-charts/
chart: otv-backend
targetRevision: v3.2.1
targetRevision: v3.2.2
plugin:
env:
- name: HELM_VALUES
Expand Down
4 changes: 2 additions & 2 deletions charts/otv-backend/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
description: 1K Validators Backend
name: otv-backend
version: v3.2.1
appVersion: v3.2.1
version: v3.2.2
appVersion: v3.2.2
apiVersion: v2
2 changes: 1 addition & 1 deletion packages/common/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@1kv/common",
"version": "3.2.1",
"version": "3.2.2",
"description": "Services for running the Thousand Validator Program.",
"main": "build/index.js",
"types": "build/index.d.ts",
Expand Down
9 changes: 6 additions & 3 deletions packages/common/src/chaindata/queries/Era.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ export const getTotalEraPoints = async (
if (!chainType) {
return {} as EraPointsInfo;
}
const [blockHash, err] = await chaindata.findEraBlockHash(era, chainType);
const [blockHash, err] = await chaindata.findEraBlockHash(
era + 1,
chainType,
);

if (blockHash) {
const apiAt = await chaindata?.api?.at(blockHash);
Expand Down Expand Up @@ -204,12 +207,12 @@ export const findEraBlockHash = async (
return ["", "API at block hash is null"];
}

const testEra = await apiAt.query.staking.currentEra();
const testEra = await apiAt.query.staking.activeEra();
if (testEra && testEra.isEmpty) {
logger.info(`Test era is empty: ${JSON.stringify(testEra)}`);
return ["", "Test era is none"];
}
const testIndex = testEra.unwrap().toNumber();
const testIndex = testEra.unwrap().index.toNumber();
if (era == testIndex) {
return [blockHash.toString(), null];
}
Expand Down
2 changes: 2 additions & 0 deletions packages/common/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,5 @@ export const USE_PROVIDER = true;
export const USE_NOMINATIONS = true;
export const USE_RPC = true;
export const USE_CLIENT = true;

export const ERAPOINTS_JOB_MAX_ERAS = 84;
14 changes: 10 additions & 4 deletions packages/common/src/constraints/CheckCandidates.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { logger } from "../index";
import { ChainData, logger } from "../index";
import { allCandidates, Candidate, setLastValid, setValid } from "../db";
import { constraintsLabel, OTV } from "./constraints";
import {
Expand All @@ -22,6 +22,7 @@ import { percentage, timeRemaining } from "../utils/util";
export const checkCandidate = async (
constraints: OTV,
candidate: Candidate,
validators: string[],
): Promise<boolean> => {
try {
let valid = false;
Expand All @@ -33,8 +34,8 @@ export const checkCandidate = async (

const validateValid = await checkValidateIntention(
constraints.config,
constraints.chaindata,
candidate,
validators,
);
if (!validateValid) {
logger.info(
Expand Down Expand Up @@ -62,7 +63,10 @@ export const checkCandidate = async (
);
}

const identityValid = await checkIdentity(constraints.chaindata, candidate);
const identityValid =
constraints.config?.constraints?.skipIdentity == true
? true
: (await checkIdentity(constraints.chaindata, candidate)) || false;
if (!identityValid) {
logger.info(`${candidate.name} identity not valid`, constraintsLabel);
}
Expand Down Expand Up @@ -166,14 +170,16 @@ export const checkCandidate = async (

export const checkAllCandidates = async (
constraints: OTV,
chaindata: ChainData,
): Promise<boolean> => {
try {
const candidates = await allCandidates();
const validators = await chaindata.getValidators();
logger.info(`checking ${candidates.length} candidates`, constraintsLabel);
for (const [index, candidate] of candidates.entries()) {
const start = Date.now();

const isValid = await constraints.checkCandidate(candidate);
const isValid = await constraints.checkCandidate(candidate, validators);
const end = Date.now();
const time = `(${end - start}ms)`;
const remaining = timeRemaining(
Expand Down
25 changes: 1 addition & 24 deletions packages/common/src/constraints/ValidityChecks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,10 @@ export const checkOnline = async (candidate: Candidate): Promise<boolean> => {
// Check the validate intention for a single validator
export const checkValidateIntention = async (
config: Config.ConfigSchema,
chaindata: ChainData,
candidate: Candidate,
validators: string[],
): Promise<boolean> => {
try {
const validators = await chaindata.getValidators();
if (
!validators?.length ||
validators.includes(Util.formatAddress(candidate?.stash, config))
Expand All @@ -65,28 +64,6 @@ export const checkValidateIntention = async (
}
};

// checks the validate intention for all validators
export const checkAllValidateIntentions = async (
config: Config.ConfigSchema,
chaindata: ChainData,
candidates: Candidate[],
): Promise<boolean> => {
try {
const validators = await chaindata.getValidators();
for (const candidate of candidates) {
if (!validators.includes(Util.formatAddress(candidate.stash, config))) {
await setValidateIntentionValidity(candidate, false);
} else {
await setValidateIntentionValidity(candidate, true);
}
}
return true;
} catch (e) {
logger.error(`Error checking validate intentions: ${e}`, constraintsLabel);
throw new Error("could not make validity check");
}
};

// checks that the validator is on the latest client version
export const checkLatestClientVersion = async (
config: Config.ConfigSchema,
Expand Down
9 changes: 6 additions & 3 deletions packages/common/src/constraints/constraints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,15 @@ export class OTV implements Constraints {

// Checks the validity of all candidates
async checkAllCandidates(): Promise<boolean> {
return await checkAllCandidates(this);
return await checkAllCandidates(this, this.chaindata);
}

// Check the candidate and set any invalidity fields
async checkCandidate(candidate: Candidate): Promise<boolean> {
return await checkCandidate(this, candidate);
async checkCandidate(
candidate: Candidate,
validators: string[],
): Promise<boolean> {
return await checkCandidate(this, candidate, validators);
}

async scoreAllCandidates(): Promise<boolean> {
Expand Down
2 changes: 1 addition & 1 deletion packages/common/src/constraints/score.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export const getStats = (arr: number[]): Stats => {
const arrQ75 = arr.length !== 0 ? q75(arr) : 0;
const arrQ90 = arr.length !== 0 ? q90(arr) : 0;
const arrMean = arr.length !== 0 ? mean(arr) : 0;
const arrStd = arr.length !== 0 ? std(arr) : 0;
const arrStd = arr.length > 1 ? std(arr) : 0;

return {
values: arr,
Expand Down
4 changes: 2 additions & 2 deletions packages/common/src/db/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ export interface ValidatorSet {
}

export const ValidatorSetSchema = new Schema({
session: Number,
era: Number,
session: { type: Number, index: true },
era: { type: Number, index: true },
validators: [String],
});

Expand Down
3 changes: 2 additions & 1 deletion packages/common/src/scorekeeper/Round.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,11 @@ export const startRound = async (

// Get all Candidates and set validity
const allCandidates = await queries.allCandidates();
const validators = await chaindata.getValidators();

// Set Validity
for (const [index, candidate] of allCandidates.entries()) {
const isValid = await constraints.checkCandidate(candidate);
const isValid = await constraints.checkCandidate(candidate, validators);

const progress = Math.floor((index / allCandidates.length) * 100);
jobStatusEmitter.emit("jobProgress", {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export const validityJob = async (
try {
const { constraints } = metadata;
const candidates = await allCandidates();
const validators = await metadata.chaindata.getValidators();
logger.info(`Checking ${candidates.length} candidates`, constraintsLabel);

// Calculate total number of candidates
Expand All @@ -43,7 +44,7 @@ export const validityJob = async (
for (const [index, candidate] of candidates.entries()) {
const start = Date.now();

const isValid = await constraints.checkCandidate(candidate);
const isValid = await constraints.checkCandidate(candidate, validators);
const end = Date.now();
const time = `(${end - start}ms)`;
const remaining = timeRemaining(index + 1, totalCandidates, end - start);
Expand Down Expand Up @@ -82,57 +83,6 @@ export const validityJobWithTiming = withExecutionTimeLogging(
"Validity Job Done",
);

export const candidateValidityJob = async (
constraints: Constraints.OTV,
candidateAddress: string,
) => {
try {
const start = Date.now();

const candidate = await queries.getCandidateByStash(candidateAddress);
if (candidate) {
await constraints.checkCandidate(candidate);

const end = Date.now();
const executionTime = (end - start) / 1000;

logger.info(
`validity for ${candidate.name} Done. (${executionTime}s)`,
constraintsLabel,
);
}
} catch (e) {
logger.error(`Error running validity job: ${e}`, constraintsLabel);
}
};

export const individualScoreJob = async (
constraints: Constraints.OTV,
candidateAddress: string,
) => {
try {
const start = Date.now();
const candidate = await queries.getCandidateByStash(candidateAddress);
if (candidate) {
let scoreMetadata = await queries.getLatestValidatorScoreMetadata();
if (!scoreMetadata) {
logger.warn(
`no score metadata, cannot score candidates`,
constraintsLabel,
);
await constraints.setScoreMetadata();
scoreMetadata = await queries.getLatestValidatorScoreMetadata();
}
await constraints.scoreCandidate(candidate, scoreMetadata);

const end = Date.now();
const executionTime = (end - start) / 1000;
}
} catch (e) {
logger.error(`Error running individual score job: ${e}`, constraintsLabel);
}
};

export const scoreJob = async (
metadata: JobRunnerMetadata,
): Promise<boolean> => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ChainData, logger, queries } from "../../../index";
import { ChainData, Constants, logger, queries } from "../../../index";
import { Job, JobConfig, JobRunnerMetadata, JobStatus } from "../JobsClass";
import { jobStatusEmitter } from "../../../Events";
import { withExecutionTimeLogging } from "../../../utils";
Expand Down Expand Up @@ -63,16 +63,15 @@ export const eraPointsJob = async (
// - if a record doesn't exist, create it
const [activeEra, err] = await chaindata.getActiveEraIndex();

// Calculate total number of eras to process
const totalEras = activeEra;
let processedEras = 0;

for (let i = activeEra - 1; i >= 0; i--) {
for (
let i = activeEra - 1, processedEras = 1;
i >= activeEra - Constants.ERAPOINTS_JOB_MAX_ERAS;
i--, processedEras++
) {
await individualEraPointsJob(chaindata, i);

// Calculate progress percentage
processedEras++;
const progress = (processedEras / totalEras) * 100;
const progress = (processedEras / Constants.ERAPOINTS_JOB_MAX_ERAS) * 100;

// Emit progress update with active era as iteration
jobStatusEmitter.emit("jobProgress", {
Expand All @@ -83,7 +82,7 @@ export const eraPointsJob = async (
});

logger.info(
`Processed Era Points for Era: ${i} (${activeEra - i}/${activeEra})`,
`Processed Era Points for Era: ${i} (${activeEra - i}/${Constants.ERAPOINTS_JOB_MAX_ERAS})`,
erapointsLabel,
);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@1kv/core",
"version": "3.2.1",
"version": "3.2.2",
"description": "Services for running the Thousand Validator Program.",
"main": "index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/gateway/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@1kv/gateway",
"version": "3.2.1",
"version": "3.2.2",
"description": "Services for running the Thousand Validator Program.",
"main": "build/index.js",
"types": "build/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/scorekeeper-status-ui/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@1kv/scorekeeper-status-ui",
"private": true,
"version": "3.2.1",
"version": "3.2.2",
"type": "module",
"scripts": {
"dev": "vite",
Expand Down
2 changes: 1 addition & 1 deletion packages/telemetry/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@1kv/telemetry",
"version": "3.2.1",
"version": "3.2.2",
"description": "Services for running the Thousand Validator Program.",
"main": "build/index.js",
"types": "build/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/worker/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@1kv/worker",
"version": "3.2.1",
"version": "3.2.2",
"description": "Services for running the Thousand Validator Program.",
"main": "build/index.js",
"types": "build/index.d.ts",
Expand Down

0 comments on commit f3e30e0

Please sign in to comment.