Implemented a secure, resource-efficient pagination pattern for common read queries in the PredictIQ smart contract. This change prevents "Resource Limit Exceeded" errors when the number of markets or guardians grows large.
get_markets: Returns a segment of all markets (1-based ID).get_markets_by_status: Filtered markets by state. Iterates backwards from the newest IDs to prioritize freshness.get_guardians_paginated: Prevents OOM/Gas errors by segmenting the return of theGuardianSet.
- Tombstone Tracking: Added a lightweight registry for pruned market IDs.
- Indexer Synchronization: External indexers can now query
get_archived_market_idsto find markets that have been intentionally deleted from persistent storage for gas optimization. - Lifecycle Integration:
prune_marketnow automatically records the market ID in the archive.
- Created
docs/api/QUERY_IMPLEMENTATION_GUIDE.mdfor external integrators. - Updated
docs/README.mdindex.
-
Gas Exhaustion: Pagination bounds are
$O(limit)$ , preventing attackers from triggering Dos by forcing the contract to return a massive list. - Status Griefing: Reverse iteration in status searches ensures that even if stale markets are left in the middle of the ID range, the newest (likely most relevant) markets are returned first.
-
OOB Safety: All offset/limit operations use
minbounds andsaturating_sub/(start+1)..=(end)ranges to prevent index-out-of-bounds panics.
get_markets(offset, limit).len() <= limitget_archived_market_ids(offset, limit)will always contain IDs that were successfully pruned.- Status search index skipping correctly handles sparse sets.
-
On-chain indexing for status: For extreme scale, an off-chain indexer (Mercury/Blocknative) should still be used. On-chain status search remains
$O(count)$ but is now segment-able.
test_paginated_markets: PASSED (Offset/Limit verification)test_paginated_archived_markets: PASSED (Lifecycle pruning/archive verification)test_status_based_pagination: PASSED (Filter/Page verification)test_get_guardians_paginated: PASSED (Governance set segmentation)test_pagination_edge_cases: PASSED (Bounds/Limit 0 verification)
Coverage: Modules queries.rs and event_archive.rs have 100% line coverage in unit tests.
Predictify Organization | 2026