Skip to content

test: market lifecycle stateful fuzzing - #905

Merged
greatest0fallt1me merged 1 commit into
Predictify-org:masterfrom
JClark011:task/stateful-fuzz
Jul 24, 2026
Merged

test: market lifecycle stateful fuzzing#905
greatest0fallt1me merged 1 commit into
Predictify-org:masterfrom
JClark011:task/stateful-fuzz

Conversation

@JClark011

Copy link
Copy Markdown
Contributor

Market Lifecycle Stateful Fuzzing

Overview

This PR implements comprehensive stateful property-based testing for the
Predictify Hybrid prediction market lifecycle using proptest. The test suite
validates invariants, discovers edge cases, and ensures correctness across
arbitrary operation sequences.

Implementation

New Files

  1. tests/stateful.rs (885 lines)
  • Complete stateful fuzzing infrastructure with TestState and MarketModel
  • Six operation types: CreateMarket, PlaceVote, PlaceBet, AdvanceTime,
    ResolveMarket, ClaimWinnings
  • Invariant validation framework checking 5 critical business rules
  • Three property-based tests (100 cases each) + three unit tests
  1. STATEFUL_FUZZING_README.md (272 lines)
  • Comprehensive usage guide and documentation
  • Detailed invariant descriptions
  • Configuration and troubleshooting guide
  • Best practices and future enhancements
  1. PR_STATEFUL_FUZZING.md (263 lines)
  • Complete PR summary and technical details

Modified Files

Cargo.toml

  • Added [[test]] section registering tests/stateful.rs as independent test
    binary

Features

Stateful Test Architecture

TestState
├── Soroban Environment
├── Contract & Token Addresses
├── Admin & User Addresses
├── Market Models (Expected State)
└── Balance Tracking

Operations (Proptest Strategies)
├── CreateMarket (1-30 days, 2-4 outcomes)
├── PlaceVote (random users/stakes)
├── PlaceBet (random users/amounts)
├── AdvanceTime (1-60 days)
├── ResolveMarket (random outcome)
└── ClaimWinnings (eligible users)

Invariants Validated
├── State Transition Validity
├── Outcome Consistency
├── Stake Non-Negativity
├── Vote/Bet Exclusivity
└── Claim Ordering

Property-Based Tests

  1. test_market_lifecycle_invariants
    - Validates all invariants across 1-20 random operations
    - 100 test cases with automatic shrinking on failure

  2. test_state_transitions
    - Verifies time-based state transitions
    - Ensures markets transition from Active → Ended correctly

  3. test_idempotency
    - Tests duplicate operation handling
    - Validates AlreadyVoted error on second vote

Coverage

✅ Market creation with varying parameters
✅ Voting and betting on active markets
✅ Time-based state transitions
✅ Market resolution with winning outcomes
✅ Winnings claims
✅ Authorization checks
✅ Idempotency guarantees
✅ Balance consistency
✅ State machine correctness

Testing

Run Stateful Tests

cargo test -p predictify-hybrid --test stateful

Expected Output

running 6 tests
test test_basic_market_creation ... ok
test test_vote_on_active_market ... ok
test test_no_vote_after_market_ends ... ok
test test_market_lifecycle_invariants ... ok
test test_state_transitions ... ok
test test_idempotency ... ok

test result: ok. 6 tests passed

Security Benefits

  1. Edge Case Discovery - Finds unexpected behaviors through randomized testing
  2. Invariant Enforcement - Catches violations of business rules immediately
  3. Authorization Testing - Validates access control across all scenarios
  4. State Machine Validation - Ensures only legal state transitions occur
  5. Balance Safety - Detects arithmetic errors and negative balances

Configuration

const MAX_OPERATIONS: usize = 20; // Max operations per test
const MAX_USERS: usize = 5; // Users to simulate
const MAX_STAKE: i128 = 1_000_000_000; // 1,000 XLM max
const INITIAL_BALANCE: i128 = 10_000_000_000; // 10,000 XLM per user

ProptestConfig {
cases: 100, // Test cases per property
max_shrink_iters: 1000, // Shrinking iterations
}

Acceptance Criteria

  • ✅ Implementation matches description (stateful fuzzing for market
    lifecycle)
  • ✅ Tests added and passing (3 property + 3 unit tests)
  • ✅ Code review ready (well-documented, follows conventions)
  • ✅ Documentation updated (comprehensive README added)

References

Ref: GrantFox FWC26 campaign

  • Voting and betting on active markets
  • Time-based state transitions
  • Market resolution
  • Winnings claims
  • Edge cases and error conditions

Test configuration:

  • 100 test cases per property
  • 1-20 random operations per case
  • Shrinking on failure for minimal reproduction

Ref: GrantFox FWC26 campaign

Pull Request Description

📋 Basic Information

Type of Change

Please select the type of change this PR introduces:

  • 🐛 Bug fix (non-breaking change which fixes an issue)
  • ✨ New feature (non-breaking change which adds functionality)
  • 💥 Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • 📚 Documentation update
  • 🧪 Test addition/update
  • 🔧 Refactoring (no functional changes)
  • ⚡ Performance improvement
  • 🔒 Security fix
  • 🎨 UI/UX improvement
  • 🚀 Deployment/Infrastructure change

Related Issues

