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

v3.1.3 - staging #2695

Merged
merged 6 commits into from
Mar 4, 2024
Merged
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
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.1.2
targetRevision: v3.1.3
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.1.2
targetRevision: v3.1.3
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.1.2
appVersion: v3.1.2
version: v3.1.3
appVersion: v3.1.3
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.1.2",
"version": "3.1.3",
"description": "Services for running the Thousand Validator Program.",
"main": "build/index.js",
"types": "build/index.d.ts",
Expand Down
18 changes: 10 additions & 8 deletions packages/common/src/chaindata/queries/Era.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,16 +203,18 @@ export const findEraBlockHash = async (
if (!blockHash) {
return ["", "Block hash is null"];
}
const testEra =
await chaindata?.api?.query.staking.activeEra.at(blockHash);
if (testEra && testEra.isNone) {
logger.info(`Test era is none`);
const apiAt = await chaindata?.api?.at(blockHash);
if (!apiAt) {
return ["", "API at block hash is null"];
}

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 && testEra?.unwrap && testEra?.unwrap().index?.toNumber()
? testEra?.unwrap().index.toNumber()
: 0;
const testEraJSON = testEra.toJSON() as { index: string; start: string };
const testIndex = testEraJSON.index ? Number(testEraJSON.index) : 0;
if (era == testIndex) {
return [blockHash.toString(), null];
}
Expand Down
3 changes: 3 additions & 0 deletions packages/common/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ export const CHAINDATA_SLEEP = 300;

export const API_PROVIDER_TIMEOUT = 10000;

// The number of eras a nominator should wait until making a next nomination
export const NOMINATOR_SHOULD_NOMINATE_ERAS_THRESHOLD = 1;

/// List of Kusama endpoints we can switch between.
export const KusamaEndpoints = [
"wss://kusama-rpc-tn.dwellir.com",
Expand Down
35 changes: 34 additions & 1 deletion packages/common/src/db/queries/Validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
ValidatorSet,
ValidatorSetModel,
} from "../models";
import { allCandidates } from "./Candidate";
import { allCandidates, getIdentityAddresses } from "./Candidate";
import { NextKeys } from "../../chaindata/queries/ValidatorPref";

export const setValidatorSet = async (
Expand Down Expand Up @@ -170,3 +170,36 @@ export const hasBeefyDummy = async (address: string): Promise<boolean> => {
const validator = await getValidator(address);
return validator?.keys?.beefy?.slice(0, 10) == "0x62656566";
};

// TODO: add tests
// Returns the number of eras a validator stash has been active
export const getValidatorActiveEras = async (
stash: string,
): Promise<number> => {
let count = 0;
const validatorSets = await getAllValidatorSets();
for (const era of validatorSets) {
if (era.validators.includes(stash)) {
count++;
}
}
return count;
};

// TODO: add tests
// return the number of eras
export const getIdentityValidatorActiveEras = async (
address: string,
): Promise<number> => {
const identityAddresses = await getIdentityAddresses(address);
let count = 0;
const validatorSets = await getAllValidatorSets();
for (const era of validatorSets) {
if (
era.validators.some((validator) => identityAddresses.includes(validator))
) {
count++;
}
}
return count;
};
124 changes: 124 additions & 0 deletions packages/common/src/nominator/NominatorChainInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import Nominator from "./nominator";
import { queries } from "../index";
import { NOMINATOR_SHOULD_NOMINATE_ERAS_THRESHOLD } from "../constants";

// Query on-chain info for a nominator
export const getNominatorChainInfo = async (nominator: Nominator) => {
const stash = await nominator.stash();
const isBonded = await nominator.chaindata.isBonded(stash);
const [bonded, err] = await nominator.chaindata.getDenomBondedAmount(stash);
const currentBlock = await nominator.chaindata.getLatestBlock();

const currentEra = (await nominator.chaindata.getCurrentEra()) || 0;
const lastNominationEra =
(await nominator.chaindata.getNominatorLastNominationEra(stash)) || 0;
nominator.lastEraNomination = lastNominationEra;
const currentTargets =
(await nominator.chaindata.getNominatorCurrentTargets(stash)) || [];
const currentNamedTargets = await Promise.all(
currentTargets.map(async (target) => {
const kyc = await queries.isKYC(target);
let name = await queries.getIdentityName(target);
if (!name) {
name = (await nominator.chaindata.getFormattedIdentity(target))?.name;
}

const scoreResult = await queries.getLatestValidatorScore(target);
const score = scoreResult && scoreResult.total ? scoreResult.total : 0;

return {
stash: target,
name: name,
kyc: kyc,
score: score,
};
}),
);

const proxyAnnouncements = await queries.getAccountDelayedTx(
nominator.bondedAddress,
);

const namedProxyTargets = await Promise.all(
(proxyAnnouncements || []).map(async (announcement) => {
const namedTargets = await Promise.all(
announcement.targets.map(async (target) => {
const kyc = await queries.isKYC(target);
let name = await queries.getIdentityName(target);

if (!name) {
const formattedIdentity =
await nominator.chaindata.getFormattedIdentity(target);
name = formattedIdentity?.name;
}

const scoreResult = await queries.getLatestValidatorScore(target);
const score =
scoreResult && scoreResult.total ? scoreResult.total : 0;

return {
stash: target,
name: name,
kyc: kyc,
score: score,
};
}),
);
const executionMsTime =
(nominator.proxyDelay + currentBlock - announcement.number) * 6 * 1000;
return {
...announcement,
targets: namedTargets,
executionTime: executionMsTime,
};
}),
);

const shouldNominate =
bonded > 50 &&
isBonded &&
currentEra - lastNominationEra >=
NOMINATOR_SHOULD_NOMINATE_ERAS_THRESHOLD &&
proxyAnnouncements.length == 0;

let status;

if (namedProxyTargets.length > 0) {
`Pending Proxy Execution at #${namedProxyTargets[0].number}`;
} else if (shouldNominate) {
status = "Awaiting New Nomination";
} else if (lastNominationEra == 0) {
status = "Not Nominating Anyone";
} else {
status = `Nominating, last nomination era: ${lastNominationEra} current era: ${currentEra}`;
}

let state;
if (shouldNominate) {
state = "Ready to Nominate";
} else if (namedProxyTargets.length > 0) {
state = "Awaiting Proxy Execution";
} else if (lastNominationEra == 0) {
state = "Not Nominating";
} else if (namedProxyTargets.length == 0 && lastNominationEra > 0) {
status = "Nominated";
}

const stale =
isBonded &&
currentEra - lastNominationEra > 8 &&
proxyAnnouncements.length == 0 &&
bonded > 50;

return {
state: state,
status: status,
isBonded: isBonded,
bondedAmount: Number(bonded),
lastNominationEra: lastNominationEra,
currentTargets: currentNamedTargets,
proxyAnnouncements: namedProxyTargets,
shouldNominate: shouldNominate,
stale: stale,
};
};
18 changes: 11 additions & 7 deletions packages/common/src/nominator/NominatorTx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ export const sendProxyDelayTx = async (
`{Nominator::nominate::proxy} starting tx for ${nominator.address} with proxy delay ${nominator.proxyDelay} blocks`,
nominatorLabel,
);
nominator.updateNominatorStatus({
await nominator.updateNominatorStatus({
state: "Nominating",
status: `[noninate] starting proxy delay tx`,
updated: Date.now(),
stale: false,
Expand All @@ -34,7 +35,7 @@ export const sendProxyDelayTx = async (
`{Nominator::nominate} there was an error getting the current block`,
nominatorLabel,
);
nominator.updateNominatorStatus({
await nominator.updateNominatorStatus({
status: `[noninate] err: no current block`,
updated: Date.now(),
stale: false,
Expand All @@ -55,7 +56,8 @@ export const sendProxyDelayTx = async (
callHash,
};
await queries.addDelayedTx(delayedTx);
nominator.updateNominatorStatus({
await nominator.updateNominatorStatus({
state: "Nominating",
status: `[noninate] tx: ${JSON.stringify(delayedTx)}`,
updated: Date.now(),
stale: false,
Expand All @@ -64,7 +66,8 @@ export const sendProxyDelayTx = async (
const allProxyTxs = await queries.getAllDelayedTxs();

const didSend = await nominator.signAndSendTx(tx);
nominator.updateNominatorStatus({
await nominator.updateNominatorStatus({
state: "Awaiting Proxy Execution",
status: `Announced Proxy Tx: ${didSend}`,
nextTargets: targets,
updated: Date.now(),
Expand All @@ -79,7 +82,7 @@ export const sendProxyDelayTx = async (
nominatorLabel,
);
logger.error(JSON.stringify(e), nominatorLabel);
nominator.updateNominatorStatus({
await nominator.updateNominatorStatus({
status: `Proxy Delay Error: ${JSON.stringify(e)}`,
updated: Date.now(),
});
Expand Down Expand Up @@ -153,7 +156,8 @@ export const sendProxyTx = async (
);
const currentEra = await chaindata.getCurrentEra();

nominator.updateNominatorStatus({
await nominator.updateNominatorStatus({
state: "Awaiting Proxy Execution",
status: "Submitted Proxy Tx",
currentTargets: namedTargets,
updated: Date.now(),
Expand All @@ -172,7 +176,7 @@ export const sendProxyTx = async (
nominatorLabel,
);
logger.error(JSON.stringify(e), nominatorLabel);
nominator.updateNominatorStatus({
await nominator.updateNominatorStatus({
status: `Proxy Error: ${JSON.stringify(e)}`,
updated: Date.now(),
});
Expand Down
Loading