ln-docker-daemons 8.0.5 → 8.1.1

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 CHANGED
@@ -126,13 +126,25 @@ await kill({});
126
126
 
127
127
  Spawn a cluster of nodes
128
128
 
129
+ When a channel capacity is specified, each node opens a channel of that size
130
+ to the next node, so the nodes form a line: A -> B -> C
131
+
129
132
  {
133
+ [capacity]: <Channel Capacity Tokens Number>
130
134
  [lnd_configuration]: [<LND Configuration Argument String>]
131
135
  [size]: <Total Lightning Nodes Number>
132
136
  }
133
137
 
134
138
  @returns via cbk or Promise
135
139
  {
140
+ channels: [{
141
+ from: <Opening Node Index Number>
142
+ id: <Standard Format Channel Id String>
143
+ to: <Partner Node Index Number>
144
+ transaction_id: <Funding Transaction Id Hex String>
145
+ transaction_vout: <Funding Transaction Output Index Number>
146
+ }]
147
+ kill: <Kill All Nodes Function> ({}) => {}
136
148
  nodes: [{
137
149
  generate: <Make Block Function> ({address, count}, [cbk]) => {}
138
150
  id: <Node Public Key Hex String>
@@ -163,6 +175,23 @@ await generate({count: 500});
163
175
  await kill({});
164
176
  ```
165
177
 
178
+ Example with channels:
179
+
180
+ ```node
181
+ const {spawnLightningCluster} = require('ln-docker-daemons');
182
+
183
+ // Launch three nodes with channels Alice -> Bob -> Carol
184
+ const {channels, kill, nodes} = await spawnLightningCluster({
185
+ capacity: 1e6,
186
+ size: 3,
187
+ });
188
+
189
+ const [alice, bob, carol] = nodes;
190
+
191
+ // Stop all the nodes
192
+ await kill({});
193
+ ```
194
+
166
195
  ## `spawnLightningDocker`
167
196
 
168
197
  Spawn an LND Docker
@@ -0,0 +1,222 @@
1
+ const asyncAuto = require('async/auto');
2
+ const asyncMapSeries = require('async/mapSeries');
3
+ const asyncRetry = require('async/retry');
4
+ const asyncTimesSeries = require('async/timesSeries');
5
+ const {returnResult} = require('asyncjs-util');
6
+
7
+ const getBlockchainInfo = require('./get_blockchain_info');
8
+ const rpc = require('./rpc');
9
+ const unknownGeneratedError = require('./unknown_generated_error');
10
+
11
+ const host = 'localhost';
12
+ const txVerbosityFlag = 2;
13
+
14
+ /** Check what blocks a failed generate request made
15
+
16
+ Generate requests run one at a time, so blocks on top of the start height
17
+ can only be from this request, unless they were mined outside this process.
18
+ They must all pay to the address, and there can't be more than requested.
19
+
20
+ A request that may still be running is given time to finish mining. When a
21
+ request finished without mining anything, its original error is returned.
22
+
23
+ {
24
+ address: <Coinbase Output Address String>
25
+ count: <Requested Blocks Count Number>
26
+ err: <Generate Request Error Object>
27
+ interval: <Poll Interval Milliseconds Number>
28
+ [is_finished]: <Request Finished Running Bool>
29
+ pass: <RPC Password String>
30
+ port: <RPC Port Number>
31
+ start: <Chain Height Before Generating Number>
32
+ times: <Poll Times Number>
33
+ user: <RPC Username String>
34
+ }
35
+
36
+ @returns via cbk or Promise
37
+ [<Generated Block Hash Hex String>]
38
+ */
39
+ module.exports = (args, cbk) => {
40
+ return new Promise((resolve, reject) => {
41
+ return asyncAuto({
42
+ // Check arguments
43
+ validate: cbk => {
44
+ if (!args.address) {
45
+ return cbk([400, 'ExpectedAddressToCheckGeneratedBlocks']);
46
+ }
47
+
48
+ if (!args.count) {
49
+ return cbk([400, 'ExpectedCountToCheckGeneratedBlocks']);
50
+ }
51
+
52
+ if (!args.err) {
53
+ return cbk([400, 'ExpectedGenerateErrorToCheckGeneratedBlocks']);
54
+ }
55
+
56
+ if (args.start === undefined) {
57
+ return cbk([400, 'ExpectedStartHeightToCheckGeneratedBlocks']);
58
+ }
59
+
60
+ return cbk();
61
+ },
62
+
63
+ // Get the chain height, waiting for blocks from a request still running
64
+ getHeight: ['validate', ({}, cbk) => {
65
+ const target = args.start + args.count;
66
+ const times = !!args.is_finished ? 1 : args.times;
67
+
68
+ return asyncRetry({times, interval: args.interval}, cbk => {
69
+ return getBlockchainInfo({
70
+ pass: args.pass,
71
+ port: args.port,
72
+ user: args.user,
73
+ },
74
+ (err, res) => {
75
+ if (!!err) {
76
+ return cbk(err);
77
+ }
78
+
79
+ if (res.blocks < target) {
80
+ return cbk([503, 'ExpectedMoreBlocks', {height: res.blocks}]);
81
+ }
82
+
83
+ return cbk(null, res.blocks);
84
+ });
85
+ },
86
+ (err, height) => {
87
+ // A chain short of the expected height still has a known height
88
+ if (!!err && err[1] === 'ExpectedMoreBlocks') {
89
+ return cbk(null, err[2].height);
90
+ }
91
+
92
+ if (!!err) {
93
+ return cbk(unknownGeneratedError({
94
+ count: args.count,
95
+ err: args.err,
96
+ start: args.start,
97
+ }));
98
+ }
99
+
100
+ return cbk(null, height);
101
+ });
102
+ }],
103
+
104
+ // Make the error for when it cannot be known what blocks were made
105
+ unknown: ['getHeight', ({getHeight}, cbk) => {
106
+ return cbk(null, unknownGeneratedError({
107
+ count: args.count,
108
+ err: args.err,
109
+ height: getHeight,
110
+ start: args.start,
111
+ }));
112
+ }],
113
+
114
+ // Make sure the chain height can be explained by the request
115
+ checkHeight: ['getHeight', 'unknown', ({getHeight, unknown}, cbk) => {
116
+ const target = args.start + args.count;
117
+
118
+ // Losing blocks or gaining extra blocks means others changed the chain
119
+ if (getHeight < args.start || getHeight > target) {
120
+ return cbk(unknown);
121
+ }
122
+
123
+ // A request that may still be running might not be done mining yet
124
+ if (getHeight < target && !args.is_finished) {
125
+ return cbk(unknown);
126
+ }
127
+
128
+ // A request that finished without mining failed without any effect
129
+ if (getHeight === args.start) {
130
+ return cbk(args.err);
131
+ }
132
+
133
+ return cbk();
134
+ }],
135
+
136
+ // Get the hashes of the blocks on top of the start height
137
+ getHashes: [
138
+ 'checkHeight',
139
+ 'getHeight',
140
+ 'unknown',
141
+ ({getHeight, unknown}, cbk) =>
142
+ {
143
+ return asyncTimesSeries(getHeight - args.start, (i, cbk) => {
144
+ return rpc({
145
+ host,
146
+ cmd: 'getblockhash',
147
+ params: [args.start + i + 1],
148
+ pass: args.pass,
149
+ port: args.port,
150
+ user: args.user,
151
+ },
152
+ cbk);
153
+ },
154
+ (err, hashes) => {
155
+ if (!!err) {
156
+ return cbk(unknown);
157
+ }
158
+
159
+ return cbk(null, hashes);
160
+ });
161
+ }],
162
+
163
+ // Get the blocks to look at their coinbase outputs
164
+ getBlocks: ['getHashes', 'unknown', ({getHashes, unknown}, cbk) => {
165
+ return asyncMapSeries(getHashes, (id, cbk) => {
166
+ return rpc({
167
+ host,
168
+ cmd: 'getblock',
169
+ params: [id, txVerbosityFlag],
170
+ pass: args.pass,
171
+ port: args.port,
172
+ user: args.user,
173
+ },
174
+ cbk);
175
+ },
176
+ (err, blocks) => {
177
+ if (!!err) {
178
+ return cbk(unknown);
179
+ }
180
+
181
+ return cbk(null, blocks);
182
+ });
183
+ }],
184
+
185
+ // Make sure the blocks pay to the address and were not mined elsewhere
186
+ check: [
187
+ 'getBlocks',
188
+ 'getHashes',
189
+ 'unknown',
190
+ ({getBlocks, getHashes, unknown}, cbk) =>
191
+ {
192
+ const others = getBlocks.filter(block => {
193
+ const [coinbase] = block.tx || [];
194
+ const {vout} = coinbase || {};
195
+ const scripts = (vout || []).map(n => n.scriptPubKey || {});
196
+
197
+ return !scripts.map(n => n.address).includes(args.address);
198
+ });
199
+
200
+ if (!!others.length) {
201
+ return cbk(unknown);
202
+ }
203
+
204
+ // Exit early when all of the requested blocks were made
205
+ if (getHashes.length === args.count) {
206
+ return cbk(null, getHashes);
207
+ }
208
+
209
+ const [, cause, details] = args.err;
210
+ const {code, message} = details || {};
211
+
212
+ // The request finished after making only some of the blocks
213
+ return cbk([
214
+ 503,
215
+ 'GeneratedFewerBlocksThanRequested',
216
+ {cause, code, message, count: args.count, hashes: getHashes},
217
+ ]);
218
+ }],
219
+ },
220
+ returnResult({reject, resolve, of: 'check'}, cbk));
221
+ });
222
+ };
@@ -1,13 +1,17 @@
1
1
  const asyncAuto = require('async/auto');
2
+ const asyncQueue = require('async/queue');
2
3
  const {returnResult} = require('asyncjs-util');
3
4
 
4
- const rpc = require('./rpc');
5
+ const mineBlocks = require('./mine_blocks');
5
6
 
6
- const cmd = 'generatetoaddress';
7
- const host = 'localhost';
7
+ const queue = asyncQueue(mineBlocks, 1);
8
8
 
9
9
  /** Generate blocks and mine coinbase outputs to an address
10
10
 
11
+ Requests run one at a time. When the outcome of a request is unclear, blocks
12
+ on top of where it started can then only be from that request, unless they
13
+ were mined outside of this process.
14
+
11
15
  {
12
16
  address: <Address to Mine Outputs Towards String>
13
17
  [count]: <Generate Count Number>
@@ -17,9 +21,7 @@ const host = 'localhost';
17
21
  }
18
22
 
19
23
  @returns via cbk or Promise
20
- {
21
- blocks: <Best Chain Block Height Number>
22
- }
24
+ [<Generated Block Hash Hex String>]
23
25
  */
24
26
  module.exports = ({address, count, pass, port, user}, cbk) => {
25
27
  return new Promise((resolve, reject) => {
@@ -45,13 +47,18 @@ module.exports = ({address, count, pass, port, user}, cbk) => {
45
47
  return cbk();
46
48
  },
47
49
 
48
- // Execute request
49
- request: ['validate', ({}, cbk) => {
50
- const params = [count || [address].length, address];
51
-
52
- return rpc({cmd, host, pass, params, port, user}, cbk);
50
+ // Mine the blocks once earlier generate requests are done
51
+ generate: ['validate', ({}, cbk) => {
52
+ return queue.push({
53
+ address,
54
+ pass,
55
+ port,
56
+ user,
57
+ count: count || [address].length,
58
+ },
59
+ cbk);
53
60
  }],
54
61
  },
55
- returnResult({reject, resolve, of: 'request'}, cbk));
62
+ returnResult({reject, resolve, of: 'generate'}, cbk));
56
63
  });
57
64
  };
@@ -0,0 +1,121 @@
1
+ const asyncAuto = require('async/auto');
2
+ const asyncRetry = require('async/retry');
3
+ const {returnResult} = require('asyncjs-util');
4
+
5
+ const checkGenerated = require('./check_generated');
6
+ const getBlockchainInfo = require('./get_blockchain_info');
7
+ const rpc = require('./rpc');
8
+
9
+ const checkInterval = 100;
10
+ const checkTimes = 100;
11
+ const cmd = 'generatetoaddress';
12
+ const hasResponse = err => isCommandError(err) || !!(err[2] || {}).status;
13
+ const host = 'localhost';
14
+ const isCommandError = err => err[1] === 'UnexpectedErrorFromRpcCommand';
15
+ const isDisconnected = err => isUnsent(err) || isLost(err);
16
+ const isInvalid = err => err[0] === 400;
17
+ const isLost = err => err[1] === 'ConnectionToBitcoindRpcServiceLost';
18
+ const isUnsent = err => err[1] === 'ConnectionToBitcoindRpcServiceFailed';
19
+ const retryRead = {errorFilter: isDisconnected, interval: 100, times: 5};
20
+ const retryUnsent = {errorFilter: isUnsent, interval: 100, times: 5};
21
+
22
+ /** Mine blocks to an address, checking the chain when the outcome is unclear
23
+
24
+ A request is only retried when it never reached bitcoind. When it fails in
25
+ any other way, it may have mined blocks, so the chain is checked for them.
26
+
27
+ {
28
+ address: <Address to Mine Outputs Towards String>
29
+ count: <Generate Count Number>
30
+ pass: <RPC Password String>
31
+ port: <RPC Port Number>
32
+ user: <RPC Username String>
33
+ }
34
+
35
+ @returns via cbk or Promise
36
+ [<Generated Block Hash Hex String>]
37
+ */
38
+ module.exports = ({address, count, pass, port, user}, cbk) => {
39
+ return new Promise((resolve, reject) => {
40
+ return asyncAuto({
41
+ // Check arguments
42
+ validate: cbk => {
43
+ if (!address) {
44
+ return cbk([400, 'ExpectedAddressToMineBlocks']);
45
+ }
46
+
47
+ if (!count) {
48
+ return cbk([400, 'ExpectedCountOfBlocksToMine']);
49
+ }
50
+
51
+ if (!pass) {
52
+ return cbk([400, 'ExpectedRpcPasswordToMineBlocks']);
53
+ }
54
+
55
+ if (!port) {
56
+ return cbk([400, 'ExpectedRpcPortNumberToMineBlocks']);
57
+ }
58
+
59
+ if (!user) {
60
+ return cbk([400, 'ExpectedRpcUsernameToMineBlocks']);
61
+ }
62
+
63
+ return cbk();
64
+ },
65
+
66
+ // Get the chain height to check against if the request fails
67
+ getStart: ['validate', ({}, cbk) => {
68
+ return asyncRetry(retryRead, cbk => {
69
+ return getBlockchainInfo({pass, port, user}, cbk);
70
+ },
71
+ cbk);
72
+ }],
73
+
74
+ // Generate the blocks, retrying when the request never reached bitcoind
75
+ generate: ['getStart', ({}, cbk) => {
76
+ const params = [count, address];
77
+
78
+ return asyncRetry(retryUnsent, cbk => {
79
+ return rpc({cmd, host, pass, params, port, user}, cbk);
80
+ },
81
+ (err, hashes) => {
82
+ if (!err) {
83
+ return cbk(null, {hashes});
84
+ }
85
+
86
+ // Pass back errors where the request never reached bitcoind
87
+ if (isUnsent(err) || isInvalid(err)) {
88
+ return cbk(err);
89
+ }
90
+
91
+ // Otherwise the request may have mined blocks, so check the chain
92
+ return cbk(null, {err});
93
+ });
94
+ }],
95
+
96
+ // Check the chain for blocks when the generate request failed
97
+ check: ['generate', 'getStart', ({generate, getStart}, cbk) => {
98
+ // Exit early when bitcoind returned the generated block hashes, since
99
+ // the blocks are known to exist and there is nothing to check
100
+ if (!generate.err) {
101
+ return cbk(null, generate.hashes);
102
+ }
103
+
104
+ return checkGenerated({
105
+ address,
106
+ count,
107
+ pass,
108
+ port,
109
+ user,
110
+ err: generate.err,
111
+ interval: checkInterval,
112
+ is_finished: hasResponse(generate.err),
113
+ start: getStart.blocks,
114
+ times: checkTimes,
115
+ },
116
+ cbk);
117
+ }],
118
+ },
119
+ returnResult({reject, resolve, of: 'check'}, cbk));
120
+ });
121
+ };
package/bitcoinrpc/rpc.js CHANGED
@@ -1,31 +1,36 @@
1
- const fetch = require('@alexbosworth/node-fetch');
2
- const http = require('http');
3
- const https = require('https');
4
-
5
1
  const asyncAuto = require('async/auto');
6
2
  const {returnResult} = require('asyncjs-util');
7
3
 
8
4
  const defaultTimeout = 1000 * 30;
9
- const agents = {};
5
+ const {isInteger} = Number;
6
+ const isSuccess = code => code >= 200 && code < 300;
7
+ const isTimeout = err => ['AbortError', 'TimeoutError'].includes(err.name);
8
+ const lostCodes = ['ECONNRESET', 'EPIPE', 'UND_ERR_CLOSED', 'UND_ERR_SOCKET'];
9
+ const {parse} = JSON;
10
10
  let requests = 0;
11
11
  const {stringify} = JSON;
12
+ const unsentCodes = ['ECONNREFUSED', 'UND_ERR_CONNECT_TIMEOUT'];
12
13
 
13
14
  /** Call JSON RPC
14
15
 
15
16
  {
16
- [cert]: <Cert Buffer Object>
17
17
  cmd: <Command String>
18
18
  host: <Host Name String>
19
19
  params: [<Parameter Object>]
20
20
  pass: <Password String>
21
21
  port: <Port Number>
22
+ [timeout]: <Request Timeout Milliseconds Number>
22
23
  user: <Username String>
23
24
  }
24
25
 
26
+ ConnectionToBitcoindRpcServiceFailed means the request never reached the
27
+ server. ConnectionToBitcoindRpcServiceLost and TimedOutWaitingForRpcResponse
28
+ mean the server may have executed the command.
29
+
25
30
  @returns via cbk or Promise
26
31
  <Result Object>
27
32
  */
28
- module.exports = ({cert, cmd, host, params, pass, port, user}, cbk) => {
33
+ module.exports = ({cmd, host, params, pass, port, timeout, user}, cbk) => {
29
34
  return new Promise((resolve, reject) => {
30
35
  return asyncAuto({
31
36
  // Check arguments
@@ -46,6 +51,10 @@ module.exports = ({cert, cmd, host, params, pass, port, user}, cbk) => {
46
51
  return cbk([400, 'ExpectedRpcPasswordToExecuteRpcCall']);
47
52
  }
48
53
 
54
+ if (timeout !== undefined && !(isInteger(timeout) && timeout > 0)) {
55
+ return cbk([400, 'ExpectedPositiveTimeoutToExecuteRpcCall']);
56
+ }
57
+
49
58
  if (!user) {
50
59
  return cbk([400, 'ExpectedRpcUsernameToExecuteRpcCall']);
51
60
  }
@@ -53,51 +62,111 @@ module.exports = ({cert, cmd, host, params, pass, port, user}, cbk) => {
53
62
  return cbk();
54
63
  },
55
64
 
56
- // Derive an HTTP agent as necessary for using a self-signed cert
57
- agent: ['validate', async ({}) => {
58
- // Exit early when there is no cert and this is a regular HTTP request
59
- if (!cert) {
60
- return new http.Agent({});
65
+ // Send request to the server, failures are returned rather than thrown
66
+ request: ['validate', async ({}) => {
67
+ const credentials = Buffer.from(`${user}:${pass}`);
68
+
69
+ try {
70
+ const response = await fetch(`http://${host}:${port}/`, {
71
+ body: stringify({params, id: `${++requests}`, method: cmd}),
72
+ headers: {
73
+ 'Authorization': `Basic ${credentials.toString('base64')}`,
74
+ 'Content-Type': 'application/json',
75
+ },
76
+ method: 'POST',
77
+ signal: AbortSignal.timeout(timeout || defaultTimeout),
78
+ });
79
+
80
+ return {body: await response.text(), statusCode: response.status};
81
+ } catch (err) {
82
+ return {err};
83
+ }
84
+ }],
85
+
86
+ // Check for a failure to get a response from the server
87
+ checkRequest: ['request', ({request}, cbk) => {
88
+ const {err} = request;
89
+
90
+ // Exit early when there is a response
91
+ if (!err) {
92
+ return cbk();
93
+ }
94
+
95
+ if (isTimeout(err)) {
96
+ return cbk([503, 'TimedOutWaitingForRpcResponse', {cmd}]);
61
97
  }
62
98
 
63
- return new https.Agent({cert, ca: [cert], ecdhCurve: 'auto'});
99
+ // Undici puts the underlying failure reason into the error cause
100
+ const cause = err.cause || {};
101
+ const code = cause.code || err.code;
102
+
103
+ // Failing to connect to every address of a host groups the failures
104
+ const [failure] = cause.errors || [];
105
+ const reason = failure || cause;
106
+
107
+ const details = {
108
+ cmd,
109
+ code,
110
+ err,
111
+ message: reason.message || err.message,
112
+ };
113
+
114
+ // The connection was never made so the command was not executed
115
+ if (unsentCodes.includes(code)) {
116
+ return cbk([503, 'ConnectionToBitcoindRpcServiceFailed', details]);
117
+ }
118
+
119
+ // The connection broke so the command may have been executed
120
+ if (lostCodes.includes(code)) {
121
+ return cbk([503, 'ConnectionToBitcoindRpcServiceLost', details]);
122
+ }
123
+
124
+ return cbk([503, 'UnexpectedErrorFromRpcService', details]);
64
125
  }],
