From 4ecfdc03ffd207676c5e37ef5a9aa0931a3e2569 Mon Sep 17 00:00:00 2001 From: Ivan Feofanov Date: Thu, 21 Mar 2024 19:35:28 +0400 Subject: [PATCH 1/5] retrieve previous vote state instead of keeping it in memory --- voting-watcher/src/agent-voting-watcher.ts | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/voting-watcher/src/agent-voting-watcher.ts b/voting-watcher/src/agent-voting-watcher.ts index 15ce4d86..88009721 100644 --- a/voting-watcher/src/agent-voting-watcher.ts +++ b/voting-watcher/src/agent-voting-watcher.ts @@ -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"; @@ -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"; @@ -127,12 +127,14 @@ async function handleActiveVotes(blockEvent: BlockEvent, findings: Finding[]) { } async function handleHugeVotes(blockEvent: BlockEvent, findings: Finding[]) { + const prevActiveVotes = await getActiveVotes(blockEvent.blockNumber - 1); Array.from(activeVotes.keys()).forEach((key) => { const vote = activeVotes.get(key); if (vote) { + const previousVote = prevActiveVotes.get(key); + const lastTotal = previousVote ? previousVote.total : new BigNumber(0); const total = vote.yea.plus(vote.nay); - const lastTotal = vote.lastReportedTotal || new BigNumber(0); - const url = `${VOTING_BASE_URL}${key}`; + if (total.gte(lastTotal.plus(HUGE_VOTE_DISTANCE)) && !vote.noMorePing) { if (vote.passed) { reportHugeVotesWithQuorum( From 96fc62e2f389174b6f649454c3cb2aa75070f93f Mon Sep 17 00:00:00 2001 From: Ivan Feofanov Date: Thu, 21 Mar 2024 19:50:35 +0400 Subject: [PATCH 2/5] removed `lastReportedTotal` field --- voting-watcher/src/agent-voting-watcher.ts | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/voting-watcher/src/agent-voting-watcher.ts b/voting-watcher/src/agent-voting-watcher.ts index 88009721..0d861159 100644 --- a/voting-watcher/src/agent-voting-watcher.ts +++ b/voting-watcher/src/agent-voting-watcher.ts @@ -46,7 +46,6 @@ interface VoteInfo { quorumDistance: number; timeLeft: number; resultsStr: string; - lastReportedTotal?: BigNumber; alertLevel?: number; noMorePing?: boolean; } @@ -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(), @@ -132,22 +131,27 @@ async function handleHugeVotes(blockEvent: BlockEvent, findings: Finding[]) { const vote = activeVotes.get(key); if (vote) { const previousVote = prevActiveVotes.get(key); - const lastTotal = previousVote ? previousVote.total : new BigNumber(0); + if (!previousVote) { + // vote was just created + return; + } const total = vote.yea.plus(vote.nay); - 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); } } @@ -222,7 +226,6 @@ async function updateVotingDurations(block: number) { async function getActiveVotes( blockNumber: number, - init: boolean = false, ): Promise> { const blockTag = { blockTag: blockNumber }; const block = await ethersProvider.getBlock(blockNumber); @@ -278,9 +281,6 @@ async function getActiveVotes( timeLeft, resultsStr, }; - if (init) { - voteInfo.lastReportedTotal = total; - } votesInfo.set(i, voteInfo); } return votesInfo; From fbb2225dac7ac8551045c66e09e175391192ab23 Mon Sep 17 00:00:00 2001 From: Ivan Feofanov Date: Fri, 22 Mar 2024 13:50:23 +0400 Subject: [PATCH 3/5] heavy blockchain call moved to `initialize` function --- voting-watcher/src/agent-voting-watcher.ts | 54 ++++++++++++---------- 1 file changed, 29 insertions(+), 25 deletions(-) diff --git a/voting-watcher/src/agent-voting-watcher.ts b/voting-watcher/src/agent-voting-watcher.ts index 0d861159..d54a3e34 100644 --- a/voting-watcher/src/agent-voting-watcher.ts +++ b/voting-watcher/src/agent-voting-watcher.ts @@ -46,11 +46,13 @@ interface VoteInfo { quorumDistance: number; timeLeft: number; resultsStr: string; + lastReportedTotal?: BigNumber; alertLevel?: number; noMorePing?: boolean; } let activeVotes: Map = new Map(); +let prevBlockVotes: Map = new Map(); let voteLength: number; let objectionsTime: number; @@ -64,6 +66,17 @@ export async function initialize( await updateVotingDurations(currentBlock); activeVotes = await getActiveVotes(currentBlock); + // 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(), }; @@ -126,34 +139,25 @@ async function handleActiveVotes(blockEvent: BlockEvent, findings: Finding[]) { } async function handleHugeVotes(blockEvent: BlockEvent, findings: Finding[]) { - const prevActiveVotes = await getActiveVotes(blockEvent.blockNumber - 1); 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); - - if ( - total.gte(previousVote.total.plus(HUGE_VOTE_DISTANCE)) && - !vote.noMorePing - ) { - if (vote.passed) { - reportHugeVotesWithQuorum( - total.minus(previousVote.total), - key, - vote, - findings, - ); - vote.noMorePing = true; - } else { - reportHugeVotes(total.minus(previousVote.total), key, vote, findings); - } - 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); } }); } From 7fe8836f5230a0a6df224a1dea41d5cd6f08ce4d Mon Sep 17 00:00:00 2001 From: Ivan Feofanov Date: Fri, 22 Mar 2024 13:56:54 +0400 Subject: [PATCH 4/5] removed unused arg --- voting-watcher/src/agent-voting-watcher.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/voting-watcher/src/agent-voting-watcher.ts b/voting-watcher/src/agent-voting-watcher.ts index d54a3e34..f6d1bacc 100644 --- a/voting-watcher/src/agent-voting-watcher.ts +++ b/voting-watcher/src/agent-voting-watcher.ts @@ -87,7 +87,7 @@ export async function handleBlock(blockEvent: BlockEvent) { await handleActiveVotes(blockEvent, findings); await handlePinger(blockEvent, findings); - await handleHugeVotes(blockEvent, findings); + await handleHugeVotes(findings); return findings; } @@ -138,7 +138,7 @@ 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) { From 6134bf41009355ef6b7bf7ba10affa0fcb859c22 Mon Sep 17 00:00:00 2001 From: Ivan Feofanov Date: Fri, 22 Mar 2024 14:36:36 +0400 Subject: [PATCH 5/5] request prev block only if there are active votes --- voting-watcher/src/agent-voting-watcher.ts | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/voting-watcher/src/agent-voting-watcher.ts b/voting-watcher/src/agent-voting-watcher.ts index f6d1bacc..36f489cc 100644 --- a/voting-watcher/src/agent-voting-watcher.ts +++ b/voting-watcher/src/agent-voting-watcher.ts @@ -66,16 +66,18 @@ export async function initialize( await updateVotingDurations(currentBlock); activeVotes = await getActiveVotes(currentBlock); - // 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); - }); + 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(),