ln-docker-daemons 8.0.4 → 8.1.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 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
package/bitcoinrpc/rpc.js CHANGED
@@ -1,19 +1,13 @@
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 = {};
10
5
  let requests = 0;
11
6
  const {stringify} = JSON;
12
7
 
13
8
  /** Call JSON RPC
14
9
 
15
10
  {
16
- [cert]: <Cert Buffer Object>
17
11
  cmd: <Command String>
18
12
  host: <Host Name String>
19
13
  params: [<Parameter Object>]
@@ -25,7 +19,7 @@ const {stringify} = JSON;
25
19
  @returns via cbk or Promise
26
20
  <Result Object>
27
21
  */
28
- module.exports = ({cert, cmd, host, params, pass, port, user}, cbk) => {
22
+ module.exports = ({cmd, host, params, pass, port, user}, cbk) => {
29
23
  return new Promise((resolve, reject) => {
30
24
  return asyncAuto({
31
25
  // Check arguments
@@ -53,26 +47,14 @@ module.exports = ({cert, cmd, host, params, pass, port, user}, cbk) => {
53
47
  return cbk();
54
48
  },
55
49
 
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({});
61
- }
62
-
63
- return new https.Agent({cert, ca: [cert], ecdhCurve: 'auto'});
64
- }],
65
-
66
50
  // Send request to the server
67
- request: ['agent', async ({agent}) => {
51
+ request: ['validate', async ({}) => {
68
52
  const credentials = Buffer.from(`${user}:${pass}`);
69
- const scheme = !!cert ? 'https' : 'http';
70
53
 
71
54
  try {
72
- const response = await fetch(
73
- `${scheme}://${host}:${port}/`,
55
+ const response = await fetch(
56
+ `http://${host}:${port}/`,
74
57
  {
75
- agent,
76
58
  body: stringify({
77
59
  id: `${++requests}`,
78
60
  method: cmd,
@@ -90,7 +72,7 @@ module.exports = ({cert, cmd, host, params, pass, port, user}, cbk) => {
90
72
 
91
73
  return result;
92
74
  } catch (err) {
93
- if (err.code === 'ECONNRESET') {
75
+ if ([err.code, err.cause?.code].includes('ECONNRESET')) {
94
76
  throw [503, 'ConnectionToBitcoindRpcServiceFailed'];
95
77
  }
96
78
 
@@ -1,6 +1,7 @@
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
7
  const {createChainAddress} = require('lightning');
@@ -9,6 +10,7 @@ const {getUtxos} = require('lightning');
9
10
  const {getIdentity} = require('lightning');
10
11
  const {returnResult} = require('asyncjs-util');
11
12
 
13
+ const {setupChannel} = require('./../setup');
12
14
  const {spawnLightningDocker} = require('./../lnd');
13
15
 
14
16
  const between = (min, max) => Math.floor(Math.random() * (max - min) + min);
@@ -17,6 +19,8 @@ const count = size => size || 1;
17
19
  const endPort = 65000;
18
20
  const generateAddress = '2N8hwP1WmJrFF5QWABn38y63uYLhnJYJYTF';
19
21
  const interval = 10;
22
+ const {isInteger} = Number;
23
+ const line = arr => arr.slice(1).map((_, i) => [i, i + 1]);
20
24
  const makeAddress = ({lnd}) => createChainAddress({lnd});
21
25
  const maturity = 100;
22
26
  const pairs = n => n.map((x, i) => n.slice(i + 1).map(y => [x, y])).flat();
@@ -26,13 +30,25 @@ const times = 3000;
26
30
 
27
31
  /** Spawn a cluster of nodes
28
32
 
33
+ When a channel capacity is specified, each node opens a channel of that size
34
+ to the next node, so the nodes form a line: A -> B -> C
35
+
29
36
  {
37
+ [capacity]: <Channel Capacity Tokens Number>
30
38
  [lnd_configuration]: [<LND Configuration Argument String>]
31
39
  [size]: <Total Lightning Nodes Number>
32
40
  }
33
41
 
34
42
  @returns via cbk or Promise
35
43
  {
44
+ channels: [{
45
+ from: <Opening Node Index Number>
46
+ id: <Standard Format Channel Id String>
47
+ to: <Partner Node Index Number>
48
+ transaction_id: <Funding Transaction Id Hex String>
49
+ transaction_vout: <Funding Transaction Output Index Number>
50
+ }]
51
+ kill: <Kill All Nodes Function> ({}) => {}
36
52
  nodes: [{
37
53
  generate: <Make Block Function> ({address, count}, [cbk]) => {}
38
54
  id: <Node Public Key Hex String>
@@ -47,8 +63,21 @@ const times = 3000;
47
63
  module.exports = (args, cbk) => {
48
64
  return new Promise((resolve, reject) => {
49
65
  return asyncAuto({
66
+ // Check arguments
67
+ validate: cbk => {
68
+ if (args.capacity === undefined) {
69
+ return cbk();
70
+ }
71
+
72
+ if (!isInteger(args.capacity) || args.capacity <= 0) {
73
+ return cbk([400, 'ExpectedPositiveClusterChannelCapacity']);
74
+ }
75
+
76
+ return cbk();
77
+ },
78
+
50
79
  // Spawn nodes
51
- spawn: async () => {
80
+ spawn: ['validate', async () => {
52
81
  return await asyncRetry({interval, times: 25}, async () => {
53
82
  const options = {startPort: between(startPort, endPort)};
54
83
 
@@ -140,7 +169,7 @@ module.exports = (args, cbk) => {
140
169
  };
141
170
  });
142
171
  });
143
- },
172
+ }],
144
173
 
145
174
  // Connect nodes in the cluster to each other
146
175
  connect: ['spawn', async ({spawn}) => {
@@ -151,9 +180,42 @@ module.exports = (args, cbk) => {
151
180
  });
152
181
  }],
153
182
 
183
+ // Open channels between neighboring nodes, one at a time
184
+ channels: ['connect', 'spawn', async ({spawn}) => {
185
+ // Exit early when there is no channel capacity to create channels
186
+ if (!args.capacity) {
187
+ return [];
188
+ }
189
+
190
+ try {
191
+ return await asyncMapSeries(line(spawn), async ([from, to]) => {
192
+ const opened = await setupChannel({
193
+ capacity: args.capacity,
194
+ generate: spawn[from].generate,
195
+ lnd: spawn[from].lnd,
196
+ to: spawn[to],
197
+ });
198
+
199
+ return {
200
+ from,
201
+ to,
202
+ id: opened.id,
203
+ transaction_id: opened.transaction_id,
204
+ transaction_vout: opened.transaction_vout,
205
+ };
206
+ });
207
+ } catch (err) {
208
+ // Clean up the spawned nodes when channel setup fails
209
+ await asyncEach(spawn, async ({kill}) => await kill({}));
210
+
211
+ throw err;
212
+ }
213
+ }],
214
+
154
215
  // Final set of nodes
155
- nodes: ['connect', 'spawn', async ({spawn}) => {
216
+ nodes: ['channels', 'connect', 'spawn', async ({channels, spawn}) => {
156
217
  return {
218
+ channels,
157
219
  kill: ({}) => asyncEach(spawn, async ({kill}) => await kill({})),
158
220
  nodes: spawn.map(node => ({
159
221
  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.2-beta"
2
+ "dockerLndImage": "lightninglabs/lnd:v0.21.4-beta"
3
3
  }
package/package.json CHANGED
@@ -1,29 +1,36 @@
1
1
  {
2
2
  "allowScripts": {
3
3
  "cpu-features@0.0.10": true,
4
- "protobufjs@7.6.5": true,
5
- "ssh2@1.17.0": true
4
+ "ssh2@1.17.0": true,
5
+ "protobufjs@7.6.6": true
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.8",
21
- "tar-stream": "3.2.0"
19
+ "lightning": "13.1.2"
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.4"
48
+ "version": "8.1.0"
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,210 +0,0 @@
1
- # Versions
2
-
3
- ## 8.0.4
4
-
5
- - Change default LND version to v0.21.2-beta
6
-
7
- ## 8.0.3
8
-
9
- - Change default LND version to v0.21.1-beta
10
-
11
- ## 8.0.2
12
-
13
- - Change default LND version to v0.21.0-beta
14
-
15
- ## 8.0.0
16
-
17
- - Require node.js v22+
18
-
19
- ## 7.0.2
20
-
21
- - Change default LND version to v0.20.1-beta
22
-
23
- ### Breaking Changes
24
-
25
- - Require node.js v20+
26
-
27
- ## 6.0.28
28
-
29
- - Change default LND version to v0.20.0-beta
30
-
31
- ## 6.0.27
32
-
33
- - Change default LND version to v0.19.3-beta
34
-
35
- ## 6.0.26
36
-
37
- - Change default LND version to v0.18.5-beta
38
-
39
- ## 6.0.23
40
-
41
- - Change default LND version to v0.18.2-beta
42
-
43
- ## 6.0.20
44
-
45
- - Change default LND version to v0.18.1-beta
46
-
47
- ## 6.0.19
48
-
49
- - Change default LND version to v0.18.0-beta
50
-
51
- ## 6.0.16
52
-
53
- - Change default LND version to v0.17.5-beta
54
-
55
- ## 6.0.13
56
-
57
- - Change default LND version to v0.17.4-beta
58
-
59
- ## 6.0.9
60
-
61
- - Change default LND version to v0.17.1-beta
62
-
63
- ## 6.0.3
64
-
65
- - Change default Bitcoin Core version to 23.0
66
-
67
- ## 6.0.1
68
-
69
- - Change default LND version to v0.17.0-beta
70
-
71
- ### Breaking Changes
72
-
73
- - node.js 16 is no longer supported, minimum node.js version is 18
74
-
75
- ## 5.1.3
76
-
77
- - Change default LND version to v0.16.4-beta
78
-
79
- ## 5.1.0
80
-
81
- - `setupChannel`: add `is_private` to setup a private channel
82
-
83
- ## 5.0.7
84
-
85
- - Change default LND version to v0.16.3-beta
86
-
87
- ## 5.0.4
88
-
89
- - Change default LND version to v0.16.2-beta
90
-
91
- ## 5.0.1
92
-
93
- - Change default LND version to v0.16.1-beta
94
-
95
- ## 5.0.0
96
-
97
- ### Breaking Changes
98
-
99
- - node.js 14 is no longer supported, minimum node.js version is 16
100
-
101
- ## 4.1.4
102
-
103
- - Change default LND version to v0.16.0-beta
104
-
105
- ## 4.1.2
106
-
107
- - `spawnLndDocker`: Add method to spawn an LND docker
108
-
109
- ## 4.0.4
110
-
111
- - `spawnLightningCluster`: Return `tower` for the watchtower server socket
112
- - `spawnLightningDocker`: Add `tower_port` to specify watchtower server port
113
- - `spawnLightningDocker`: Return `tower_socket` for the watchtower socket
114
-
115
- ### Breaking Changes
116
-
117
- - `spawnLightningDocker`: `tower_port` is now a required argument
118
-
119
- ## 3.1.6
120
-
121
- - Change default LND version to v0.15.5-beta
122
-
123
- ## 3.1.5
124
-
125
- - Change default LND version to v0.15.4-beta
126
-
127
- ## 3.1.3
128
-
129
- - Change default LND version to v0.15.3-beta
130
-
131
- ## 3.1.1
132
-
133
- - Change default LND version to v0.15.2-beta
134
-
135
- ## 3.1.0
136
-
137
- - `spawnLightningDocker`: Add argument `seed` to use a predetermined seed
138
-
139
- ## 3.0.1
140
-
141
- - `spawnLightningCluster`: Add `rpc` for custom macaroon rpc connections
142
-
143
- ### Breaking Changes
144
-
145
- - Versions of node.js before 14 are no longer supported
146
-
147
- ## 2.3.6
148
-
149
- - Change default LND version to v0.15.1-beta
150
-
151
- ## 2.3.5
152
-
153
- - Change default LND version to v0.15.0-beta
154
-
155
- ## 2.3.1
156
-
157
- - `setupChannel`: Add method to setup a channel between LND nodes
158
-
159
- ## 2.2.12
160
-
161
- - Change default LND version to v0.14.3-beta
162
-
163
- ## 2.2.8
164
-
165
- - Change default LND version to v0.14.2-beta
166
-
167
- ## 2.2.3
168
-
169
- - Change Bitcoin Core version to 22.0
170
-
171
- ## 2.2.2
172
-
173
- - Change default LND version to v0.14.1-beta
174
-
175
- ## 2.2.1
176
-
177
- - `spawnLightningCluster`, `spawnLightningDocker`: Add `lnd_configuration` to
178
- allow specifying additional LND configuration parameters
179
-
180
- ## 2.1.1
181
-
182
- - `DOCKER_LND_VERSION`: add environment variable to control docker lnd image
183
-
184
- ## 2.0.0
185
-
186
- - `spawnLightningCluster`: add `socket` to reveal node LN p2p sockets
187
- - `spawnLightningDocker`: add `add_chain_peer` method to add a chain peer
188
- - `spawnLightningDocker`: add `chain_socket` method for the chain p2p socket
189
- - `spawnLightningDocker`: add `ln_socket` method for the chain p2p socket
190
-
191
- ### Breaking Changes
192
-
193
- - Node 12+ is required
194
-
195
- ## 1.2.0
196
-
197
- - `spawnLightningCluster` add `generate` to generate coins for a node
198
-
199
- ## 1.1.0
200
-
201
- - Add `spawnLightningCluster` to spawn multiple lightning dockers
202
-
203
- ## 1.0.2
204
-
205
- - Update lnd to 0.13.1 and Bitcoin Core to 0.21.1
206
-
207
- ## 1.0.1
208
-
209
- - `spawnBitcoindDocker`: Add method to spawn a Bitcoind Docker image
210
- - `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
- });