How NOT to write handlers! How we missed a edge case due to hander bias

3 min read 5 months ago
Published on Jul 12, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Step-by-Step Tutorial:

  1. Identify the Bug in the Handler:

    • Watch the video to understand the bug in the handler code.
    • Note the specifics of the bug, which is the fetch price returning a different price in the same transaction.
  2. Understand the Impact of the Bug:

    • Recognize the potential risk of a 50% arbitrage due to the bug.
    • Understand the importance of addressing this bug in the handler code.
  3. Navigate to the Contract Repository:

    • Download the contract repository from the provided link.
    • Use a terminal to access the medusa.json file in the contracts folder.
  4. Modify the Contract to Use Aida Price Feed Tester:

    • Change the medusa.json file to utilize the Aida Price Feed Tester.
    • Ensure the modifications compile successfully.
  5. Set Up Medusa for Testing:

    • Understand the challenges of running Medusa and Kidna locally.
    • Set up a CI/CD system, like Antonio did, to facilitate contract testing.
  6. Run Medusa and Analyze Coverage Report:

    • Run Medusa to generate a coverage report for the contracts.
    • Analyze the coverage report to identify areas with low coverage.
  7. Make Educated Guesses for Improving Coverage:

    • Identify potential reasons for low coverage based on the coverage report.
    • Make educated guesses on how to improve the coverage of the handler code.
  8. Iteratively Improve Coverage:

    • Modify the handler code to increase coverage gradually.
    • Test different scenarios and adjust the code accordingly.
  9. Address Specific Issues:

    • Address specific issues highlighted in the coverage report.
    • Implement changes to handle edge cases and improve the robustness of the handler code.
  10. Verify Changes and Re-run Medusa:

    • Verify the changes made to the handler code.
    • Re-run Medusa to check if the modifications have positively impacted the coverage.
  11. Evaluate Results and Patience:

    • Assess the coverage improvements achieved.
    • Understand the importance of patience in letting the testing tool explore various scenarios.
  12. Consider Alternative Testing Approaches:

    • Explore the possibility of creating alternative versions of the tester code.
    • Experiment with different testing strategies to enhance coverage.
  13. Finalize Testing and Bug Resolution:

    • Continue testing and refining the handler code to maximize coverage.
    • Aim to find and resolve the identified bug related to the handler bias.
  14. Conclusion and Recommendations:

    • Reflect on the testing process and coverage improvements.
    • Consider the impact of handler bias and the importance of thorough testing.

By following these steps, you can effectively address the handler bug and improve the coverage of the handler code to ensure its reliability and robustness.