Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: invalidate proposal score when out-of-range #280

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ router.get('/scores/:proposalId', async (req, res) => {
try {
const result = await serve(proposalId, updateProposalAndVotes, [proposalId]);
return res.json({ result });
} catch (e) {
} catch (e: any) {
capture(e);
log.warn(`[api] updateProposalAndVotes() failed ${proposalId}, ${JSON.stringify(e)}`);
return res.json({ error: 'failed', message: e });
return res.json({ error: 'failed', message: e.message || e });
}
});

Expand Down
27 changes: 22 additions & 5 deletions src/scores.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ async function updateProposalScores(proposalId: string, scores: any, votes: numb
]);
}

async function invalidateProposalScore(proposalId: string) {
const query = `UPDATE proposals SET scores_state = ? WHERE id = ? LIMIT 1;`;
await db.queryAsync(query, ['invalid', proposalId]);
}

const pendingRequests = {};

export async function updateProposalAndVotes(proposalId: string, force = false) {
Expand All @@ -114,7 +119,7 @@ export async function updateProposalAndVotes(proposalId: string, force = false)
(proposal.votes > 30000 && proposal.scores_updated > ts - 300) ||
pendingRequests[proposalId]
) {
console.log(
log.info(
'ignore score calculation',
proposal.space,
proposalId,
Expand Down Expand Up @@ -171,10 +176,22 @@ export async function updateProposalAndVotes(proposalId: string, force = false)
if (!isFinal) await updateVotesVp(votes, vpState, proposalId);

// Store scores
await updateProposalScores(proposalId, results, votes.length);
log.info(
`[scores] Proposal updated ${proposal.id}, ${proposal.space}, ${results.scores_state}, ${votes.length}`
);
try {
await updateProposalScores(proposalId, results, votes.length);
log.info(
`[scores] Proposal updated ${proposal.id}, ${proposal.space}, ${results.scores_state}, ${votes.length}`
);
} catch (e: any) {
if (e.code === 'ER_WARN_DATA_OUT_OF_RANGE') {
log.info(`[scores] Invalid final scores_total: ${results.scores_total}`, e);
if (proposal.state === 'closed') {
await invalidateProposalScore(proposalId);
}
throw new Error('Invalid out of range score');
} else {
throw e;
}
}

delete pendingRequests[proposalId];
return true;
Expand Down