Skip to content

SSUE-CU03-110: Validate endBlock in BuyTicket to reject purchases after scheduled duration #506

Description

@davidmelendez

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)

  1. Create a lottery with CreateNewDraw.
  2. Simulate block advancement beyond endBlock using start_cheat_block_number.
  3. Attempt to buy a ticket with BuyTicket.
  4. 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:

  1. 🔹 Verify in BuyTicket if the current block > endBlock using IsDrawActive.
  2. 🔹 Reject purchases if the lottery has ended based on blocks.
  3. 🔹 Maintain compatibility with legacy draws (where endBlock == 0).
  4. 🔹 Improve user experience by avoiding invalid purchases.
  5. 🔹 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:

  1. 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.
  2. 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

  • Modify BuyTicket to validate IsDrawActive.
  • Execute existing tests.
  • Create new tests for functionality.
  • Verify backward compatibility.

⚠️ Before Applying

Please read this guide: Contributor Guidelines

Metadata

Metadata

Assignees

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions