Skip to content

Commit

Permalink
Merge pull request #2879 from w3f/staging
Browse files Browse the repository at this point in the history
Staging
  • Loading branch information
ironoa authored Apr 16, 2024
2 parents 5cbb53c + 8146894 commit d99f8b6
Show file tree
Hide file tree
Showing 27 changed files with 218 additions and 51 deletions.
5 changes: 5 additions & 0 deletions apps/1kv-backend-staging/templates/kusama-otv-backend.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ spec:
"minSelfStake": 10000000000000,
"commission": 150000000,
"unclaimedEraThreshold": 4,
"sanctionedGeoArea": {
"skip": true,
"sanctionedCountries": ["XXX"],
"sanctionedRegions": ["XXX" ]
},
"forceClientVersion": "v1.10.0"
},
"cron": {
Expand Down
5 changes: 5 additions & 0 deletions apps/1kv-backend-staging/templates/polkadot-otv-backend.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ spec:
"minSelfStake": 50000000000000,
"commission": 50000000,
"unclaimedEraThreshold": 1,
"sanctionedGeoArea": {
"skip": true,
"sanctionedCountries": ["XXX"],
"sanctionedRegions": ["XXX" ]
},
"forceClientVersion": "v1.10.0"
},
"cron": {
Expand Down
7 changes: 6 additions & 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.16
targetRevision: v3.2.0
plugin:
env:
- name: HELM_VALUES
Expand Down Expand Up @@ -59,6 +59,11 @@ spec:
"minSelfStake": 10000000000000,
"commission": 150000000,
"unclaimedEraThreshold": 4,
"sanctionedGeoArea": {
"skip": true,
"sanctionedCountries": ["XXX"],
"sanctionedRegions": ["XXX" ]
},
"forceClientVersion": "v1.10.0"
},
"cron": {
Expand Down
7 changes: 6 additions & 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.16
targetRevision: v3.2.0
plugin:
env:
- name: HELM_VALUES
Expand Down Expand Up @@ -58,6 +58,11 @@ spec:
"minSelfStake": 50000000000000,
"commission": 50000000,
"unclaimedEraThreshold": 1,
"sanctionedGeoArea": {
"skip": true,
"sanctionedCountries": ["XXX"],
"sanctionedRegions": ["XXX" ]
},
"forceClientVersion": "v1.10.0"
},
"cron": {
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.16
appVersion: v3.1.16
version: v3.2.0
appVersion: v3.2.0
apiVersion: v2
7 changes: 6 additions & 1 deletion charts/otv-backend/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ config: |
},
"constraints": {
"skipConnectionTime": false,
"skipIdentity": false
"skipIdentity": false,
"sanctionedGeoArea": {
"skip": false,
"sanctionedCountries": ["XXX"],
"sanctionedRegions": ["XXX" ]
}
},
"db": {
"mongo": {
Expand Down
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.16",
"version": "3.2.0",
"description": "Services for running the Thousand Validator Program.",
"main": "build/index.js",
"types": "build/index.d.ts",
Expand Down
5 changes: 5 additions & 0 deletions packages/common/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ export type ConfigSchema = {
minSelfStake: number;
commission: number;
unclaimedEraThreshold: number;
sanctionedGeoArea?: {
skip: boolean;
sanctionedCountries: string[];
sanctionedRegions: string[];
};
};
cron: {
monitor: string;
Expand Down
10 changes: 9 additions & 1 deletion packages/common/src/constraints/CheckCandidates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
checkSelfStake,
checkUnclaimed,
checkValidateIntention,
checkSanctionedGeoArea,
} from "./ValidityChecks";
import { percentage, timeRemaining } from "../utils/util";

Expand Down Expand Up @@ -129,6 +130,12 @@ export const checkCandidate = async (
logger.info(`${candidate.name} beefy keys not valid`, constraintsLabel);
}

const sanctionedGeoAreaValid =
constraints.config?.constraints?.sanctionedGeoArea?.skip == true
? true
: (await checkSanctionedGeoArea(constraints.config, candidate)) ||
false;

valid =
onlineValid &&
validateValid &&
Expand All @@ -142,7 +149,8 @@ export const checkCandidate = async (
blockedValid &&
kusamaValid &&
providerValid &&
beefyValid;
beefyValid &&
sanctionedGeoAreaValid;

await setValid(candidate, valid);

Expand Down
59 changes: 59 additions & 0 deletions packages/common/src/constraints/ValidityChecks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
setSelfStakeInvalidity,
setUnclaimedInvalidity,
setValidateIntentionValidity,
setSanctionedGeoAreaValidity,
} from "../db";
import { ChainData, Config, Constants, queries, Util } from "../index";
import axios from "axios";
Expand Down Expand Up @@ -434,3 +435,61 @@ export const checkBeefyKeys = async (
throw new Error("could not make validity check");
}
};

// Checks if the candidate is in a sanctioned location
export const checkSanctionedGeoArea = async (
config: Config.ConfigSchema,
candidate: Candidate,
): Promise<boolean> => {
try {
if (
!config.constraints?.sanctionedGeoArea?.sanctionedCountries?.length &&
!config.constraints?.sanctionedGeoArea?.sanctionedRegions?.length
) {
await setSanctionedGeoAreaValidity(candidate, true);
return true;
}

const location = await queries.getCandidateLocation(candidate.slotId);
if (!location?.region || !location?.country) {
await setSanctionedGeoAreaValidity(candidate, true);
return true;
}

const sanctionedCountries = config.constraints?.sanctionedGeoArea
?.sanctionedCountries
? config.constraints.sanctionedGeoArea.sanctionedCountries.map((x) =>
x.trim().toLowerCase(),
)
: [];
const sanctionedRegions = config.constraints?.sanctionedGeoArea
?.sanctionedRegions
? config.constraints.sanctionedGeoArea.sanctionedRegions.map((x) =>
x.trim().toLowerCase(),
)
: [];

if (
sanctionedCountries.includes(location.country.trim().toLowerCase()) ||
sanctionedRegions.includes(location.region.trim().toLowerCase())
) {
logger.info(
`${candidate.name} is in a sanctioned location: Country: ${location.country}, Region: ${location.region}`,
{
label: "Constraints",
},
);
await setSanctionedGeoAreaValidity(candidate, false);
return false;
}

await setSanctionedGeoAreaValidity(candidate, true);
return true;
} catch (e) {
logger.error(
`Error checking location for sanctions: ${e}`,
constraintsLabel,
);
throw new Error("could not make validity check");
}
};
2 changes: 2 additions & 0 deletions packages/common/src/db/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export enum InvalidityReasonType {
KUSAMA_RANK = "KUSAMA_RANK",
PROVIDER = "PROVIDER",
BEEFY = "BEEFY",
SANCTIONED_GEO_AREA = "SANCTIONED_GEO_AREA",
}

export interface InvalidityReason {
Expand Down Expand Up @@ -117,6 +118,7 @@ export const InvalidityReason = new Schema({
"KUSAMA_RANK",
"PROVIDER",
"BEEFY",
"SANCTIONED_GEO_AREA",
],
default: "NEW",
},
Expand Down
46 changes: 30 additions & 16 deletions packages/common/src/db/queries/Candidate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ export const getIdentityAddresses = async (
export const setCandidateOnlineValid = async (
candidate: Candidate,
): Promise<void> => {
setCandidateInvalidity(candidate, InvalidityReasonType.ONLINE, true);
await setCandidateInvalidity(candidate, InvalidityReasonType.ONLINE, true);

await CandidateModel.findOneAndUpdate(
{
Expand All @@ -355,7 +355,7 @@ export const setCandidateOnlineNotValid = async (
candidate: Candidate,
): Promise<void> => {
const invalidityMessage = `Candidate ${candidate.name} offline. Offline since ${Date.now()}.`;
setCandidateInvalidity(
await setCandidateInvalidity(
candidate,
InvalidityReasonType.ONLINE,
false,
Expand Down Expand Up @@ -827,7 +827,7 @@ export const setOnlineValidity = async (
): Promise<void> => {
const candidate = await getCandidateBySlotId(slotId);
const invalidityMessage = `${candidate.name} offline. Last seen online ${candidate.onlineSince}.`;
setCandidateInvalidity(
await setCandidateInvalidity(
candidate,
InvalidityReasonType.ONLINE,
isValid,
Expand All @@ -841,7 +841,7 @@ export const setValidateIntentionValidity = async (
isValid: boolean,
): Promise<void> => {
const invalidityMessage = `${candidate.name} does not have a validate intention.`;
setCandidateInvalidity(
await setCandidateInvalidity(
candidate,
InvalidityReasonType.VALIDATE_INTENTION,
isValid,
Expand All @@ -855,7 +855,7 @@ export const setLatestClientReleaseValidity = async (
isValid: boolean,
): Promise<void> => {
const invalidityMessage = `${candidate.name} is not on the latest client version`;
setCandidateInvalidity(
await setCandidateInvalidity(
candidate,
InvalidityReasonType.CLIENT_UPGRADE,
isValid,
Expand All @@ -870,7 +870,7 @@ export const setConnectionTimeInvalidity = async (
isValid: boolean,
): Promise<void> => {
const invalidityMessage = `${candidate.name} has not been connected for minimum length`;
setCandidateInvalidity(
await setCandidateInvalidity(
candidate,
InvalidityReasonType.CONNECTION_TIME,
isValid,
Expand All @@ -888,7 +888,7 @@ export const setIdentityInvalidity = async (
const invalidityMessage = message
? message
: `${candidate.name} has not properly set their identity`;
setCandidateInvalidity(
await setCandidateInvalidity(
candidate,
InvalidityReasonType.IDENTITY,
isValid,
Expand All @@ -903,7 +903,7 @@ export const setOfflineAccumulatedInvalidity = async (
isValid: boolean,
): Promise<void> => {
const invalidityMessage = `${candidate.name} has been offline ${candidate.offlineAccumulated / 1000 / 60} minutes this week.`;
setCandidateInvalidity(
await setCandidateInvalidity(
candidate,
InvalidityReasonType.ACCUMULATED_OFFLINE_TIME,
isValid,
Expand All @@ -919,7 +919,7 @@ export const setRewardDestinationInvalidity = async (
isValid: boolean,
): Promise<void> => {
const invalidityMessage = `${candidate.name} does not have reward destination as Staked`;
setCandidateInvalidity(
await setCandidateInvalidity(
candidate,
InvalidityReasonType.REWARD_DESTINATION,
isValid,
Expand All @@ -937,7 +937,7 @@ export const setCommissionInvalidity = async (
const invalidityMessage = message
? message
: `${candidate.name} has not properly set their commission`;
setCandidateInvalidity(
await setCandidateInvalidity(
candidate,
InvalidityReasonType.COMMISION,
isValid,
Expand All @@ -955,7 +955,7 @@ export const setSelfStakeInvalidity = async (
const invalidityMessage = message
? message
: `${candidate.name} has not properly bonded enough self stake`;
setCandidateInvalidity(
await setCandidateInvalidity(
candidate,
InvalidityReasonType.SELF_STAKE,
isValid,
Expand All @@ -973,7 +973,7 @@ export const setUnclaimedInvalidity = async (
const invalidityMessage = message
? message
: `${candidate.name} has not properly claimed era rewards`;
setCandidateInvalidity(
await setCandidateInvalidity(
candidate,
InvalidityReasonType.UNCLAIMED_REWARDS,
isValid,
Expand All @@ -991,7 +991,7 @@ export const setBlockedInvalidity = async (
const invalidityMessage = message
? message
: `${candidate.name} blocks external nominations`;
setCandidateInvalidity(
await setCandidateInvalidity(
candidate,
InvalidityReasonType.BLOCKED,
isValid,
Expand All @@ -1009,7 +1009,7 @@ export const setProviderInvalidity = async (
const invalidityMessage = message
? message
: `${candidate.name} has banned infrastructure provider`;
setCandidateInvalidity(
await setCandidateInvalidity(
candidate,
InvalidityReasonType.PROVIDER,
isValid,
Expand All @@ -1027,7 +1027,7 @@ export const setKusamaRankInvalidity = async (
const invalidityMessage = message
? message
: `${candidate.name} has not properly claimed era rewards`;
setCandidateInvalidity(
await setCandidateInvalidity(
candidate,
InvalidityReasonType.KUSAMA_RANK,
isValid,
Expand All @@ -1044,7 +1044,7 @@ export const setBeefyKeysInvalidity = async (
const invalidityMessage = message
? message
: `${candidate.name} does not have beefy keys`;
setCandidateInvalidity(
await setCandidateInvalidity(
candidate,
InvalidityReasonType.BEEFY,
isValid,
Expand All @@ -1053,6 +1053,20 @@ export const setBeefyKeysInvalidity = async (
);
};

// Set Sanctions Validity Status
export const setSanctionedGeoAreaValidity = async (
candidate: Candidate,
isValid: boolean,
): Promise<void> => {
const invalidityMessage = `${candidate.name} in sanctioned area.`;
await setCandidateInvalidity(
candidate,
InvalidityReasonType.SANCTIONED_GEO_AREA,
isValid,
invalidityMessage,
);
};

// Sets valid boolean for node
export const setValid = async (
candidate: Candidate,
Expand Down
Loading

0 comments on commit d99f8b6

Please sign in to comment.