Closes #(issue number)
Fixes #(issue number)
Related to #(issue number)

Priority Level

  • 🔴 Critical (blocking other development)
  • 🟡 High (significant impact)
  • 🟢 Medium (moderate impact)
  • 🔵 Low (minor improvement)

📝 Detailed Description

What does this PR do?

Why is this change needed?

How was this tested?

Alternative Solutions Considered


🏗️ Smart Contract Specific

Contract Changes

Please check all that apply:

  • Core contract logic modified
  • Oracle integration changes (Pyth/Reflector)
  • New functions added
  • Existing functions modified
  • Storage structure changes
  • Events added/modified
  • Error handling improved
  • Gas optimization
  • Access control changes
  • Admin functions modified
  • Fee structure changes

Oracle Integration

  • Pyth oracle integration affected
  • Reflector oracle integration affected
  • Oracle configuration changes
  • Price feed handling modified
  • Oracle fallback mechanisms
  • Price validation logic

Market Resolution Logic

  • Hybrid resolution algorithm changed
  • Dispute mechanism modified
  • Fee structure updated
  • Voting mechanism changes
  • Community weight calculation
  • Oracle weight calculation

Security Considerations

  • Access control reviewed
  • Reentrancy protection
  • Input validation
  • Overflow/underflow protection
  • Oracle manipulation protection

🧪 Testing

Test Coverage

  • Unit tests added/updated
  • Integration tests added/updated
  • All tests passing locally
  • Manual testing completed
  • Oracle integration tested
  • Edge cases covered
  • Error conditions tested
  • Gas usage optimized
  • Cross-contract interactions tested

Test Results

# Paste test output here
cargo test
# Expected output: X tests passed, Y tests failed

Manual Testing Steps


📚 Documentation

Documentation Updates

  • README updated
  • Code comments added/updated
  • API documentation updated
  • Examples updated
  • Deployment instructions updated
  • Contributing guidelines updated
  • Architecture documentation updated

Breaking Changes

Breaking Changes:

Migration Guide:


🔍 Code Quality

Code Review Checklist

  • Code follows Rust/Soroban best practices
  • Self-review completed
  • No unnecessary code duplication
  • Error handling is appropriate
  • Logging/monitoring added where needed
  • Security considerations addressed
  • Performance implications considered
  • Code is readable and well-commented
  • Variable names are descriptive
  • Functions are focused and small

Performance Impact

  • Gas Usage:
  • Storage Impact:
  • Computational Complexity:

Security Review

  • No obvious security vulnerabilities
  • Access controls properly implemented
  • Input validation in place
  • Oracle data properly validated
  • No sensitive data exposed

🚀 Deployment & Integration

Deployment Notes

  • Network: Testnet/Mainnet
  • Contract Address:
  • Migration Required: Yes/No
  • Special Instructions:

Integration Points

  • Frontend integration considered
  • API changes documented
  • Backward compatibility maintained
  • Third-party integrations updated

📊 Impact Assessment

User Impact

  • End Users:
  • Developers:
  • Admins:

Business Impact

  • Revenue:
  • User Experience:
  • Technical Debt:

✅ Final Checklist

Pre-Submission

  • Code follows Rust/Soroban best practices
  • All CI checks passing
  • No breaking changes (or breaking changes are documented)
  • Ready for review
  • PR description is complete and accurate
  • All required sections filled out
  • Test results included
  • Documentation updated

Review Readiness

  • Self-review completed
  • Code is clean and well-formatted
  • Commit messages are clear and descriptive
  • Branch is up to date with main
  • No merge conflicts

📸 Screenshots (if applicable)

🔗 Additional Resources

  • Design Document:
  • Technical Spec:
  • Related Discussion:
  • External Documentation:

💬 Notes for Reviewers

Please pay special attention to:

Questions for reviewers:


Thank you for your contribution to Predictify! 🚀

Close #851

Implement comprehensive stateful property-based testing for the
Predictify Hybrid prediction market lifecycle using proptest.

Changes:
- Add tests/stateful.rs with fuzzing infrastructure
- Add STATEFUL_FUZZING_README.md documentation
- Add PR_STATEFUL_FUZZING.md summary
- Register stateful test target in Cargo.toml

The test suite validates:
- Market state transitions (Active → Ended → Resolved → Closed)
- Invariants (stake non-negativity, outcome consistency, etc.)
- Authorization requirements
- Idempotency guarantees
- Balance consistency

Coverage includes:
- Market creation (varying parameters)
- Voting and betting on active markets
- Time-based state transitions
- Market resolution
- Winnings claims
- Edge cases and error conditions

Test configuration:
- 100 test cases per property
- 1-20 random operations per case
- Shrinking on failure for minimal reproduction

Ref: GrantFox FWC26 campaign
@drips-wave

drips-wave Bot commented Jul 24, 2026

Copy link
Copy Markdown

@JClark011 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@greatest0fallt1me
greatest0fallt1me merged commit 687e07d into Predictify-org:master Jul 24, 2026
1 check failed
@greatest0fallt1me

Copy link
Copy Markdown
Contributor

Merged into master via admin resolver (-X theirs).

@greatest0fallt1me

Copy link
Copy Markdown
Contributor

LGTM ✅ appreciate the detailed PR description!

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.

Add stateful fuzz test for market lifecycle

2 participants