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
|
// bun test names/withdraw.test.js
import { test, expect } from 'bun:test';
import { withdrawService, RETRY_DELAYS } from './withdraw.js';
const RETRIES = RETRY_DELAYS.length;
const OUR = '02' + 'aa'.repeat(32), THEIRS = '03' + 'bb'.repeat(32);
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
function rig({ payBehaviour = 'ok', circularCap = 1e9 } = {}) {
const state = {};
const invoices = {}; // our invoices: hash -> { status }
const pays = {}; // outbound: hash -> status
const fakes = {}; // bolt11 string -> decoded
let n = 0;
const ln = {
async call(m, p) {
if (m === 'invoice') { const hash = 'f' + (++n); invoices[hash] = { status: 'unpaid' }; return { bolt11: 'lnfund' + hash, payment_hash: hash }; }
if (m === 'listinvoices') return { invoices: invoices[p.payment_hash] ? [invoices[p.payment_hash]] : [] };
if (m === 'decode') { const d = fakes[p.string]; if (!d) throw new Error('bad'); return d; }
if (m === 'pay') {
const d = fakes[p.bolt11];
if (payBehaviour === 'fail') { pays[d.payment_hash] = 'failed'; throw new Error('no route'); }
if (payBehaviour === 'hang') { pays[d.payment_hash] = 'pending'; throw new Error('timed out'); }
pays[d.payment_hash] = 'complete';
return { status: 'complete', amount_msat: d.amount_msat, amount_sent_msat: d.amount_msat + 3000 };
}
if (m === 'waitsendpay') { pays[p.payment_hash] = 'complete'; return { status: 'complete' }; }
if (m === 'listsendpays') return { payments: pays[p.payment_hash] ? [{ status: pays[p.payment_hash] }] : [] };
throw new Error('unexpected ' + m);
},
};
let circular = 0;
const svc = withdrawService({
state, persist: () => {}, lnFor: () => ln, ourNodeId: async () => OUR,
circularPick: async () => ({ cap: circularCap }), payCircular: async () => { circular++; return { feeMsat: 1500 }; },
wait: async () => {},
publicBase: 'https://names.test',
});
const invoice = (sat, payee = THEIRS, extra = {}) => {
const hash = 'c' + (++n); const s = 'lnbc' + hash;
fakes[s] = { valid: true, payment_hash: hash, amount_msat: sat * 1000, payee, created_at: Math.floor(Date.now() / 1000), expiry: 3600, ...extra };
return s;
};
return { svc, state, invoices, pays, invoice, circular: () => circular };
}
async function funded(r, opts) {
const c = await r.svc.create({ pubkey: 'me', ...opts });
expect(r.svc.params(c.id).status).toBe('ERROR'); // unfunded is invisible
r.invoices[c.paymentHash].status = 'paid';
expect(r.svc.onSettled(c.paymentHash)).toBe(true);
return c;
}
test('a funded link pays a claim, then is used up', async () => {
const r = rig();
const c = await funded(r, { sat: 1000 });
const p = r.svc.params(c.id);
expect(p.tag).toBe('withdrawRequest');
expect(p.maxWithdrawable).toBe(1_000_000);
expect(p.callback).toBe(`https://names.test/w/${c.id}/cb`);
expect(await r.svc.callback(c.id, 'nope', r.invoice(1000))).toMatchObject({ status: 'ERROR' });
expect(await r.svc.callback(c.id, p.k1, r.invoice(1001))).toMatchObject({ status: 'ERROR' });
expect(await r.svc.callback(c.id, p.k1, r.invoice(1000))).toEqual({ status: 'OK' });
await sleep(5);
const v = r.svc.mine('me')[0];
expect(v.remainingSat).toBe(0);
expect(v.claims[0].status).toBe('paid');
expect(r.svc.params(c.id).status).toBe('ERROR');
expect(await r.svc.callback(c.id, p.k1, r.invoice(1))).toMatchObject({ status: 'ERROR' });
});
test('multi-use: each claim up to the per-claim amount, no invoice paid twice', async () => {
const r = rig();
const c = await funded(r, { sat: 500, uses: 3 });
const k1 = r.svc.params(c.id).k1;
const inv = r.invoice(500);
expect((await r.svc.callback(c.id, k1, inv)).status).toBe('OK');
expect((await r.svc.callback(c.id, k1, inv)).status).toBe('ERROR');
expect((await r.svc.callback(c.id, k1, r.invoice(200))).status).toBe('OK');
await sleep(5);
const v = r.svc.mine('me')[0];
expect(v.used).toBe(2);
expect(v.remainingSat).toBe(800);
expect(r.svc.params(c.id).maxWithdrawable).toBe(500_000);
});
test('an invoice on our own node goes round the circular route', async () => {
const r = rig();
const c = await funded(r, { sat: 1000 });
expect((await r.svc.callback(c.id, r.svc.params(c.id).k1, r.invoice(1000, OUR))).status).toBe('OK');
await sleep(5);
expect(r.circular()).toBe(1);
expect(r.svc.mine('me')[0].claims[0]).toMatchObject({ status: 'paid' });
});
test('a failed payout goes back on the link', async () => {
const r = rig({ payBehaviour: 'fail' });
const c = await funded(r, { sat: 1000 });
expect((await r.svc.callback(c.id, r.svc.params(c.id).k1, r.invoice(1000))).status).toBe('OK');
await sleep(5);
const v = r.svc.mine('me')[0];
expect(v.remainingSat).toBe(1000);
expect(v.used).toBe(0);
expect(v.claims[0]).toMatchObject({ status: 'failed', tries: RETRIES + 1 });
expect(r.svc.params(c.id).tag).toBe('withdrawRequest');
});
test('an unresolved payout stays reserved', async () => {
const r = rig({ payBehaviour: 'hang' });
const c = await funded(r, { sat: 1000 });
await r.svc.callback(c.id, r.svc.params(c.id).k1, r.invoice(1000));
await sleep(5);
expect(r.svc.mine('me')[0]).toMatchObject({ remainingSat: 0 });
});
test('expired and amountless invoices are refused', async () => {
const r = rig();
const c = await funded(r, { sat: 1000 });
const k1 = r.svc.params(c.id).k1;
expect((await r.svc.callback(c.id, k1, r.invoice(100, THEIRS, { created_at: 1000, expiry: 60 }))).status).toBe('ERROR');
expect((await r.svc.callback(c.id, k1, r.invoice(0))).status).toBe('ERROR');
});
test('reclaim pays the rest to the creator and closes the link', async () => {
const r = rig();
const c = await funded(r, { sat: 400, uses: 2 });
const k1 = r.svc.params(c.id).k1;
await r.svc.callback(c.id, k1, r.invoice(400));
await sleep(5);
await expect(r.svc.reclaim(c.id, 'someone-else', r.invoice(400, OUR))).rejects.toThrow('not your link');
const out = await r.svc.reclaim(c.id, 'me', r.invoice(400, OUR));
expect(out).toMatchObject({ status: 'closed', remainingSat: 0, paid: true });
expect(r.svc.params(c.id).status).toBe('ERROR');
});
test('confirmFunded checks the invoice', async () => {
const r = rig();
const c = await r.svc.create({ pubkey: 'me', sat: 10 });
await expect(r.svc.confirmFunded(c.id, 'me')).rejects.toThrow('not paid');
r.invoices[c.paymentHash].status = 'paid';
expect((await r.svc.confirmFunded(c.id, 'me')).status).toBe('open');
expect(r.svc.onSettled(c.paymentHash)).toBe(false); // the settle loop arriving later is a no-op
});
test('a claim with no route right now is tried again, and pays when one turns up', async () => {
const waits = [];
const state = {}; const pays = {}; const fakes = {}; let tries = 0;
const ln = {
async call(m, p) {
if (m === 'invoice') return { bolt11: 'lnfundx', payment_hash: 'fx' };
if (m === 'decode') return fakes[p.string];
if (m === 'pay') { tries++; const d = fakes[p.bolt11]; if (tries < 3) { pays[d.payment_hash] = 'failed'; throw new Error('could not find a usable set of paths'); } pays[d.payment_hash] = 'complete'; return { status: 'complete', amount_msat: d.amount_msat, amount_sent_msat: d.amount_msat }; }
if (m === 'listsendpays') return { payments: pays[p.payment_hash] ? [{ status: pays[p.payment_hash] }] : [] };
throw new Error('unexpected ' + m);
},
};
const svc = withdrawService({ state, persist: () => {}, lnFor: () => ln, ourNodeId: async () => OUR, publicBase: 'https://names.test', wait: async (ms) => { waits.push(ms); } });
const made = await svc.create({ pubkey: 'me', sat: 1000 });
svc.onSettled('fx');
fakes.lnbcz = { valid: true, payment_hash: 'cz', amount_msat: 1_000_000, payee: THEIRS, created_at: Math.floor(Date.now() / 1000), expiry: 3600 };
expect((await svc.callback(made.id, svc.params(made.id).k1, 'lnbcz')).status).toBe('OK');
await sleep(10);
expect(tries).toBe(3);
expect(waits.length).toBe(2);
expect(svc.mine('me')[0]).toMatchObject({ remainingSat: 0, used: 1 });
expect(svc.mine('me')[0].claims[0]).toMatchObject({ status: 'paid', tries: 3 });
});
test('retries stop short of the invoice expiring, then the sats go back', async () => {
const state = {}; const pays = {}; let tries = 0;
const ln = {
async call(m, p) {
if (m === 'invoice') return { bolt11: 'lnfundy', payment_hash: 'fy' };
if (m === 'decode') return { valid: true, payment_hash: 'cy', amount_msat: 500_000, payee: THEIRS, created_at: Math.floor(Date.now() / 1000), expiry: 120 };
if (m === 'pay') { tries++; pays.cy = 'failed'; throw new Error('no route'); }
if (m === 'listsendpays') return { payments: pays[p.payment_hash] ? [{ status: pays[p.payment_hash] }] : [] };
throw new Error('unexpected ' + m);
},
};
const svc = withdrawService({ state, persist: () => {}, lnFor: () => ln, ourNodeId: async () => OUR, publicBase: 'https://names.test', wait: async () => {} });
const made = await svc.create({ pubkey: 'me', sat: 500 });
svc.onSettled('fy');
await svc.callback(made.id, svc.params(made.id).k1, 'lnbcy');
await sleep(10);
expect(tries).toBe(2); // 30 s + a minute's margin fits in 120 s, 90 s more doesn't
expect(svc.mine('me')[0]).toMatchObject({ remainingSat: 500, used: 0 });
});
|