Skip to content

refactor(Election): Extract duplicated ballot init into ensureBallot modifier#246

Open
Muneerali199 wants to merge 1 commit intoAOSSIE-Org:mainfrom
Muneerali199:fix/election-ensure-ballot-modifier
Open

refactor(Election): Extract duplicated ballot init into ensureBallot modifier#246
Muneerali199 wants to merge 1 commit intoAOSSIE-Org:mainfrom
Muneerali199:fix/election-ensure-ballot-modifier

Conversation

@Muneerali199
Copy link

Summary

Eliminates a copy-pasted lazy-initialization block that existed identically in two functions, replacing it with a single ensureBallot modifier.

Closes #230


Problem

Both userVote() and ccipVote() contained the identical 3-line block:

if (ballotInitialized == false) {
    ballot.init(candidates.length);
    ballotInitialized = true;
}

This duplication means:

  • Any future change to ballot initialization (e.g. different arguments, additional setup) must be applied in both places — easy to miss
  • The intent ("ensure the ballot is ready before voting") is not named or encapsulated anywhere

Fix

Extracted the block into a ensureBallot modifier and applied it to both functions:

// New modifier
modifier ensureBallot() {
    if (!ballotInitialized) {
        ballot.init(candidates.length);
        ballotInitialized = true;
    }
    _;
}

// Before
function userVote(uint[] memory voteArr) external electionInactive {
    if (userVoted[msg.sender]) revert AlreadyVoted();
    if (ballotInitialized == false) {
        ballot.init(candidates.length);
        ballotInitialized = true;
    }
    ballot.vote(voteArr);
    ...
}

// After
function userVote(uint[] memory voteArr) external electionInactive ensureBallot {
    if (userVoted[msg.sender]) revert AlreadyVoted();
    ballot.vote(voteArr);
    ...
}

Same change applied to ccipVote(). Runtime behaviour is identical — only the structure changes.


Files Changed

File Change
blockchain/contracts/Election.sol Added ensureBallot modifier; removed duplicated init blocks from userVote() and ccipVote()

Checklist

  • No behaviour change — pure structural refactor
  • Both userVote and ccipVote retain correct modifier order (electionInactive before ensureBallot)
  • No unrelated changes included
  • Branch created fresh from upstream/main

userVote() and ccipVote() both contained an identical 3-line block
to lazily initialise the ballot on first vote:

    if (ballotInitialized == false) {
        ballot.init(candidates.length);
        ballotInitialized = true;
    }

Copy-pasting this logic across two functions is error-prone: a future
bug-fix or change (e.g. passing different args to init) would need to
be applied in both places, and it is easy to miss one.

Extract the block into a dedicated modifier ensureBallot() and apply
it to both functions. Behaviour is identical; only the structure changes.

Closes AOSSIE-Org#230
@coderabbitai
Copy link

coderabbitai bot commented Mar 13, 2026

Warning

Rate limit exceeded

@Muneerali199 has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 5 minutes and 1 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 5bfcb6bd-66b8-4126-b322-5747ff479e62

📥 Commits

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

📒 Files selected for processing (1)
  • blockchain/contracts/Election.sol
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
📝 Coding Plan
  • Generate coding plan for human review 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.

❤️ Share

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

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.

BUG: Refactoring Lazy Ballot Initialization

1 participant