65
126
 
66
- // Send request to the server
67
- request: ['agent', async ({agent}) => {
68
- const credentials = Buffer.from(`${user}:${pass}`);
69
- const scheme = !!cert ? 'https' : 'http';
127
+ // Parse the response body as a JSON-RPC response object
128
+ parseBody: ['checkRequest', 'request', ({request}, cbk) => {
129
+ const details = {cmd, status: request.statusCode};
130
+ let body;
70
131
 
71
132
  try {
72
- const response = await fetch(
73
- `${scheme}://${host}:${port}/`,
74
- {
75
- agent,
76
- body: stringify({
77
- id: `${++requests}`,
78
- method: cmd,
79
- params: params,
80
- }),
81
- headers: {
82
- 'Authorization': `Basic ${credentials.toString('base64')}`,
83
- 'Content-Type': 'application/json',
84
- },
85
- method: 'POST',
86
- },
87
- );
133
+ body = parse(request.body);
134
+ } catch (err) {
135
+ return cbk([503, 'FailedToParseRpcServiceResponse', details]);
136
+ }
88
137
 
89
- const {result} = await response.json();
138
+ if (!body || typeof body !== 'object') {
139
+ return cbk([503, 'UnexpectedRpcServiceResponse', details]);
140
+ }
90
141
 
91
- return result;
92
- } catch (err) {
93
- if (err.code === 'ECONNRESET') {
94
- throw [503, 'ConnectionToBitcoindRpcServiceFailed'];
95
- }
142
+ return cbk(null, body);
143
+ }],
144
+
145
+ // Check the response for a command failure and pull out the result
146
+ result: ['parseBody', 'request', ({parseBody, request}, cbk) => {
147
+ const {error} = parseBody;
148
+
149
+ // A JSON-RPC command failure is reported in the error attribute
150
+ if (error !== undefined && error !== null) {
151
+ return cbk([
152
+ 503,
153
+ 'UnexpectedErrorFromRpcCommand',
154
+ {cmd, code: error.code, message: error.message},
155
+ ]);
156
+ }
96
157
 
97
- throw [503, 'UnexpectedErrorFromRpcService', {err}];
158
+ // Without a command failure the HTTP response should be a success
159
+ if (!isSuccess(request.statusCode)) {
160
+ return cbk([
161
+ 503,
162
+ 'UnexpectedRpcServiceResponse',
163
+ {cmd, status: request.statusCode},
164
+ ]);
98
165
  }
166
+
167
+ return cbk(null, parseBody.result);
99
168
  }],
100
169
  },
101
- returnResult({reject, resolve, of: 'request'}, cbk));
170
+ returnResult({reject, resolve, of: 'result'}, cbk));
102
171
  });
