The has_claimed_slice()
function contains redundant and inefficient logic that performs the same table lookup twice, leading to unnecessary gas consumption and potential confusion.
The normal behavior should be to check if a user has claimed their pizza slice by looking up their address in the claimed_users
table once and returning the result.
The specific issue is that the function performs the same table::contains()
operation twice - once in the if condition and again in the return statement, which is redundant and wastes gas.
Likelihood: High
This inefficiency occurs every time the function is called
The redundant logic is always executed when the user has claimed
Gas waste accumulates with frequent view function calls
Impact: Low
Unnecessary gas consumption for view function calls
Code maintainability issues due to redundant logic
Potential confusion for developers reading the code
No security risk, but impacts efficiency
To concretely demonstrate the inefficiency, a dedicated test case, test_redundant_logic_in_has_claimed_slice
, was developed. This test contrasts the behavior of the function when called for a user who has claimed versus one who has not. The debug output serves as a clear, step-by-step analysis, quantifying the performance impact.
Setup
Add the following test case to the end of the sources/pizza_drop.move
file.
Test Code
Execution Command
Test Results and Analysis
The test passes, confirming the function's logical correctness. However, the debug output provides a clear narrative that proves the inefficiency.
Analysis :
The test results clearly illustrate the issue. The test is structured in phases to be easily followed:
Phase 1 confirms the efficient path for an unclaimed user (1 lookup).
Phase 3 confirms the inefficient path for a claimed user (2 lookups).
Phase 4 provides a quantitative comparison, demonstrating that checking a claimed user's status requires double the table lookups (100% overhead) compared to an unclaimed user. This wasted computation translates directly to unnecessary gas costs for off-chain services or users querying this view function.
Alternatively, if you want to maintain the explicit logic:
This mitigation:
Eliminates redundant table lookup operations
Reduces gas consumption for view function calls
Improves code readability and maintainability
Maintains the same functional behavior
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.