President Elector

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

Lack of Events for Key Election Processes Reduces Transparency

Summary

The RankedChoice.sol contract does not emit events when a new president is selected or when the election is reset. Emitting events is a best practice in smart contract development as it allows external services, dapps, and users to track important actions and changes on-chain. The lack of emitted events reduces transparency in the election process and makes it harder for off-chain systems and users to monitor election outcomes.

Vulnerability Details

In the RankedChoice.sol contract, several key processes do not emit events. These include:

  1. Selection of a New President: When a new president is selected through the selectPresident function, there is no event emitted to notify external systems or users of this significant change.

  2. Election Reset: When the election is reset (e.g., when s_voteNumber is incremented or s_candidateList is cleared), no event is emitted to signal the start of a new election cycle.

  3. Vote Casting: When a voter ranks candidates through the rankCandidates or rankCandidatesBySig functions, no events are emitted to track that a vote has been cast.

These omissions make it difficult for off-chain systems to track the election process, reducing the transparency and usability of the contract for dapps, front-end interfaces, and monitoring tools.

Impact

Without events, it becomes harder for external services to track the election results and monitor contract state changes. This lack of transparency can lead to confusion for users and external systems relying on real-time updates, diminishing trust in the contract's operations.

Tools Used

Manual code review.

Recommendations

  • Emit Event for New President Selection: Add an event that is emitted when a new president is selected through the selectPresident function. This will allow external observers to track who the current president is without needing to query the blockchain repeatedly.

Example:

event PresidentSelected(address newPresident);
function selectPresident() external {
// ... existing logic ...
s_currentPresident = winnerList[0];
emit PresidentSelected(s_currentPresident); // Emit event
// ... existing logic ...
}
  • Emit Event for Election Reset: Add an event that is emitted when the election is reset (e.g., when the vote number is incremented or when the candidate list is cleared). This will notify off-chain systems of the start of a new election cycle.

Example:

event ElectionReset(uint256 newVoteNumber);
function selectPresident() external {
// ... existing logic ...
s_voteNumber += 1;
s_candidateList = new address ;
emit ElectionReset(s_voteNumber); // Emit event
}
  • Emit Event for Vote Casting: Add events that are emitted when a voter casts their vote through either rankCandidates or rankCandidatesBySig. This allows external systems to track when votes are cast and by whom, increasing transparency in the voting process.

Example:

event VoteCast(address voter, address[] orderedCandidates);
function rankCandidates(address[] memory orderedCandidates) external {
// ... existing logic ...
_rankCandidates(orderedCandidates, msg.sender);
emit VoteCast(msg.sender, orderedCandidates); // Emit event
}
function rankCandidatesBySig(
address[] memory orderedCandidates,
bytes memory signature
) external {
// ... existing logic ...
address signer = ECDSA.recover(_hashTypedDataV4(keccak256(abi.encode(TYPEHASH, orderedCandidates))), signature);
_rankCandidates(orderedCandidates, signer);
emit VoteCast(signer, orderedCandidates); // Emit event
}

By emitting these events, you ensure that the contract is transparent and that its important actions (like casting votes or selecting a new president) are easily observable by off-chain systems. This improves the user experience, enhances real-time monitoring, and promotes trust in the election process.

Updates

Lead Judging Commences

inallhonesty Lead Judge about 1 year ago
Submission Judgement Published
Invalidated
Reason: Non-acceptable severity

Support

FAQs

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