Skip to content

Conversation

Hany-Almnaem
Copy link
Contributor

Summary

This PR introduces a suite of tests to validate the correctness and stability of the sumTreeCounts structure in the PDPVerifier smart contract.

The tests ensure that each node in the SumTree maintains the invariant:

  • sumTreeCounts[setId][index] == sum of rootLeafCounts[setId][j] for the correct range derived from the node’s height.

This directly addresses and resolves issue: #45

Summary of Changes

Core Tests (Basic Coverage)

  • testSumTreeInvariantAfterAddsAndRemovals()
    • Adds a controlled set of roots, removes a few, and asserts the sumtree structure.
  • assertSumTreeInvariant(uint256 setId)
    • Shared invariant validation used across tests.
  • testAdvancedSumTreeInvariant()
    • Simulates multiple add/remove rounds using dynamic values.
  • testFuzzedSumTreeInvariant(uint256 seed)
    • Uses seeded randomness to test with pseudo-random leaf counts and root IDs.

Advanced Suite (Tagged [ADVANCED])

Implemented in a separate SumTreeEnhancedTest contract:

  • testSumTreeStressTest()
    • Applies add/remove operations over several rounds with increasing load.
  • testSumTreeEdgeCases()
    • Verifies correctness under very small, very large, and alternating root sizes.
  • testSumTreePerformance()
    • Executes 100 add/remove operations and logs gas usage.
  • testSumTreeRandomized(uint256 seed)
    • Interleaves randomized add/remove logic with proving period progression.
  • Shared internal assertSumTreeInvariant() with more detailed error messaging for diagnostics.

Benefits

  • Validates the structural integrity of the SumTree under normal and edge-case usage.
  • Improves test coverage for internal tree logic, including depth-aware summation.
  • Reduces regression risk from future updates to root addition/removal logic.
  • Keeps invariant assertions reusable and centralized for clarity and reliability.

Notes

  • Tests pass locally using forge test
  • No contract logic has been modified—only test coverage has been added.
  • Code has been linted and follows existing style conventions.
  • Advanced tests are separated to keep the default suite performant and focused.

Please let me know if you prefer these broken into separate PRs or test files.

@rjan90 rjan90 moved this to 🔎 Awaiting review in PDP Jun 10, 2025
@rjan90 rjan90 added this to FS Jul 15, 2025
@github-project-automation github-project-automation bot moved this to 📌 Triage in FS Jul 15, 2025
@rjan90 rjan90 moved this from 📌 Triage to 🔎 Awaiting review in FS Jul 15, 2025
@rjan90 rjan90 linked an issue Jul 15, 2025 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: 🔎 Awaiting review
Status: 🔎 Awaiting review
Development

Successfully merging this pull request may close these issues.

Invariant tests of sumtree
2 participants