103
172
  };
@@ -0,0 +1,22 @@
1
+ /** Make an error for when it cannot be known that blocks were generated
2
+
3
+ {
4
+ count: <Expected Generated Blocks Count Number>
5
+ err: <Generate Request Error Object>
6
+ [height]: <Current Chain Height Number>
7
+ start: <Chain Height Before Generating Number>
8
+ }
9
+
10
+ @returns
11
+ [<Error Code Number>, <Error Message String>, <Error Details Object>]
12
+ */
13
+ module.exports = ({count, err, height, start}) => {
14
+ const [, cause, details] = err;
15
+ const {code} = details || {};
16
+
17
+ return [
18
+ 503,
19
+ 'UnknownIfBlocksWereGenerated',
20
+ {cause, code, count, height, start},
21
+ ];
22
+ };
@@ -0,0 +1,82 @@
1
+ const asyncAuto = require('async/auto');
2
+ const asyncRetry = require('async/retry');
3
+ const {createChainAddress} = require('lightning');
4
+ const {getUtxos} = require('lightning');
5
+ const {returnResult} = require('asyncjs-util');
6
+
7
+ const interval = 10;
8
+ const maturity = 100;
9
+ const times = 3000;
10
+
11
+ /** Generate blocks for a node, waiting for a UTXO when enough blocks mature
12
+
13
+ {
14
+ [address]: <Mine Coinbase Outputs to Address String>
15
+ [count]: <Generate Blocks Count Number>
16
+ generate: <Generate Blocks Function> ({address, count}, cbk) => {}
17
+ lnd: <Authenticated LND API Object>
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.generate) {
28
+ return cbk([400, 'ExpectedGenerateFunctionToGenerateBlocks']);
29
+ }
30
+
31
+ if (!args.lnd) {
32
+ return cbk([400, 'ExpectedAuthenticatedLndToGenerateBlocks']);
33
+ }
34
+
35
+ return cbk();
36
+ },
37
+
38
+ // Get a node address to mine to when no address is specified
39
+ getAddress: ['validate', ({}, cbk) => {
40
+ // Exit early when there is an address to mine to
41
+ if (!!args.address) {
42
+ return cbk(null, {address: args.address});
43
+ }
44
+
45
+ return createChainAddress({lnd: args.lnd}, cbk);
46
+ }],
47
+
48
+ // Generate the blocks
49
+ generate: ['getAddress', ({getAddress}, cbk) => {
50
+ return args.generate({
51
+ address: getAddress.address,
52
+ count: args.count,
53
+ },
54
+ cbk);
55
+ }],
56
+
57
+ // Wait for a UTXO to show up when there are enough blocks to mature it
58
+ waitForUtxo: ['generate', ({}, cbk) => {
59
+ // Exit early when there are not enough blocks to mature a coinbase
60
+ if (!args.count || args.count < maturity) {
61
+ return cbk();
62
+ }
63
+
64
+ return asyncRetry({interval, times}, cbk => {
65
+ return getUtxos({lnd: args.lnd}, (err, res) => {
66
+ if (!!err) {
67
+ return cbk(err);
68
+ }
69
+
70
+ if (!res.utxos.length) {
71
+ return cbk([503, 'ExpectedUtxoInUtxos']);
72
+ }
73
+
74
+ return cbk();
75
+ });
76
+ },
77
+ cbk);
78
+ }],
79
+ },
80
+ returnResult({reject, resolve}, cbk));
81
+ });
82
+ };
@@ -1,14 +1,15 @@
1
1
  const asyncAuto = require('async/auto');
2
2
  const asyncEach = require('async/each');
3
3
  const asyncMap = require('async/map');
4
+ const asyncMapSeries = require('async/mapSeries');
4
5
  const asyncRetry = require('async/retry');
5
6
  const {authenticatedLndGrpc} = require('lightning');
6
- const {createChainAddress} = require('lightning');
7
7
  const {findFreePorts} = require('find-free-ports');
8
- const {getUtxos} = require('lightning');
9
8
  const {getIdentity} = require('lightning');
10
9
  const {returnResult} = require('asyncjs-util');
11
10
 
11
+ const generateBlocks = require('./generate_blocks');
12
+ const {setupChannel} = require('./../setup');
12
13
  const {spawnLightningDocker} = require('./../lnd');
13
14
 
14
15
  const between = (min, max) => Math.floor(Math.random() * (max - min) + min);
@@ -17,22 +18,33 @@ const count = size => size || 1;
17
18
  const endPort = 65000;
18
19
  const generateAddress = '2N8hwP1WmJrFF5QWABn38y63uYLhnJYJYTF';
19
20
  const interval = 10;
20
- const makeAddress = ({lnd}) => createChainAddress({lnd});
21
- const maturity = 100;
21
+ const {isInteger} = Number;
22
+ const line = arr => arr.slice(1).map((_, i) => [i, i + 1]);
22
23
  const pairs = n => n.map((x, i) => n.slice(i + 1).map(y => [x, y])).flat();
23
24
  const portsPerLnd = 7;
24
25
  const startPort = 1025;
25
- const times = 3000;
26
26
 
27
27
  /** Spawn a cluster of nodes
28
28
 
29
+ When a channel capacity is specified, each node opens a channel of that size
30
+ to the next node, so the nodes form a line: A -> B -> C
31
+
29
32
  {
33
+ [capacity]: <Channel Capacity Tokens Number>
30
34
  [lnd_configuration]: [<LND Configuration Argument String>]
31
35
  [size]: <Total Lightning Nodes Number>
32
36
  }
33
37
 
34
38
  @returns via cbk or Promise
35
39
  {
40
+ channels: [{
41
+ from: <Opening Node Index Number>
42
+ id: <Standard Format Channel Id String>
43
+ to: <Partner Node Index Number>
44
+ transaction_id: <Funding Transaction Id Hex String>
45
+ transaction_vout: <Funding Transaction Output Index Number>
46
+ }]
47
+ kill: <Kill All Nodes Function> ({}) => {}
36
48
  nodes: [{
37
49
  generate: <Make Block Function> ({address, count}, [cbk]) => {}
38
50
  id: <Node Public Key Hex String>
@@ -47,8 +59,21 @@ const times = 3000;
47
59
  module.exports = (args, cbk) => {
48
60
  return new Promise((resolve, reject) => {
49
61
  return asyncAuto({
62
+ // Check arguments
63
+ validate: cbk => {
64
+ if (args.capacity === undefined) {
65
+ return cbk();
66
+ }
67
+
68
+ if (!isInteger(args.capacity) || args.capacity <= 0) {
69
+ return cbk([400, 'ExpectedPositiveClusterChannelCapacity']);
70
+ }
71
+
72
+ return cbk();
73
+ },
74
+
50
75
  // Spawn nodes
51
- spawn: async () => {
76
+ spawn: ['validate', async () => {
52
77
  return await asyncRetry({interval, times: 25}, async () => {
53
78
  const options = {startPort: between(startPort, endPort)};
54
79
 
@@ -100,29 +125,14 @@ module.exports = (args, cbk) => {
100
125
  getBlockInfo: lightningDocker.get_block_info,
101
126
  socket: lightningDocker.chain_socket,
102
127
  },
103
- generate: ({address, count}) => {
104
- return new Promise(async (resolve, reject) => {
105
- await lightningDocker.generate({
106
- count,
107
- address: address || (await makeAddress({lnd})).address,
108
- });
109
-
110
- if (!count || count < maturity) {
111
- return resolve();
112
- }
113
-
114
- await asyncRetry({interval, times}, async () => {
115
- const [utxo] = (await getUtxos({lnd})).utxos;
116
-
117
- if (!utxo) {
118
- throw new Error('ExpectedUtxoInUtxos');
119
- }
120
-
121
- return utxo;
122
- });
123
-
124
- return resolve();
125
- });
128
+ generate: ({address, count}, cbk) => {
129
+ return generateBlocks({
130
+ address,
131
+ count,
132
+ lnd,
133
+ generate: lightningDocker.generate,
134
+ },
135
+ cbk);
126
136
  },
127
137
  kill: lightningDocker.kill,
128
138
  public_key: id,
@@ -140,7 +150,7 @@ module.exports = (args, cbk) => {
140
150
  };
141
151
  });
142
152
  });
143
- },
153
+ }],
144
154
 
145
155
  // Connect nodes in the cluster to each other
146
156
  connect: ['spawn', async ({spawn}) => {
@@ -151,9 +161,42 @@ module.exports = (args, cbk) => {
151
161
  });
152
162
  }],
153
163
 
164
+ // Open channels between neighboring nodes, one at a time
165
+ channels: ['connect', 'spawn', async ({spawn}) => {
166
+ // Exit early when there is no channel capacity to create channels
167
+ if (!args.capacity) {
168
+ return [];
169
+ }
170
+
171
+ try {
172
+ return await asyncMapSeries(line(spawn), async ([from, to]) => {
173
+ const opened = await setupChannel({
174
+ capacity: args.capacity,
175
+ generate: spawn[from].generate,
176
+ lnd: spawn[from].lnd,
177
+ to: spawn[to],
178
+ });
179
+
180
+ return {
181
+ from,
182
+ to,
183
+ id: opened.id,
184
+ transaction_id: opened.transaction_id,
185
+ transaction_vout: opened.transaction_vout,
186
+ };
187
+ });
188
+ } catch (err) {
189
+ // Clean up the spawned nodes when channel setup fails
190
+ await asyncEach(spawn, async ({kill}) => await kill({}));
191
+
192
+ throw err;
193
+ }
194
+ }],
195
+
154
196
  // Final set of nodes
155
- nodes: ['connect', 'spawn', async ({spawn}) => {
197
+ nodes: ['channels', 'connect', 'spawn', async ({channels, spawn}) => {
156
198
  return {
199
+ channels,
157
200
  kill: ({}) => asyncEach(spawn, async ({kill}) => await kill({})),
158
201
  nodes: spawn.map(node => ({
159
202
  generate: node.generate,
@@ -0,0 +1,47 @@
1
+ const blockSize = 512;
2
+ const {ceil} = Math;
3
+ const {fromCharCode} = String;
4
+ const isZero = n => !n;
5
+ const metaTypes = ['g', 'K', 'L', 'x'];
6
+ const parseOctal = n => parseInt(n.replace(/\0.*$/, '').trim(), 8) || 0;
7
+ const sizeEnd = 136;
8
+ const sizeStart = 124;
9
+ const typeOffset = 156;
10
+
11
+ /** Get the contents of the first file in a tar archive
12
+
13
+ Pax and GNU long name metadata entries are skipped
14
+
15
+ {
16
+ tar: <Tar Archive Buffer Object>
17
+ }
18
+
19
+ @returns
20
+ {
21
+ [file]: <File Buffer Object>
22
+ }
23
+ */
24
+ module.exports = ({tar}) => {
25
+ let offset = 0;
26
+
27
+ while (offset + blockSize <= tar.length) {
28
+ const header = tar.subarray(offset, offset + blockSize);
29
+
30
+ // Exit early when hitting the end of archive marker
31
+ if (header.every(isZero)) {
32
+ return {};
33
+ }
34
+
35
+ const size = parseOctal(header.toString('ascii', sizeStart, sizeEnd));
36
+ const start = offset + blockSize;
37
+
38
+ // Exit early when this is a regular entry
39
+ if (!metaTypes.includes(fromCharCode(header[typeOffset]))) {
40
+ return {file: tar.subarray(start, start + size)};
41
+ }
42
+
43
+ offset = start + ceil(size / blockSize) * blockSize;
44
+ }
45
+
46
+ return {};
47
+ };
@@ -1,6 +1,9 @@
1
1
  const asyncAuto = require('async/auto');
