Skip to content

Conversation

@augustin-v
Copy link

Related issue: #79

Description

This PR implements sophisticated query functions that enable efficient data retrieval and analytics for the Season and Audition contract. The implementation addresses the lack of filtering and query capabilities that made it difficult to build rich user interfaces and provide meaningful insights.

Features

Storage:

  • Added count trackers (season_count, audition_count) for efficient iteration
  • Added ID tracking arrays (all_season_ids, all_audition_ids) for ordered access
  • Added analytics caching (audition_vote_counts, performer_total_weights) for fast lookups
  • Cached vote analytics for instant retrieval

New Query Functions:

  • Genre Filtering: get_seasons_by_genre(), get_auditions_by_genre()
  • Time Queries: get_active_auditions(), get_auditions_in_time_range()
  • Season Queries: get_auditions_by_season()
  • Analytics: get_audition_vote_count(), get_total_vote_weight_for_performer()
  • Pagination: get_seasons_by_ids(), get_auditions_by_ids()
  • Complex Filtering: get_auditions_by_criteria()
  • Utilities: is_audition_active(), get_audition_status(), count_votes_for_audition()

Updated Core Functions:

  • Modified create_season() to maintain count and ID tracking
  • Modified create_audition() to maintain count and ID tracking
  • Modified record_vote() to update analytics cache

Testing

Comprehensive test suite covering:

  • Genre-based filtering scenarios
  • Time-based query edge cases
  • Analytics function accuracy
  • Complex filtering with multiple criteria combinations
  • Pagination and batch operations
  • Utility function correctness
  • Edge cases and performance limits

Breaking Changes

None. All existing functionality remains unchanged. New functions are additive only.

The query system provides a solid foundation for building sophisticated dApps on top of the Season and Audition contract.

@CollinsC1O
Copy link
Collaborator

CollinsC1O commented Jul 7, 2025

Hi boss @augustin-v a lovely implementation, thanks a lot for your contribution we really appreciate, Just need a few improvement; firstly I will love you to implement functions to track individual user performance over time, and also implement historical data aggregation per user. secondly, considering that results are returned in insertion order only, can you implement sorting capabilities. I noticed that only basic vote counting cache was implemented, can you implement comprehensive caching for complex queries, Can you also provide aggregation of participation trends, popular genres, etc. this is because there is a missing platform-wide analysis
I noticed you are using single linear array for all ID and no specialized indexes for different query types. Is it possible to improve on this. And actually seems like there is storage inefficiency, Considering the linear search through all IDS for every query, can you implement indexing for frequently queried fields. Thirdly is it possible to implement data export functionality for users.
I kinda noticed some potential vulnerabilities:

  • There is an integer overflow risk with this: 


self.performer_total_weights.write(performer_key, current_weight + weight);

so there is no overflow protection when adding vote weights. can you fix this

  • There is a missing input validation; no validation for time range (start > end), and there is no validation for reasonable max-_result values
  • With this type conversion: 

audition.start_timestamp.try_into().unwrap()

with unwrap(), let’s use a proper error handling. Just using unwrap() could cause panic, consider using expect('').

  • finally can you improve on this few tests; considering there are no stress testing with large datasets can you push on this and also test on performance benchmarking and edge case testing for numeric overflows
    


    

can you follow up your improvements with some comment so I can review faster at best it kinda makes it a bit readable for someone new to the repo. Although your code is super readable, I just gave this a thought.
    


    Can we achieve this from now till tomorrow so I can merge your work. Incase you didn't get to provide an implementation or fix just pinpoint it and let me know about it so I can do it from my part, this is so we don't miss it.


Thanks for you help. I do really appreciate

@CollinsC1O
Copy link
Collaborator

Hello @augustin-v been waiting for your responds
hope you good

@augustin-v
Copy link
Author

Hi @CollinsC1O thanks for the review!
I'm working on it, will have a PR ready tomorrow

@augustin-v
Copy link
Author

Hey @CollinsC1O, I've addressed most of the points in the updated changes, sorry it took a bit longer than I expected. Here's a quick summary:

  • I took the liberty to change the weights to u64 since u64 has built-in overflow checks in Cairo, which resolves the integer overflow risk in performer_total_weights.

  • For sorting and exporting, I think these would be better handled off-chain. For example, a frontend or API can call functions like get_auditions_by_genre or get_performer_history and then sort/export the data (e.g., to CSV/JSON) locally, as doing it on-chain would be gas-intensive and inefficient.

  • I implemented a simulated list pattern using paired maps: one for items (e.g., Map<(genre, index), felt252> to store values at specific positions) and one for lengths (e.g., Map<genre, u32> to track how many items are in each list). This allows efficient O(1) appends by writing to the current length index and incrementing the length, and O(n) reads by looping up to the known length. It improves query performance for things like genres and seasons by enabling direct access without scanning all records, reducing storage inefficiency from linear searches.

  • For error handling, I've replaced unwrap() with expect('Error message') in type conversions, e.g., audition.start_timestamp.try_into().expect('Timestamp conversion failed'), to prevent panics and provide better error messages.

  • I've added some new tests as recommended, including a large dataset stress test (creating 50+ seasons/auditions and querying them) and overflow edge cases with should_panic expectations.

Let me know if this covers everything or if I missed something, I'll be on it right away

@CollinsC1O
Copy link
Collaborator

Hello @augustin-v can you resolve the conflict pls

@augustin-v
Copy link
Author

augustin-v commented Jul 22, 2025

Hello @augustin-v can you resolve the conflict pls

Hi @CollinsC1O
done, hope everything is fine

@CollinsC1O
Copy link
Collaborator

hello @augustin-v can you use scarb fmt to format your code

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.

2 participants