Spaces:
Sleeping
Sleeping
tanaymitra98 commited on
Commit ·
155f69d
1
Parent(s): 7998b79
Emit per-task inference logs and restore 3-task spec
Browse files- inference.py +21 -63
- openenv.yaml +0 -29
inference.py
CHANGED
|
@@ -37,7 +37,6 @@ DEFAULT_TASKS = [
|
|
| 37 |
"task_1_best_practices",
|
| 38 |
"task_2_gas_optimization",
|
| 39 |
"task_3_security",
|
| 40 |
-
"task_4_comprehensive_audit",
|
| 41 |
]
|
| 42 |
|
| 43 |
MAX_STEPS = 1 # Each task has 1 step in our environment
|
|
@@ -92,7 +91,6 @@ def _build_prompt(source_code: str, task_id: str) -> str:
|
|
| 92 |
"task_1_best_practices": "Find syntax and best-practice issues: missing SPDX license, old compiler version (<0.8.x), missing NatSpec comments, deprecated constructor syntax.",
|
| 93 |
"task_2_gas_optimization": "Find gas optimization opportunities: unbounded loops, redundant storage reads, missing custom errors (use custom errors instead of require strings).",
|
| 94 |
"task_3_security": "Find security vulnerabilities: reentrancy bugs, missing access control, tx.origin usage for authorization, integer overflow/underflow.",
|
| 95 |
-
"task_4_comprehensive_audit": "Find a complete mix of issues across best-practices, gas optimization, and security vulnerabilities.",
|
| 96 |
}
|
| 97 |
return (
|
| 98 |
"Review this Solidity contract and return ONLY a JSON array of findings. "
|
|
@@ -178,46 +176,6 @@ def _fallback_actions(source_code: str, task_id: str) -> List[Dict[str, Any]]:
|
|
| 178 |
}
|
| 179 |
]
|
| 180 |
|
| 181 |
-
if task_id == "task_4_comprehensive_audit":
|
| 182 |
-
findings: List[Dict[str, Any]] = []
|
| 183 |
-
if "pragma solidity" in lowered:
|
| 184 |
-
findings.append(
|
| 185 |
-
{
|
| 186 |
-
"issue_type": "old_compiler_version",
|
| 187 |
-
"line_number": _find_line_number(source_code, "pragma solidity", 2),
|
| 188 |
-
"description": "Compiler pragma should use ^0.8.x",
|
| 189 |
-
"severity": "Low",
|
| 190 |
-
}
|
| 191 |
-
)
|
| 192 |
-
if "for" in lowered and ".length" in lowered:
|
| 193 |
-
findings.append(
|
| 194 |
-
{
|
| 195 |
-
"issue_type": "unbounded_loop",
|
| 196 |
-
"line_number": _find_line_number(source_code, "for", 10),
|
| 197 |
-
"description": "Loop uses dynamic array length without bounds",
|
| 198 |
-
"severity": "Medium",
|
| 199 |
-
}
|
| 200 |
-
)
|
| 201 |
-
if "tx.origin" in lowered:
|
| 202 |
-
findings.append(
|
| 203 |
-
{
|
| 204 |
-
"issue_type": "tx_origin_auth",
|
| 205 |
-
"line_number": _find_line_number(source_code, "tx.origin", 11),
|
| 206 |
-
"description": "Authorization uses tx.origin",
|
| 207 |
-
"severity": "Critical",
|
| 208 |
-
}
|
| 209 |
-
)
|
| 210 |
-
if not findings:
|
| 211 |
-
findings = [
|
| 212 |
-
{
|
| 213 |
-
"issue_type": "missing_spdx",
|
| 214 |
-
"line_number": 1,
|
| 215 |
-
"description": "Missing SPDX license identifier",
|
| 216 |
-
"severity": "Low",
|
| 217 |
-
}
|
| 218 |
-
]
|
| 219 |
-
return findings
|
| 220 |
-
|
| 221 |
# Default fallback
|
| 222 |
return [
|
| 223 |
{
|
|
@@ -264,20 +222,22 @@ def _call_model(client: OpenAI, prompt: str) -> List[Dict[str, Any]]:
|
|
| 264 |
def main() -> None:
|
| 265 |
client = OpenAI(base_url=API_BASE_URL, api_key=API_KEY or "missing-key")
|
| 266 |
env = SolidityGuardEnv()
|
| 267 |
-
|
| 268 |
-
rewards: List[float] = []
|
| 269 |
-
steps_taken = 0
|
| 270 |
-
score = 0.0
|
| 271 |
-
success = False
|
| 272 |
task_list = [TASK_NAME] if TASK_NAME else DEFAULT_TASKS
|
|
|
|
| 273 |
|
| 274 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 275 |
|
| 276 |
-
|
| 277 |
-
|
|
|
|
| 278 |
observation = env.reset(task_id=task_id)
|
| 279 |
|
| 280 |
for _ in range(MAX_STEPS):
|
|
|
|
| 281 |
steps_taken += 1
|
| 282 |
error: Optional[str] = None
|
| 283 |
action_text = "[]"
|
|
@@ -301,7 +261,7 @@ def main() -> None:
|
|
| 301 |
|
| 302 |
rewards.append(reward)
|
| 303 |
log_step(
|
| 304 |
-
step=
|
| 305 |
action=action_text,
|
| 306 |
reward=reward,
|
| 307 |
done=done,
|
|
@@ -311,20 +271,18 @@ def main() -> None:
|
|
| 311 |
if done:
|
| 312 |
break
|
| 313 |
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
success = score >= SUCCESS_SCORE_THRESHOLD
|
| 317 |
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
print(f"[DEBUG] Exception during run: {exc}", flush=True)
|
| 325 |
|
| 326 |
-
|
| 327 |
-
|
| 328 |
|
| 329 |
|
| 330 |
if __name__ == "__main__":
|
|
|
|
| 37 |
"task_1_best_practices",
|
| 38 |
"task_2_gas_optimization",
|
| 39 |
"task_3_security",
|
|
|
|
| 40 |
]
|
| 41 |
|
| 42 |
MAX_STEPS = 1 # Each task has 1 step in our environment
|
|
|
|
| 91 |
"task_1_best_practices": "Find syntax and best-practice issues: missing SPDX license, old compiler version (<0.8.x), missing NatSpec comments, deprecated constructor syntax.",
|
| 92 |
"task_2_gas_optimization": "Find gas optimization opportunities: unbounded loops, redundant storage reads, missing custom errors (use custom errors instead of require strings).",
|
| 93 |
"task_3_security": "Find security vulnerabilities: reentrancy bugs, missing access control, tx.origin usage for authorization, integer overflow/underflow.",
|
|
|
|
| 94 |
}
|
| 95 |
return (
|
| 96 |
"Review this Solidity contract and return ONLY a JSON array of findings. "
|
|
|
|
| 176 |
}
|
| 177 |
]
|
| 178 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
# Default fallback
|
| 180 |
return [
|
| 181 |
{
|
|
|
|
| 222 |
def main() -> None:
|
| 223 |
client = OpenAI(base_url=API_BASE_URL, api_key=API_KEY or "missing-key")
|
| 224 |
env = SolidityGuardEnv()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
task_list = [TASK_NAME] if TASK_NAME else DEFAULT_TASKS
|
| 226 |
+
global_step = 0
|
| 227 |
|
| 228 |
+
for task_id in task_list:
|
| 229 |
+
rewards: List[float] = []
|
| 230 |
+
steps_taken = 0
|
| 231 |
+
score = 0.0
|
| 232 |
+
success = False
|
| 233 |
|
| 234 |
+
log_start(task=task_id, env=BENCHMARK, model=MODEL_NAME)
|
| 235 |
+
|
| 236 |
+
try:
|
| 237 |
observation = env.reset(task_id=task_id)
|
| 238 |
|
| 239 |
for _ in range(MAX_STEPS):
|
| 240 |
+
global_step += 1
|
| 241 |
steps_taken += 1
|
| 242 |
error: Optional[str] = None
|
| 243 |
action_text = "[]"
|
|
|
|
| 261 |
|
| 262 |
rewards.append(reward)
|
| 263 |
log_step(
|
| 264 |
+
step=global_step,
|
| 265 |
action=action_text,
|
| 266 |
reward=reward,
|
| 267 |
done=done,
|
|
|
|
| 271 |
if done:
|
| 272 |
break
|
| 273 |
|
| 274 |
+
score = _safe_score(sum(rewards) / max(len(rewards), 1))
|
| 275 |
+
success = score >= SUCCESS_SCORE_THRESHOLD
|
|
|
|
| 276 |
|
| 277 |
+
except Exception as exc:
|
| 278 |
+
if not rewards:
|
| 279 |
+
rewards = [0.01]
|
| 280 |
+
steps_taken = 1
|
| 281 |
+
score = _safe_score(sum(rewards) / max(len(rewards), 1))
|
| 282 |
+
print(f"[DEBUG] Exception during run for {task_id}: {exc}", flush=True)
|
|
|
|
| 283 |
|
| 284 |
+
finally:
|
| 285 |
+
log_end(success=success, steps=steps_taken, score=score, rewards=rewards)
|
| 286 |
|
| 287 |
|
| 288 |
if __name__ == "__main__":
|
openenv.yaml
CHANGED
|
@@ -4,49 +4,20 @@ description: OpenEnv RL environment for Solidity security review
|
|
| 4 |
entrypoint: environment.py:SolidityGuardEnv
|
| 5 |
tasks:
|
| 6 |
- id: task_1_best_practices
|
| 7 |
-
name: Best Practices Audit
|
| 8 |
difficulty: easy
|
| 9 |
-
enabled: true
|
| 10 |
description: Detect syntax and best-practice issues in Solidity contracts.
|
| 11 |
max_steps: 1
|
| 12 |
grader: "graders:grade_task_1"
|
| 13 |
-
graders:
|
| 14 |
-
- name: task_1_grader
|
| 15 |
-
enabled: true
|
| 16 |
-
entrypoint: "graders:grade_task_1"
|
| 17 |
- id: task_2_gas_optimization
|
| 18 |
-
name: Gas Optimization Audit
|
| 19 |
difficulty: medium
|
| 20 |
-
enabled: true
|
| 21 |
description: Detect gas optimization opportunities in Solidity contracts.
|
| 22 |
max_steps: 1
|
| 23 |
grader: "graders:grade_task_2"
|
| 24 |
-
graders:
|
| 25 |
-
- name: task_2_grader
|
| 26 |
-
enabled: true
|
| 27 |
-
entrypoint: "graders:grade_task_2"
|
| 28 |
- id: task_3_security
|
| 29 |
-
name: Security Vulnerability Audit
|
| 30 |
difficulty: hard
|
| 31 |
-
enabled: true
|
| 32 |
description: Detect security vulnerabilities in Solidity contracts.
|
| 33 |
max_steps: 1
|
| 34 |
grader: "graders:grade_task_3"
|
| 35 |
-
graders:
|
| 36 |
-
- name: task_3_grader
|
| 37 |
-
enabled: true
|
| 38 |
-
entrypoint: "graders:grade_task_3"
|
| 39 |
-
- id: task_4_comprehensive_audit
|
| 40 |
-
name: Comprehensive Audit
|
| 41 |
-
difficulty: hard
|
| 42 |
-
enabled: true
|
| 43 |
-
description: Perform a complete audit covering best practices, gas, and security.
|
| 44 |
-
max_steps: 1
|
| 45 |
-
grader: "graders:grade_task_4"
|
| 46 |
-
graders:
|
| 47 |
-
- name: task_4_grader
|
| 48 |
-
enabled: true
|
| 49 |
-
entrypoint: "graders:grade_task_4"
|
| 50 |
schemas:
|
| 51 |
observation:
|
| 52 |
type: object
|
|
|
|
| 4 |
entrypoint: environment.py:SolidityGuardEnv
|
| 5 |
tasks:
|
| 6 |
- id: task_1_best_practices
|
|
|
|
| 7 |
difficulty: easy
|
|
|
|
| 8 |
description: Detect syntax and best-practice issues in Solidity contracts.
|
| 9 |
max_steps: 1
|
| 10 |
grader: "graders:grade_task_1"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
- id: task_2_gas_optimization
|
|
|
|
| 12 |
difficulty: medium
|
|
|
|
| 13 |
description: Detect gas optimization opportunities in Solidity contracts.
|
| 14 |
max_steps: 1
|
| 15 |
grader: "graders:grade_task_2"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
- id: task_3_security
|
|
|
|
| 17 |
difficulty: hard
|
|
|
|
| 18 |
description: Detect security vulnerabilities in Solidity contracts.
|
| 19 |
max_steps: 1
|
| 20 |
grader: "graders:grade_task_3"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
schemas:
|
| 22 |
observation:
|
| 23 |
type: object
|