Skip to content

Commit

Permalink
added recalculation of totalEnabledApprovalWeight to the resetSchedul…
Browse files Browse the repository at this point in the history
…e action
  • Loading branch information
bt-cryptomancer committed Jun 28, 2024
1 parent ccab16b commit f03d455
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions contracts/witnesses.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,26 @@ const GOVERNANCE_TOKEN_PRECISION = '${CONSTANTS.GOVERNANCE_TOKEN_PRECISION}$';
// eslint-disable-next-line no-template-curly-in-string
const GOVERNANCE_TOKEN_MIN_VALUE = '${CONSTANTS.GOVERNANCE_TOKEN_MIN_VALUE}$';

const recalcTotalEnabledApprovalWeight = async () => {
let totalEnabledApprovalWeight = '0';
let offset = 0;
let wits;

do {
wits = await api.db.find('witnesses', {}, 1000, offset, [{ index: '_id', descending: false }]);
for (let i = 0; i < wits.length; i += 1) {
const wit = wits[i];
if (wit.enabled) {
totalEnabledApprovalWeight = api.BigNumber(totalEnabledApprovalWeight)
.plus(wit.approvalWeight.$numberDecimal).toFixed(GOVERNANCE_TOKEN_PRECISION);
}
}
offset += 1000;
} while (wits.length === 1000);

return totalEnabledApprovalWeight;
};

actions.createSSC = async () => {
const tableExists = await api.db.tableExists('witnesses');

Expand Down Expand Up @@ -58,21 +78,7 @@ actions.createSSC = async () => {
const params = await api.db.findOne('params', {});
// This should be removed after being deployed
if (!params.totalEnabledApprovalWeight || params.totalEnabledApprovalWeight === 'NaN') {
let totalEnabledApprovalWeight = '0';
let offset = 0;
let wits;
do {
wits = await api.db.find('witnesses', {}, 1000, offset, [{ index: '_id', descending: false }]);
for (let i = 0; i < wits.length; i += 1) {
const wit = wits[i];
if (wit.enabled) {
totalEnabledApprovalWeight = api.BigNumber(totalEnabledApprovalWeight)
.plus(wit.approvalWeight.$numberDecimal).toFixed(GOVERNANCE_TOKEN_PRECISION);
}
}
offset += 1000;
} while (wits.length === 1000);
params.totalEnabledApprovalWeight = totalEnabledApprovalWeight;
params.totalEnabledApprovalWeight = await recalcTotalEnabledApprovalWeight();
await api.db.update('params', params);
}
// End block to remove
Expand All @@ -93,6 +99,7 @@ actions.resetSchedule = async () => {
params.currentWitness = null;
params.blockNumberWitnessChange = 0;
params.lastWitnesses = [];
params.totalEnabledApprovalWeight = await recalcTotalEnabledApprovalWeight();
await api.db.update('params', params);
};

Expand Down

0 comments on commit f03d455

Please sign in to comment.