Fix: Amortise TTL refresh cost for paginated index views (#408) - #493
Open
OluwapelumiElisha wants to merge 1 commit into
Open
Conversation
|
@OluwapelumiElisha 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! 🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description:
What does this PR do?
Addresses a simulation footprint / instruction budget DoS vulnerability in the factory contract's streams_by_sender and streams_by_recipient views. This is achieved by bounding the TTL-refresh loop (extend_page_ttls) to refresh a maximum of 3 pages per query, effectively reducing the read complexity from O(total pages) to O(limit).
Why is this change needed?
Prior to this change, every query to read_index invoked extend_page_ttls, which iterated over all 0..=(count-1)/PAGE_SIZE pages doing a persistent has() and extend_ttl() for each. For heavy senders with thousands of streams (e.g., 100 pages), this incurred up to 100 TTL refresh operations on every read, predictably exhausting the simulation instruction/footprint budget.
Changes proposed:
contracts/factory/src/index.rs: Refactored the extend_page_ttls function to perform a bounded amortised walk instead of a full loop.
The function now refreshes at most 3 pages (BATCH_LIMIT = 3) per call.
Uses env.ledger().sequence() % num_pages as a pseudo-random starting point, naturally distributing the TTL refresh cost over time without needing a persistent on-chain cursor.
Ensures queried pages in the window are still strictly refreshed on read (handled inherently by collect_page), while keeping older unread pages alive incrementally.
Related Issues
Closes #408