You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
2.0 KiB
71 lines
2.0 KiB
# ds_call.py
|
|
import json
|
|
import subprocess
|
|
import pathlib
|
|
import time
|
|
|
|
def calc_pow_with_node(
|
|
node_runner_path: str,
|
|
wasm_path: str,
|
|
algorithm: str,
|
|
challenge: str,
|
|
salt: str,
|
|
difficulty: int,
|
|
expire_at: int,
|
|
timeout: int = 120,
|
|
):
|
|
"""
|
|
通过 Node 的 runner 调 ds.js + wasm,返回 {"ok": bool, "answer": number|None, "error"?: str}
|
|
"""
|
|
payload = {
|
|
"wasmPath": wasm_path,
|
|
"algorithm": algorithm,
|
|
"challenge": challenge,
|
|
"salt": salt,
|
|
"difficulty": difficulty,
|
|
"expireAt": expire_at,
|
|
}
|
|
print("pload",payload)
|
|
proc = subprocess.run(
|
|
["node", node_runner_path],
|
|
input=json.dumps(payload),
|
|
text=True,
|
|
capture_output=True,
|
|
timeout=timeout,
|
|
cwd=str(pathlib.Path(node_runner_path).resolve().parent),
|
|
)
|
|
out = proc.stdout.strip()
|
|
if proc.returncode != 0:
|
|
# Node 可能也输出了 JSON,尽量解析
|
|
try:
|
|
data = json.loads(out or "{}")
|
|
except Exception:
|
|
data = {"ok": False, "error": f"node exited {proc.returncode}: {proc.stderr}"}
|
|
return data
|
|
try:
|
|
return json.loads(out)
|
|
except Exception as e:
|
|
return {"ok": False, "error": f"invalid JSON from node: {e}", "_raw": out}
|
|
|
|
|
|
if __name__ == "__main__":
|
|
base = pathlib.Path(__file__).resolve().parent
|
|
js_data_path = base / "js_data"
|
|
|
|
# 1) 指向 js_data 文件夹中的 Node 小封装
|
|
node_runner = str(js_data_path / "js_runner.js")
|
|
|
|
# 2) 指向 js_data 文件夹中的 wasm 文件
|
|
wasm_file = str(js_data_path / "sha3_wasm_bg.wasm")
|
|
|
|
# 3) 用你给的那组参数
|
|
res = calc_pow_with_node(
|
|
node_runner_path=node_runner,
|
|
wasm_path=wasm_file,
|
|
algorithm="DeepSeekHashV1",
|
|
challenge="b9eea2caa6f7da27d6bb7aa3a1a34e77669029095f3362a1781d349ebfedff7f",
|
|
salt="a2b3975c7401892ea34d",
|
|
difficulty=144000,
|
|
expire_at=1760945235071,
|
|
)
|
|
print(res)
|