Skip to content

fix: implement time validation in createElection#243

Open
ArshLabs wants to merge 1 commit intoAOSSIE-Org:mainfrom
ArshLabs:fix/election-time-validation
Open

fix: implement time validation in createElection#243
ArshLabs wants to merge 1 commit intoAOSSIE-Org:mainfrom
ArshLabs:fix/election-time-validation

Conversation

@ArshLabs
Copy link

@ArshLabs ArshLabs commented Mar 12, 2026

Problem

createElection() in ElectionFactory.sol has a comment on line 64:

//add checks of time

...but no actual time checks were implemented. This means:

  • Elections can be created with startTime in the past (already active/ended on creation)
  • Elections can be created with 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:

error InvalidStartTime();
error InvalidEndTime();

if (_electionInfo.startTime <= block.timestamp) revert InvalidStartTime();
if (_electionInfo.endTime <= _electionInfo.startTime) revert InvalidEndTime();

Custom errors are used (consistent with the existing error pattern in the contract) and are cheaper than require strings.

Summary by CodeRabbit

  • Bug Fixes
    • Added validation for election creation to prevent invalid time configurations. Elections cannot now be created with a start time in the past or an end time that doesn't exceed the start time, with specific error messages provided for each validation failure.

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
@coderabbitai
Copy link

coderabbitai bot commented Mar 12, 2026

📝 Walkthrough

Walkthrough

Added two validation error types and timing checks to the createElection function in ElectionFactory. The validation ensures election start times are in the future and end times occur after start times, reverting with specific errors if conditions are not met.

Changes

Cohort / File(s) Summary
Election Timing Validation
blockchain/contracts/ElectionFactory.sol
Added InvalidStartTime() and InvalidEndTime() error definitions. Implemented validation in createElection() to ensure startTime is in the future (greater than block.timestamp) and endTime is after startTime, with appropriate error reversions.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐰 Hop hop, elections must be fair!
Start times floating in the future air,
End times after starts, so right,
Validation checks, burning bright!

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix: implement time validation in createElection' directly and clearly summarizes the main change: adding time validation to the createElection function.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
📝 Coding Plan for PR comments
  • Generate coding plan

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 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, and endTime < startTime so 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

📥 Commits

Reviewing files that changed from the base of the PR and between e4df55f and d417b0c.

📒 Files selected for processing (1)
  • blockchain/contracts/ElectionFactory.sol

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant