-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprobe.ts
More file actions
847 lines (736 loc) · 25.7 KB
/
Copy pathprobe.ts
File metadata and controls
847 lines (736 loc) · 25.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
import { execFileSync } from 'node:child_process';
import fs from 'node:fs';
import path from 'node:path';
import { getAppId } from './constants';
export type ProbeTargetType = 'lightningAddress' | 'lnurlCallback' | 'nodeId';
export type ProbeTarget = {
name: string;
type: ProbeTargetType;
required?: boolean;
amountMsat?: number;
amountsMsat?: number[];
address?: string;
url?: string;
nodeId?: string;
};
export type ProbeResult = {
targetName: string;
targetType: ProbeTargetType;
probeMode: 'invoice' | 'keysend';
amountMsat: number;
amountSats: number;
required: boolean;
attempt: number;
retries: number;
invoiceFetched: boolean;
success: boolean;
durationMs: number;
routeFeeMsat?: number;
bolt11?: string;
nodeId?: string;
rawProviderResult?: string;
error?: string;
};
type LnurlPayResponse = {
callback?: string;
minSendable?: number;
maxSendable?: number;
status?: string;
reason?: string;
};
type LnurlInvoiceResponse = {
pr?: string;
status?: string;
reason?: string;
};
export type ProbeCommandResult = {
success: boolean;
durationMs?: number;
routeFeeMsat?: number;
};
const DEFAULT_PROBE_TIMEOUT_SECONDS = 90;
const DEFAULT_PROBE_FETCH_RETRIES = 2;
const DEFAULT_PROBE_FETCH_RETRY_DELAY_MS = 1_000;
const DEFAULT_READINESS_TIMEOUT_MS = 180_000;
const DEFAULT_READINESS_POLL_MS = 5_000;
const DEFAULT_MIN_GRAPH_CHANNELS = 10_000;
const DEFAULT_RESET_SCORES_TIMEOUT_SECONDS = 180;
const DEFAULT_SCORES_SYNC_MAX_AGE_S = 900;
const DEFAULT_PROBE_AMOUNT_PROFILE = 'full';
const PROBE_AMOUNT_PROFILES = {
small: [1_000_000],
large: [80_000_000],
cover: [1_000_000, 25_000_000, 80_000_000],
full: [1_000_000, 10_000_000, 25_000_000, 50_000_000, 80_000_000],
} as const;
type ProbeAmountProfile = keyof typeof PROBE_AMOUNT_PROFILES;
export type ProbeReadiness = {
ready: boolean;
nodeRunning: boolean;
lifecycle: string;
peers: number;
connectedPeers: number;
channels: number;
readyChannels: number;
usableChannels: number;
outboundCapacitySats: number;
syncHealthy: boolean;
nodeId?: string;
graphNodeCount?: number;
graphChannelCount?: number;
latestRgsSyncTimestamp?: number;
latestPathfindingScoresSyncTimestamp?: number;
};
export function resolveProbeTargets(): ProbeTarget[] {
const raw = process.env.PROBE_TARGETS_JSON;
if (!raw) {
throw new Error('Missing PROBE_TARGETS_JSON env var');
}
const parsed: unknown = JSON.parse(raw);
if (!Array.isArray(parsed)) {
throw new Error('PROBE_TARGETS_JSON must be a JSON array');
}
return parsed.map(parseProbeTarget);
}
export type ProbeOrder = 'desc' | 'random' | 'config';
export type ProbeQueueEntry = { target: ProbeTarget; amountMsat: number };
export function resolveProbeOrder(): ProbeOrder {
const raw = process.env.PROBE_ORDER?.toLowerCase();
if (!raw) return 'config';
if (raw === 'desc' || raw === 'random' || raw === 'config') return raw;
throw new Error(`Invalid PROBE_ORDER value: ${raw} (expected desc, random or config)`);
}
export function resolveProbeAmountProfile(): ProbeAmountProfile {
const profile = process.env.PROBE_AMOUNT_PROFILE ?? DEFAULT_PROBE_AMOUNT_PROFILE;
if (profile === 'small' || profile === 'large' || profile === 'cover' || profile === 'full') {
return profile;
}
throw new Error(
`Invalid PROBE_AMOUNT_PROFILE: ${profile}. Expected 'small', 'large', 'cover', or 'full'.`
);
}
export function buildProbeQueue(targets: ProbeTarget[], order: ProbeOrder): ProbeQueueEntry[] {
const queue = targets.flatMap((target) => {
const amounts = expandProbeTargetAmounts(target);
if (order === 'desc') {
amounts.sort((a, b) => b - a);
}
return amounts.map((amountMsat) => ({ target, amountMsat }));
});
if (order === 'random') {
shuffleInPlace(queue);
}
return queue;
}
function shuffleInPlace<T>(items: T[]): T[] {
for (let i = items.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[items[i], items[j]] = [items[j], items[i]];
}
return items;
}
export function expandProbeTargetAmounts(target: ProbeTarget): number[] {
const amounts =
target.amountsMsat ??
(target.amountMsat
? [target.amountMsat]
: [...PROBE_AMOUNT_PROFILES[resolveProbeAmountProfile()]]);
return amounts.map((amountMsat) => {
if (!Number.isInteger(amountMsat) || amountMsat <= 0) {
throw new Error(`Probe target '${target.name}' has invalid amountMsat '${amountMsat}'`);
}
if (amountMsat % 1000 !== 0) {
throw new Error(
`Probe target '${target.name}' amountMsat must be whole sats: '${amountMsat}'`
);
}
return amountMsat;
});
}
export async function fetchBolt11ForProbe(
target: ProbeTarget,
amountMsat: number
): Promise<string> {
const callback =
target.type === 'lightningAddress' ? await fetchLightningAddressCallback(target) : target.url;
if (!callback) {
throw new Error(`Probe target '${target.name}' is missing LNURL callback URL`);
}
const url = new URL(callback);
url.searchParams.set('amount', amountMsat.toString());
const response = await fetchJson<LnurlInvoiceResponse>(url.toString());
if (response.status?.toUpperCase() === 'ERROR') {
throw new Error(response.reason ?? `LNURL invoice request failed for '${target.name}'`);
}
if (!response.pr) {
throw new Error(`LNURL invoice response for '${target.name}' did not include pr`);
}
return response.pr;
}
export function probeModeForTargetType(type: ProbeTargetType): 'invoice' | 'keysend' {
return type === 'nodeId' ? 'keysend' : 'invoice';
}
export function runProbeInvoiceCommand(
target: ProbeTarget,
amountMsat: number,
bolt11: string
): string {
const amountSats = amountMsat / 1000;
const method = process.env.PROBE_INVOICE_METHOD ?? 'probeInvoice';
const timeoutSeconds =
parsePositiveIntEnv('PROBE_TIMEOUT_SECONDS') ?? DEFAULT_PROBE_TIMEOUT_SECONDS;
const payload = {
targetName: target.name,
bolt11,
amountMsat,
amountSats,
timeoutSeconds,
};
return runDevToolsCommand(method, payload, timeoutSeconds);
}
export function runProbeNodeCommand(target: ProbeTarget, amountMsat: number): string {
const nodeId = target.nodeId;
if (!nodeId) {
throw new Error(`Probe target '${target.name}' is missing nodeId`);
}
const amountSats = amountMsat / 1000;
const method = process.env.PROBE_NODE_METHOD ?? 'probeNode';
const timeoutSeconds =
parsePositiveIntEnv('PROBE_TIMEOUT_SECONDS') ?? DEFAULT_PROBE_TIMEOUT_SECONDS;
const payload = {
targetName: target.name,
nodeId,
amountMsat,
amountSats,
timeoutSeconds,
};
return runDevToolsCommand(method, payload, timeoutSeconds);
}
function parseDevResultPayload(raw: string): Record<string, unknown> | null {
const result = extractContentCallResult(raw);
if (!result) return null;
let parsed: unknown;
try {
parsed = JSON.parse(result);
} catch {
return null;
}
if (typeof parsed !== 'object' || parsed === null) return null;
return parsed as Record<string, unknown>;
}
function parseDevResultSuccess(payload: Record<string, unknown>): boolean {
if ('success' in payload) return payload.success === true;
const type = payload.type;
return (
typeof type === 'string' && (type === 'Success' || type.endsWith('.ProbeSuccess'))
);
}
export function parseProbeCommandResult(raw: string): ProbeCommandResult | null {
const payload = parseDevResultPayload(raw);
if (!payload) return null;
return {
success: parseDevResultSuccess(payload),
durationMs: parseOptionalNumber(payload.durationMs),
routeFeeMsat: parseOptionalNumber(payload.routeFeeMsat),
};
}
export function parseProbeCommandSuccess(raw: string): boolean {
const payload = parseDevResultPayload(raw);
return payload ? parseDevResultSuccess(payload) : false;
}
function parseOptionalNumber(value: unknown): number | undefined {
if (typeof value === 'number' && Number.isFinite(value)) {
return value;
}
if (typeof value === 'string') {
const parsedValue = Number.parseInt(value, 10);
return Number.isFinite(parsedValue) ? parsedValue : undefined;
}
return undefined;
}
export function summarizeProbeCommandFailure(raw: string): string {
const json = extractContentCallResult(raw);
if (json) {
try {
return JSON.stringify(JSON.parse(json), null, 2);
} catch {
return json;
}
}
return (
(raw.match(/\[ERROR\]\s*(.+)/)?.[1]?.trim() ?? raw.trim()) ||
'Probe command returned a failed result'
);
}
export function resolveProbeResetScores(): boolean {
return parseBooleanEnv('PROBE_RESET_SCORES') ?? false;
}
/**
* Resets the persisted pathfinding scores via devtools and returns the
* device-clock epoch seconds to be used as a floor for the scores sync
* timestamp in readiness checks (the sync timestamp persisted in node metrics
* survives the restart, so only a sync strictly newer than the reset proves
* the external scores were re-downloaded). The app reports the floor as the
* moment after the node stop + VSS deletes and before the restart, so any
* newer sync can only come from the rebuilt node; if the app is too old to
* report it, falls back to the device time captured before the reset call.
* The floor uses the device clock because the sync timestamp is also
* device-generated, making the comparison immune to host/device clock skew.
*/
export async function resetPathfindingScores({
logPrefix,
}: {
logPrefix: string;
}): Promise<number> {
const method = process.env.PROBE_RESET_SCORES_METHOD ?? 'resetScores';
const timeoutSeconds =
parsePositiveIntEnv('PROBE_RESET_SCORES_TIMEOUT_SECONDS') ??
DEFAULT_RESET_SCORES_TIMEOUT_SECONDS;
console.info(`→ [${logPrefix}] Resetting pathfinding scores (timeout ${timeoutSeconds}s)...`);
const fallbackFloorS = getDeviceEpochSeconds();
const raw = runDevToolsCommand(method, {}, timeoutSeconds);
const payload = parseDevResultPayload(raw);
if (!payload || !parseDevResultSuccess(payload)) {
throw new Error(`Pathfinding scores reset failed: ${summarizeProbeCommandFailure(raw)}`);
}
const deviceResetAtS = parseResetTimestamp(payload);
if (deviceResetAtS === null) {
console.warn(
`→ [${logPrefix}] Reset result has no timestamp (old app build?); using pre-reset device time as scores sync floor`
);
}
const resetFloorS = deviceResetAtS ?? fallbackFloorS;
console.info(`→ [${logPrefix}] Pathfinding scores reset done (floor ${resetFloorS})`);
return resetFloorS;
}
function parseResetTimestamp(payload: Record<string, unknown>): number | null {
const timestamp = payload.timestamp;
return typeof timestamp === 'number' && Number.isFinite(timestamp) && timestamp > 0
? timestamp
: null;
}
function getDeviceEpochSeconds(): number {
const raw = execFileSync('adb', ['shell', 'date', '+%s'], {
encoding: 'utf8',
timeout: 10_000,
});
const epoch = Number.parseInt(raw.trim(), 10);
if (!Number.isFinite(epoch) || epoch <= 0) {
throw new Error(`Failed to read device epoch time: ${raw.trim() || 'empty output'}`);
}
return epoch;
}
export function runReadinessCommand(): string {
const method = process.env.PROBE_READINESS_METHOD ?? 'probeReadiness';
const command = [
'content',
'call',
'--uri',
shellQuote(`content://${getAppId()}.devtools`),
'--method',
shellQuote(method),
].join(' ');
return execFileSync('adb', ['shell', command], {
encoding: 'utf8',
timeout: 30_000,
});
}
export function parseProbeReadiness(raw: string): ProbeReadiness | null {
const result = extractContentCallResult(raw);
if (!result) return null;
try {
const parsed: unknown = JSON.parse(result);
if (isProbeReadinessShape(parsed)) {
return parsed;
}
} catch {
return null;
}
return null;
}
function isProbeReadinessShape(value: unknown): value is ProbeReadiness {
return (
typeof value === 'object' &&
value !== null &&
typeof (value as ProbeReadiness).ready === 'boolean' &&
typeof (value as ProbeReadiness).nodeRunning === 'boolean'
);
}
function summarizeReadinessError(raw: string): string {
const json = extractContentCallResult(raw);
if (json) {
try {
const record = JSON.parse(json) as Record<string, unknown>;
if (typeof record.message === 'string' && record.message) return record.message;
} catch {
// fall through
}
}
return raw.trim().slice(0, 200) || 'unparseable readiness response';
}
export function isProbeReadinessSufficient(
readiness: ProbeReadiness,
minGraphChannels: number,
maxScoresSyncAgeS: number | null = null,
minScoresSyncTimestamp: number | null = null,
nowS: number = Date.now() / 1000
): boolean {
return (
readiness.ready &&
readiness.nodeRunning &&
readiness.connectedPeers > 0 &&
readiness.usableChannels > 0 &&
readiness.syncHealthy &&
(readiness.graphChannelCount ?? 0) >= minGraphChannels &&
isScoresSyncFresh(readiness, maxScoresSyncAgeS, minScoresSyncTimestamp, nowS)
);
}
function isScoresSyncFresh(
readiness: ProbeReadiness,
maxAgeS: number | null,
minTimestamp: number | null,
nowS: number = Date.now() / 1000
): boolean {
if (maxAgeS === null) return true;
const timestamp = readiness.latestPathfindingScoresSyncTimestamp;
if (!timestamp) return false;
if (nowS - timestamp > maxAgeS) return false;
// Both timestamps come from the device clock; the floor is captured by the
// app after the node stop + VSS deletes, so any strictly newer sync can
// only come from the rebuilt node (post-reset).
if (minTimestamp !== null && timestamp <= minTimestamp) return false;
return true;
}
export function summarizeProbeReadiness(readiness: ProbeReadiness): string {
return [
`running=${readiness.nodeRunning}`,
`peers=${readiness.connectedPeers}/${readiness.peers}`,
`usableChannels=${readiness.usableChannels}`,
`outboundSats=${readiness.outboundCapacitySats}`,
`graphChannels=${readiness.graphChannelCount ?? 'n/a'}`,
`graphNodes=${readiness.graphNodeCount ?? 'n/a'}`,
`scoresSync=${readiness.latestPathfindingScoresSyncTimestamp ?? 'n/a'}`,
`syncHealthy=${readiness.syncHealthy}`,
`ready=${readiness.ready}`,
].join(' ');
}
type WaitForProbeReadinessOptions = {
logPrefix: string;
requireScoresSync?: boolean;
/** Device-clock epoch seconds; scores sync must be strictly newer than this (the reset floor reported by the app). */
minScoresSyncTimestamp?: number | null;
};
export async function waitForProbeReadiness({
logPrefix,
requireScoresSync = false,
minScoresSyncTimestamp = null,
}: WaitForProbeReadinessOptions): Promise<ProbeReadiness> {
const timeoutMs =
parsePositiveIntEnv('PROBE_READINESS_TIMEOUT_MS') ?? DEFAULT_READINESS_TIMEOUT_MS;
const pollMs = parsePositiveIntEnv('PROBE_READINESS_POLL_MS') ?? DEFAULT_READINESS_POLL_MS;
const minGraphChannels =
parseNonNegativeIntEnv('PROBE_MIN_GRAPH_CHANNELS') ?? DEFAULT_MIN_GRAPH_CHANNELS;
const maxScoresSyncAgeS = requireScoresSync
? (parsePositiveIntEnv('PROBE_SCORES_SYNC_MAX_AGE_S') ?? DEFAULT_SCORES_SYNC_MAX_AGE_S)
: null;
const minSyncTimestamp = requireScoresSync ? minScoresSyncTimestamp : null;
console.info(
`→ [${logPrefix}] Waiting for probe readiness (timeout ${timeoutMs / 1000}s, minGraphChannels ${minGraphChannels}, requireScoresSync ${requireScoresSync})...`
);
const deadline = Date.now() + timeoutMs;
let lastSummary = 'no readiness response';
while (Date.now() < deadline) {
let raw = '';
try {
raw = runReadinessCommand();
} catch (error) {
lastSummary = error instanceof Error ? error.message : String(error);
}
const readiness = raw ? parseProbeReadiness(raw) : null;
if (readiness) {
lastSummary = summarizeProbeReadiness(readiness);
// Use the device clock for the scores sync age check so it is measured
// against the same clock that produced the sync timestamp.
const nowS = maxScoresSyncAgeS !== null ? getDeviceEpochSeconds() : Date.now() / 1000;
if (
isProbeReadinessSufficient(
readiness,
minGraphChannels,
maxScoresSyncAgeS,
minSyncTimestamp,
nowS
)
) {
console.info(`→ [${logPrefix}] Probe readiness satisfied: ${lastSummary}`);
return readiness;
}
} else if (raw) {
lastSummary = summarizeReadinessError(raw);
}
console.info(
`→ [${logPrefix}] Not ready yet (${lastSummary}), polling again in ${pollMs / 1000}s...`
);
await delay(pollMs);
}
throw new Error(`Probe readiness not reached within ${timeoutMs / 1000}s: ${lastSummary}`);
}
export function writeProbeArtifacts(
results: ProbeResult[],
readiness?: ProbeReadiness | null,
options: { writeStepSummary?: boolean } = {}
): void {
const artifactsDir = resolveArtifactsDir();
fs.mkdirSync(artifactsDir, { recursive: true });
const jsonPath = path.join(artifactsDir, 'probe-results.json');
const reportPath = path.join(artifactsDir, 'probe-report.md');
const report = renderProbeReport(results, readiness);
fs.writeFileSync(jsonPath, `${JSON.stringify(results, null, 2)}\n`);
fs.writeFileSync(reportPath, report);
if (readiness) {
const readinessPath = path.join(artifactsDir, 'probe-readiness.json');
fs.writeFileSync(readinessPath, `${JSON.stringify(readiness, null, 2)}\n`);
}
if (options.writeStepSummary !== false && process.env.GITHUB_STEP_SUMMARY) {
fs.appendFileSync(
process.env.GITHUB_STEP_SUMMARY,
`\n## Attempt ${resolveAttempt()}\n\n${report}\n`
);
}
}
export function renderProbeReport(
results: ProbeResult[],
readiness?: ProbeReadiness | null
): string {
const failedRequired = results.filter((it) => it.required && !it.success);
const lines = [
'# Lightning Probe Report',
'',
`Required failures: ${failedRequired.length}`,
`Probe order: ${probeOrderForReport()}`,
`Scores reset: ${scoresResetForReport()}`,
`Readiness at probe start: ${readiness ? summarizeProbeReadiness(readiness) : 'not captured'}`,
'',
'| Target | Type | Amount sats | Required | Fetch | Probe | Retries | Duration ms | Route fee msat | Failure |',
'| --- | --- | ---: | --- | --- | --- | ---: | ---: | ---: | --- |',
];
for (const result of results) {
lines.push(
`| ${[
result.targetName,
result.probeMode,
result.amountSats.toString(),
result.required ? 'yes' : 'no',
formatFetchCell(result),
result.success ? '✅' : '❌',
result.retries.toString(),
result.durationMs.toString(),
formatRouteFeeCell(result),
result.success ? '' : formatFailureCell(result.error ?? ''),
].join(' | ')} |`
);
}
return `${lines.join('\n')}\n`;
}
// Report rendering runs from the spec's finally block, so it must never throw
// and mask the original test failure (e.g. an invalid PROBE_ORDER value).
function probeOrderForReport(): string {
try {
return resolveProbeOrder();
} catch {
return `invalid (${process.env.PROBE_ORDER})`;
}
}
function scoresResetForReport(): string {
try {
return String(resolveProbeResetScores());
} catch {
return `invalid (${process.env.PROBE_RESET_SCORES})`;
}
}
function parseProbeTarget(value: unknown): ProbeTarget {
if (typeof value !== 'object' || value === null) {
throw new Error('Each probe target must be an object');
}
const target = value as Partial<ProbeTarget>;
if (!target.name || typeof target.name !== 'string') {
throw new Error('Each probe target must define a string name');
}
if (
target.type !== 'lightningAddress' &&
target.type !== 'lnurlCallback' &&
target.type !== 'nodeId'
) {
throw new Error(`Probe target '${target.name}' has unsupported type '${String(target.type)}'`);
}
if (target.type === 'lightningAddress' && !target.address) {
throw new Error(`Probe target '${target.name}' must define address`);
}
if (target.type === 'lnurlCallback' && !target.url) {
throw new Error(`Probe target '${target.name}' must define url`);
}
if (target.type === 'nodeId' && !target.nodeId) {
throw new Error(`Probe target '${target.name}' must define nodeId`);
}
return {
name: target.name,
type: target.type,
required: target.required ?? true,
amountMsat: target.amountMsat,
amountsMsat: target.amountsMsat,
address: target.address,
url: target.url,
nodeId: target.nodeId,
};
}
async function fetchLightningAddressCallback(target: ProbeTarget): Promise<string> {
const address = target.address ?? '';
const [username, domain] = address.split('@');
if (!username || !domain) {
throw new Error(`Invalid Lightning Address for '${target.name}': '${address}'`);
}
const metadataUrl = `https://${domain}/.well-known/lnurlp/${encodeURIComponent(username)}`;
const response = await fetchJson<LnurlPayResponse>(metadataUrl);
if (response.status?.toUpperCase() === 'ERROR') {
throw new Error(response.reason ?? `LNURL metadata request failed for '${target.name}'`);
}
if (!response.callback) {
throw new Error(`LNURL metadata for '${target.name}' did not include callback`);
}
return response.callback;
}
async function fetchJson<T>(url: string): Promise<T> {
const maxRetries = parseNonNegativeIntEnv('PROBE_FETCH_RETRIES') ?? DEFAULT_PROBE_FETCH_RETRIES;
const retryDelayMs =
parseNonNegativeIntEnv('PROBE_FETCH_RETRY_DELAY_MS') ?? DEFAULT_PROBE_FETCH_RETRY_DELAY_MS;
let lastError: Error | null = null;
for (let retry = 0; retry <= maxRetries; retry++) {
try {
return await fetchJsonOnce<T>(url);
} catch (error) {
lastError = error instanceof Error ? error : new Error(String(error));
}
if (retry < maxRetries && retryDelayMs > 0) {
await delay(retryDelayMs);
}
}
throw new Error(
`${lastError?.message ?? `Failed to fetch ${url}`} after ${maxRetries + 1} attempts`
);
}
async function fetchJsonOnce<T>(url: string): Promise<T> {
const response = await fetch(url);
const text = await response.text();
if (!response.ok) {
throw new Error(`HTTP ${response.status} for ${url}${formatResponseBody(text)}`);
}
if (text.trim().length === 0) {
throw new Error(`HTTP ${response.status} for ${url} returned an empty response body`);
}
return JSON.parse(text) as T;
}
function extractContentCallResult(raw: string): string | null {
return raw.match(/result=(\{[\s\S]*\})\}\]\s*$/)?.[1] ?? null;
}
function parsePositiveIntEnv(name: string): number | null {
const raw = process.env[name];
if (!raw) return null;
const value = Number.parseInt(raw, 10);
if (!Number.isInteger(value) || value <= 0) {
throw new Error(`Invalid ${name} value: ${raw}`);
}
return value;
}
export function parseNonNegativeIntEnv(name: string): number | null {
const raw = process.env[name];
if (!raw) return null;
const value = Number.parseInt(raw, 10);
if (!Number.isInteger(value) || value < 0) {
throw new Error(`Invalid ${name} value: ${raw}`);
}
return value;
}
function parseBooleanEnv(name: string): boolean | null {
const raw = process.env[name]?.trim().toLowerCase();
if (!raw) return null;
if (raw === 'true' || raw === '1' || raw === 'yes') return true;
if (raw === 'false' || raw === '0' || raw === 'no') return false;
throw new Error(`Invalid ${name} value: ${raw} (expected true or false)`);
}
function delay(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
function formatResponseBody(body: string): string {
const normalized = body.replace(/\s+/g, ' ').trim();
if (!normalized) return '';
return `: ${normalized.slice(0, 300)}`;
}
function resolveArtifactsDir(): string {
const attempt = process.env.ATTEMPT;
return attempt ? path.join('artifacts', `attempt-${attempt}`) : 'artifacts';
}
function resolveAttempt(): string {
return process.env.ATTEMPT ?? '1';
}
function sanitizeMarkdownCell(value: string): string {
return value.replace(/\|/g, '\\|').replace(/\s+/g, ' ').trim();
}
function formatFailureCell(error: string): string {
const sanitized = sanitizeMarkdownCell(error);
if (!sanitized) return '';
if (sanitized.includes('`')) {
return `\`${sanitized.replace(/`/g, "'")}\``;
}
return `\`${sanitized}\``;
}
function formatFetchCell(result: ProbeResult): string {
if (result.probeMode === 'keysend') return 'n/a';
return result.invoiceFetched ? 'ok' : 'failed';
}
function formatRouteFeeCell(result: ProbeResult): string {
return result.routeFeeMsat === undefined ? '' : result.routeFeeMsat.toString();
}
function runDevToolsCommand(
method: string,
payload: Record<string, unknown>,
timeoutSeconds: number
): string {
const command = [
'content',
'call',
'--uri',
shellQuote(`content://${getAppId()}.devtools`),
'--method',
shellQuote(method),
'--arg',
shellQuote(JSON.stringify(payload)),
].join(' ');
try {
return execFileSync('adb', ['shell', command], {
encoding: 'utf8',
timeout: (timeoutSeconds + 10) * 1000,
});
} catch (error) {
throw new Error(formatDevToolsCommandError(method, error));
}
}
function shellQuote(value: string): string {
return `'${value.replace(/'/g, "'\\''")}'`;
}
function formatDevToolsCommandError(method: string, error: unknown): string {
if (!(error instanceof Error)) {
return `DevTools command '${method}' failed: ${String(error)}`;
}
const details = error as Error & {
status?: number;
signal?: NodeJS.Signals;
stdout?: string | Buffer;
stderr?: string | Buffer;
};
const output = [
details.stdout ? `stdout: ${details.stdout.toString().trim()}` : '',
details.stderr ? `stderr: ${details.stderr.toString().trim()}` : '',
details.status !== undefined ? `status: ${details.status}` : '',
details.signal ? `signal: ${details.signal}` : '',
].filter(Boolean);
return [`DevTools command '${method}' failed: ${error.message}`, ...output].join('\n');
}