Skip to content

Commit

Permalink
Merge pull request #1945 from w3f/will-delegation-score
Browse files Browse the repository at this point in the history
Add Delegation Scoring
  • Loading branch information
wpank authored Aug 29, 2022
2 parents f51c799 + b71a8a2 commit c125a6b
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 27 deletions.
2 changes: 1 addition & 1 deletion charts/otv-backend/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
description: 1K Validators Backend
name: otv-backend
version: v2.6.41
version: v2.6.42
apiVersion: v2
2 changes: 1 addition & 1 deletion charts/otv-backend/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ resources:

image:
repo: web3f/otv-backend
tag: v2.6.41
tag: v2.6.42

certificate:
enabled: true
Expand Down
6 changes: 3 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,20 +97,20 @@ services:
ipv4_address: 172.28.1.6

telemetry-backend-core:
image: parity/substrate-telemetry-backend:09b44ad0-beta
image: parity/substrate-telemetry-backend:latest
environment:
- PORT=8000
ports:
- 8000:8000
expose:
- 8000
command: telemetry_core --listen 0.0.0.0:8000 --expose-node-ips
command: telemetry_core --listen 0.0.0.0:8000 --expose-node-ips --feed-timeout 60
networks:
testing_net:
ipv4_address: 172.28.1.10

telemetry-backend-shard:
image: parity/substrate-telemetry-backend:09b44ad0-beta
image: parity/substrate-telemetry-backend:latest
environment:
- PORT=8001
ports:
Expand Down
4 changes: 2 additions & 2 deletions helmfile.d/10-otv-backend.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ releases:
namespace: kusama
{{ if eq .Environment.Name "production" }}
chart: w3f/otv-backend
version: v2.6.41
version: v2.6.42
{{ else }}
chart: ../charts/otv-backend
{{ end }}
Expand All @@ -27,7 +27,7 @@ releases:
namespace: polkadot
{{ if eq .Environment.Name "production" }}
chart: w3f/otv-backend
version: v2.6.41
version: v2.6.42
{{ else }}
chart: ../charts/otv-backend
{{ end }}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "1k-validators-be",
"version": "2.6.41",
"version": "2.6.42",
"description": "Services for running the Thousand Validator Program.",
"main": "index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/chaindata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -968,7 +968,7 @@ class ChainData {
switch (conviction) {
case "None":
{
effectiveBalance = balance / denom / 0.1;
effectiveBalance = (balance / denom) * 0.1;
}
break;
case "Locked1x":
Expand Down
37 changes: 33 additions & 4 deletions src/constraints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,23 @@ export class OTV implements Constraints {
}
}
if (nominatorStakeValues.length == 0) nominatorStakeValues.push(0);
const nominatorStats = getStats(nominatorStakeValues);

// Delegations
const delegationValues = [];
for (const candidate of validCandidates) {
const delegations = await db.getDelegations(candidate.stash);
if (delegations != undefined) {
const { totalBalance, delegators } = delegations;

let total = 0;
for (const delegator of delegators) {
total += Math.sqrt(delegator.effectiveBalance);
}
delegationValues.push(total);
}
}
const delegationStats = getStats(delegationValues);

// Council Stake
const councilStakeValues = validCandidates.map((candidate) => {
Expand Down Expand Up @@ -697,10 +714,20 @@ export class OTV implements Constraints {
0.95
);
const nominatorStakeScore = scaledNominatorStake * this.BONDED_WEIGHT;
// logger.info(`nominator stake values: ${candidate.stash}`);
// logger.info(JSON.stringify(nominatorStakeValues));
// logger.info(`nominator stake: ${totalNominatorStake}`);
// logger.info(`score: ${nominatorStakeScore} / 50`);

const delegations = await db.getDelegations(candidate.stash);
const { totalBalance, delegators } = delegations;
let totalDelegations = 0;
for (const delegator of delegators) {
totalDelegations += Math.sqrt(delegator.effectiveBalance);
}
const scaledDelegations = scaledDefined(
totalDelegations,
delegationValues,
0.1,
0.95
);
const delegationScore = scaledDelegations * this.BONDED_WEIGHT;

// Score the council backing weight based on what percentage of their staking bond it is
const denom = await this.chaindata.getDenom();
Expand Down Expand Up @@ -767,6 +794,7 @@ export class OTV implements Constraints {
councilStake: councilStakeScore,
democracy: scaledDemocracyScore,
nominatorStake: nominatorStakeScore,
delegations: delegationScore,
randomness: randomness,
updated: Date.now(),
};
Expand All @@ -793,6 +821,7 @@ export class OTV implements Constraints {
score.councilStake,
score.democracy,
score.nominatorStake,
score.delegations,
score.randomness
);

Expand Down
3 changes: 3 additions & 0 deletions src/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1684,6 +1684,7 @@ export default class Db {
councilStake: number,
democracy: number,
nominatorStake: number,
delegations: number,
randomness: number
): Promise<boolean> {
// logger.info(
Expand Down Expand Up @@ -1717,6 +1718,7 @@ export default class Db {
councilStake,
democracy,
nominatorStake,
delegations,
randomness,
});

Expand Down Expand Up @@ -1749,6 +1751,7 @@ export default class Db {
councilStake,
democracy,
nominatorStake,
delegations,
randomness,
}
)
Expand Down
1 change: 1 addition & 0 deletions src/db/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ export const ValidatorScoreSchema = new Schema({
// democracy score
democracy: Number,
nominatorStake: Number,
delegations: Number,
// The randomness factor used to buffer the total
randomness: Number,
});
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { startClearAccumulatedOfflineTimeJob, startMonitorJob } from "./cron";

const isCI = process.env.CI;

const version = "v2.6.41";
const version = "v2.6.42";

const catchAndQuit = async (fn: any) => {
try {
Expand Down
24 changes: 13 additions & 11 deletions src/scorekeeper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,19 +115,21 @@ export const autoNumNominations = async (
let amount = 1;

// Loop until we find the amount of validators that the account can get in.
while (sum < bufferedBalance) {
// An offset so the slice isn't the immediate lowest validators in the set
const offset = 5;
const lowestNum = sorted.slice(offset, offset + amount);
sum = lowestNum.reduce((a, b) => a + b, 0);

if (sum < bufferedBalance) {
amount++;
} else {
amount--;
if (chainType.toString() != "Local Testnet") {
while (sum < bufferedBalance) {
// An offset so the slice isn't the immediate lowest validators in the set
const offset = 5;
const lowestNum = sorted.slice(offset, offset + amount);
sum = lowestNum.reduce((a, b) => a + b, 0);
break;

if (sum < bufferedBalance) {
amount++;
} else {
amount--;
const lowestNum = sorted.slice(offset, offset + amount);
sum = lowestNum.reduce((a, b) => a + b, 0);
break;
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ export default class TelemetryClient {

const options = {
WebSocket: WS,
connectionTimeout: 10000,
maxRetries: 20,
connectionTimeout: 4000,
maxRetries: Infinity,
};

this.socket = new ReconnectingWebSocket(this.host, [], options);
Expand Down

0 comments on commit c125a6b

Please sign in to comment.