apow-cli 0.9.4 → 0.9.5

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/config.js CHANGED
@@ -114,6 +114,7 @@ exports.config = {
114
114
  useX402Grind: process.env.USE_X402_GRIND !== undefined
115
115
  ? process.env.USE_X402_GRIND === "true"
116
116
  : useX402, // defaults to USE_X402 value when not explicitly set
117
+ staleCheckIntervalMs: parseInt(process.env.STALE_CHECK_INTERVAL ?? "60", 10) * 1000,
117
118
  };
118
119
  function requirePrivateKey() {
119
120
  if (!exports.config.privateKey) {
package/dist/miner.js CHANGED
@@ -269,14 +269,16 @@ async function startMining(tokenId) {
269
269
  const smhlElapsed = elapsedSeconds(smhlStart);
270
270
  console.log(` ${ui.dim(`SMHL solved (${(smhlElapsed * 1000).toFixed(1)}ms)`)}`);
271
271
  // Grind nonce — native GPU/CPU if available, JS fallback
272
- // Abort and re-fetch challenge every STALE_CHECK_INTERVAL_MS to avoid
273
- // grinding a dead nonce after another miner wins the block.
274
- const STALE_CHECK_INTERVAL_MS = 20_000;
272
+ // Abort and re-fetch challenge periodically to avoid grinding a dead
273
+ // nonce after another miner wins the block. Configurable via
274
+ // STALE_CHECK_INTERVAL env var (seconds, default 60).
275
+ const staleCheckMs = config_1.config.staleCheckIntervalMs;
275
276
  let grind = null;
277
+ let staleRestarts = 0;
276
278
  while (!grind) {
277
279
  const abortController = new AbortController();
278
280
  const nonceSpinner = ui.spinner(`Grinding nonce (${modeLabel})...`);
279
- // Background staleness checker — polls challenge every 20s
281
+ // Background staleness checker
280
282
  const staleTimer = setInterval(async () => {
281
283
  try {
282
284
  const fresh = (await wallet_1.publicClient.readContract({
@@ -285,14 +287,15 @@ async function startMining(tokenId) {
285
287
  functionName: "getMiningChallenge",
286
288
  }));
287
289
  if (fresh[0] !== challengeNumber) {
288
- nonceSpinner.stop("Challenge changed — restarting grind");
290
+ staleRestarts++;
291
+ nonceSpinner.stop(`Challenge changed — restarting grind (stale #${staleRestarts})`);
289
292
  abortController.abort();
290
293
  }
291
294
  }
292
295
  catch {
293
296
  // RPC hiccup — don't abort, just skip this check
294
297
  }
295
- }, STALE_CHECK_INTERVAL_MS);
298
+ }, staleCheckMs);
296
299
  try {
297
300
  // Race all available grinders — first valid nonce wins
298
301
  const grinders = [];
@@ -302,6 +305,7 @@ async function startMining(tokenId) {
302
305
  if (abortController.signal.aborted)
303
306
  throw err;
304
307
  // Native failed but don't abort the race — others may still win
308
+ console.log(` ${ui.dim(`Native grinder error: ${err instanceof Error ? err.message : String(err)}`)}`);
305
309
  return new Promise(() => { }); // hang forever (race will resolve via another grinder)
306
310
  }));
307
311
  }
@@ -310,6 +314,8 @@ async function startMining(tokenId) {
310
314
  .catch((err) => {
311
315
  if (abortController.signal.aborted)
312
316
  throw err;
317
+ // Log x402 errors visibly — payment failures, timeouts, etc.
318
+ console.log(` ${ui.dim(`x402 GPU grinder error: ${err instanceof Error ? err.message : String(err)}`)}`);
313
319
  return new Promise(() => { });
314
320
  }));
315
321
  }
@@ -343,6 +349,10 @@ async function startMining(tokenId) {
343
349
  const freshSmhl = (0, smhl_1.normalizeSmhlChallenge)(freshChallenge[2]);
344
350
  smhlSolution = (0, smhl_1.solveSmhlAlgorithmic)(freshSmhl);
345
351
  console.log(` ${ui.dim("SMHL re-solved, grinding fresh challenge...")}`);
352
+ if (staleRestarts >= 3 && staleRestarts % 3 === 0) {
353
+ ui.warn(`Challenge has gone stale ${staleRestarts} times — grinder may be too slow for current difficulty.`);
354
+ ui.warn(`Tip: increase STALE_CHECK_INTERVAL (current: ${staleCheckMs / 1000}s) or use a faster grinder.`);
355
+ }
346
356
  grind = null; // loop again
347
357
  continue;
348
358
  }
@@ -32,6 +32,9 @@
32
32
  #define MAX_ADDRS 8
33
33
  #define MAX_LINE 4096
34
34
 
35
+ // Signal state — declared early so run_grind can check it
36
+ static volatile sig_atomic_t got_signal = 0;
37
+
35
38
  // ── Keccak round constants ──
36
39
 
