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 2 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
36 changes: 19 additions & 17 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 All @@ -46,7 +46,6 @@ interface VoteInfo {
quorumDistance: number;
timeLeft: number;
resultsStr: string;
lastReportedTotal?: BigNumber;
alertLevel?: number;
noMorePing?: boolean;
}
Expand All @@ -63,7 +62,7 @@ export async function initialize(
console.log(`[${name}]`);

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

return {
activeVotes: Array.from(activeVotes.keys()).toString(),
Expand Down Expand Up @@ -127,25 +126,32 @@ async function handleActiveVotes(blockEvent: BlockEvent, findings: Finding[]) {
}

async function handleHugeVotes(blockEvent: BlockEvent, findings: Finding[]) {
const prevActiveVotes = await getActiveVotes(blockEvent.blockNumber - 1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to reduce call count?

Array.from(activeVotes.keys()).forEach((key) => {
const vote = activeVotes.get(key);
if (vote) {
const previousVote = prevActiveVotes.get(key);
if (!previousVote) {
// vote was just created
return;
}
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 (
total.gte(previousVote.total.plus(HUGE_VOTE_DISTANCE)) &&
!vote.noMorePing
) {
if (vote.passed) {
reportHugeVotesWithQuorum(
total.minus(lastTotal),
total.minus(previousVote.total),
key,
vote,
findings,
);
vote.noMorePing = true;
} else {
reportHugeVotes(total.minus(lastTotal), key, vote, findings);
reportHugeVotes(total.minus(previousVote.total), key, vote, findings);
}
vote.lastReportedTotal = total;
activeVotes.set(key, vote);
}
}
Expand Down Expand Up @@ -220,7 +226,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 +281,6 @@ async function getActiveVotes(
timeLeft,
resultsStr,
};
if (init) {
voteInfo.lastReportedTotal = total;
}
votesInfo.set(i, voteInfo);
}
return votesInfo;
Expand Down
Loading