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
}
}

function testSumTreeInvariantAfterAddsAndRemovals() public {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merge with latest master
run forge fmt

@rvagg
Copy link
Contributor

rvagg commented Dec 4, 2025

@Hany-Almnaem please see the above comment - this needs a merge from master (or rebase) plus a fmt. Otherwise this seems pretty good I think.

- Resolve conflict in test/PDPVerifier.t.sol
- Fix failing SumTreeEnhancedTest tests
- Apply forge fmt formatting
@Hany-Almnaem
Copy link
Contributor Author

@rvagg
Done! Merged upstream/main, resolved conflicts, and ran forge fmt.
All tests passing. Ready for review.

@rvagg
Copy link
Contributor

rvagg commented Dec 5, 2025

Thanks @Hany-Almnaem . I'm going to have to personally put off reviewing this for now because it's a large-enough context switch for me to do it and I have a lot on my plate currently. Maybe @ZenGround0 gets inspired to come have a look, his head is more full of the details here than mine.

@rjan90 rjan90 removed the request for review from aarshkshah1992 December 8, 2025 08:10
@BigLep BigLep added this to FilOz Dec 17, 2025
@github-project-automation github-project-automation bot moved this to 📌 Triage in FilOz Dec 17, 2025
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

4 participants