37
40
  __constant__ uint64_t RC[24] = {
@@ -271,7 +274,7 @@ static void compute_target_be(uint64_t target_be[4], const uint8_t target[32]) {
271
274
  static void configure_dispatch(GrindParams* params, const int weights[], int num_addrs,
272
275
  uint64_t* out_batch_size, int* out_grid_size) {
273
276
  const int BLOCK_SIZE = 256;
274
- const uint64_t RAW_BATCH = 1ULL << 26; // 64M
277
+ const uint64_t RAW_BATCH = 1ULL << 24; // 16M — faster first-nonce latency (check 16x more often)
275
278
 
276
279
  int total_weight = 0;
277
280
  for (int a = 0; a < num_addrs; a++) total_weight += weights[a];
@@ -332,19 +335,51 @@ static bool run_grind(GrindParams* params, GrindResult* result,
332
335
  }
333
336
  }
334
337
 
335
- // In daemon mode, check for ABORT between batches
336
- if (check_abort && stdin_has_data()) {
337
- char abort_buf[MAX_LINE];
338
- if (fgets(abort_buf, sizeof(abort_buf), stdin)) {
339
- // Trim
340
- char* p = abort_buf;
341
- while (*p == ' ' || *p == '\t') p++;
342
- if (strncmp(p, "ABORT", 5) == 0) {
338
+ // Check CUDA errors
339
+ cudaError_t err = cudaGetLastError();
340
+ if (err != cudaSuccess) {
341
+ fprintf(stderr, "CUDA error: %s\n", cudaGetErrorString(err));
342
+ cudaDeviceReset();
343
+ auto t1 = std::chrono::steady_clock::now();
344
+ double elapsed = std::chrono::duration<double>(t1 - t0).count();
345
+ printf("D %llu %.3f\n", (unsigned long long)totalAttempts, elapsed);
346
+ fflush(stdout);
347
+ return false;
348
+ }
349
+
350
+ // In daemon mode, check for ABORT / EOF / signal between batches
351
+ if (check_abort) {
352
+ // Check for fatal signal (SIGHUP from SSH death, SIGTERM, etc.)
353
+ if (got_signal) {
354
+ cudaDeviceSynchronize();
355
+ auto t1 = std::chrono::steady_clock::now();
356
+ double elapsed = std::chrono::duration<double>(t1 - t0).count();
357
+ printf("D %llu %.3f\n", (unsigned long long)totalAttempts, elapsed);
358
+ fflush(stdout);
359
+ return false; // Signal received — exit grind
360
+ }
361
+ if (stdin_has_data()) {
362
+ char abort_buf[MAX_LINE];
363
+ if (fgets(abort_buf, sizeof(abort_buf), stdin)) {
364
+ // Trim
365
+ char* p = abort_buf;
366
+ while (*p == ' ' || *p == '\t') p++;
367
+ if (strncmp(p, "ABORT", 5) == 0) {
368
+ cudaDeviceSynchronize();
369
+ auto t1 = std::chrono::steady_clock::now();
370
+ double elapsed = std::chrono::duration<double>(t1 - t0).count();
371
+ printf("D %llu %.3f\n", (unsigned long long)totalAttempts, elapsed);
372
+ fflush(stdout);
373
+ return false; // Aborted
374
+ }
375
+ } else {
376
+ // fgets returned NULL = EOF (SSH connection died)
377
+ cudaDeviceSynchronize();
343
378
  auto t1 = std::chrono::steady_clock::now();
344
379
  double elapsed = std::chrono::duration<double>(t1 - t0).count();
345
380
  printf("D %llu %.3f\n", (unsigned long long)totalAttempts, elapsed);
346
381
  fflush(stdout);
347
- return false; // Aborted
382
+ return false; // EOF — exit immediately
348
383
  }
349
384
  }
350
385
  }
@@ -357,8 +392,7 @@ static bool run_grind(GrindParams* params, GrindResult* result,
357
392
  return true;
358
393
  }
359
394
 
360
- // Signal handler for debugging
361
- static volatile sig_atomic_t got_signal = 0;
395
+ // Signal handler — sets flag so grind loop exits quickly
362
396
  static void signal_handler(int sig) {
363
397
  // Write signal info to stderr (async-signal-safe)
364
398
  const char* name = "UNKNOWN";
@@ -396,13 +430,16 @@ static int run_daemon() {
396
430
  grind_kernel<<<1, 1>>>(params, result);
397
431
  cudaDeviceSynchronize();
398
432
 
399
- // Install signal handlers for debugging
433
+ // Ignore SIGPIPE (critical for daemon writing to SSH channel)
434
+ signal(SIGPIPE, SIG_IGN);
435
+ // Install signal handlers for debugging other signals
400
436
  signal(SIGTERM, signal_handler);
401
437
  signal(SIGINT, signal_handler);
402
- signal(SIGPIPE, signal_handler);
403
438
  signal(SIGHUP, signal_handler);
404
439
  signal(SIGUSR1, signal_handler);
405
440
  signal(SIGUSR2, signal_handler);
441
+ signal(SIGSEGV, signal_handler);
442
+ signal(SIGABRT, signal_handler);
406
443
 
407
444
  fprintf(stderr, "CUDA daemon ready (pid=%d)\n", getpid());
408
445
  printf("READY\n");
@@ -410,7 +447,7 @@ static int run_daemon() {
410
447
 
411
448
  // Main loop: read challenges from stdin
412
449
  char line[MAX_LINE];
413
- while (fgets(line, sizeof(line), stdin)) {
450
+ while (!got_signal && fgets(line, sizeof(line), stdin)) {
414
451
  // Trim leading whitespace
415
452
  char* p = line;
416
453
  while (*p == ' ' || *p == '\t') p++;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "apow-cli",
3
- "version": "0.9.4",
3
+ "version": "0.9.5",
4
4
  "description": "Mine AGENT tokens on Base L2 with Agentic Proof of Work",
5
5
  "keywords": [
6
6
  "apow",