-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprobe.e2e.ts
More file actions
281 lines (250 loc) · 8.91 KB
/
Copy pathprobe.e2e.ts
File metadata and controls
281 lines (250 loc) · 8.91 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
import { restoreWallet, sleep } from '../../helpers/actions';
import { waitForMainnetWalletReady } from '../../helpers/mainnet';
import {
buildProbeQueue,
fetchBolt11ForProbe,
parseNonNegativeIntEnv,
parseProbeCommandResult,
probeModeForTargetType,
resolveProbeAmountProfile,
resetPathfindingScores,
resolveProbeOrder,
resolveProbeResetScores,
resolveProbeTargets,
runProbeInvoiceCommand,
runProbeNodeCommand,
summarizeProbeCommandFailure,
waitForProbeReadiness,
writeProbeArtifacts,
type ProbeReadiness,
type ProbeResult,
type ProbeTarget,
} from '../../helpers/probe';
import { ciIt } from '../../helpers/suite';
const DEFAULT_PROBE_DELAY_MS = 10_000;
const DEFAULT_PROBE_RETRIES = 2;
const DEFAULT_PROBE_RETRY_DELAY_MS = 5_000;
function resolveEnvValue(name: string): string {
const value = process.env[name];
if (!value) {
throw new Error(`Missing ${name} env var`);
}
return value;
}
function resolveProbeDelayMs(): number {
return parseNonNegativeIntEnv('PROBE_DELAY_MS') ?? DEFAULT_PROBE_DELAY_MS;
}
function resolveProbeRetries(): number {
return parseNonNegativeIntEnv('PROBE_RETRIES') ?? DEFAULT_PROBE_RETRIES;
}
function resolveProbeRetryDelayMs(): number {
return parseNonNegativeIntEnv('PROBE_RETRY_DELAY_MS') ?? DEFAULT_PROBE_RETRY_DELAY_MS;
}
async function runInvoiceProbe(target: ProbeTarget, amountMsat: number): Promise<ProbeResult> {
const startedAt = Date.now();
const amountSats = amountMsat / 1000;
const baseResult = {
targetName: target.name,
targetType: target.type,
probeMode: probeModeForTargetType(target.type),
amountMsat,
amountSats,
required: target.required ?? true,
attempt: Number.parseInt(process.env.ATTEMPT ?? '1', 10),
};
let bolt11: string | undefined;
try {
console.info(`→ [Probe] Fetching invoice for '${target.name}' (${amountSats} sats)...`);
bolt11 = await fetchBolt11ForProbe(target, amountMsat);
} catch (error) {
return {
...baseResult,
retries: 0,
invoiceFetched: false,
success: false,
durationMs: Date.now() - startedAt,
error: error instanceof Error ? error.message : String(error),
};
}
const maxRetries = resolveProbeRetries();
const retryDelayMs = resolveProbeRetryDelayMs();
let lastRawProviderResult = '';
let lastError = 'Probe command returned a failed result';
for (let retry = 0; retry <= maxRetries; retry++) {
try {
console.info(
`→ [Probe] Probing '${target.name}' (${amountSats} sats), attempt ${retry + 1}/${
maxRetries + 1
}...`
);
const rawProviderResult = runProbeInvoiceCommand(target, amountMsat, bolt11);
lastRawProviderResult = rawProviderResult;
const providerResult = parseProbeCommandResult(rawProviderResult);
if (providerResult?.success) {
return {
...baseResult,
retries: retry,
invoiceFetched: true,
success: true,
durationMs: providerResult.durationMs ?? Date.now() - startedAt,
routeFeeMsat: providerResult.routeFeeMsat,
bolt11,
rawProviderResult,
};
}
lastError = summarizeProbeCommandFailure(rawProviderResult);
} catch (error) {
lastError = error instanceof Error ? error.message : String(error);
}
if (retry < maxRetries && retryDelayMs > 0) {
console.info(`→ [Probe] Retrying '${target.name}' in ${retryDelayMs / 1000}s...`);
await sleep(retryDelayMs);
}
}
const providerResult = parseProbeCommandResult(lastRawProviderResult);
return {
...baseResult,
retries: maxRetries,
invoiceFetched: true,
success: false,
durationMs: providerResult?.durationMs ?? Date.now() - startedAt,
routeFeeMsat: providerResult?.routeFeeMsat,
bolt11,
rawProviderResult: lastRawProviderResult,
error: lastError,
};
}
async function runNodeProbe(target: ProbeTarget, amountMsat: number): Promise<ProbeResult> {
const startedAt = Date.now();
const amountSats = amountMsat / 1000;
const nodeId = target.nodeId;
if (!nodeId) {
throw new Error(`Probe target '${target.name}' is missing nodeId`);
}
const baseResult = {
targetName: target.name,
targetType: target.type,
probeMode: probeModeForTargetType(target.type),
amountMsat,
amountSats,
required: target.required ?? true,
attempt: Number.parseInt(process.env.ATTEMPT ?? '1', 10),
nodeId,
invoiceFetched: false,
};
const maxRetries = resolveProbeRetries();
const retryDelayMs = resolveProbeRetryDelayMs();
let lastRawProviderResult = '';
let lastError = 'Probe command returned a failed result';
for (let retry = 0; retry <= maxRetries; retry++) {
try {
console.info(
`→ [Probe] Keysend probing '${target.name}' (${amountSats} sats), attempt ${retry + 1}/${
maxRetries + 1
}...`
);
const rawProviderResult = runProbeNodeCommand(target, amountMsat);
lastRawProviderResult = rawProviderResult;
const providerResult = parseProbeCommandResult(rawProviderResult);
if (providerResult?.success) {
return {
...baseResult,
retries: retry,
success: true,
durationMs: providerResult.durationMs ?? Date.now() - startedAt,
routeFeeMsat: providerResult.routeFeeMsat,
rawProviderResult,
};
}
lastError = summarizeProbeCommandFailure(rawProviderResult);
} catch (error) {
lastError = error instanceof Error ? error.message : String(error);
}
if (retry < maxRetries && retryDelayMs > 0) {
console.info(`→ [Probe] Retrying '${target.name}' in ${retryDelayMs / 1000}s...`);
await sleep(retryDelayMs);
}
}
const providerResult = parseProbeCommandResult(lastRawProviderResult);
return {
...baseResult,
retries: maxRetries,
success: false,
durationMs: providerResult?.durationMs ?? Date.now() - startedAt,
routeFeeMsat: providerResult?.routeFeeMsat,
rawProviderResult: lastRawProviderResult,
error: lastError,
};
}
async function runProbe(target: ProbeTarget, amountMsat: number): Promise<ProbeResult> {
if (target.type === 'nodeId') {
return runNodeProbe(target, amountMsat);
}
return runInvoiceProbe(target, amountMsat);
}
describe('@probe_mainnet - Lightning probe smoke', () => {
let probeSeed: string;
let targets: ProbeTarget[];
before(() => {
probeSeed = resolveEnvValue('PROBE_SEED');
targets = resolveProbeTargets();
});
ciIt('@probe_mainnet_1 - Can probe configured mainnet LNURL targets', async () => {
const results: ProbeResult[] = [];
let readiness: ProbeReadiness | null = null;
try {
console.info('→ [Probe] Restoring probe wallet...');
await restoreWallet(probeSeed, {
expectBackupSheet: false,
reinstall: false,
});
await waitForMainnetWalletReady({ logPrefix: 'Probe' });
const resetScores = resolveProbeResetScores();
let scoresResetFloorS: number | null = null;
if (resetScores) {
scoresResetFloorS = await resetPathfindingScores({ logPrefix: 'Probe' });
}
readiness = await waitForProbeReadiness({
logPrefix: 'Probe',
requireScoresSync: resetScores,
minScoresSyncTimestamp: scoresResetFloorS,
});
const probeOrder = resolveProbeOrder();
const probes = buildProbeQueue(targets, probeOrder);
const probeDelayMs = resolveProbeDelayMs();
const probeRetries = resolveProbeRetries();
console.info(`→ [Probe] Probe amount profile configured: ${resolveProbeAmountProfile()}`);
console.info(`→ [Probe] Probe order configured: ${resolveProbeOrder()}`);
console.info(`→ [Probe] Probe retries configured: ${probeRetries}`);
console.info(
`→ [Probe] Probe order '${probeOrder}': ${probes
.map((it) => `${it.target.name}:${it.amountMsat / 1000}`)
.join(', ')}`
);
for (const [index, { target, amountMsat }] of probes.entries()) {
const result = await runProbe(target, amountMsat);
results.push(result);
writeProbeArtifacts(results, readiness, { writeStepSummary: false });
console.info(
`→ [Probe] ${result.targetName} ${result.amountSats} sats (${result.probeMode}): ${
result.success ? '✅ success' : `❌ failed (${result.error ?? 'unknown'})`
}`
);
if (index < probes.length - 1 && probeDelayMs > 0) {
console.info(`→ [Probe] Waiting ${probeDelayMs / 1000}s before next probe...`);
await sleep(probeDelayMs);
}
}
} finally {
writeProbeArtifacts(results, readiness);
}
const failedRequired = results.filter((it) => it.required && !it.success);
if (failedRequired.length > 0) {
throw new Error(
`Required probe targets failed: ${failedRequired
.map((it) => `${it.targetName}:${it.amountSats} (${it.error ?? 'unknown'})`)
.join('; ')}`
);
}
});
});