refactor: replace WASM-based PoW with a high-performance native Go implementation and add context support for cancellation.

This commit is contained in:
CJACK
2026-04-07 01:20:01 +08:00
parent e7d561694a
commit da778a18fb
11 changed files with 31 additions and 27 deletions

View File

@@ -109,7 +109,7 @@ func (c *Client) GetPow(ctx context.Context, a *auth.RequestAuth, maxAttempts in
data, _ := resp["data"].(map[string]any)
bizData, _ := data["biz_data"].(map[string]any)
challenge, _ := bizData["challenge"].(map[string]any)
answer, err := ComputePow(challenge)
answer, err := ComputePow(ctx, challenge)
if err != nil {
attempts++
continue

View File

@@ -1,6 +1,7 @@
package deepseek
import (
"context"
"encoding/base64"
"encoding/json"
"errors"
@@ -9,7 +10,7 @@ import (
)
// ComputePow 使用纯 Go 实现求解 PoW challenge (DeepSeekHashV1)。
func ComputePow(challenge map[string]any) (int64, error) {
func ComputePow(ctx context.Context, challenge map[string]any) (int64, error) {
algo, _ := challenge["algorithm"].(string)
if algo != "DeepSeekHashV1" {
return 0, errors.New("unsupported algorithm")
@@ -19,7 +20,7 @@ func ComputePow(challenge map[string]any) (int64, error) {
expireAt := toInt64(challenge["expire_at"], 1680000000)
difficulty := toInt64FromFloat(challenge["difficulty"], 144000)
return pow.SolvePow(challengeStr, salt, expireAt, difficulty)
return pow.SolvePow(ctx, challengeStr, salt, expireAt, difficulty)
}
// BuildPowHeader 序列化 {algorithm,challenge,salt,answer,signature,target_path} 为 base64(JSON)。

View File

@@ -13,7 +13,7 @@ func TestPreloadPowNoOp(t *testing.T) {
}
func TestComputePowUnsupportedAlgorithm(t *testing.T) {
_, err := ComputePow(map[string]any{"algorithm": "unknown"})
_, err := ComputePow(context.Background(), map[string]any{"algorithm": "unknown"})
if err == nil {
t.Fatal("expected error for unsupported algorithm")
}