ln-docker-daemons 8.1.4 → 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.
@@ -34,11 +34,12 @@ const defaultBitcoindRpcPort = 18443;
34
34
  add_chain_peer: <Add Peer Function> ({socket}) => {}
35
35
  cert: <LND Base64 Serialized TLS Cert>
36
36
  chain_socket: <Chain P2P Socket String>
37
+ docker_host: <LND Docker Network Host String>
37
38
  generate: ({address, count}, cbk) => <Generate to Address Promise>
38
39
  get_block_info: <Get Block Info Function> ({id}) => {}
39
40
  kill: ({}, [cbk]) => <Kill LND Daemon Promise>
40
41
  ln_socket: <LN P2P Socket String>
41
- macaroon: <LND Base64 Serialized Macaroon String>
42
+ macaroon: <LND Hex Serialized Macaroon String>
42
43
  public_key: <Identity Public Key Hex String>
43
44
  socket: <LND RPC Host:Port Network Address String>
44
45
  tower_socket: <LND Tower Socket Host:Port Network Address String>
@@ -125,24 +126,25 @@ module.exports = (args, cbk) => {
125
126
  const dockers = [spawnChainDaemon, spawnLightningDaemon];
126
127
 
127
128
  return cbk(null, {
128
- add_chain_peer: ({socket}) => addPeer({
129
- socket,
129
+ add_chain_peer: args => addPeer({
130
130
  pass: spawnChainDaemon.rpc_pass,
131
131
  port: spawnChainDaemon.rpc_port,
132
+ socket: args.socket,
132
133
  user: spawnChainDaemon.rpc_user,
133
134
  }),
134
135
  cert: spawnLightningDaemon.cert,
135
136
  chain_socket: `${spawnChainDaemon.host}`,
136
- generate: ({address, count}, cbk) => generateToAddress({
137
- address,
138
- count,
137
+ docker_host: spawnLightningDaemon.host,
138
+ generate: (args, cbk) => generateToAddress({
139
+ address: args.address,
140
+ count: args.count,
139
141
  pass: spawnChainDaemon.rpc_pass,
140
142
  port: spawnChainDaemon.rpc_port,
141
143
  user: spawnChainDaemon.rpc_user,
142
144
  },
143
145
  cbk),
144
- get_block_info: ({id}) => getBlockInfo({
145
- id,
146
+ get_block_info: args => getBlockInfo({
147
+ id: args.id,
146
148
  pass: spawnChainDaemon.rpc_pass,
147
149
  port: spawnChainDaemon.rpc_port,
148
150
  user: spawnChainDaemon.rpc_user,
@@ -1,4 +1,5 @@
1
1
  const asyncAuto = require('async/auto');
2
+ const asyncify = require('async/asyncify');
2
3
  const asyncRetry = require('async/retry');
3
4
  const {authenticatedLndGrpc} = require('lightning');
4
5
  const {getWalletInfo} = require('lightning/lnd_methods');
@@ -11,7 +12,7 @@ const {spawnDockerImage} = require('./../docker');
11
12
  const imageName = ver => !!ver ? `lightninglabs/lnd:${ver}` : dockerLndImage;
12
13
  const interval = 100;
13
14
  const join = n => n.join('');
14
- const localSocket = port => `localhost:${port}`;
15
+ const localSocket = port => `localhost:${port}`; // grpc-js warns on IP names
15
16
  const macaroonPath = '/root/.lnd/data/chain/bitcoin/regtest/admin.macaroon';
16
17
  const p2pPort = '9735/tcp';
17
18
  const rpcPort = '10009/tcp';
@@ -42,7 +43,7 @@ const towerPort = '9911/tcp';
42
43
  {
43
44
  cert: <LND Base64 Serialized TLS Cert Base64 String>
44
45
  kill: ({}, [cbk]) => <Kill LND Dockers Promise>
45
- macaroon: <LND Base64 Serialized Macaroon String>
46
+ macaroon: <LND Hex Serialized Macaroon String>
46
47
  public_key: <LND Public Key Hex String>
47
48
  socket: <LND RPC Host:Port Network Address String>
48
49
  tower_socket: <LND Tower Socket Host:Port Network Address String>
@@ -146,26 +147,29 @@ module.exports = (args, cbk) => {
146
147
  cbk);
147
148
  }],
148
149
 
149
- // Create a wallet for the LND
150
- createWallet: [
150
+ // Connect to the wallet unlocker, which throws for bad details
151
+ unauthenticated: [
151
152
  'getCertificate',
152
153
  'spawnDocker',
153
- ({getCertificate, spawnDocker}, cbk) =>
154
+ asyncify(({getCertificate, spawnDocker}) =>
154
155
  {
155
- const cert = getCertificate.file.toString('hex');
156
- const socket = localSocket(spawnDocker.ports[rpcPort]);
157
-
158
- const {lnd} = unauthenticatedLndGrpc({cert, socket});
156
+ return unauthenticatedLndGrpc({
157
+ cert: getCertificate.file.toString('hex'),
158
+ socket: localSocket(spawnDocker.ports[rpcPort]),
159
+ }).lnd;
160
+ })],
159
161
 
162
+ // Create a wallet for the LND
163
+ createWallet: ['unauthenticated', ({unauthenticated}, cbk) => {
160
164
  return asyncRetry({interval, times}, cbk => {
161
- return lnd.unlocker.genSeed({}, (err, res) => {
165
+ return unauthenticated.unlocker.genSeed({}, (err, res) => {
162
166
  if (!!err) {
163
167
  return cbk([503, 'UnexpectedErrorGeneratingSeed', {err}]);
164
168
  }
165
169
 
166
170
  const seed = res.cipher_seed_mnemonic.join(' ');
167
171
 
168
- lnd.unlocker.initWallet({
172
+ unauthenticated.unlocker.initWallet({
169
173
  cipher_seed_mnemonic: (args.seed || seed).split(' '),
170
174
  wallet_password: Buffer.from('password', 'utf8'),
171
175
  },
@@ -181,21 +185,24 @@ module.exports = (args, cbk) => {
181
185
  cbk);
182
186
  }],
183
187
 
184
- // Wait for gRPC to respond
185
- waitForRpc: [
188
+ // Connect with the wallet macaroon, which throws for bad details
189
+ authenticated: [
186
190
  'createWallet',
187
191
  'getCertificate',
188
192
  'spawnDocker',
189
- ({createWallet, getCertificate, spawnDocker}, cbk) =>
193
+ asyncify(({createWallet, getCertificate, spawnDocker}) =>
190
194
  {
191
- const {lnd} = authenticatedLndGrpc({
195
+ return authenticatedLndGrpc({
192
196
  cert: getCertificate.file.toString('hex'),
193
197
  macaroon: createWallet.toString('hex'),
194
198
  socket: localSocket(spawnDocker.ports[rpcPort]),
195
- });
199
+ }).lnd;
200
+ })],
196
201
 
202
+ // Wait for gRPC to respond
203
+ waitForRpc: ['authenticated', ({authenticated}, cbk) => {
197
204
  return asyncRetry({interval, times}, cbk => {
198
- return getWalletInfo({lnd}, cbk);
205
+ return getWalletInfo({lnd: authenticated}, cbk);
199
206
  },
200
207
  cbk);
201
208
  }],
@@ -0,0 +1,119 @@
1
+ const asyncAuto = require('async/auto');
2
+ const {returnResult} = require('asyncjs-util');
3
+
4
+ const {getAddedNodeInfo} = require('./../bitcoinrpc');
5
+ const {getBlockchainInfo} = require('./../bitcoinrpc');
6
+ const {getPeerInfo} = require('./../bitcoinrpc');
7
+
8
+ const {isSafeInteger} = Number;
9
+ const timeout = 1000 * 5;
10
+
11
+ /** Check that a Bitcoin Core has the blocks a peer had when it connected
12
+
13
+ {
14
+ pass: <RPC Password String>
15
+ port: <RPC Port Number>
16
+ socket: <Peer Socket String>
17
+ user: <RPC Username String>
18
+ }
19
+
20
+ @returns via cbk or Promise
21
+ */
22
+ module.exports = (args, cbk) => {
23
+ return new Promise((resolve, reject) => {
24
+ return asyncAuto({
25
+ // Check arguments
26
+ validate: cbk => {
27
+ if (!args.pass) {
28
+ return cbk([400, 'ExpectedRpcPasswordToCheckChainSynced']);
29
+ }
30
+
31
+ if (!args.port) {
32
+ return cbk([400, 'ExpectedRpcPortToCheckChainSynced']);
33
+ }
34
+
35
+ if (!args.socket) {
36
+ return cbk([400, 'ExpectedPeerSocketToCheckChainSynced']);
37
+ }
38
+
39
+ if (!args.user) {
40
+ return cbk([400, 'ExpectedRpcUserToCheckChainSynced']);
41
+ }
42
+
43
+ return cbk();
44
+ },
45
+
46
+ // Get the actual address of the configured peer
47
+ getAddedNode: ['validate', ({}, cbk) => {
48
+ return getAddedNodeInfo({
49
+ timeout,
50
+ pass: args.pass,
51
+ port: args.port,
52
+ socket: args.socket,
53
+ user: args.user,
54
+ },
55
+ cbk);
56
+ }],
57
+
58
+ // Get the height of the chain
59
+ getInfo: ['validate', ({}, cbk) => {
60
+ return getBlockchainInfo({
61
+ timeout,
62
+ pass: args.pass,
63
+ port: args.port,
64
+ user: args.user,
65
+ },
66
+ cbk);
67
+ }],
68
+
69
+ // Get the heights that the peers had when they connected
70
+ getPeers: ['getAddedNode', ({getAddedNode}, cbk) => {
71
+ if (!getAddedNode.addresses.length) {
72
+ return cbk([503, 'ExpectedPeerToSyncChainWith']);
73
+ }
74
+
75
+ return getPeerInfo({
76
+ timeout,
77
+ pass: args.pass,
78
+ port: args.port,
79
+ user: args.user,
80
+ },
81
+ cbk);
82
+ }],
83
+
84
+ // The chain has to be as far along as the peer was when it connected
85
+ check: [
86
+ 'getAddedNode',
87
+ 'getInfo',
88
+ 'getPeers',
89
+ ({getAddedNode, getInfo, getPeers}, cbk) =>
90
+ {
91
+ const {addresses} = getAddedNode;
92
+
93
+ // Bitcoin Core can report the original addnode argument in getpeerinfo
94
+ const peer = getPeers.peers.find(n => {
95
+ return n.socket === args.socket || addresses.includes(n.socket);
96
+ });
97
+
98
+ // Exit early when the peer has not connected yet
99
+ if (!peer) {
100
+ return cbk([503, 'ExpectedPeerToSyncChainWith']);
101
+ }
102
+
103
+ const height = peer.starting_height;
104
+
105
+ // Bitcoin Core shows -1 until the peer has said what its height is
106
+ if (!isSafeInteger(height) || height < 0) {
107
+ return cbk([503, 'ExpectedPeerToAdvertiseItsChainHeight']);
108
+ }
109
+
110
+ if (getInfo.blocks < height) {
111
+ return cbk([503, 'ExpectedChainToHaveTheBlocksOfItsPeer']);
112
+ }
113
+
114
+ return cbk();
115
+ }],
116
+ },
117
+ returnResult({reject, resolve}, cbk));
118
+ });
119
+ };
@@ -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
+ };
@@ -0,0 +1,3 @@
1
+ {
2
+ "dockerLoopServerImage": "lightninglabs/loopserver:v0.1.1"
3
+ }
package/loop/index.js ADDED
@@ -0,0 +1,3 @@
1
+ const spawnLoopServerDocker = require('./spawn_loop_server_docker');
2
+
3
+ module.exports = {spawnLoopServerDocker};
@@ -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
+ };