Skip to content

Counter overflow detection #70

Description

@Just-Bamford

Summary

The vote counter in the contract is a 64-bit integer. If a ballot receives more than 2^63 votes, the counter will overflow. While overflow is unlikely in practice, a production system must handle it gracefully. The contract should detect overflow and reject votes when the counter is near its limit.

This is a Milestone 2 hardening issue. Edge case handling is required before mainnet.

Background

In contracts/anonvote/src/lib.rs, the vote counter is a u64. Mathematically, overflow is nearly impossible with realistic vote counts, but the contract must not silently wrap around or produce incorrect results.

Scope

Contract

  • Add a constant MAX_VOTES_PER_BALLOT = 2^63 - 1 (leaving one bit of headroom)
  • Before incrementing the vote counter, check if it has reached MAX_VOTES_PER_BALLOT
  • If so, return an error code COUNTER_OVERFLOW and reject the vote
  • Log a warning event indicating the counter is near limit
  • Document this limit in the contract README

Tests

  • Vote counter increments correctly up to the limit
  • Votes at the limit are accepted
  • Vote beyond the limit is rejected with COUNTER_OVERFLOW
  • Error message is descriptive

Relevant Files

  • contracts/anonvote/src/lib.rs
  • contracts/README.md

Acceptance Criteria

  • Overflow detection implemented
  • Votes rejected when limit reached
  • Tests pass
  • Error code defined and documented

Out of Scope

  • Dynamic counter resizing — not applicable for blockchain
  • Custom limits per ballot — use global constant

Note for Contributors

This is defensive programming. The likelihood of hitting this limit is near zero with realistic usage, but the code must fail gracefully rather than silently produce incorrect results. Include the limit in documentation so deployers are aware of it.

Metadata

Metadata

Assignees

Labels

GrantFox OSSIssue tracked in GrantFox OSSMaybe RewardedIssue may be eligible for a GrantFox rewardOfficial Campaign | FWC26Campaign: Official Campaign | FWC26edge-caseLabel: edge-caseenhancementNew feature or requestsecurityLabel: security

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions