File size: 2,102 Bytes
808efc6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python3
"""Score one model answer for a DeFiReplay task.

Usage:
  python3 scripts/evaluate.py <incident> <answer.sol>
  cat answer.sol | python3 scripts/evaluate.py <incident>

<answer.sol> is the model's completion: either a bare Solidity test-function body,
or a ```solidity ...``` fenced block. Extra helper code may be appended after the
marker lines `// === ATTACK_AUX_MEMBERS ===` and `// === ATTACK_AUX_CONTRACTS ===`
(see the dataset card). Prints JSON: {"incident","score","reason"}. score is 1
(the exploit/sandwich reached the calibrated profit threshold) or 0.

Requires Foundry (`forge`) on PATH and a mainnet RPC (ETH_RPC_URL, defaults to a
public endpoint). The task's grading oracle lives in tasks/<incident>/meta.json and
is injected by the verifier — it is never model-controlled.
"""
import json, sys, os
from pathlib import Path

REPO = Path(__file__).resolve().parent.parent
os.environ.setdefault("DEFIREPLAY_ROOT", str(REPO))
sys.path.insert(0, str(REPO / "verifier"))
import foundry_verifier as fv  # noqa: E402


def main():
    if len(sys.argv) < 2:
        print(__doc__); sys.exit(2)
    incident = sys.argv[1]
    if len(sys.argv) >= 3:
        answer = Path(sys.argv[2]).read_text()
    else:
        answer = sys.stdin.read()
    task_dir = REPO / "tasks" / incident
    if not (task_dir / "template.sol").exists():
        print(json.dumps({"incident": incident, "score": 0,
                          "reason": f"unknown task (no tasks/{incident}/template.sol)"}))
        sys.exit(2)
    # tier only changes template selection for the "scratch" variant (not shipped
    # in v0.1); every v0.1 task uses template.sol, so tier is informational.
    ref = json.dumps({"incident": incident, "registry": f"tasks/{incident}",
                      "tier": "scaffolded"})
    if "```" not in answer:
        answer = "```solidity\n" + answer + "\n```"
    score, info = fv.evaluate_foundry(answer, ref)
    print(json.dumps({"incident": incident, "score": score,
                      "reason": info["reason"][:300]}))


if __name__ == "__main__":
    main()