Skip to content

Add Counter Overflow Detection and Logging #30

@Just-Bamford

Description

@Just-Bamford

��# Issue 11: Add Counter Overflow Detection and Logging

Problem

u32 counters can overflow at 4B+ tokens without detection. No audit trail of overflow attempts.

Solution

  • Add overflow check before incrementing in record_token and record_vote
  • Return CounterOverflow error when limit reached (u32::MAX)
  • Emit structured error event with ballot ID and current count
  • Prevents silent data corruption

Implementation Tasks

  • Add overflow check: if count == u32::MAX return Err(CounterOverflow)
  • Implement in both record_token and record_vote
  • Emit error event with ballot ID and final count
  • Add CounterOverflow to ContractError enum
  • Unit tests: counter at MAX+1 triggers error
  • Test: error event is properly emitted

Note for Contributors

Complete Issue #2 (error handling) first to have the ContractError enum framework in place. While overflow at 4B tokens is unlikely in practice, this guard is critical for production robustness and prevents silent data corruption. When counter reaches u32::MAX, block further increments and return an explicit error (don't allow wrapping). Emit an error event that includes the ballot ID and current count for audit trail. Consider this a hard limit that cannot be exceeded � future enhancements might use u64 counters if larger counts are needed. Test edge cases at MAX-1, MAX, and attempted MAX+1.

Metadata

Metadata

Assignees

No one assigned

    Labels

    GrantFox OSSIssue tracked in GrantFox OSSMaybe RewardedIssue may be eligible for a GrantFox rewardOfficial CampaignCampaign: Official Campaign

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions