Skip to content

Commit

Permalink
Merge pull request #2677 from w3f/will-filtered
Browse files Browse the repository at this point in the history
bump version to v3.0.26
  • Loading branch information
wpank authored Mar 2, 2024
2 parents a6c5db4 + d765048 commit 3b2649f
Show file tree
Hide file tree
Showing 17 changed files with 158 additions and 75 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.25
targetRevision: v3.0.26
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.25
targetRevision: v3.0.26
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.25
appVersion: v3.0.25
version: v3.0.26
appVersion: v3.0.26
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.25",
"version": "3.0.26",
"description": "Services for running the Thousand Validator Program.",
"main": "build/index.js",
"types": "build/index.d.ts",
Expand Down
50 changes: 23 additions & 27 deletions packages/common/src/ApiHandler/ApiHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,21 +87,19 @@ class ApiHandler extends EventEmitter {
);

wsProvider.on("disconnected", async () => {
logger.warn(
`WS provider for rpc ${endpoints[0]} disconnected!`,
apiLabel,
);
if (!this.healthCheckInProgress) {
try {
const isHealthy = await this.healthCheck();
logger.info(
`[Disconnection] ${this._currentEndpoint}} Health check result: ${isHealthy}`,
apiLabel,
);
resolve(wsProvider);
} catch (error: any) {
reject(error);
}
try {
const isHealthy = await this.healthCheck();
logger.info(
`[Disconnection] ${this._currentEndpoint}} Health check result: ${isHealthy}`,
apiLabel,
);
resolve(wsProvider);
} catch (error: any) {
logger.warn(
`WS provider for rpc ${endpoints[0]} disconnected!`,
apiLabel,
);
reject(error);
}
});
wsProvider.on("connected", () => {
Expand All @@ -110,18 +108,16 @@ class ApiHandler extends EventEmitter {
resolve(wsProvider);
});
wsProvider.on("error", async () => {
logger.error(`Error thrown for rpc ${this._endpoints[0]}`, apiLabel);
if (!this.healthCheckInProgress) {
try {
const isHealthy = await this.healthCheck();
logger.info(
`[Error] ${this._currentEndpoint} Health check result: ${isHealthy}`,
apiLabel,
);
resolve(wsProvider);
} catch (error: any) {
reject(error);
}
try {
const isHealthy = await this.healthCheck();
logger.info(
`[Error] ${this._currentEndpoint} Health check result: ${isHealthy}`,
apiLabel,
);
resolve(wsProvider);
} catch (error: any) {
logger.error(`Error thrown for rpc ${this._endpoints[0]}`, apiLabel);
reject(error);
}
});
});
Expand Down
9 changes: 1 addition & 8 deletions packages/common/src/chaindata/chaindata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { ApiPromise } from "@polkadot/api";
import ApiHandler from "../ApiHandler/ApiHandler";
import logger from "../logger";
import { NumberResult } from "../types";
import { sleep } from "../utils/util";
import {
getApiAt,
getApiAtBlockHash,
Expand Down Expand Up @@ -64,7 +63,7 @@ import {
getNominators,
NominatorInfo,
} from "./queries/Nomination";
import { CHAINDATA_RETRIES, CHAINDATA_SLEEP } from "../constants";
import { CHAINDATA_RETRIES } from "../constants";
import { Identity } from "../db";
import { Block } from "@polkadot/types/interfaces";
import { ApiDecoration } from "@polkadot/api/types";
Expand All @@ -85,12 +84,6 @@ export class ChainData {
checkApiConnection = async (retries = 0): Promise<boolean> => {
if (!this.handler.getApi()?.isConnected) {
if (retries < CHAINDATA_RETRIES) {
// logger.warn(
// `Retries: ${retries} - API is not connected, waiting...`,
// chaindataLabel,
// );
await sleep(CHAINDATA_SLEEP);

retries++;
return await this.checkApiConnection(retries);
} else {
Expand Down
4 changes: 2 additions & 2 deletions packages/common/src/chaindata/queries/ValidatorPref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ export const getBlocked = async (
// bondedAddress - formerly controller
export const isBonded = async (
chaindata: ChainData,
bondedAddress: string,
stash: string,
): Promise<boolean> => {
try {
if (!(await chaindata.checkApiConnection())) {
return false;
}
const bonded = await chaindata?.api?.query.staking.bonded(bondedAddress);
const bonded = await chaindata?.api?.query.staking.bonded(stash);
if (bonded) {
return bonded.isSome;
} else {
Expand Down
19 changes: 17 additions & 2 deletions packages/common/src/nominator/NominatorTx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ export const sendProxyDelayTx = async (
`{Nominator::nominate::proxy} starting tx for ${nominator.address} with proxy delay ${nominator.proxyDelay} blocks`,
nominatorLabel,
);
nominator.updateNominatorStatus({
status: `[noninate] starting proxy delay tx`,
updated: Date.now(),
stale: false,
});

const innerTx = api?.tx.staking.nominate(targets);

Expand All @@ -29,6 +34,11 @@ export const sendProxyDelayTx = async (
`{Nominator::nominate} there was an error getting the current block`,
nominatorLabel,
);
nominator.updateNominatorStatus({
status: `[noninate] err: no current block`,
updated: Date.now(),
stale: false,
});
return false;
}
const callHash = innerTx.method.hash.toString();
Expand All @@ -45,12 +55,17 @@ export const sendProxyDelayTx = async (
callHash,
};
await queries.addDelayedTx(delayedTx);
nominator.updateNominatorStatus({
status: `[noninate] tx: ${JSON.stringify(delayedTx)}`,
updated: Date.now(),
stale: false,
});

const allProxyTxs = await queries.getAllDelayedTxs();

const didSend = await nominator.signAndSendTx(tx);
nominator.updateNominatorStatus({
status: "Announced New Tx",
status: `Announced Proxy Tx: ${didSend}`,
nextTargets: targets,
updated: Date.now(),
stale: false,
Expand All @@ -63,7 +78,7 @@ export const sendProxyDelayTx = async (
`{Nominator::nominate} there was an error sending the tx`,
nominatorLabel,
);
logger.error(JSON.stringify(e));
logger.error(JSON.stringify(e), nominatorLabel);
nominator.updateNominatorStatus({
status: `Proxy Delay Error: ${JSON.stringify(e)}`,
updated: Date.now(),
Expand Down
76 changes: 66 additions & 10 deletions packages/common/src/nominator/nominator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ export default class Nominator extends EventEmitter {
reason: "",
};

public lastEraNomination: number;

public _shouldNominate = false;

private _status: NominatorStatus = {
Expand Down Expand Up @@ -137,11 +139,14 @@ export default class Nominator extends EventEmitter {
const stash = await this.stash();
const isBonded = await this.chaindata.isBonded(stash);
const [bonded, err] = await this.chaindata.getDenomBondedAmount(stash);
const proxyTxs = await queries.getAccountDelayedTx(this.bondedAddress);

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

Expand All @@ -154,6 +159,7 @@ export default class Nominator extends EventEmitter {
const currentEra = (await this.chaindata.getCurrentEra()) || 0;
const lastNominationEra =
(await this.chaindata.getNominatorLastNominationEra(stash)) || 0;
this.lastEraNomination = lastNominationEra;
const currentTargets =
(await this.chaindata.getNominatorCurrentTargets(stash)) || [];
const currentNamedTargets = await Promise.all(
Expand Down Expand Up @@ -273,11 +279,12 @@ export default class Nominator extends EventEmitter {
return "";
}
try {
const isBonded = await this.chaindata.isBonded(this.bondedAddress);
const stash = await this.stash();
const isBonded = await this.chaindata.isBonded(stash);
if (!isBonded) {
return "";
}
const stash = await this.stash();

const rewardDestination =
await this.chaindata.getRewardDestination(stash);
if (rewardDestination) {
Expand All @@ -301,21 +308,37 @@ export default class Nominator extends EventEmitter {
try {
if (this._dryRun) {
logger.info(`DRY RUN ENABLED, SKIPPING TX`, nominatorLabel);
this.updateNominatorStatus({
status: `[signAndSend] DRY RUN TX`,
updated: Date.now(),
stale: false,
});
return false;
} else {
logger.info(`Sending tx: ${tx.method.toString()}`, nominatorLabel);
await tx.signAndSend(this.signer);
this.updateNominatorStatus({
status: `[signAndSend] signed and sent tx`,
updated: Date.now(),
stale: false,
});
}

return true;
} catch (e) {
logger.error(`Error sending tx: `, nominatorLabel);
logger.error(JSON.stringify(e), nominatorLabel);
this.updateNominatorStatus({
status: `[signAndSend] Error signing and sending tx: ${JSON.stringify(e)}`,
updated: Date.now(),
stale: false,
});
return false;
}
}

public async nominate(targets: Types.Stash[]): Promise<boolean> {
logger.info(`noninate start top of noninate`, nominatorLabel);
try {
const now = new Date().getTime();

Expand All @@ -325,18 +348,47 @@ export default class Nominator extends EventEmitter {
return false;
}

const currentEra = await this.chaindata.getCurrentEra();
const nominatorStatus: NominatorStatus = {
status: `[nominate] start`,
updated: Date.now(),
stale: false,
};

this.updateNominatorStatus(nominatorStatus);
let isBonded;
try {
const isBonded = await this.chaindata.isBonded(this.bondedAddress);
const stash = await this.stash();
isBonded = await this.chaindata.isBonded(stash);
if (!isBonded) return false;
} catch (e) {
logger.error(`Error checking if ${this.bondedAddress} is bonded: ${e}`);
return false;
}
logger.info(`nominator is bonded: ${isBonded}`, nominatorLabel);

this.updateNominatorStatus({
status: `[nominate] bonded; ${isBonded}`,
updated: Date.now(),
stale: false,
});
let tx: SubmittableExtrinsic<"promise">;

logger.info(
`[nominate] proxy ${this._isProxy}; delay ${this._proxyDelay}`,
nominatorLabel,
);
logger.info(
`[nominate] is delay and greater than 0 ${this._isProxy && this._proxyDelay > 0}`,
nominatorLabel,
);
// Start an announcement for a delayed proxy tx
if (this._isProxy && this._proxyDelay > 0) {
this.updateNominatorStatus({
status: `[nominate] proxy ${this._isProxy}; delay ${this._proxyDelay}`,
updated: Date.now(),
stale: false,
});
logger.info(
`Starting a delayed proxy tx for ${this.bondedAddress}`,
nominatorLabel,
Expand All @@ -358,7 +410,7 @@ export default class Nominator extends EventEmitter {
tx = api.tx.staking.nominate(targets);
await this.sendStakingTx(tx, targets);
}

await queries.setLastNominatedEraIndex(currentEra);
return true;
} catch (e) {
logger.error(`Error nominating: ${e}`, nominatorLabel);
Expand Down Expand Up @@ -419,14 +471,18 @@ export default class Nominator extends EventEmitter {
targets.map(async (target) => {
const kyc = await queries.isKYC(target);
let name = await queries.getIdentityName(target);

// Fetch name using chaindata.getFormattedIdentity only if the name wasn't found initially
if (!name) {
name = (await this.chaindata.getFormattedIdentity(target))?.name;
const formattedIdentity =
await this.chaindata.getFormattedIdentity(target);
name = formattedIdentity?.name;
}

return {
stash: target,
name: name,
kyc: kyc,
name, // shorthand for name: name
kyc, // shorthand for kyc: kyc
score: 0,
};
}),
Expand Down
9 changes: 9 additions & 0 deletions packages/common/src/scorekeeper/Nominating.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ export const doNominations = async (
return null;
}

for (const nom of nominatorGroups) {
const nominatorStatus: NominatorStatus = {
status: `Doing Nominations.....`,
updated: Date.now(),
stale: false,
};
nom.updateNominatorStatus(nominatorStatus);
}

const allTargets = candidates.map((c) => {
return { stash: c.stash };
});
Expand Down
Loading

0 comments on commit 3b2649f

Please sign in to comment.