apow-cli 0.11.2 → 0.11.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/miner.js +65 -38
- package/package.json +1 -1
package/dist/miner.js
CHANGED
|
@@ -68,6 +68,20 @@ function elapsedSeconds(start) {
|
|
|
68
68
|
function sleep(ms) {
|
|
69
69
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
70
70
|
}
|
|
71
|
+
function withTimeout(promise, ms, label) {
|
|
72
|
+
return new Promise((resolve, reject) => {
|
|
73
|
+
const timer = setTimeout(() => {
|
|
74
|
+
reject(new Error(`${label} timed out after ${ms / 1000}s`));
|
|
75
|
+
}, ms);
|
|
76
|
+
promise.then((value) => {
|
|
77
|
+
clearTimeout(timer);
|
|
78
|
+
resolve(value);
|
|
79
|
+
}, (error) => {
|
|
80
|
+
clearTimeout(timer);
|
|
81
|
+
reject(error);
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
}
|
|
71
85
|
function rejectOnAbort(signal) {
|
|
72
86
|
if (signal.aborted) {
|
|
73
87
|
const err = new Error("Aborted");
|
|
@@ -163,6 +177,13 @@ function formatTime(seconds) {
|
|
|
163
177
|
return `${(seconds / 60).toFixed(1)} min`;
|
|
164
178
|
return `${(seconds / 3600).toFixed(1)} hr`;
|
|
165
179
|
}
|
|
180
|
+
async function readMiningChallenge(timeoutMs = 8_000) {
|
|
181
|
+
return withTimeout(wallet_1.publicClient.readContract({
|
|
182
|
+
address: config_1.config.agentCoinAddress,
|
|
183
|
+
abi: agentCoinAbi,
|
|
184
|
+
functionName: "getMiningChallenge",
|
|
185
|
+
}), timeoutMs, "Mining challenge refresh");
|
|
186
|
+
}
|
|
166
187
|
function readCliVersion() {
|
|
167
188
|
try {
|
|
168
189
|
const pkg = JSON.parse((0, node_fs_1.readFileSync)((0, node_path_1.join)(__dirname, "..", "package.json"), "utf8"));
|
|
@@ -317,11 +338,7 @@ async function startMining(tokenId) {
|
|
|
317
338
|
await showStartupBanner(tokenId);
|
|
318
339
|
console.log(` Grinder: ${ui.bold(modeLabel)}`);
|
|
319
340
|
// Difficulty-aware preflight gate
|
|
320
|
-
const preflight =
|
|
321
|
-
address: config_1.config.agentCoinAddress,
|
|
322
|
-
abi: agentCoinAbi,
|
|
323
|
-
functionName: "getMiningChallenge",
|
|
324
|
-
}));
|
|
341
|
+
const preflight = await readMiningChallenge();
|
|
325
342
|
const preflightTarget = preflight[1];
|
|
326
343
|
const expectedHashes = preflightTarget > 0n ? (2n ** 256n) / preflightTarget : 0n;
|
|
327
344
|
const estHashrate = estimateHashrate(grinderInfo, useNative, useHttpGrind);
|
|
@@ -420,11 +437,7 @@ async function startMining(tokenId) {
|
|
|
420
437
|
}
|
|
421
438
|
mineCount++;
|
|
422
439
|
console.log(` ${ui.bold(`[Mine #${mineCount}]`)}`);
|
|
423
|
-
const miningChallenge =
|
|
424
|
-
address: config_1.config.agentCoinAddress,
|
|
425
|
-
abi: agentCoinAbi,
|
|
426
|
-
functionName: "getMiningChallenge",
|
|
427
|
-
}));
|
|
440
|
+
const miningChallenge = await readMiningChallenge();
|
|
428
441
|
let [challengeNumber, target] = [miningChallenge[0], miningChallenge[1]];
|
|
429
442
|
const smhl = (0, smhl_1.normalizeSmhlChallenge)(miningChallenge[2]);
|
|
430
443
|
// Solve SMHL algorithmically (sub-millisecond)
|
|
@@ -449,23 +462,45 @@ async function startMining(tokenId) {
|
|
|
449
462
|
const abortController = new AbortController();
|
|
450
463
|
const nonceSpinner = ui.spinner(`Grinding nonce (${modeLabel})...`);
|
|
451
464
|
// Background staleness checker
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
staleRestarts++;
|
|
461
|
-
nonceSpinner.stop(`Challenge changed — restarting grind (stale #${staleRestarts})`);
|
|
462
|
-
abortController.abort();
|
|
463
|
-
}
|
|
465
|
+
let staleTimer = null;
|
|
466
|
+
let staleCheckStopped = false;
|
|
467
|
+
let staleCheckInFlight = false;
|
|
468
|
+
const stopStaleChecks = () => {
|
|
469
|
+
staleCheckStopped = true;
|
|
470
|
+
if (staleTimer) {
|
|
471
|
+
clearTimeout(staleTimer);
|
|
472
|
+
staleTimer = null;
|
|
464
473
|
}
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
474
|
+
};
|
|
475
|
+
const scheduleStaleCheck = () => {
|
|
476
|
+
if (staleCheckStopped || abortController.signal.aborted)
|
|
477
|
+
return;
|
|
478
|
+
staleTimer = setTimeout(async () => {
|
|
479
|
+
if (staleCheckStopped || abortController.signal.aborted || staleCheckInFlight) {
|
|
480
|
+
scheduleStaleCheck();
|
|
481
|
+
return;
|
|
482
|
+
}
|
|
483
|
+
staleCheckInFlight = true;
|
|
484
|
+
try {
|
|
485
|
+
const fresh = await readMiningChallenge(Math.min(staleCheckMs, 5_000));
|
|
486
|
+
if (fresh[0] !== challengeNumber && !abortController.signal.aborted) {
|
|
487
|
+
staleRestarts++;
|
|
488
|
+
nonceSpinner.stop(`Challenge changed — restarting grind (stale #${staleRestarts})`);
|
|
489
|
+
abortController.abort();
|
|
490
|
+
stopStaleChecks();
|
|
491
|
+
return;
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
catch {
|
|
495
|
+
// RPC hiccup — don't abort, just skip this check
|
|
496
|
+
}
|
|
497
|
+
finally {
|
|
498
|
+
staleCheckInFlight = false;
|
|
499
|
+
}
|
|
500
|
+
scheduleStaleCheck();
|
|
501
|
+
}, staleCheckMs);
|
|
502
|
+
};
|
|
503
|
+
scheduleStaleCheck();
|
|
469
504
|
try {
|
|
470
505
|
// Race all available grinders — first valid nonce wins
|
|
471
506
|
const grinders = [];
|
|
@@ -546,11 +581,7 @@ async function startMining(tokenId) {
|
|
|
546
581
|
nonceSpinner.stop(`Nonce found (${grind.elapsed.toFixed(1)}s${grind.hashrate > 0 ? `, ${khs}k H/s` : ""})`);
|
|
547
582
|
// Final stale check: a challenge can change between the periodic
|
|
548
583
|
// poll and transaction submission, which would make the SMHL stale.
|
|
549
|
-
const latestChallenge =
|
|
550
|
-
address: config_1.config.agentCoinAddress,
|
|
551
|
-
abi: agentCoinAbi,
|
|
552
|
-
functionName: "getMiningChallenge",
|
|
553
|
-
}));
|
|
584
|
+
const latestChallenge = await readMiningChallenge();
|
|
554
585
|
if (latestChallenge[0] !== challengeNumber) {
|
|
555
586
|
staleRestarts++;
|
|
556
587
|
[challengeNumber, target] = [latestChallenge[0], latestChallenge[1]];
|
|
@@ -562,14 +593,10 @@ async function startMining(tokenId) {
|
|
|
562
593
|
}
|
|
563
594
|
}
|
|
564
595
|
catch (err) {
|
|
565
|
-
|
|
596
|
+
stopStaleChecks();
|
|
566
597
|
if (abortController.signal.aborted) {
|
|
567
598
|
// Challenge went stale — re-fetch and retry
|
|
568
|
-
const freshChallenge =
|
|
569
|
-
address: config_1.config.agentCoinAddress,
|
|
570
|
-
abi: agentCoinAbi,
|
|
571
|
-
functionName: "getMiningChallenge",
|
|
572
|
-
}));
|
|
599
|
+
const freshChallenge = await readMiningChallenge();
|
|
573
600
|
[challengeNumber, target] = [freshChallenge[0], freshChallenge[1]];
|
|
574
601
|
const freshSmhl = (0, smhl_1.normalizeSmhlChallenge)(freshChallenge[2]);
|
|
575
602
|
smhlSolution = (0, smhl_1.solveSmhlAlgorithmic)(freshSmhl);
|
|
@@ -585,7 +612,7 @@ async function startMining(tokenId) {
|
|
|
585
612
|
throw err; // non-abort error — propagate
|
|
586
613
|
}
|
|
587
614
|
finally {
|
|
588
|
-
|
|
615
|
+
stopStaleChecks();
|
|
589
616
|
}
|
|
590
617
|
}
|
|
591
618
|
// Submit transaction with spinner
|