Skip to content

[Audit] Product Voting Contract #102

Description

@aguilar1x

🎯 Objective

Audit the Product Voting Contract, a Soroban-based smart contract designed to facilitate decentralized product feedback through upvotes and downvotes. It implements real-time ranking, vote history tracking, spam prevention, and dynamic trend analysis—all with strong anti-abuse protections and strict voting limits.


🏗 Contract Structure

product-voting-contract/src/

  lib.rs         // Main contract logic and entry points
  vote.rs        // Voting system and state transitions
  ranking.rs     // Product scoring and trending calculations
  limits.rs      // Vote restriction enforcement
  types.rs       // Enums, vote types, and data models
  test.rs        // Unit tests

🔍 Audit Scope by Component

1️⃣ Product Management

  • Function: create_product(env, id, name)

  • Checks:

    • Prevent duplicate product creation via unique ID validation
    • Enforce non-empty name and ID fields
    • Validate time-bound voting period (30 days limit)
    • Store product metadata safely and persistently

2️⃣ Voting Operations

  • Function: cast_vote(env, product_id, vote_type, voter)

  • Checks:

    • Verify product existence
    • Check voter eligibility (account age > 7 days)
    • Enforce 1 vote per user per product
    • Allow vote modification within a 24-hour reversal window
    • Update vote records, product score, and user vote history atomically
    • Validate vote type (Upvote/Downvote only)

3️⃣ Voting Limits & Eligibility

  • Module: limits.rs

  • Checks:

    • Daily limit of 10 votes per user
    • Account creation time must exceed minimum age
    • Block multiple votes on same product
    • Time-based validation: no votes allowed beyond product's voting window
    • Prevent cast_vote bypass via replay or stale state

4️⃣ Ranking & Trending System

  • Functions: get_product_score, get_trending_products

  • Checks:

    • Real-time scoring must correctly reflect current upvotes/downvotes
    • Score decays with time to prioritize recent activity
    • Trending calculation over 48-hour activity window
    • Prevent manipulation via mass voting or vote-spamming
    • Ensure stable sorting and deterministic trending output

5️⃣ Data Tracking & Audit Trail

  • Implied by structure: vote history tracking per user/product

  • Checks:

    • Complete traceability of who voted, when, and how
    • Accurate timestamping and vote versioning
    • No data loss on vote update
    • Protect vote records from overwriting or duplication

🔐 Security Considerations

  • Authorization:

    • Prevent unauthorized function access or misuse
    • Validate input types and limits strictly (vote types, product IDs)
  • Anti-Gaming Protections:

    • Minimum account age prevents bots or rapid voting
    • Daily vote limits mitigate coordinated vote farming
    • Reversal window prevents abuse of trending calculations
  • Vote History Integrity:

    • Store every vote with timestamp and voter ID
    • Prevent vote deletion or hidden alterations
  • Input Validation:

    • No empty or malformed symbols or names
    • No invalid timestamps or vote types

📋 Error Handling

The contract implements detailed error codes. Audit checks should ensure:

  • Each Result::Err path corresponds to well-scoped input validations

  • Unrecoverable errors (ProductNotFound) are enforced early

  • Test coverage for all errors:

    • VotingPeriodEnded
    • AlreadyVoted
    • ReversalWindowExpired
    • DailyLimitReached
    • AccountTooNew
    • ProductNotFound
    • ProductExists

📊 Voting Logic Summary

Vote Type Value Effect
Upvote 1 Adds score
Downvote 2 Subtracts score
Modify Within 24h Allowed
Limit 10/day Enforced

✅ Testing Recommendations

  • Create a product and cast votes within and beyond voting window
  • Attempt duplicate votes and ensure they’re rejected
  • Modify vote within 24h and ensure reversal logic is accurate
  • Simulate new accounts voting and ensure enforcement of age check
  • Monitor trending output with burst activity and decay logic
  • Test daily vote cap with multiple products in one session

📌 Additional Notes

  • Suggest including vote weight modifiers based on user level or activity in future versions
  • Potential integration with external product catalogs or e-commerce platforms
  • For transparency, consider emitting events for each vote and rank change
  • Explore real-time trending caching for performance enhancement

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions