ln-docker-daemons 8.1.3 → 9.0.0
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/README.md +119 -33
- package/bitcoinrpc/add_peer.js +16 -8
- package/bitcoinrpc/check_generated.js +1 -8
- package/bitcoinrpc/generate_to_address.js +15 -15
- package/bitcoinrpc/get_added_node_info.js +79 -0
- package/bitcoinrpc/get_block_info.js +16 -8
- package/bitcoinrpc/get_blockchain_info.js +15 -6
- package/bitcoinrpc/get_peer_info.js +73 -0
- package/bitcoinrpc/index.js +4 -0
- package/bitcoinrpc/mine_blocks.js +36 -25
- package/bitcoinrpc/rpc.js +41 -36
- package/bitcoinrpc/unknown_generated_error.js +9 -3
- package/cluster/spawn_lightning_cluster.js +110 -86
- package/docker/first_file_in_tar.js +4 -4
- package/docker/get_file.js +4 -4
- package/docker/kill_docker.js +4 -4
- package/docker/kill_dockers.js +6 -3
- package/docker/make_tar.js +69 -0
- package/docker/spawn_docker_image.js +88 -27
- package/index.js +2 -0
- package/lnd/constants.json +1 -1
- package/lnd/spawn_lightning_docker.js +10 -8
- package/lnd/spawn_lnd_docker.js +24 -17
- package/loop/check_chain_synced.js +119 -0
- package/loop/check_grpc_server.js +300 -0
- package/loop/constants.json +3 -0
- package/loop/index.js +3 -0
- package/loop/retry_within.js +71 -0
- package/loop/spawn_loop_server_docker.js +263 -0
- package/package.json +5 -3
- package/setup/setup_channel.js +65 -28
- package/setup/wait_for_channel.js +30 -20
- package/setup/wait_for_pending_channel.js +3 -3
- package/tls/index.js +4 -0
- package/tls/make_self_signed_cert.js +167 -0
- package/tls/start_tls_proxy.js +196 -0
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
const {connect} = require('node:http2');
|
|
2
|
+
|
|
3
|
+
const asyncAuto = require('async/auto');
|
|
4
|
+
const asyncify = require('async/asyncify');
|
|
5
|
+
const {returnResult} = require('asyncjs-util');
|
|
6
|
+
|
|
7
|
+
const {concat} = Buffer;
|
|
8
|
+
const emptyRequest = Buffer.alloc(5); // Uncompressed frame, zero length body
|
|
9
|
+
const frameHeaderBytes = 5;
|
|
10
|
+
const grpcContentType = 'application/grpc';
|
|
11
|
+
const healthPath = '/grpc.health.v1.Health/Check';
|
|
12
|
+
const ignore = () => {};
|
|
13
|
+
const isGrpcAnswer = (code, type) => code === 200 && isGrpcType.test(type);
|
|
14
|
+
const isGrpcType = /^application\/grpc(\+|;|$)/;
|
|
15
|
+
const okStatus = '0';
|
|
16
|
+
const servingStatus = 1;
|
|
17
|
+
const statusFieldKey = 0x08; // Field 1, varint wire type
|
|
18
|
+
const timeoutMs = 1000 * 5;
|
|
19
|
+
const unavailableStatus = '14';
|
|
20
|
+
const unimplementedStatus = '12';
|
|
21
|
+
|
|
22
|
+
// Read one uncompressed gRPC message out of a response body, if there is one
|
|
23
|
+
const readMessage = body => {
|
|
24
|
+
// Exit early when there is no message, as in an answer with a failure
|
|
25
|
+
if (!body.length) {
|
|
26
|
+
return {};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// A message has an uncompressed flag and a size before its bytes
|
|
30
|
+
if (body.length < frameHeaderBytes || body[0] !== 0) {
|
|
31
|
+
return {is_invalid: true};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const message = body.subarray(frameHeaderBytes);
|
|
35
|
+
|
|
36
|
+
// The size says how many bytes the one message has
|
|
37
|
+
if (message.length !== body.readUInt32BE(1)) {
|
|
38
|
+
return {is_invalid: true};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return {message};
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
// Start a call of a unary gRPC method, to wait on its status and message
|
|
45
|
+
//
|
|
46
|
+
// Opening a request throws on some Node versions, as when the session has
|
|
47
|
+
// been told to go away, so the call is started in an asyncify step
|
|
48
|
+
const startCall = args => {
|
|
49
|
+
const chunks = [];
|
|
50
|
+
const outcome = {};
|
|
51
|
+
|
|
52
|
+
const req = args.session.request({
|
|
53
|
+
':method': 'POST',
|
|
54
|
+
':path': args.path,
|
|
55
|
+
'content-type': grpcContentType,
|
|
56
|
+
te: 'trailers',
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
// A request that is not answered in time is given up on
|
|
60
|
+
const timer = setTimeout(() => req.close(), timeoutMs);
|
|
61
|
+
|
|
62
|
+
// The call is finished with once, whatever the outcome
|
|
63
|
+
const finished = (err, res) => {
|
|
64
|
+
clearTimeout(timer);
|
|
65
|
+
|
|
66
|
+
req.removeListener('close', failed);
|
|
67
|
+
|
|
68
|
+
outcome.result = [err, res];
|
|
69
|
+
|
|
70
|
+
return !!outcome.cbk && outcome.cbk(err, res);
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
// A request that closes without a gRPC status is a failure
|
|
74
|
+
const failed = () => finished([503, 'FailedToGetAnswerFromGrpcServer']);
|
|
75
|
+
|
|
76
|
+
// A gRPC status means a gRPC server answered
|
|
77
|
+
const answered = status => {
|
|
78
|
+
const read = readMessage(concat(chunks));
|
|
79
|
+
|
|
80
|
+
if (!!read.is_invalid) {
|
|
81
|
+
return finished([503, 'ExpectedResponseMessageFromGrpcServer']);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return finished(null, {status, message: read.message});
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
// Failures close the request, so they are handled when it closes
|
|
88
|
+
req.on('error', ignore);
|
|
89
|
+
|
|
90
|
+
req.once('close', failed);
|
|
91
|
+
|
|
92
|
+
req.on('data', chunk => chunks.push(chunk));
|
|
93
|
+
|
|
94
|
+
req.once('response', headers => {
|
|
95
|
+
const contentType = headers['content-type'] || String();
|
|
96
|
+
|
|
97
|
+
// Only a gRPC server answers with a 200 and a gRPC content type
|
|
98
|
+
if (!isGrpcAnswer(headers[':status'], contentType)) {
|
|
99
|
+
return finished([503, 'ExpectedGrpcAnswerFromGrpcServer']);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// A trailers-only answer has its gRPC status in the headers
|
|
103
|
+
if (headers['grpc-status'] !== undefined) {
|
|
104
|
+
return answered(headers['grpc-status']);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// Otherwise the gRPC status comes in the trailers, after the body
|
|
108
|
+
return req.once('trailers', trailers => {
|
|
109
|
+
if (trailers['grpc-status'] === undefined) {
|
|
110
|
+
return finished([503, 'ExpectedGrpcStatusFromGrpcServer']);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return answered(trailers['grpc-status']);
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
req.end(args.request);
|
|
118
|
+
|
|
119
|
+
return {
|
|
120
|
+
// Wait for the outcome of the call, which may already be known
|
|
121
|
+
wait: cbk => {
|
|
122
|
+
// Exit early when the call is already finished
|
|
123
|
+
if (!!outcome.result) {
|
|
124
|
+
return cbk(...outcome.result);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
outcome.cbk = cbk;
|
|
128
|
+
|
|
129
|
+
return;
|
|
130
|
+
},
|
|
131
|
+
};
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
// Connect to a server, which throws for a socket that is not valid
|
|
135
|
+
const connectTo = socket => {
|
|
136
|
+
const connection = {};
|
|
137
|
+
const session = connect(`http://${socket}`);
|
|
138
|
+
|
|
139
|
+
// A session closed by a go away can leave its socket open, so it is kept
|
|
140
|
+
session.once('connect', (connected, socket) => connection.socket = socket);
|
|
141
|
+
|
|
142
|
+
// Failures to connect close the requests, so they are handled there
|
|
143
|
+
session.on('error', ignore);
|
|
144
|
+
|
|
145
|
+
return {
|
|
146
|
+
session,
|
|
147
|
+
close: () => {
|
|
148
|
+
session.destroy();
|
|
149
|
+
|
|
150
|
+
return !!connection.socket && connection.socket.destroy();
|
|
151
|
+
},
|
|
152
|
+
};
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
/** Check that a gRPC server without TLS is ready to answer requests
|
|
156
|
+
|
|
157
|
+
The standard gRPC health check is called, and the server is ready when the
|
|
158
|
+
health handler answers OK with a SERVING status.
|
|
159
|
+
|
|
160
|
+
The health service is optional in gRPC servers. When a fallback method is
|
|
161
|
+
given, a server without the health service is ready when that method
|
|
162
|
+
answers OK with a response message.
|
|
163
|
+
|
|
164
|
+
{
|
|
165
|
+
[fallback]: {
|
|
166
|
+
path: <Unary Method Path String>
|
|
167
|
+
request: <Framed gRPC Request Message Buffer Object>
|
|
168
|
+
}
|
|
169
|
+
socket: <gRPC Server Host:Port String>
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
@returns via cbk or Promise
|
|
173
|
+
*/
|
|
174
|
+
module.exports = (args, cbk) => {
|
|
175
|
+
return new Promise((resolve, reject) => {
|
|
176
|
+
return asyncAuto({
|
|
177
|
+
// Check arguments
|
|
178
|
+
validate: cbk => {
|
|
179
|
+
if (!!args.fallback && !args.fallback.path) {
|
|
180
|
+
return cbk([400, 'ExpectedFallbackMethodPathToCheckGrpcServer']);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
if (!!args.fallback && !Buffer.isBuffer(args.fallback.request)) {
|
|
184
|
+
return cbk([400, 'ExpectedFallbackRequestToCheckGrpcServer']);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
if (!args.socket) {
|
|
188
|
+
return cbk([400, 'ExpectedSocketToCheckGrpcServer']);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
return cbk();
|
|
192
|
+
},
|
|
193
|
+
|
|
194
|
+
// Connect to the server for the health check
|
|
195
|
+
healthSession: ['validate', asyncify(({}) => connectTo(args.socket))],
|
|
196
|
+
|
|
197
|
+
// Start the health check of the server
|
|
198
|
+
healthCall: ['healthSession', asyncify(({healthSession}) => {
|
|
199
|
+
return startCall({
|
|
200
|
+
path: healthPath,
|
|
201
|
+
request: emptyRequest,
|
|
202
|
+
session: healthSession.session,
|
|
203
|
+
});
|
|
204
|
+
})],
|
|
205
|
+
|
|
206
|
+
// Get the answer of the health check
|
|
207
|
+
health: ['healthCall', ({healthCall}, cbk) => healthCall.wait(cbk)],
|
|
208
|
+
|
|
209
|
+
// Connect again for the fallback method, as the server may have told
|
|
210
|
+
// the health check session to go away
|
|
211
|
+
fallbackSession: ['health', asyncify(({health}) => {
|
|
212
|
+
// Exit early when the health service answered
|
|
213
|
+
if (health.status !== unimplementedStatus || !args.fallback) {
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
return connectTo(args.socket);
|
|
218
|
+
})],
|
|
219
|
+
|
|
220
|
+
// Start the fallback method when the server has no health service
|
|
221
|
+
fallbackCall: ['fallbackSession', asyncify(({fallbackSession}) => {
|
|
222
|
+
// Exit early when there is no fallback method to call
|
|
223
|
+
if (!fallbackSession) {
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
return startCall({
|
|
228
|
+
path: args.fallback.path,
|
|
229
|
+
request: args.fallback.request,
|
|
230
|
+
session: fallbackSession.session,
|
|
231
|
+
});
|
|
232
|
+
})],
|
|
233
|
+
|
|
234
|
+
// Get the answer of the fallback method
|
|
235
|
+
fallback: ['fallbackCall', ({fallbackCall}, cbk) => {
|
|
236
|
+
// Exit early when the fallback method was not called
|
|
237
|
+
if (!fallbackCall) {
|
|
238
|
+
return cbk();
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
return fallbackCall.wait(cbk);
|
|
242
|
+
}],
|
|
243
|
+
|
|
244
|
+
// Check that the answer says the server is ready
|
|
245
|
+
ready: ['fallback', 'health', ({fallback, health}, cbk) => {
|
|
246
|
+
const answer = fallback || health;
|
|
247
|
+
|
|
248
|
+
// An unavailable server is not ready yet
|
|
249
|
+
if (answer.status === unavailableStatus) {
|
|
250
|
+
return cbk([503, 'GrpcServerIsUnavailable']);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// Exit early when no method shows that the server is ready
|
|
254
|
+
if (answer.status === unimplementedStatus) {
|
|
255
|
+
return cbk([503, 'ExpectedHealthServiceOnGrpcServer']);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
if (answer.status !== okStatus) {
|
|
259
|
+
return cbk([503, 'UnexpectedGrpcStatusFromGrpcServer', {
|
|
260
|
+
status: answer.status,
|
|
261
|
+
}]);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// A fallback method shows the server is ready with any response
|
|
265
|
+
if (!!fallback && !fallback.message) {
|
|
266
|
+
return cbk([503, 'ExpectedFallbackResponseFromGrpcServer']);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
// Exit early when the fallback method answered
|
|
270
|
+
if (!!fallback) {
|
|
271
|
+
return cbk();
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
const {message} = health;
|
|
275
|
+
|
|
276
|
+
// Statuses are small enum values, so they are a single varint byte
|
|
277
|
+
const isServing = !!message &&
|
|
278
|
+
message.length === 2 &&
|
|
279
|
+
message[0] === statusFieldKey &&
|
|
280
|
+
message[1] === servingStatus;
|
|
281
|
+
|
|
282
|
+
if (!isServing) {
|
|
283
|
+
return cbk([503, 'ExpectedServingStatusFromGrpcServer']);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
return cbk();
|
|
287
|
+
}],
|
|
288
|
+
},
|
|
289
|
+
(err, res) => {
|
|
290
|
+
const {fallbackSession, healthSession} = res || {};
|
|
291
|
+
|
|
292
|
+
const sessions = [healthSession, fallbackSession];
|
|
293
|
+
|
|
294
|
+
// The sessions are closed whether the server is ready or not
|
|
295
|
+
sessions.filter(n => !!n).forEach(n => n.close());
|
|
296
|
+
|
|
297
|
+
return returnResult({reject, resolve}, cbk)(err);
|
|
298
|
+
});
|
|
299
|
+
});
|
|
300
|
+
};
|
package/loop/index.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/** Retry a method until it works or until a time limit is up
|
|
2
|
+
|
|
3
|
+
A try is never started after the time limit, even when the event loop runs
|
|
4
|
+
a wait late, and a wait between tries ends early when the time limit is
|
|
5
|
+
reached during it. Time is measured on a monotonic clock, so changes to the
|
|
6
|
+
system time do not move the time limit
|
|
7
|
+
|
|
8
|
+
{
|
|
9
|
+
interval: <Wait Between Tries Milliseconds Number>
|
|
10
|
+
method: <Method to Try Function> (cbk) => {}
|
|
11
|
+
ms: <Time Limit Milliseconds Number>
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
@returns via cbk
|
|
15
|
+
<Method Result Object>
|
|
16
|
+
*/
|
|
17
|
+
module.exports = (args, cbk) => {
|
|
18
|
+
const startedAt = performance.now();
|
|
19
|
+
const state = {};
|
|
20
|
+
|
|
21
|
+
// The time is up once the time limit has passed since retrying started
|
|
22
|
+
const isLate = () => performance.now() - startedAt >= args.ms;
|
|
23
|
+
|
|
24
|
+
// The callback is only called once, however the retrying ends
|
|
25
|
+
const done = (err, res) => {
|
|
26
|
+
// Exit early when the retrying is already over
|
|
27
|
+
if (!!state.is_done) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
state.is_done = true;
|
|
32
|
+
|
|
33
|
+
clearTimeout(state.deadline);
|
|
34
|
+
clearTimeout(state.wait);
|
|
35
|
+
|
|
36
|
+
return cbk(err, res);
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
// Try the method, and wait to try again when it fails in time
|
|
40
|
+
const attempt = () => {
|
|
41
|
+
return args.method((err, res) => {
|
|
42
|
+
// Exit early when the method worked or when the time is up
|
|
43
|
+
if (!err || isLate()) {
|
|
44
|
+
return done(err, res);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
state.last_error = err;
|
|
48
|
+
|
|
49
|
+
return state.wait = setTimeout(() => {
|
|
50
|
+
state.wait = undefined;
|
|
51
|
+
|
|
52
|
+
// A wait can run late, so the time is checked right before a try
|
|
53
|
+
if (isLate()) {
|
|
54
|
+
return done(state.last_error);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return attempt();
|
|
58
|
+
},
|
|
59
|
+
args.interval);
|
|
60
|
+
});
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
// When the time is up during a wait, the wait ends with the last failure,
|
|
64
|
+
// while a try that is running is left to finish
|
|
65
|
+
state.deadline = setTimeout(() => {
|
|
66
|
+
return !!state.wait && done(state.last_error);
|
|
67
|
+
},
|
|
68
|
+
args.ms);
|
|
69
|
+
|
|
70
|
+
return attempt();
|
|
71
|
+
};
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
const asyncAuto = require('async/auto');
|
|
2
|
+
const asyncify = require('async/asyncify');
|
|
3
|
+
const {returnResult} = require('asyncjs-util');
|
|
4
|
+
|
|
5
|
+
const {addPeer} = require('./../bitcoinrpc');
|
|
6
|
+
const checkChainSynced = require('./check_chain_synced');
|
|
7
|
+
const checkGrpcServer = require('./check_grpc_server');
|
|
8
|
+
const {dockerLoopServerImage} = require('./constants');
|
|
9
|
+
const {killDockers} = require('./../docker');
|
|
10
|
+
const {makeSelfSignedCert} = require('./../tls');
|
|
11
|
+
const retryWithin = require('./retry_within');
|
|
12
|
+
const {spawnBitcoindDocker} = require('./../bitcoind');
|
|
13
|
+
const {spawnDockerImage} = require('./../docker');
|
|
14
|
+
const {startTlsProxy} = require('./../tls');
|
|
15
|
+
|
|
16
|
+
const bitcoindRpcPort = 18443;
|
|
17
|
+
const bufferAsBase64 = buffer => Buffer.from(buffer).toString('base64');
|
|
18
|
+
const certPath = '/root/.lnd/tls.cert';
|
|
19
|
+
const imageName = ver => !!ver ? `lightninglabs/loopserver:${ver}` : null;
|
|
20
|
+
const interval = 100;
|
|
21
|
+
const isBase64 = n => typeof n === 'string' && /^[a-z0-9+/]+={0,2}$/i.test(n);
|
|
22
|
+
const isHex = n => typeof n === 'string' && /^([0-9a-f]{2})+$/i.test(n);
|
|
23
|
+
const localSocket = port => `localhost:${port}`; // Proxy is on both loopbacks
|
|
24
|
+
const loopbackSocket = port => `127.0.0.1:${port}`; // Where ports are bound
|
|
25
|
+
const lndRpcPort = 10009;
|
|
26
|
+
const macaroonPath = '/root/.lnd/admin.macaroon';
|
|
27
|
+
const termsPath = '/looprpc.SwapServer/LoopOutTerms';
|
|
28
|
+
const termsRequest = Buffer.from('0000000002080b', 'hex'); // Protocol v11
|
|
29
|
+
const maxSwapTokens = 5e6;
|
|
30
|
+
const rpcPort = '11009/tcp';
|
|
31
|
+
const waitMs = 1000 * 60;
|
|
32
|
+
|
|
33
|
+
/** Spawn a Lightning Labs regtest Loop server Docker image, backed by an LND
|
|
34
|
+
|
|
35
|
+
{
|
|
36
|
+
chain_socket: <LND Chain P2P Socket String>
|
|
37
|
+
lnd_cert: <LND Base64 Serialized TLS Cert String>
|
|
38
|
+
lnd_docker_host: <LND Docker Network Host String>
|
|
39
|
+
lnd_macaroon: <LND Admin Macaroon Hex String>
|
|
40
|
+
[rpc_port]: <Loop Server RPC Port Number>
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
@returns via cbk or Promise
|
|
44
|
+
{
|
|
45
|
+
cert: <TLS Socket Base64 Serialized TLS Cert String>
|
|
46
|
+
kill: ({}, [cbk]) => <Kill Loop Server Promise>
|
|
47
|
+
socket: <Loop Server RPC Host:Port Network Address String>
|
|
48
|
+
tls_socket: <Loop Server TLS RPC Host:Port Network Address String>
|
|
49
|
+
}
|
|
50
|
+
*/
|
|
51
|
+
module.exports = (args, cbk) => {
|
|
52
|
+
return new Promise((resolve, reject) => {
|
|
53
|
+
return asyncAuto({
|
|
54
|
+
// Check arguments
|
|
55
|
+
validate: cbk => {
|
|
56
|
+
if (!args.chain_socket) {
|
|
57
|
+
return cbk([400, 'ExpectedChainSocketToSpawnLoopServerDocker']);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (!args.lnd_cert) {
|
|
61
|
+
return cbk([400, 'ExpectedLndCertToSpawnLoopServerDocker']);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// The cert is base64, the way spawnLightningDocker returns it
|
|
65
|
+
if (!isBase64(args.lnd_cert) || args.lnd_cert.length % 4) {
|
|
66
|
+
return cbk([400, 'ExpectedBase64LndCertToSpawnLoopServerDocker']);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (!args.lnd_docker_host) {
|
|
70
|
+
return cbk([400, 'ExpectedLndDockerHostToSpawnLoopServerDocker']);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (!args.lnd_macaroon) {
|
|
74
|
+
return cbk([400, 'ExpectedLndMacaroonToSpawnLoopServerDocker']);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// The macaroon is hex, the way spawnLightningDocker returns it
|
|
78
|
+
if (!isHex(args.lnd_macaroon)) {
|
|
79
|
+
return cbk([400, 'ExpectedHexLndMacaroonToSpawnLoopServerDocker']);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return cbk();
|
|
83
|
+
},
|
|
84
|
+
|
|
85
|
+
// Decode the LND cert and macaroon to copy them into the container
|
|
86
|
+
lndFiles: ['validate', ({}, cbk) => {
|
|
87
|
+
return cbk(null, [
|
|
88
|
+
{data: Buffer.from(args.lnd_cert, 'base64'), path: certPath},
|
|
89
|
+
{data: Buffer.from(args.lnd_macaroon, 'hex'), path: macaroonPath},
|
|
90
|
+
]);
|
|
91
|
+
}],
|
|
92
|
+
|
|
93
|
+
// Make a TLS certificate for the proxy in front of the Loop server
|
|
94
|
+
makeCert: ['validate', asyncify(({}) => makeSelfSignedCert({}))],
|
|
95
|
+
|
|
96
|
+
// Spin up a Bitcoin Core for the Loop server
|
|
97
|
+
spawnChain: ['lndFiles', 'makeCert', ({}, cbk) => {
|
|
98
|
+
return spawnBitcoindDocker({}, cbk);
|
|
99
|
+
}],
|
|
100
|
+
|
|
101
|
+
// The Loop server chain follows the chain of the LND
|
|
102
|
+
addChainPeer: ['spawnChain', ({spawnChain}, cbk) => {
|
|
103
|
+
return addPeer({
|
|
104
|
+
pass: spawnChain.rpc_pass,
|
|
105
|
+
port: spawnChain.rpc_port,
|
|
106
|
+
socket: args.chain_socket,
|
|
107
|
+
user: spawnChain.rpc_user,
|
|
108
|
+
},
|
|
109
|
+
err => {
|
|
110
|
+
// Stop the chain when it cannot follow the LND chain
|
|
111
|
+
if (!!err) {
|
|
112
|
+
return spawnChain.kill({}, () => cbk(err));
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return cbk();
|
|
116
|
+
});
|
|
117
|
+
}],
|
|
118
|
+
|
|
119
|
+
// Wait for the Loop server chain to have the blocks of the LND chain
|
|
120
|
+
waitForChain: ['addChainPeer', 'spawnChain', ({spawnChain}, cbk) => {
|
|
121
|
+
// The LND chain is its peer, so it has the blocks the LND chain had
|
|
122
|
+
const method = cbk => checkChainSynced({
|
|
123
|
+
pass: spawnChain.rpc_pass,
|
|
124
|
+
port: spawnChain.rpc_port,
|
|
125
|
+
socket: args.chain_socket,
|
|
126
|
+
user: spawnChain.rpc_user,
|
|
127
|
+
},
|
|
128
|
+
cbk);
|
|
129
|
+
|
|
130
|
+
return retryWithin({interval, method, ms: waitMs}, err => {
|
|
131
|
+
// Stop the chain when it never has the LND blocks
|
|
132
|
+
if (!!err) {
|
|
133
|
+
return spawnChain.kill({}, () => cbk(err));
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return cbk();
|
|
137
|
+
});
|
|
138
|
+
}],
|
|
139
|
+
|
|
140
|
+
// Spin up the docker image with the LND cert and macaroon in it
|
|
141
|
+
spawnDocker: [
|
|
142
|
+
'lndFiles',
|
|
143
|
+
'spawnChain',
|
|
144
|
+
'waitForChain',
|
|
145
|
+
({lndFiles, spawnChain}, cbk) =>
|
|
146
|
+
{
|
|
147
|
+
const chain = `${spawnChain.host}:${bitcoindRpcPort}`;
|
|
148
|
+
|
|
149
|
+
return spawnDockerImage({
|
|
150
|
+
arguments: [
|
|
151
|
+
'daemon',
|
|
152
|
+
`--bitcoin.host=${chain}`,
|
|
153
|
+
`--bitcoin.password=${spawnChain.rpc_pass}`,
|
|
154
|
+
`--bitcoin.user=${spawnChain.rpc_user}`,
|
|
155
|
+
// The daemon serves TLS unless told to serve plain gRPC
|
|
156
|
+
'--insecure',
|
|
157
|
+
`--lnd.host=${args.lnd_docker_host}:${lndRpcPort}`,
|
|
158
|
+
`--lnd.macaroonpath=${macaroonPath}`,
|
|
159
|
+
`--lnd.tlspath=${certPath}`,
|
|
160
|
+
`--maxamt=${maxSwapTokens}`,
|
|
161
|
+
],
|
|
162
|
+
files: lndFiles,
|
|
163
|
+
// A Loop server version can be picked in the environment
|
|
164
|
+
image: imageName(process.env.DOCKER_LOOP_SERVER_VERSION) ||
|
|
165
|
+
dockerLoopServerImage,
|
|
166
|
+
// Docker picks a free host port when no RPC port is given
|
|
167
|
+
ports: {[rpcPort]: args.rpc_port},
|
|
168
|
+
},
|
|
169
|
+
(err, res) => {
|
|
170
|
+
// Stop the chain when the Loop server does not start
|
|
171
|
+
if (!!err) {
|
|
172
|
+
return spawnChain.kill({}, () => cbk(err));
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
return cbk(null, res);
|
|
176
|
+
});
|
|
177
|
+
}],
|
|
178
|
+
|
|
179
|
+
// Wait for the Loop server to answer requests
|
|
180
|
+
waitForServer: [
|
|
181
|
+
'spawnChain',
|
|
182
|
+
'spawnDocker',
|
|
183
|
+
({spawnChain, spawnDocker}, cbk) =>
|
|
184
|
+
{
|
|
185
|
+
const dockers = [spawnDocker, spawnChain];
|
|
186
|
+
const socket = loopbackSocket(spawnDocker.ports[rpcPort]);
|
|
187
|
+
|
|
188
|
+
// Loop server images without the health service answer swap terms
|
|
189
|
+
const fallback = {path: termsPath, request: termsRequest};
|
|
190
|
+
|
|
191
|
+
const method = cbk => checkGrpcServer({fallback, socket}, cbk);
|
|
192
|
+
|
|
193
|
+
return retryWithin({interval, method, ms: waitMs}, err => {
|
|
194
|
+
// Exit early when the server answers
|
|
195
|
+
if (!err) {
|
|
196
|
+
return cbk();
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
return killDockers({dockers}, () => {
|
|
200
|
+
return cbk([503, 'FailedToConnectToLoopServer', {err}]);
|
|
201
|
+
});
|
|
202
|
+
});
|
|
203
|
+
}],
|
|
204
|
+
|
|
205
|
+
// The Loop server takes gRPC without TLS, so TLS clients use a proxy
|
|
206
|
+
startProxy: [
|
|
207
|
+
'makeCert',
|
|
208
|
+
'spawnChain',
|
|
209
|
+
'spawnDocker',
|
|
210
|
+
'waitForServer',
|
|
211
|
+
({makeCert, spawnChain, spawnDocker}, cbk) =>
|
|
212
|
+
{
|
|
213
|
+
return startTlsProxy({
|
|
214
|
+
cert: makeCert.cert,
|
|
215
|
+
key: makeCert.key,
|
|
216
|
+
port: spawnDocker.ports[rpcPort],
|
|
217
|
+
},
|
|
218
|
+
(err, res) => {
|
|
219
|
+
// Stop the Loop server and its chain when the proxy does not start
|
|
220
|
+
if (!!err) {
|
|
221
|
+
return killDockers({dockers: [spawnDocker, spawnChain]}, () => {
|
|
222
|
+
return cbk(err);
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
return cbk(null, res);
|
|
227
|
+
});
|
|
228
|
+
}],
|
|
229
|
+
|
|
230
|
+
// Final Loop server details
|
|
231
|
+
loopServer: [
|
|
232
|
+
'makeCert',
|
|
233
|
+
'spawnChain',
|
|
234
|
+
'spawnDocker',
|
|
235
|
+
'startProxy',
|
|
236
|
+
({makeCert, spawnChain, spawnDocker, startProxy}, cbk) =>
|
|
237
|
+
{
|
|
238
|
+
const dockers = [spawnDocker, spawnChain];
|
|
239
|
+
|
|
240
|
+
return cbk(null, {
|
|
241
|
+
cert: bufferAsBase64(makeCert.cert),
|
|
242
|
+
kill: ({}, cbk) => new Promise((resolve, reject) => {
|
|
243
|
+
return asyncAuto({
|
|
244
|
+
// Stop passing connections on to the Loop server
|
|
245
|
+
closeProxy: cbk => startProxy.close({}, cbk),
|
|
246
|
+
|
|
247
|
+
// Stop the Loop server and its chain
|
|
248
|
+
killDockers: ['closeProxy', ({}, cbk) => {
|
|
249
|
+
return killDockers({dockers}, cbk);
|
|
250
|
+
}],
|
|
251
|
+
},
|
|
252
|
+
returnResult({reject, resolve}, cbk));
|
|
253
|
+
}),
|
|
254
|
+
// The socket checked for readiness, as gRPC without TLS takes an IP
|
|
255
|
+
socket: loopbackSocket(spawnDocker.ports[rpcPort]),
|
|
256
|
+
// A TLS host name, as grpc-js warns about IP addresses as TLS names
|
|
257
|
+
tls_socket: localSocket(startProxy.port),
|
|
258
|
+
});
|
|
259
|
+
}],
|
|
260
|
+
},
|
|
261
|
+
returnResult({reject, resolve, of: 'loopServer'}, cbk));
|
|
262
|
+
});
|
|
263
|
+
};
|
package/package.json
CHANGED
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"async": "3.2.6",
|
|
16
16
|
"asyncjs-util": "1.2.12",
|
|
17
17
|
"dockerode": "5.0.1",
|
|
18
|
-
"lightning": "
|
|
18
|
+
"lightning": "14.0.0"
|
|
19
19
|
},
|
|
20
20
|
"description": "Spawn and control LN Docker daemons",
|
|
21
21
|
"engines": {
|
|
@@ -28,7 +28,9 @@
|
|
|
28
28
|
"docker",
|
|
29
29
|
"index.js",
|
|
30
30
|
"lnd",
|
|
31
|
-
"
|
|
31
|
+
"loop",
|
|
32
|
+
"setup",
|
|
33
|
+
"tls"
|
|
32
34
|
],
|
|
33
35
|
"keywords": [
|
|
34
36
|
"docker",
|
|
@@ -45,5 +47,5 @@
|
|
|
45
47
|
"integration-tests": "node --test \"test/integration/*.js\"",
|
|
46
48
|
"test": "node --experimental-test-coverage --test \"test/!(integration)/**/*.js\""
|
|
47
49
|
},
|
|
48
|
-
"version": "
|
|
50
|
+
"version": "9.0.0"
|
|
49
51
|
}
|