2
2
  const {returnResult} = require('asyncjs-util');
3
- const tar = require('tar-stream');
3
+
4
+ const firstFileInTar = require('./first_file_in_tar');
5
+
6
+ const {concat} = Buffer;
4
7
 
5
8
  /** Get a file from inside a docker container
6
9
 
@@ -42,26 +45,17 @@ module.exports = ({container, path}, cbk) => {
42
45
  return cbk([503, 'UnexpectedErrorGettingFileFromDocker', {err}]);
43
46
  }
44
47
 
45
- const entries = [];
46
- const extract = tar.extract();
47
-
48
- extract.once('entry', (header, stream, cbk) => {
49
- const parts = [];
50
-
51
- stream.on('data', part => parts.push(part));
48
+ const parts = [];
52
49
 
53
- stream.once('end', () => {
54
- entries.push(Buffer.concat(parts));
50
+ stream.on('data', part => parts.push(part));
55
51
 
56
- return cbk();
57
- });
58
-
59
- return stream.resume();
52
+ stream.once('error', err => {
53
+ return cbk([503, 'FailedToReadFileArchiveFromDocker', {err}]);
60
54
  });
61
55
 
62
- extract.once('finish', () => cbk(null, entries));
63
-
64
- stream.pipe(extract);
56
+ stream.once('end', () => {
57
+ return cbk(null, firstFileInTar({tar: concat(parts)}).file);
58
+ });
65
59
 
66
60
  return;
67
61
  });
@@ -69,9 +63,7 @@ module.exports = ({container, path}, cbk) => {
69
63
 
70
64
  // Final resulting file
71
65
  file: ['getArchive', ({getArchive}, cbk) => {
72
- const [file] = getArchive;
73
-
74
- return cbk(null, {file});
66
+ return cbk(null, {file: getArchive});
75
67
  }],
76
68
  },
77
69
  returnResult({reject, resolve, of: 'file'}, cbk));
@@ -1,3 +1,3 @@
1
1
  {
2
- "dockerLndImage": "lightninglabs/lnd:v0.21.3-beta"
2
+ "dockerLndImage": "lightninglabs/lnd:v0.21.4-beta"
3
3
  }
@@ -141,13 +141,14 @@ module.exports = (args, cbk) => {
141
141
  }),
142
142
  cert: spawnLightningDaemon.cert,
143
143
  chain_socket: `${spawnChainDaemon.host}`,
144
- generate: ({address, count}) => generateToAddress({
144
+ generate: ({address, count}, cbk) => generateToAddress({
145
145
  address,
146
146
  count,
147
147
  pass: spawnChainDaemon.rpc_pass,
148
148
  port: args.chain_rpc_port,
149
149
  user: spawnChainDaemon.rpc_user,
150
- }),
150
+ },
151
+ cbk),
151
152
  get_block_info: ({id}) => getBlockInfo({
152
153
  id,
153
154
  pass: spawnChainDaemon.rpc_pass,
package/package.json CHANGED
@@ -6,24 +6,31 @@
6
6
  },
7
7
  "author": {
8
8
  "name": "Alex Bosworth",
9
- "url": "https://twitter.com/alexbosworth"
9
+ "url": "https://x.com/alexbosworth"
10
10
  },
11
11
  "bugs": {
12
12
  "url": "https://github.com/alexbosworth/ln-docker-daemons/issues"
13
13
  },
14
14
  "dependencies": {
15
- "@alexbosworth/node-fetch": "2.6.2",
16
15
  "async": "3.2.6",
17
16
  "asyncjs-util": "1.2.12",
18
17
  "dockerode": "5.0.1",
19
18
  "find-free-ports": "3.1.1",
20
- "lightning": "12.2.11",
21
- "tar-stream": "3.2.1"
19
+ "lightning": "13.1.4"
22
20
  },
23
21
  "description": "Spawn and control LN Docker daemons",
24
22
  "engines": {
25
23
  "node": ">=22"
26
24
  },
25
+ "files": [
26
+ "bitcoind",
27
+ "bitcoinrpc",
28
+ "cluster",
29
+ "docker",
30
+ "index.js",
31
+ "lnd",
32
+ "setup"
33
+ ],
27
34
  "keywords": [
28
35
  "docker",
29
36
  "lnd"
@@ -33,10 +40,10 @@
33
40
  "name": "ln-docker-daemons",
34
41
  "repository": {
35
42
  "type": "git",
36
- "url": "https://github.com/alexbosworth/ln-docker-daemons.git"
43
+ "url": "git+https://github.com/alexbosworth/ln-docker-daemons.git"
37
44
  },
38
45
  "scripts": {
39
46
  "test": "node --experimental-test-coverage --test"
40
47
  },
41
- "version": "8.0.5"
48
+ "version": "8.1.1"
42
49
  }
package/.gitattributes DELETED
@@ -1 +0,0 @@
1
- package-lock.json binary
package/.tm_properties DELETED
@@ -1 +0,0 @@
1
- exclude = '{$exclude,node_modules}'
package/CHANGELOG.md DELETED
@@ -1,214 +0,0 @@
1
- # Versions
2
-
3
- ## 8.0.5
4
-
5
- - Change default LND version to v0.21.3-beta
6
-
7
- ## 8.0.4
8
-
9
- - Change default LND version to v0.21.2-beta
10
-
11
- ## 8.0.3
12
-
13
- - Change default LND version to v0.21.1-beta
14
-
15
- ## 8.0.2
16
-
17
- - Change default LND version to v0.21.0-beta
18
-
19
- ## 8.0.0
20
-
21
- - Require node.js v22+
22
-
23
- ## 7.0.2
24
-
25
- - Change default LND version to v0.20.1-beta
26
-
27
- ### Breaking Changes
28
-
29
- - Require node.js v20+
30
-
31
- ## 6.0.28
32
-
33
- - Change default LND version to v0.20.0-beta
34
-
35
- ## 6.0.27
36
-
37
- - Change default LND version to v0.19.3-beta
38
-
39
- ## 6.0.26
40
-
41
- - Change default LND version to v0.18.5-beta
42
-
43
- ## 6.0.23
44
-
45
- - Change default LND version to v0.18.2-beta
46
-
47
- ## 6.0.20
48
-
49
- - Change default LND version to v0.18.1-beta
50
-
51
- ## 6.0.19
52
-
53
- - Change default LND version to v0.18.0-beta
54
-
55
- ## 6.0.16
56
-
57
- - Change default LND version to v0.17.5-beta
58
-
59
- ## 6.0.13
60
-
61
- - Change default LND version to v0.17.4-beta
62
-
63
- ## 6.0.9
64
-
65
- - Change default LND version to v0.17.1-beta
66
-
67
- ## 6.0.3
68
-
69
- - Change default Bitcoin Core version to 23.0
70
-
71
- ## 6.0.1
72
-
73
- - Change default LND version to v0.17.0-beta
74
-
75
- ### Breaking Changes
76
-
77
- - node.js 16 is no longer supported, minimum node.js version is 18
78
-
79
- ## 5.1.3
80
-
81
- - Change default LND version to v0.16.4-beta
82
-
83
- ## 5.1.0
84
-
85
- - `setupChannel`: add `is_private` to setup a private channel
86
-
87
- ## 5.0.7
88
-
89
- - Change default LND version to v0.16.3-beta
90
-
91
- ## 5.0.4
92
-
93
- - Change default LND version to v0.16.2-beta
94
-
95
- ## 5.0.1
96
-
97
- - Change default LND version to v0.16.1-beta
98
-
99
- ## 5.0.0
100
-
101
- ### Breaking Changes
102
-
103
- - node.js 14 is no longer supported, minimum node.js version is 16
104
-
105
- ## 4.1.4
106
-
107
- - Change default LND version to v0.16.0-beta
108
-
109
- ## 4.1.2
110
-
111
- - `spawnLndDocker`: Add method to spawn an LND docker
112
-
113
- ## 4.0.4
114
-
115
- - `spawnLightningCluster`: Return `tower` for the watchtower server socket
116
- - `spawnLightningDocker`: Add `tower_port` to specify watchtower server port
117
- - `spawnLightningDocker`: Return `tower_socket` for the watchtower socket
118
-
119
- ### Breaking Changes
120
-
121
- - `spawnLightningDocker`: `tower_port` is now a required argument
122
-
123
- ## 3.1.6
124
-
125
- - Change default LND version to v0.15.5-beta
126
-
127
- ## 3.1.5
128
-
129
- - Change default LND version to v0.15.4-beta
130
-
131
- ## 3.1.3
132
-
133
- - Change default LND version to v0.15.3-beta
134
-
135
- ## 3.1.1
136
-
137
- - Change default LND version to v0.15.2-beta
138
-
139
- ## 3.1.0
140
-
141
- - `spawnLightningDocker`: Add argument `seed` to use a predetermined seed
142
-
143
- ## 3.0.1
144
-
145
- - `spawnLightningCluster`: Add `rpc` for custom macaroon rpc connections
146
-
147
- ### Breaking Changes
148
-
149
- - Versions of node.js before 14 are no longer supported
150
-
151
- ## 2.3.6
152
-
153
- - Change default LND version to v0.15.1-beta
154
-
155
- ## 2.3.5
156
-
157
- - Change default LND version to v0.15.0-beta
158
-
159
- ## 2.3.1
160
-
161
- - `setupChannel`: Add method to setup a channel between LND nodes
162
-
163
- ## 2.2.12
164
-
165
- - Change default LND version to v0.14.3-beta
166
-
167
- ## 2.2.8
168
-
169
- - Change default LND version to v0.14.2-beta
170
-
171
- ## 2.2.3
172
-
173
- - Change Bitcoin Core version to 22.0
174
-
175
- ## 2.2.2
176
-
177
- - Change default LND version to v0.14.1-beta
178
-
179
- ## 2.2.1
180
-
181
- - `spawnLightningCluster`, `spawnLightningDocker`: Add `lnd_configuration` to
182
- allow specifying additional LND configuration parameters
183
-
184
- ## 2.1.1
185
-
186
- - `DOCKER_LND_VERSION`: add environment variable to control docker lnd image
187
-
188
- ## 2.0.0
189
-
190
- - `spawnLightningCluster`: add `socket` to reveal node LN p2p sockets
191
- - `spawnLightningDocker`: add `add_chain_peer` method to add a chain peer
192
- - `spawnLightningDocker`: add `chain_socket` method for the chain p2p socket
193
- - `spawnLightningDocker`: add `ln_socket` method for the chain p2p socket
194
-
195
- ### Breaking Changes
196
-
197
- - Node 12+ is required
198
-
199
- ## 1.2.0
200
-
201
- - `spawnLightningCluster` add `generate` to generate coins for a node
202
-
203
- ## 1.1.0
204
-
205
- - Add `spawnLightningCluster` to spawn multiple lightning dockers
206
-
207
- ## 1.0.2
208
-
209
- - Update lnd to 0.13.1 and Bitcoin Core to 0.21.1
210
-
211
- ## 1.0.1
212
-
213
- - `spawnBitcoindDocker`: Add method to spawn a Bitcoind Docker image
214
- - `spawnLightningDocker`: Add method to spawn linked Bitcoind and LND images
@@ -1,24 +0,0 @@
1
- const test = require('node:test');
2
-
3
- const {findFreePorts} = require('find-free-ports');
4
-
5
- const {spawnBitcoindDocker} = require('./../../');
6
-
7
- // Spawning a Bitcoin Core docker image should launch
8
- test('Spawn Bitcoin Core Docker image', async () => {
9
- const [p2pPort, rpcPort, zmqBlockPort, zmqTxPort] = await findFreePorts(
10
- 4,
11
- {startPort: 5000}
12
- );
13
-
14
- const {kill} = await spawnBitcoindDocker({
15
- p2p_port: p2pPort,
16
- rpc_port: rpcPort,
17
- zmq_block_port: zmqBlockPort,
18
- zmq_tx_port: zmqTxPort,
19
- });
20
-
21
- await kill({});
22
-
23
- return;
24
- });
@@ -1,21 +0,0 @@
1
- const test = require('node:test');
2
-
3
- const {spawnLightningDocker} = require('./../../');
4
-
5
- // Spawning a Lightning node docker image should launch
6
- test('Spawn Lightning node image', async () => {
7
- const {kill} = await spawnLightningDocker({
8
- chain_p2p_port: 18458,
9
- chain_rpc_port: 18459,
10
- chain_zmq_block_port: 18460,
11
- chain_zmq_tx_port: 18461,
12
- generate_address: '2N8hwP1WmJrFF5QWABn38y63uYLhnJYJYTF',
13
- lightning_p2p_port: 18462,
14
- lightning_rpc_port: 18463,
15
- lightning_tower_port: 18464,
16
- });
17
-
18
- await kill({});
19
-
20
- return;
21
- });