Higher precision of referenda graphs (#10690)

* the fix

* same fix for extended graph as well

* lint
This commit is contained in:
Przemek Rzad 2024-06-28 17:03:21 +02:00 committed by GitHub
parent 2cd0078cda
commit eb5574af17
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 6 deletions

View File

@ -141,13 +141,16 @@ function getChartResult (totalEligible: BN, isConvictionVote: boolean, info: Pal
supx = (supn || supx !== -1) ? supx : i;
}
const step = x[1].sub(x[0]);
// Bringing it to a higher precision.
// Otherwise, graphs with short periods (on dev chains) are invalid.
const stepWithPrecision = x[x.length - 1].sub(x[0]).muln(100).divn(x.length);
const lastIndex = x.length - 1;
const lastBlock = endConfirm?.add(track.minEnactmentPeriod);
// if the confirmation end is later than shown on our graph, we extend it
if (lastBlock?.gt(since.add(x[lastIndex]))) {
let currentBlock = x[lastIndex].add(since).add(step);
let currentBlockWithPrecision = x[lastIndex].add(since).muln(100).add(stepWithPrecision);
let currentBlock = currentBlockWithPrecision.divn(100);
do {
labels.push(formatNumber(currentBlock));
@ -163,7 +166,8 @@ function getChartResult (totalEligible: BN, isConvictionVote: boolean, info: Pal
values[1][1].push(values[1][1][lastIndex]);
values[1][2].push(values[1][2][lastIndex]);
currentBlock = currentBlock.add(step);
currentBlockWithPrecision = currentBlockWithPrecision.add(stepWithPrecision);
currentBlock = currentBlockWithPrecision.divn(100);
} while (currentBlock.lt(lastBlock));
}

View File

@ -227,16 +227,20 @@ export function calcCurves ({ decisionPeriod, minApproval, minSupport }: PalletR
const approval = new Array<BN>(CURVE_LENGTH);
const support = new Array<BN>(CURVE_LENGTH);
const x = new Array<BN>(CURVE_LENGTH);
const step = decisionPeriod.divn(CURVE_LENGTH);
const last = CURVE_LENGTH - 1;
let current = new BN(0);
// Bringing it to a higher precision before dividing by curve length.
// Otherwise, graphs with short periods (on dev chains) are invalid.
const stepWithPrecision = decisionPeriod.muln(100).divn(CURVE_LENGTH);
let currentWithPrecision = new BN(0);
for (let i = 0; i < last; i++) {
const current = currentWithPrecision.divn(100);
approval[i] = curveThreshold(minApproval, current, decisionPeriod);
support[i] = curveThreshold(minSupport, current, decisionPeriod);
x[i] = current;
current = current.add(step);
currentWithPrecision = currentWithPrecision.add(stepWithPrecision);
}
// since we may be lossy with the step, we explicitly calc the final point at 100%