lfglabs-dev / api.starknet.quest

starknet.quest rust backend
6 stars 24 forks source link

Fix questions ids #292

Open Marchand-Nicolas opened 1 week ago

Marchand-Nicolas commented 1 week ago

In src/endpoints/admin/quiz/create_question.rs around line 68, it doesn't really makes sense: the next_quiz_question_id should not be the last_task_id + 1 but instead it should be the last_question_id + 1.

Proposed to do:

Reading the db every time is important to make sure it's always synced, and also if we restart the server the state is lost. But writing in the state is also important, because when receiving simultaneous requests, reading to the db is too slow and we would end up with multiple questions having the same id.

Abeeujah commented 1 week ago

Hi, can I work on this?

ikemHood commented 5 days ago

I am applying to this issue via OnlyDust platform.

My background and how it can be leveraged

Hello @Marchand-Nicolas , I would like to fix this...

async fn get_last_question_id(collection: &mongodb::Collection) -> i64 { let options = mongodb::options::FindOneOptions::builder() .sort(doc! { "id": -1 }) .build();

match collection.find_one(None, options).await {
    Ok(Some(doc)) => doc.id,
    _ => 0, // If no documents found or error occurred, start from 0
}

}


- finally, update `/admin/tasks/quiz/question/create` endpoint to use `get_last_question_id`
```rs
  let next_quiz_question_id = get_next_question_id(&state, &quiz_questions_collection).await;

ETA: 1 hrs.

martinvibes commented 4 days ago

I am applying to this issue via OnlyDust platform.

My background and how it can be leveraged

hello @ i'm an experienced frontend developer and a blockchain developer i would love to work on this issue Pleasee kindly assign :)

CollinsC1O commented 4 days ago

I am applying to this issue via OnlyDust platform.

My background and how it can be leveraged

Hello I'm a front-end and a blockchain developer and will love to work on this issue

Ugo-X commented 4 days ago

I am applying to this issue via OnlyDust platform.

My background and how it can be leveraged

I'm a Full Stack Blockchain Developer with demonstrable expertise across modern web and blockchain technologies. My tech stack centers on Next.js, TypeScript, React, and Node.js for web development, complemented by Three.js for 3D experiences, and Solidity/Rust for blockchain solutions.

What sets me apart is my proven track record on OnlyDust, where I've made 83 significant contributions across 15 different projects since Edition 1. This extensive involvement reflects my ability to deliver quality solutions under tight deadlines while adapting to diverse project requirements.

With my deep experience in hackathon environments, I bring both technical excellence and practical development skills to the table. I'm confident I can contribute meaningfully to pushing the boundaries of what's possible in blockchain development while maintaining a focus on user-centric solutions.

My profile (https://app.onlydust.com/u/Ugo-X) showcases my consistent ability to deliver results across various blockchain challenges. I'm excited to bring this experience and drive for innovation to your team.

How I plan on tackling this issue

First, I'll add a last_question_id to the state struct - just like we have for tasks. Makes sense to keep them consistent.

Then for the ID generation:

The state acts like a cache here - keeps things fast for bulk requests while the db check ensures we don't get out of sync after restarts.

Main thing is avoiding duplicate IDs while staying performant. Pretty standard race condition stuff.

Benjtalkshow commented 4 days ago

I am applying to this issue via OnlyDust platform.

My background and how it can be leveraged

I am a Full Stack Developer specializing in Next.js, TypeScript, Node.js, Cairo and Rust . With over 31 contributions across projects in the OnlyDust ecosystem, I’ve developed strong proficiency in delivering high-quality solutions and resolving complex issues within tight deadlines. My experience spans frontend, backend, smart contracts, and the optimization and maintenance of scalable codebases.

How I plan on tackling this issue

I will fix the question IDs in the create_question.rs file. The current implementation incorrectly sets the next_quiz_question_id as the last_task_id + 1. Instead, it should be based on the last_question_id + 1. To address this, I will first retrieve the last question document from the database and collect its ID. Then, I will add a new last_question_idfield in the state, similar to how we handle the last_task_id. By comparing the two IDs, I will ensure that we return the correct one for the next question.

It’s important to read from the database every time to maintain synchronization, especially since the state can be lost if the server restarts. However, I will also ensure that writing to the state happens to manage simultaneous requests effectively. This way, we can prevent multiple questions from having the same ID, maintaining the integrity of our quiz system.

Dprof-in-tech commented 4 days ago

I am applying to this issue via OnlyDust platform.

My background and how it can be leveraged

Hello, I'm Dprof-in-tech, a seasoned Full Stack Blockchain Developer, and I'm excited to be part of ODHACK 9! I have a strong foundation in technologies such as Next.js, TypeScript, JavaScript, React, Node.js, Rust, and Cairo, I've built extensive experience across the blockchain development landscape.

I first got involved with OnlyDust during Edition 2, and since then, I've made 39 contributions across 11 different projects. Working on the platform has really helped me sharpen my skills, especially when it comes to delivering great solutions under tight deadlines. I love combining technical know-how with a user-focused approach, whether it's building immersive 3D experiences or crafting smart contracts that solve real-world problems.

Throughout, I've consistently demonstrated the ability to adapt and contribute effectively to diverse challenges. I'm confident in my ability to tackle new problems and drive innovation within the blockchain space. As we kick off ODHACK 9, I'm eager to apply my previous experience and technical expertise to push the boundaries of what's possible.

You can view my public profile on OnlyDust here: https://app.onlydust.com/u/Dprof-in-tech

How I plan on tackling this issue

Here's my approach to fixing the question ID generation logic in Rust:

  1. State Management Update: I’ll create a new field as specified to take the last question ID. I would also add this new field to the state so that the value of the last question ID can be accessible across the app.

  2. ID Generation Logic:

    • I'll implement a new function to handle ID synchronization: This function would read the ID from the db at set periodic intervals and update the state of the last question ID field so that it is always sychronized. This would be done taking into account how slow it is to read from the DB and this process would be optimized or speed and security.
  3. Integration:

    • Update create_question.rs to use new logic
    • Replace existing ID generation around line 68
    • Ensure atomic operations for thread safety
  4. Testing Strategy:

    • Add concurrent request tests
    • Test server restart scenarios
    • Verify ID sequence integrity
    • Test edge cases (empty DB, overflow)

Implementation Plan:

Key Considerations:

jaiminRaiyani commented 4 days ago

I am applying to this issue via OnlyDust platform.

My background and how it can be leveraged

I have a strong foundation in software engineering, particularly in developing web applications and working with various programming languages and technologies, including JavaScript, Node.js, Rust, and database management systems like MongoDB.

How I plan on tackling this issue

Understand Requirements: Begin by clarifying the requirements for generating unique quiz question IDs. Identify the need for synchronization between the database and the in-memory state.

Design State Management:

Extend the application state to include a last_question_id field. Use a mutex to safely manage access to this state in a concurrent environment. Implement Database Interaction:

Create a function to retrieve the last question ID from the database. This function will be called every time a new question is created to ensure accuracy. Implement error handling to manage potential failures during database access. Modify the ID Generation Logic:

In the handler, retrieve the last question ID from both the database and the state. Calculate the next question ID by taking the maximum of the last IDs from both sources and adding one. Testing and Validation:

Test the implementation under various conditions, including simultaneous requests, to ensure that the ID generation remains robust and consistent. Validate that the application behaves correctly when the server is restarted and the state is reset. Documentation:

Document the changes made, explaining the rationale behind the new logic for future reference and maintenance.

mimisavage commented 4 days ago

I am applying to this issue via OnlyDust platform.

My background and how it can be leveraged

Hi, I’m Mimi Sav, a frontend and blockchain developer. This is my first time contributing to this repository, and I'm excited about the opportunity to collaborate.

How I plan on tackling this issue

To address the issue regarding the calculation of the next_quiz_question_id in src/endpoints/admin/quiz/create_question.rs, I’ll implement the following steps:

Retrieve the Last Question ID: I'll modify the code to fetch the last question document from the database to get its ID instead of relying on the last_task_id. Add last_question_id Field: I’ll introduce a new field called last_question_id in the state to track the most recent question ID similarly to how last_task_id is managed. Compare and Determine Next ID: I’ll implement logic to compare the last_question_id from the state with the ID retrieved from the database. The greater value will determine the next_quiz_question_id. Update State Accordingly: After creating a new question, I'll update the state with the new last_question_id to ensure it reflects the most recent addition. Maintain Sync with the Database: By reading from the database each time, I’ll ensure that the state is always synchronized, especially in scenarios where the server is restarted, which could otherwise result in ID conflicts. Optimize for Concurrent Requests: Since reading from the database can be slow, the new state management will help prevent race conditions where multiple requests could lead to the creation of questions with the same ID.

saimeunt commented 4 days ago

I am applying to this issue via OnlyDust platform.

My background and how it can be leveraged

I'm a senior Full Stack Web3 Developer having contributed to several OD Hacks in the past.

How I plan on tackling this issue

I will fix the route as described in the issue which is pretty self-explanatory.

0xdevcollins commented 4 days ago

I am applying to this issue via OnlyDust platform.

My background and how it can be leveraged

Hi, I'm Collins a frontend and blockchain developer, and an active contributor on OnlyDust. You can check out my profile here: https://app.onlydust.com/u/0xdevcollins.

How I plan on tackling this issue

To address the issue regarding the next_quiz_question_id in src/endpoints/admin/quiz/create_question.rs, I will implement the following plan:

I will start by modifying the code to fetch the last question document from the database instead of relying on the last_task_id. This involves querying the database to retrieve the most recent question and collecting its ID. I will introduce a new field, last_question_id, in the state to store this value, similar to how last_task_id is handled.

Next, I will compare the last_question_id with the existing last_task_id to determine which one is newer. Based on this comparison, I will increment the relevant ID by one to generate the next_quiz_question_id.

aniruddhaaps commented 4 days ago

I am applying to this issue via OnlyDust platform.

My background and how it can be leveraged

I am a python dev . A new-comer here, willing and ready to contribute to solve the issue.