President Elector

First Flight #24
Beginner FriendlyFoundry
100 EXP
View results
Submission Details
Severity: low
Invalid

Lack of Event Emissions for State Changes

Summary

The RankedChoice contract does not emit events when significant state changes occur, such as when a new president is selected or when votes are submitted. Emitting events is a best practice in Solidity to ensure that off-chain systems and users can track and react to important changes.

Description

In Solidity, emitting events is essential for providing off-chain monitoring tools (e.g., front-end applications, analytics platforms, or wallets) with information about the internal state changes in the contract. This contract lacks events for important actions like the selection of a new president or the ranking of candidates. Without events, external parties cannot easily detect or monitor these actions without repeatedly querying the blockchain, which is inefficient and costly in terms of gas usage.

Events could be emitted in the following instances:

  • When a new president is selected in the selectPresident() function.

  • When a voter ranks candidates, which occurs in the rankCandidates() and rankCandidatesBySig() functions.

Impact

  • Lack of Transparency: Users, dApps, or off-chain systems cannot be easily notified when a key event like a presidential election takes place, leading to reduced transparency.

  • Difficult to Track Changes: Off-chain systems need to rely on manual or gas-intensive methods like polling the blockchain to detect state changes.

  • Reduced Efficiency: Failing to emit events increases the load on systems that need to continuously query the blockchain, which could be optimized by simply emitting events when important changes happen.

Recommendations

  1. Emit Events for Key Actions: It is recommended to emit events in key functions such as:

    • NewPresidentSelected: Emit an event when a new president is selected in the selectPresident() function.

event NewPresidentSelected(address newPresident);
function selectPresident() external {
// Contract logic...
emit NewPresidentSelected(s_currentPresident);
}
  • VotesRanked: Emit an event when a voter ranks candidates.

event VotesRanked(address voter, address[] rankedCandidates);
function _rankCandidates(address[] memory orderedCandidates, address voter) internal {
// Contract logic...
emit VotesRanked(voter, orderedCandidates);
}
  1. Implement Events for Significant State Changes: Events should be emitted for other significant changes to improve off-chain monitoring and transparency.

Updates

Lead Judging Commences

inallhonesty Lead Judge 9 months ago
Submission Judgement Published
Invalidated
Reason: Non-acceptable severity

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.