(self, prefix: str, target_md5: str, *, timeout_ms: int = 30000)
| 156 | } |
| 157 | |
| 158 | def solve_pow(self, prefix: str, target_md5: str, *, timeout_ms: int = 30000) -> dict[str, str]: |
| 159 | normalized_prefix = prefix.strip() |
| 160 | normalized_target = target_md5.strip().lower() |
| 161 | if not normalized_prefix or not normalized_target: |
| 162 | raise BadRequestError("pow 计算缺少 prefix 或 md5 目标值") |
| 163 | |
| 164 | started = time.perf_counter() |
| 165 | suffix = 0 |
| 166 | timeout_seconds = max(timeout_ms, 1) / 1000 |
| 167 | while True: |
| 168 | candidate = f"{normalized_prefix}{suffix}" |
| 169 | digest = hashlib.md5(candidate.encode("utf-8")).hexdigest() |
| 170 | if digest == normalized_target: |
| 171 | duration_ms = int((time.perf_counter() - started) * 1000) |
| 172 | return { |
| 173 | "pow_answer": candidate, |
| 174 | "pow_suffix": str(suffix), |
| 175 | "pow_calc_time": str(duration_ms), |
| 176 | } |
| 177 | if (time.perf_counter() - started) >= timeout_seconds: |
| 178 | raise BadRequestError( |
| 179 | "pow 计算超时,腾讯这活儿有点脏,得换线程或 node worker 顶上。", |
| 180 | details={ |
| 181 | "prefix": normalized_prefix, |
| 182 | "target_md5": normalized_target, |
| 183 | "last_suffix": suffix, |
| 184 | }, |
| 185 | ) |
| 186 | suffix += 1 |
| 187 | |
| 188 | def _normalize_collect(self, collect: str | None) -> str: |
| 189 | normalized = (collect or "").strip() |
no test coverage detected