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/huge-voting-detection-after-restart #500

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
68 changes: 38 additions & 30 deletions voting-watcher/src/agent-voting-watcher.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import BigNumber from "bignumber.js";

import {
ethers,
BlockEvent,
TransactionEvent,
Finding,
FindingType,
FindingSeverity,
FindingType,
TransactionEvent,
ethers,
} from "forta-agent";

import VOTING_ABI from "./abi/AragonVoting.json";
Expand All @@ -16,15 +16,15 @@ import {
ETH_DECIMALS,
HUGE_VOTE_DISTANCE,
LIDO_VOTING_ADDRESS,
VOTING_BASE_URL,
PINGER_SCHEDULE,
VOTING_BASE_URL,
} from "./constants";

import { ethersProvider } from "./ethers";
import {
abbreviateNumber,
formatLink,
getResultStr,
abbreviateNumber,
secondsToDaysAndHours,
} from "./helpers";

Expand Down Expand Up @@ -52,6 +52,7 @@ interface VoteInfo {
}

let activeVotes: Map<number, VoteInfo> = new Map<number, VoteInfo>();
let prevBlockVotes: Map<number, VoteInfo> = new Map<number, VoteInfo>();
let voteLength: number;
let objectionsTime: number;

Expand All @@ -63,7 +64,20 @@ export async function initialize(
console.log(`[${name}]`);

await updateVotingDurations(currentBlock);
activeVotes = await getActiveVotes(currentBlock, true);
activeVotes = await getActiveVotes(currentBlock);

if (activeVotes.size !== 0) {
// Get previous block votes to correctly compare with the current one after the restart.
prevBlockVotes = await getActiveVotes(currentBlock - 1);
activeVotes.forEach((vote, key) => {
if (!vote) {
return;
}
const prevVote = prevBlockVotes.get(key);
vote.lastReportedTotal = prevVote ? prevVote.total : new BigNumber(0);
activeVotes.set(key, vote);
});
}

return {
activeVotes: Array.from(activeVotes.keys()).toString(),
Expand All @@ -75,7 +89,7 @@ export async function handleBlock(blockEvent: BlockEvent) {

await handleActiveVotes(blockEvent, findings);
await handlePinger(blockEvent, findings);
await handleHugeVotes(blockEvent, findings);
await handleHugeVotes(findings);

return findings;
}
Expand Down Expand Up @@ -126,28 +140,26 @@ async function handleActiveVotes(blockEvent: BlockEvent, findings: Finding[]) {
});
}

async function handleHugeVotes(blockEvent: BlockEvent, findings: Finding[]) {
async function handleHugeVotes(findings: Finding[]) {
Array.from(activeVotes.keys()).forEach((key) => {
const vote = activeVotes.get(key);
if (vote) {
const total = vote.yea.plus(vote.nay);
const lastTotal = vote.lastReportedTotal || new BigNumber(0);
const url = `${VOTING_BASE_URL}${key}`;
BATMAH69 marked this conversation as resolved.
Show resolved Hide resolved
if (total.gte(lastTotal.plus(HUGE_VOTE_DISTANCE)) && !vote.noMorePing) {
if (vote.passed) {
reportHugeVotesWithQuorum(
total.minus(lastTotal),
key,
vote,
findings,
);
vote.noMorePing = true;
} else {
reportHugeVotes(total.minus(lastTotal), key, vote, findings);
}
vote.lastReportedTotal = total;
activeVotes.set(key, vote);
if (!vote) {
return;
}

const total = vote.yea.plus(vote.nay);
const lastTotal = vote.lastReportedTotal || new BigNumber(0);

if (total.gte(lastTotal.plus(HUGE_VOTE_DISTANCE)) && !vote.noMorePing) {
if (vote.passed) {
reportHugeVotesWithQuorum(total.minus(lastTotal), key, vote, findings);
vote.noMorePing = true;
} else {
reportHugeVotes(total.minus(lastTotal), key, vote, findings);
}

vote.lastReportedTotal = total;
activeVotes.set(key, vote);
}
});
}
Expand Down Expand Up @@ -220,7 +232,6 @@ async function updateVotingDurations(block: number) {

async function getActiveVotes(
blockNumber: number,
init: boolean = false,
): Promise<Map<number, VoteInfo>> {
const blockTag = { blockTag: blockNumber };
const block = await ethersProvider.getBlock(blockNumber);
Expand Down Expand Up @@ -276,9 +287,6 @@ async function getActiveVotes(
timeLeft,
resultsStr,
};
if (init) {
voteInfo.lastReportedTotal = total;
}
votesInfo.set(i, voteInfo);
}
return votesInfo;
Expand Down
Loading