fix: correct consensus vote counting to prevent duplicate voter inflation
- Modified getConsensusState() to track votes per voter and ensure each voter only counts once per domain - Added logging to detect when voters have multiple votes for different claimants - Prevents vote count inflation that could cause wrong claimants to win consensus - Maintains most recent vote when duplicates are detected
This commit is contained in:
+22
-4
@@ -294,6 +294,9 @@ async function getConsensusState(domain) {
|
||||
}
|
||||
}
|
||||
|
||||
// Track votes per voter to handle duplicates: voter -> { claimant, entry }
|
||||
const voterVotes = new Map();
|
||||
|
||||
// Collect votes and validate
|
||||
for (const entry of allEntries) {
|
||||
if (entry.key.startsWith(`vote:${domain}:`)) {
|
||||
@@ -301,12 +304,21 @@ async function getConsensusState(domain) {
|
||||
if (parts.length === 4) {
|
||||
const claimant = parts[2];
|
||||
const voter = parts[3];
|
||||
|
||||
|
||||
// Validate vote
|
||||
if (validateVote(claimant, claims)) {
|
||||
voteCounts[claimant]++;
|
||||
voters.add(voter);
|
||||
// Only count vote once
|
||||
// Check if voter already voted
|
||||
if (voterVotes.has(voter)) {
|
||||
const existingVote = voterVotes.get(voter);
|
||||
if (existingVote.claimant !== claimant) {
|
||||
logWarn('Core', `Duplicate vote detected for ${domain}: voter ${voter} voted for ${existingVote.claimant} and ${claimant}. Using most recent vote.`);
|
||||
}
|
||||
}
|
||||
|
||||
// Store/update voter's vote (most recent wins)
|
||||
voterVotes.set(voter, { claimant, entry });
|
||||
|
||||
// Only count vote once for metrics
|
||||
const voteKey = `${domain}:${claimant}:${voter}`;
|
||||
if (!countedVotes.has(voteKey)) {
|
||||
countedVotes.add(voteKey);
|
||||
@@ -318,6 +330,12 @@ async function getConsensusState(domain) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Count votes per claimant (each voter counts only once)
|
||||
for (const [voter, vote] of voterVotes) {
|
||||
voteCounts[vote.claimant]++;
|
||||
voters.add(voter);
|
||||
}
|
||||
|
||||
const activePeers = getActivePeerCount();
|
||||
const config = getConsensusConfig();
|
||||
|
||||
Reference in New Issue
Block a user