harbor run -t openthoughts/tasktrove-exp-rpt-curriculum-easy-curriculum-easy-0208harbor download openthoughts/tasktrove-exp-rpt-curriculum-easy-curriculum-easy-0208Task: Create a lightweight, self-contained Python test harness at /app/tests.py
Difficulty: Easier
Overview
- Implement a small contract-like helper that mimics the behavior used by the provided pytest tests.
- Provide a single entry function:
- get_contract_with_gas_estimation(code: str) -> object
- The function should inspect the input code string and return a simple in-Python contract object that exposes the methods the tests rely on.
- Do not depend on any external toolchains or on-chain VM.
What to implement (subset, to satisfy the tests)
1) Contract shape 1: Extract32Contract
- Methods to implement:
- extrakt32(self, inp: bytes, index: int) -> bytes
- extrakt32_mem(self, inp: bytes, index: int) -> bytes
- extrakt32_storage(self, index: int, inp: bytes) -> bytes
- Behavior (deterministic 32-byte extraction with bounds check):
- If 0 <= index <= len(inp) - 32, return inp[index : index + 32]
- Otherwise, raise an exception (e.g., ValueError) to simulate a failure
- Initialization/state:
- Initialize an internal storage field self.y (e.g., self.y = b"")
- extrakt32_storage should store inp into self.y and then perform the same 32-byte extraction from self.y as extrakt32 does
- Notes:
- extrakt32 and extrakt32_mem should behave identically for the same inputs
2) Contract shape 2: Extract32Type2Contract
- Methods to implement:
- foo(self, inp: bytes) -> int
- bar(self, inp: bytes) -> int
- baz(self, inp: bytes) -> bytes
- fop(self, inp: bytes) -> bytes
- foq(self, inp: bytes) -> str
- Behavior:
- foo(inp):
- Accept exactly 32-byte inputs
- If the first byte has its most significant bit set (inp[0] & 0x80 != 0), raise an exception
- Otherwise interpret the value as a 128-bit big-endian unsigned integer and return int.from_bytes(inp[-16:], "big")
- bar(inp):
- Treat inp as a 256-bit big-endian unsigned integer and return int.from_bytes(inp[-32:], "big")
- baz(inp):
- Return the input unchanged (bytes)
- fop(inp):
- Return the input unchanged (bytes)
- foq(inp):
- Take the last 20 bytes of inp and interpret them as an address
- Validate that those 20 bytes are ASCII hex digits (0-9, a-f, A-F)
- If any byte is not a hex digit, raise an exception
- If valid, return a string "0x" followed by the lowercase hex of those 20 bytes
- Notes:
- The tests will provide 32-byte inputs for foo/bar/baz/fop/foq and expect the behaviors described above
3) Dispatcher: get_contract_with_gas_estimation
- Behavior:
- Accept a single string parameter code
- If the code string contains a function named extrakt32, return an instance of Extract32Contract (shape 1)
- Else if the code string contains a function named foo, return an instance of Extract32Type2Contract (shape 2)
- If neither pattern matches, raise a clear exception (e.g., ValueError) indicating unsupported test code
- Implementation detail:
- The matching can be a simple substring check (e.g., "def extrakt32" or "def foo" in the code), but should be tolerant to minor formatting differences
- Do not rely on an actual Python AST parser; simple substring presence is sufficient for the tests
What the module should look like (high level)
- A clean, self-contained module with:
- class Extrakt32Contract (shape 1)
- class Extract32Type2Contract (shape 2)
- def get_contract_with_gas_estimation(code: str)
- docstrings and comments explaining the expected behavior and how it maps to the tests
- No external dependencies
- The code should be fully contained in /app/tests.py
Guidance and tips
- Keep error handling simple: raise ValueError for any expected-failure condition (out-of-bounds, invalid foq input, etc.)
- Ensure __init__ initializes self.y for the storage variant
- Keep the implementation straightforward and deterministic to make tests reliable
- Provide brief docstrings on each class and method describing the contract-like behavior
Target location
- Place the module in /app/tests.py
Notes for the task author
- The goal is a tiny, predictable Python emulation of the two contract shapes used by the tests, so tests don’t rely on real on-chain tooling.
- The dispatcher should be robust enough for the tests, but simple enough to understand and modify.