Skip to content

Commit

Permalink
Merge pull request #2676 from w3f/will-filtered
Browse files Browse the repository at this point in the history
Add Nominator FIltering
  • Loading branch information
wpank committed Mar 2, 2024
2 parents 5762a38 + ade07a4 commit a6c5db4
Show file tree
Hide file tree
Showing 16 changed files with 60 additions and 29 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.0.24
targetRevision: v3.0.25
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.0.24
targetRevision: v3.0.25
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.0.24
appVersion: v3.0.24
version: v3.0.25
appVersion: v3.0.25
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.0.24",
"version": "3.0.25",
"description": "Services for running the Thousand Validator Program.",
"main": "build/index.js",
"types": "build/index.d.ts",
Expand Down
12 changes: 6 additions & 6 deletions packages/common/src/chaindata/chaindata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,19 @@ export class ChainData {
}

checkApiConnection = async (retries = 0): Promise<boolean> => {
if (!this.api?.isConnected) {
if (!this.handler.getApi()?.isConnected) {
if (retries < CHAINDATA_RETRIES) {
logger.warn(
`Retries: ${retries} - API is not connected, waiting...`,
chaindataLabel,
);
// logger.warn(
// `Retries: ${retries} - API is not connected, waiting...`,
// chaindataLabel,
// );
await sleep(CHAINDATA_SLEEP);

retries++;
return await this.checkApiConnection(retries);
} else {
logger.warn(`Performing health check on api...`, chaindataLabel);
await this.api?.disconnect();
await this.handler.getApi()?.disconnect();
const healthy = await this.handler.healthCheck();
if (healthy) {
this.api = this.handler.getApi();
Expand Down
2 changes: 1 addition & 1 deletion packages/common/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const POLKADOT_API_TIMEOUT = 1000000;

export const CHAINDATA_RETRIES = 20;

export const CHAINDATA_SLEEP = 10000;
export const CHAINDATA_SLEEP = 300;

export const API_PROVIDER_TIMEOUT = 10000;

Expand Down
24 changes: 22 additions & 2 deletions packages/common/src/nominator/nominator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export interface NominatorStatus {
rewardDestination?: string;
stale?: boolean;
dryRun?: boolean;
shouldNominate?: boolean;
}

export default class Nominator extends EventEmitter {
Expand All @@ -61,6 +62,8 @@ export default class Nominator extends EventEmitter {
reason: "",
};

public _shouldNominate = false;

private _status: NominatorStatus = {
status: "Init",
bondedAddress: "",
Expand Down Expand Up @@ -130,12 +133,25 @@ export default class Nominator extends EventEmitter {
this._status = { ...this._status, ...newStatus };
};

public async shouldNominate(): Promise<boolean> {
const stash = await this.stash();
const isBonded = await this.chaindata.isBonded(stash);
const [bonded, err] = await this.chaindata.getDenomBondedAmount(stash);

const currentEra = (await this.chaindata.getCurrentEra()) || 0;
const lastNominationEra =
(await this.chaindata.getNominatorLastNominationEra(stash)) || 0;
this._shouldNominate = isBonded && currentEra - lastNominationEra >= 1;
return this._shouldNominate;
}

public async init(): Promise<NominatorStatus> {
try {
const stash = await this.stash();
const isBonded = await this.chaindata.isBonded(stash);
const [bonded, err] = await this.chaindata.getDenomBondedAmount(stash);

const currentEra = (await this.chaindata.getCurrentEra()) || 0;
const lastNominationEra =
(await this.chaindata.getNominatorLastNominationEra(stash)) || 0;
const currentTargets =
Expand Down Expand Up @@ -163,11 +179,14 @@ export default class Nominator extends EventEmitter {
this.signer.address,
);

this._shouldNominate =
bonded > 50 && isBonded && currentEra - lastNominationEra >= 1;

const rewardDestination = await this.payee();
const currentEra = (await this.chaindata.getCurrentEra()) || 0;

const stale = isBonded && currentEra - lastNominationEra > 8;
const status: NominatorStatus = {
status: "Init",
status: this._shouldNominate ? "Initialized" : "Existing Nomination",
bondedAddress: this.bondedAddress,
stashAddress: await this.stash(),
bondedAmount: Number(bonded),
Expand All @@ -182,6 +201,7 @@ export default class Nominator extends EventEmitter {
stale: stale,
dryRun: this._dryRun,
updated: Date.now(),
shouldNominate: this._shouldNominate,
};
this.updateNominatorStatus(status);
this._canNominate = {
Expand Down
1 change: 0 additions & 1 deletion packages/common/src/scorekeeper/Nominating.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ export const doNominations = async (
return null;
}

// ensure the group is sorted by least avg stake
for (const nominator of nominatorGroups) {
const nomStash = await nominator.stash();
const nominatorLastNominated =
Expand Down
12 changes: 9 additions & 3 deletions packages/common/src/scorekeeper/Round.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ export const startRound = async (
if (nominating) return [];
nominating = true;

const filteredNominators = await Promise.all(
nominatorGroups.filter(async (nom) => {
return await nom.shouldNominate();
}),
);

const now = new Date().getTime();

// The nominations sent now won't be active until the next era.
Expand All @@ -48,7 +54,7 @@ export const startRound = async (
`New round is starting! Era ${newEra} will begin new nominations.`,
);

for (const nom of nominatorGroups) {
for (const nom of filteredNominators) {
const nominatorStatus: NominatorStatus = {
status: `Round Started`,
updated: Date.now(),
Expand Down Expand Up @@ -97,7 +103,7 @@ export const startRound = async (
}
}

for (const nom of nominatorGroups) {
for (const nom of filteredNominators) {
const nominatorStatus: NominatorStatus = {
status: `Scoring Candidates...`,
updated: Date.now(),
Expand Down Expand Up @@ -135,7 +141,7 @@ export const startRound = async (
// TODO unit test that assets this value
const numValidatorsNominated = await doNominations(
sortedCandidates,
nominatorGroups,
filteredNominators,
chaindata,
handler,
bot,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ export const mainScorekeeperJob = async (
mainScoreKeeperLabel,
);
const hasOld = await Promise.all(
nominatorGroups.map(async (nom) => {
nominatorGroups.filter(async (nom) => {
const stash = await nom.stash();
if (!stash || stash === "0x") return false;
const lastNominatedEra =
await chaindata.getNominatorLastNominationEra(stash);
return lastNominatedEra < activeEra - eraBuffer;
return lastNominatedEra <= activeEra - 1;
}),
);

Expand Down Expand Up @@ -112,7 +112,7 @@ export const mainScorekeeperJob = async (
nominating,
bot,
constraints,
nominatorGroups,
hasOld,
chaindata,
handler,
config,
Expand Down
12 changes: 9 additions & 3 deletions packages/common/src/scorekeeper/scorekeeper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,17 +246,23 @@ export default class ScoreKeeper {

await setAllIdentities(this.chaindata, scorekeeperLabel);

const filteredNominators = await Promise.all(
this.nominatorGroups.filter(async (nom) => {
return await nom.shouldNominate();
}),
);

// If force round is set in the configs
if (this.config.scorekeeper.forceRound) {
if (this.config.scorekeeper.forceRound || filteredNominators.length > 0) {
logger.info(
`Force Round: ${this.config.scorekeeper.forceRound} starting round....`,
`Force Round: ${this.config.scorekeeper.forceRound} ${filteredNominators.length} nominators old - starting round....`,
scorekeeperLabel,
);
await startRound(
this.nominating,
this.bot,
this.constraints,
this.nominatorGroups,
filteredNominators,
this.chaindata,
this.handler,
this.config,
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.0.24`",
"version": "3.0.25",
"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.0.23",
"version": "3.0.25",
"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.0.24",
"version": "3.0.25",
"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.0.24",
"version": "3.0.25",
"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.0.24g",
"version": "3.0.25",
"description": "Services for running the Thousand Validator Program.",
"main": "build/index.js",
"types": "build/index.d.ts",
Expand Down

0 comments on commit a6c5db4

Please sign in to comment.