Skip to content

Commit

Permalink
fix: fixed finished proposals notifications (#84)
Browse files Browse the repository at this point in the history
* fix: fixed finished proposals notifications

* chore: less verbosity

* chore: fixed test
  • Loading branch information
freak12techno committed May 11, 2024
1 parent 89b29a3 commit a03e92d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 42 deletions.
74 changes: 36 additions & 38 deletions pkg/report/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,42 @@ func (g *Generator) GenerateReport(oldState, newState state.State) reporters.Rep
}

for proposalID, proposalVotes := range chainInfo.ProposalVotes {
oldProposal, oldProposalFound := oldState.GetProposal(chainName, proposalID)
newProposal := proposalVotes.Proposal

if oldProposalFound {
// There can be the following cases:
// 1) old proposal not found - this is a new proposal, no custom logic needed.
// 2) old proposal is found, it's not in voting and the current one is not in voting
// (like deposit -> deposit) - we don't need to report it at all.
// 3) old proposal is found, it's not in voting but the current one is in voting
// (like deposit -> deposit) - no custom logic needed.
// 4) old proposal is found, it's in voting and the current one is in voting
// (like voting -> voting) - no custom logic needed.
// 5) old proposal is found, it's in voting and the current one is not in voting
// (like voting -> passed) -> send a new event that the voting has finished.

// case 1, 3 and 4 is handled outside of this if case
if oldProposal.IsInVoting() && !newProposal.IsInVoting() { // case 5
g.Logger.Debug().
Str("chain", chainName).
Str("proposal", proposalID).
Msg("Previously proposal was in voting, but it's not now - sending an alert")

entries = append(entries, events.FinishedVotingEvent{
Chain: g.Chains.FindByName(chainName),
Proposal: newProposal,
})
continue
} else if !oldProposal.IsInVoting() && !newProposal.IsInVoting() { // case 2
g.Logger.Trace().
Str("chain", chainName).
Str("proposal", proposalID).
Msg("Previously proposal was and is not in voting period - ignoring.")
continue
}
}

for wallet := range proposalVotes.Votes {
g.Logger.Trace().
Str("name", chainName).
Expand All @@ -56,44 +92,6 @@ func (g *Generator) GenerateReport(oldState, newState state.State) reporters.Rep
oldVote, _ := oldState.GetVote(chainName, proposalID, wallet)
newVote, _ := newState.GetVote(chainName, proposalID, wallet)

oldProposal, oldProposalFound := oldState.GetProposal(chainName, proposalID)
newProposal := proposalVotes.Proposal

if oldProposalFound {
// There can be the following cases:
// 1) old proposal not found - this is a new proposal, no custom logic needed.
// 2) old proposal is found, it's not in voting and the current one is not in voting
// (like deposit -> deposit) - we don't need to report it at all.
// 3) old proposal is found, it's not in voting but the current one is in voting
// (like deposit -> deposit) - no custom logic needed.
// 4) old proposal is found, it's in voting and the current one is in voting
// (like voting -> voting) - no custom logic needed.
// 5) old proposal is found, it's in voting and the current one is not in voting
// (like voting -> passed) -> send a new event that the voting has finished.

// case 1, 3 and 4 is handled outside of this if case
if oldProposal.IsInVoting() && !newProposal.IsInVoting() { // case 5
g.Logger.Debug().
Str("chain", chainName).
Str("proposal", proposalID).
Str("wallet", wallet).
Msg("Previously proposal was in voting, but it's not now - sending an alert")

entries = append(entries, events.FinishedVotingEvent{
Chain: g.Chains.FindByName(chainName),
Proposal: newProposal,
})
continue
} else if !oldProposal.IsInVoting() && !newProposal.IsInVoting() { // case 2
g.Logger.Debug().
Str("chain", chainName).
Str("proposal", proposalID).
Str("wallet", wallet).
Msg("Previously proposal was and is not in voting period - ignoring.")
continue
}
}

// Error querying for vote - need to notify via Telegram.
if newVote.IsError() {
g.Logger.Debug().
Expand Down
8 changes: 4 additions & 4 deletions pkg/report/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,9 @@ func TestReportGeneratorWithFinishedVoting(t *testing.T) {
Status: types.ProposalStatusVoting,
},
Votes: map[string]state.ProposalVote{
"wallet": {},
"wallet": {
Vote: &types.Vote{Options: types.VoteOptions{{Option: "Yes"}}},
},
},
},
},
Expand All @@ -316,9 +318,7 @@ func TestReportGeneratorWithFinishedVoting(t *testing.T) {
ID: "proposal",
Status: types.ProposalStatusPassed,
},
Votes: map[string]state.ProposalVote{
"wallet": {},
},
Votes: map[string]state.ProposalVote{},
},
},
},
Expand Down

0 comments on commit a03e92d

Please sign in to comment.