-
Notifications
You must be signed in to change notification settings - Fork 0
Updates checksum calculation from MD5 to SHA256 #160
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -9,6 +9,9 @@ | |||||
| from io import BytesIO | ||||||
| from django.conf import settings | ||||||
|
|
||||||
| from problems.services.storage import get_problem_testcase_hash | ||||||
|
|
||||||
|
|
||||||
| logger = logging.getLogger(__name__) | ||||||
|
|
||||||
| # Sandbox API 設定 | ||||||
|
|
@@ -58,6 +61,7 @@ def submit_to_sandbox(submission): | |||||
|
|
||||||
| Raises: | ||||||
| requests.RequestException: API 請求失敗 | ||||||
| ValueError: 題目測資包不存在 | ||||||
| """ | ||||||
| from problems.models import Problems, Problem_subtasks | ||||||
|
|
||||||
|
|
@@ -71,7 +75,7 @@ def submit_to_sandbox(submission): | |||||
| time_limit = subtask.time_limit_ms / 1000.0 # 轉換成秒 | ||||||
| else: | ||||||
| time_limit = 1.0 # 預設 1 秒 | ||||||
|
|
||||||
| if subtask and subtask.memory_limit_mb: | ||||||
| memory_limit = subtask.memory_limit_mb * 1024 # 轉換成 KB | ||||||
| else: | ||||||
|
|
@@ -80,66 +84,54 @@ def submit_to_sandbox(submission): | |||||
| # 3. 轉換語言代碼 | ||||||
| language = convert_language_code(submission.language_type) | ||||||
|
|
||||||
| # 4. 組裝 payload(multipart/form-data) | ||||||
| # 4. 取得題目包 hash | ||||||
| problem_hash = get_problem_testcase_hash(submission.problem_id) | ||||||
| if not problem_hash: | ||||||
| raise ValueError(f"Problem {submission.problem_id} has no testcase package") | ||||||
|
|
||||||
| # 5. 組裝 payload(multipart/form-data) | ||||||
| data = { | ||||||
| 'submission_id': str(submission.id), | ||||||
| 'problem_id': str(submission.problem_id), | ||||||
| 'problem_hash': f'TODO_HASH_{submission.problem_id}', # TODO: 實現題目包管理後取得真實 hash | ||||||
| 'mode': 'normal', # 目前只支援 single file | ||||||
| 'problem_hash': problem_hash, | ||||||
| 'mode': 'normal', | ||||||
| 'language': language, | ||||||
| 'file_hash': submission.code_hash, | ||||||
| 'time_limit': time_limit, | ||||||
| 'memory_limit': memory_limit, | ||||||
| # 只有在啟用自訂 checker 時才使用設定的 checker_name,否則強制使用 'diff' | ||||||
| 'use_checker': problem.use_custom_checker, | ||||||
| 'checker_name': problem.checker_name if problem.use_custom_checker else 'diff', | ||||||
| 'use_static_analysis': False, # TODO: 從 assignment 設定取得 | ||||||
| 'priority': 0, # 一般優先級 | ||||||
| 'callback_url': f'{settings.BACKEND_BASE_URL}/submissions/callback/', # Sandbox 判題完成後回傳結果的 URL | ||||||
| 'use_checker': getattr(problem, 'use_custom_checker', False), | ||||||
| 'checker_name': getattr(problem, 'checker_name', 'diff') or 'diff', | ||||||
| 'use_static_analysis': False, | ||||||
| 'priority': 0, | ||||||
| 'callback_url': f'{settings.BACKEND_BASE_URL}/submissions/callback/', | ||||||
| } | ||||||
|
|
||||||
| # 5. 準備檔案 | ||||||
| filename = f'solution.{get_file_extension(language)}' | ||||||
| file_content = submission.source_code.encode('utf-8') | ||||||
| # 6. 準備程式碼檔案 | ||||||
| source_code = submission.source_code or '' | ||||||
| file_extension = get_file_extension(submission.language_type) | ||||||
|
||||||
| file_extension = get_file_extension(submission.language_type) | |
| file_extension = get_file_extension(language) |
Copilot
AI
Dec 27, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The filename is missing a dot before the extension. The current code generates filenames like 'solutionpy' or 'solutioncpp' instead of 'solution.py' or 'solution.cpp'. This should be changed to f'solution.{file_extension}'.
| 'file': (f'solution{file_extension}', BytesIO(source_code.encode('utf-8')), 'text/plain') | |
| 'file': (f'solution.{file_extension}', BytesIO(source_code.encode('utf-8')), 'text/plain') |
Copilot
AI
Dec 27, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The informative logging statements that were present in the old code have been removed. The old implementation logged the submission details before sending the request and the sandbox response upon success. These logs are valuable for debugging and monitoring. Consider re-adding at least a log statement before sending the request (e.g., logger.info(f'Submitting to Sandbox: submission_id={submission.id}, problem_id={submission.problem_id}')) and after receiving a successful response.
Copilot
AI
Dec 27, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error handling has been reduced compared to the previous implementation. The old code had specific handling for requests.RequestException and a general Exception catch with appropriate logging. Without these handlers, network errors and other exceptions will propagate without being logged, making debugging difficult. Consider re-adding error handling for requests.RequestException and ValueError (for missing testcase packages) with appropriate logging.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line still references "MD5" when describing the checksum verification process. Since the PR updates checksum calculation from MD5 to SHA256 throughout the system, this should be updated to say "SHA256" instead of "MD5" for consistency with the rest of the changes.