fix(ElectionFactory): Key electionOwner on stable electionCount, fix deleteElection swap logic#249
Conversation
…deleteElection swap logic createElection() keyed electionOwner on openBasedElections.length (the array index) instead of electionCount. Because deleteElection() uses a swap-and-pop strategy, array indices change after every deletion, making it impossible to reliably identify election ownership by index. electionCount was already passed to Election.initialize() as the stable electionId and was stored on the Election clone, but was not used as the ownership key -- a clear inconsistency. Two changes: 1. createElection: record electionOwner[electionCount] before incrementing electionCount, so the stable monotonic ID is the key. 2. deleteElection: find the target election in the array by comparing Election.electionId() values (stable), swap the last element into that slot, pop, and delete electionOwner[_electionId] by stable ID. The ownership entry for the swapped element is untouched (correct -- its stable ID has not changed). Closes AOSSIE-Org#248
|
Warning Rate limit exceeded
⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the 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 configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
✨ Finishing Touches🧪 Generate unit tests (beta)
📝 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. Comment |
Summary
Fixes a logic bug where election ownership was tracked by mutable array index instead of the stable monotonic
electionCount, causingdeleteElection's swap-and-pop to corrupt the ownership mapping.Closes #248
Problem
In
createElection():electionCountwas already passed toElection.initialize()as the stableelectionIdstored on the clone — but the ownership mapping usedopenBasedElections.length(the array position) instead. These two identifiers diverge after any deletion.deleteElection()uses swap-and-pop:After a swap, the moved election's array index changes — but its
electionOwnerentry is keyed on the old index. SubsequentdeleteElectioncalls using the correct_electionIdfromElection.electionId()will hit the wrong (or empty) ownership entry.Concrete scenario:
electionOwner[0] = Alice,A.electionId() = 0electionOwner[1] = Bob,B.electionId() = 1_electionId = 0): B is swapped to index 0,electionOwner[0] = Bob,electionOwner[1]deleteddeleteElection(1)(his stable ID fromB.electionId()) →electionOwner[1]is empty →OnlyOwnerrevert — Bob cannot delete his own electionFix
createElection— record ownership againstelectionCount(the stable ID) before incrementing:deleteElection— locate the target slot by comparingElection.electionId()(stable), swap the last element in, pop, and delete by stable ID. The swapped election'selectionOwnerentry is untouched — its stable ID has not changed.Files Changed
blockchain/contracts/ElectionFactory.solcreateElection; rewrotedeleteElectionswap to use stable IDsChecklist
electionCountis now the single source of truth for election identity throughout the factorydeleteElectionusesElection.electionId()(public getter already present) to locate the correct slotupstream/main