From 4cf1375f63e9a57b6d44fd9dde79cb7022348556 Mon Sep 17 00:00:00 2001 From: onmax Date: Tue, 19 Nov 2024 07:55:21 -0600 Subject: [PATCH 1/8] chore: compute dominance size --- .../nimiq-validators-trustscore/src/score.ts | 23 +++++-------------- server/utils/validators.ts | 8 ++++++- 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/packages/nimiq-validators-trustscore/src/score.ts b/packages/nimiq-validators-trustscore/src/score.ts index bdb1e75..eb49fbf 100644 --- a/packages/nimiq-validators-trustscore/src/score.ts +++ b/packages/nimiq-validators-trustscore/src/score.ts @@ -1,7 +1,6 @@ import type { ScoreParams, ScoreValues } from './types' -import { defu } from 'defu' -export function getDominance({ threshold, steepness, dominanceRatio }: ScoreParams['dominance']) { +export function getDominance({ threshold = 0.25, steepness = 4, dominanceRatio }: ScoreParams['dominance']) { if (!threshold || !steepness || !dominanceRatio) throw new Error('Balance, threshold, steepness, or total balance is not set') if (dominanceRatio < 0 || dominanceRatio > 1) @@ -10,7 +9,7 @@ export function getDominance({ threshold, steepness, dominanceRatio }: ScorePara return s } -export function getAvailability({ activeEpochStates, weightFactor }: ScoreParams['availability']) { +export function getAvailability({ activeEpochStates, weightFactor = 0.5 }: ScoreParams['availability']) { if (!activeEpochStates || !weightFactor || activeEpochStates.length === 0) throw new Error(`Invalid availability params: ${JSON.stringify({ activeEpochStates, weightFactor })}`) @@ -32,7 +31,7 @@ export function getAvailability({ activeEpochStates, weightFactor }: ScoreParams return availability } -export function getReliability({ inherentsPerEpoch, weightFactor, curveCenter }: ScoreParams['reliability']) { +export function getReliability({ inherentsPerEpoch, weightFactor = 0.5, curveCenter = 0.16 }: ScoreParams['reliability']) { if (!inherentsPerEpoch || !weightFactor || !curveCenter) throw new Error(`Invalid reliability params: ${JSON.stringify({ inherentsPerEpoch, weightFactor, curveCenter })}`) @@ -68,20 +67,10 @@ export function getReliability({ inherentsPerEpoch, weightFactor, curveCenter }: return Math.max(-curveCenter + 1 - Math.sqrt(discriminant), 1) } -// The default values for the computeScore function -// Negative values and empty arrays are used to indicate that the user must provide the values or an error will be thrown -const defaultScoreParams: ScoreParams = { - dominance: { threshold: 0.25, steepness: 4, dominanceRatio: -1 }, - availability: { weightFactor: 0.5, activeEpochStates: [] }, - reliability: { weightFactor: 0.5, curveCenter: -0.16, inherentsPerEpoch: new Map() }, -} - export function computeScore(params: ScoreParams) { - const computeScoreParams = defu(params, defaultScoreParams) - - const dominance = getDominance(computeScoreParams.dominance) - const availability = getAvailability(computeScoreParams.availability) - const reliability = getReliability(computeScoreParams.reliability) + const dominance = getDominance(params.dominance) + const availability = getAvailability(params.availability) + const reliability = getReliability(params.reliability) const total = dominance * availability * reliability const score: ScoreValues = { dominance, availability, reliability, total } diff --git a/server/utils/validators.ts b/server/utils/validators.ts index 592c966..8cb129c 100644 --- a/server/utils/validators.ts +++ b/server/utils/validators.ts @@ -6,6 +6,7 @@ import { readdir, readFile } from 'node:fs/promises' import path from 'node:path' import { consola } from 'consola' import { desc, inArray, isNotNull } from 'drizzle-orm' +import { getDominance } from 'nimiq-validators-trustscore' import { getBrandingParameters } from './branding' import { defaultValidatorJSON, validatorSchema } from './schemas' @@ -192,10 +193,15 @@ export async function fetchValidators(params: FetchValidatorsOptions): Result { + const dominance = v.score?.dominance // @ts-expect-error The wallet expects a score object, but until these values are stable, we will use null v.score = nullScore + if (dominance) + // @ts-expect-error The wallet expects a score object, but until these values are stable, we will use null + v.score.dominance = dominance + else if (v.dominanceRatio) // @ts-expect-error The wallet expects a score object, but until these values are stable, we will use null - v.score.dominance = v.dominanceRatio + v.score = { ...nullScore, dominance: getDominance(v.dominanceRatio) } }) return { data: validators, error: undefined } From b96ae45bc0bcc26b62b21e2c5c94c6c546e217e7 Mon Sep 17 00:00:00 2001 From: onmax Date: Tue, 19 Nov 2024 07:59:00 -0600 Subject: [PATCH 2/8] chore: readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f3289bb..ae5d7e8 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@

-Details of validators in the Nimiq Blockchain and their scores, calculated using Nimiq's VTS algorithm. + Details of validators in the Nimiq Blockchain and their scores, calculated using Nimiq's VTS algorithm.

[![Sync Mainnet](https://github.com/nimiq/validators-api/actions/workflows/sync-mainnet.yml/badge.svg)](https://github.com/nimiq/validators-api/actions/workflows/sync-mainnet.yml) From 0577161c52e5560059703cf2e8ad7a9243feec81 Mon Sep 17 00:00:00 2001 From: Alberto Monterroso <14013679+Albermonte@users.noreply.github.com> Date: Tue, 19 Nov 2024 08:02:08 -0600 Subject: [PATCH 3/8] fix: correctly calculate dominance with dominanceRatio --- server/utils/validators.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/utils/validators.ts b/server/utils/validators.ts index 8cb129c..348fecf 100644 --- a/server/utils/validators.ts +++ b/server/utils/validators.ts @@ -201,7 +201,7 @@ export async function fetchValidators(params: FetchValidatorsOptions): Result Date: Tue, 19 Nov 2024 08:08:46 -0600 Subject: [PATCH 4/8] chore: return both dominance ratios --- server/utils/validators.ts | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/server/utils/validators.ts b/server/utils/validators.ts index 348fecf..cb2e513 100644 --- a/server/utils/validators.ts +++ b/server/utils/validators.ts @@ -127,7 +127,8 @@ export type FetchValidatorsOptions = Zod.infer & { addre type FetchedValidator = Omit & { logo?: string score?: Score - dominanceRatio?: number + dominanceRatioViaBalance?: number + dominanceRatioViaSlots?: number } export async function fetchValidators(params: FetchValidatorsOptions): Result { @@ -162,11 +163,8 @@ export async function fetchValidators(params: FetchValidatorsOptions): Result` - COALESCE( - NULLIF(${tables.activity.dominanceRatioViaBalance}, -1), - NULLIF(${tables.activity.dominanceRatioViaSlots}, -1) - )`, + dominanceRatioViaBalance: tables.activity.dominanceRatioViaBalance, + dominanceRatioViaSlots: tables.activity.dominanceRatioViaSlots, }) .from(tables.validators) .where(and(...filters)) @@ -199,9 +197,9 @@ export async function fetchValidators(params: FetchValidatorsOptions): Result Date: Tue, 19 Nov 2024 08:14:35 -0600 Subject: [PATCH 5/8] chore: fixed build --- server/utils/validators.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/server/utils/validators.ts b/server/utils/validators.ts index cb2e513..a53798e 100644 --- a/server/utils/validators.ts +++ b/server/utils/validators.ts @@ -197,9 +197,12 @@ export async function fetchValidators(params: FetchValidatorsOptions): Result Date: Tue, 19 Nov 2024 08:21:48 -0600 Subject: [PATCH 6/8] fix: new curves for size --- packages/nimiq-validators-trustscore/src/score.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/nimiq-validators-trustscore/src/score.ts b/packages/nimiq-validators-trustscore/src/score.ts index eb49fbf..c2957bb 100644 --- a/packages/nimiq-validators-trustscore/src/score.ts +++ b/packages/nimiq-validators-trustscore/src/score.ts @@ -1,6 +1,6 @@ import type { ScoreParams, ScoreValues } from './types' -export function getDominance({ threshold = 0.25, steepness = 4, dominanceRatio }: ScoreParams['dominance']) { +export function getDominance({ threshold = 0.15, steepness = 7.5, dominanceRatio }: ScoreParams['dominance']) { if (!threshold || !steepness || !dominanceRatio) throw new Error('Balance, threshold, steepness, or total balance is not set') if (dominanceRatio < 0 || dominanceRatio > 1) From 7f62d4a407ebb38fc92915548fa27ff7ed77a1cd Mon Sep 17 00:00:00 2001 From: onmax Date: Tue, 19 Nov 2024 08:29:04 -0600 Subject: [PATCH 7/8] chore: fixed css --- app/components/ScorePie.vue | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/components/ScorePie.vue b/app/components/ScorePie.vue index 2433a0c..711d8fb 100644 --- a/app/components/ScorePie.vue +++ b/app/components/ScorePie.vue @@ -35,7 +35,7 @@ const strokeColor = computed(() => {