ISSUE-CU03-110: Validate endBlock in BuyTicket to reject purchases after scheduled duration
✨ Issue Request
Implement validation in the BuyTicket function to check if the current block is greater than the lottery's endBlock and reject purchases if applicable, aligning with the new block-based duration system.
📌 Description
This issue addresses the need to synchronize ticket purchase logic with the lottery's scheduled duration based on blocks. Currently, BuyTicket only checks the isActive flag, but does not compare the current block with endBlock, allowing purchases even after the lottery should have ended according to the block system. A validation is required that uses the IsDrawActive function to reject purchases outside the block range.
🛠️ Steps to Reproduce (if applicable)
- Create a lottery with
CreateNewDraw.
- Simulate block advancement beyond
endBlock using start_cheat_block_number.
- Attempt to buy a ticket with
BuyTicket.
- Observe that the purchase is allowed even though the draw should be inactive based on blocks.
🖼️ Screenshots (if applicable)
Not applicable for this functionality request.
🎯 Expected Behavior
The implementation must:
- 🔹 Verify in
BuyTicket if the current block > endBlock using IsDrawActive.
- 🔹 Reject purchases if the lottery has ended based on blocks.
- 🔹 Maintain compatibility with legacy draws (where
endBlock == 0).
- 🔹 Improve user experience by avoiding invalid purchases.
- 🔹 Align with the block system as the source of truth for duration.
🚀 Suggested Solution / Feature Request
Implement the following modifications in the Lottery.cairo contract:
1. Modification of BuyTicket:
- Add validation after
AssertDrawExists to check self.IsDrawActive(drawId).
- Emit specific error if the draw has ended based on blocks.
2. Reuse of existing logic:
- Use the already implemented
IsDrawActive function, which compares blocks and handles fallback to timestamps.
3. Interface update:
- No changes required in
ILottery, as it uses existing functions.
📌 Additional Notes
- Compatibility: Changes must be backward compatible and not affect already deployed contracts.
- Testing: All existing tests must be executed to identify regressions and create new tests for block validation.
- Gas: Consider impact on gas consumption due to the additional call to
get_block_number.
🧪 Testing Requirements
Tests to create in test_CU03.cairo:
-
Test for block validation in BuyTicket:
- Create a lottery.
- Verify that purchase is allowed before
endBlock.
- Simulate block >
endBlock and verify that purchase is rejected.
-
Test for compatibility with legacy draws:
- Use draw with
endBlock == 0.
- Verify that timestamp fallback works correctly.
Existing tests to verify:
- Execute all existing tests to identify regressions.
- Modify tests that use
BuyTicket to include advanced block scenarios.
📋 Implementation Checklist
⚠️ Before Applying
Please read this guide: Contributor Guidelines
ISSUE-CU03-110: Validate endBlock in BuyTicket to reject purchases after scheduled duration
✨ Issue Request
Implement validation in the
BuyTicketfunction to check if the current block is greater than the lottery'sendBlockand reject purchases if applicable, aligning with the new block-based duration system.📌 Description
This issue addresses the need to synchronize ticket purchase logic with the lottery's scheduled duration based on blocks. Currently,
BuyTicketonly checks theisActiveflag, but does not compare the current block withendBlock, allowing purchases even after the lottery should have ended according to the block system. A validation is required that uses theIsDrawActivefunction to reject purchases outside the block range.🛠️ Steps to Reproduce (if applicable)
CreateNewDraw.endBlockusingstart_cheat_block_number.BuyTicket.🖼️ Screenshots (if applicable)
Not applicable for this functionality request.
🎯 Expected Behavior
The implementation must:
BuyTicketif the current block >endBlockusingIsDrawActive.endBlock == 0).🚀 Suggested Solution / Feature Request
Implement the following modifications in the
Lottery.cairocontract:1. Modification of
BuyTicket:AssertDrawExiststo checkself.IsDrawActive(drawId).2. Reuse of existing logic:
IsDrawActivefunction, which compares blocks and handles fallback to timestamps.3. Interface update:
ILottery, as it uses existing functions.📌 Additional Notes
get_block_number.🧪 Testing Requirements
Tests to create in
test_CU03.cairo:Test for block validation in BuyTicket:
endBlock.endBlockand verify that purchase is rejected.Test for compatibility with legacy draws:
endBlock == 0.Existing tests to verify:
BuyTicketto include advanced block scenarios.📋 Implementation Checklist
BuyTicketto validateIsDrawActive.Please read this guide: Contributor Guidelines