fix: implement time validation in createElection#243
fix: implement time validation in createElection#243ArshLabs wants to merge 1 commit intoAOSSIE-Org:mainfrom
Conversation
The createElection function had a TODO comment (//add checks of time) on line 64 but no actual validation. This allows creating elections where startTime is in the past or endTime is before startTime, breaking the election state machine entirely. Added checks: - startTime must be in the future (> block.timestamp) - endTime must be after startTime
📝 WalkthroughWalkthroughAdded two validation error types and timing checks to the Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
📝 Coding Plan for PR comments
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
blockchain/contracts/ElectionFactory.sol (1)
66-67: Please lock in the equality boundaries with tests.I’d add negative coverage for
startTime == block.timestamp,startTime < block.timestamp,endTime == startTime, andendTime < startTimeso these<=checks don’t get accidentally weakened later.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@blockchain/contracts/ElectionFactory.sol` around lines 66 - 67, Add unit tests that assert the factory reverts with InvalidStartTime when startTime == block.timestamp and when startTime < block.timestamp, and that it reverts with InvalidEndTime when endTime == startTime and when endTime < startTime. Target the ElectionFactory method that creates elections (the function that takes _electionInfo) and use test helpers to set the block timestamp (e.g., evm_mine/evm_setNextBlockTimestamp or increaseTime) so you can produce the exact equality and less-than scenarios; in each case call the creation function and assert the specific custom error (InvalidStartTime or InvalidEndTime) is thrown. Ensure four negative tests are added: startTime == now, startTime < now, endTime == startTime, endTime < startTime.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@blockchain/contracts/ElectionFactory.sol`:
- Around line 66-67: Add unit tests that assert the factory reverts with
InvalidStartTime when startTime == block.timestamp and when startTime <
block.timestamp, and that it reverts with InvalidEndTime when endTime ==
startTime and when endTime < startTime. Target the ElectionFactory method that
creates elections (the function that takes _electionInfo) and use test helpers
to set the block timestamp (e.g., evm_mine/evm_setNextBlockTimestamp or
increaseTime) so you can produce the exact equality and less-than scenarios; in
each case call the creation function and assert the specific custom error
(InvalidStartTime or InvalidEndTime) is thrown. Ensure four negative tests are
added: startTime == now, startTime < now, endTime == startTime, endTime <
startTime.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: e6540c8d-a240-4077-b6d9-d3caa648f1c3
📒 Files selected for processing (1)
blockchain/contracts/ElectionFactory.sol
Problem
createElection()in ElectionFactory.sol has a comment on line 64://add checks of time...but no actual time checks were implemented. This means:
startTimein the past (already active/ended on creation)endTime <= startTime(zero-duration elections)Both cases break the election lifecycle — voters either can't participate or the election is permanently in a broken state.
Fix
Added two custom errors and corresponding checks:
Custom errors are used (consistent with the existing error pattern in the contract) and are cheaper than require strings.
Summary by CodeRabbit