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.
@@ -14,12 +14,12 @@ const {isArray} = Array;
14
14
 
15
15
  @returns via cbk or Promise
16
16
  */
17
- module.exports = ({dockers}, cbk) => {
17
+ module.exports = (args, cbk) => {
18
18
  return new Promise((resolve, reject) => {
19
19
  return asyncAuto({
20
20
  // Check arguments
21
21
  validate: cbk => {
22
- if (!isArray(dockers)) {
22
+ if (!isArray(args.dockers)) {
23
23
  return cbk([400, 'ExpectedArrayOfDockersToKill']);
24
24
  }
25
25
 
@@ -28,7 +28,10 @@ module.exports = ({dockers}, cbk) => {
28
28
 
29
29
  // Kill the dockers
30
30
  kill: ['validate', ({}, cbk) => {
31
- return asyncEach(dockers, (docker, cbk) => docker.kill({}, cbk), cbk);
31
+ return asyncEach(args.dockers, (docker, cbk) => {
32
+ return docker.kill({}, cbk);
33
+ },
34
+ cbk);
32
35
  }],
33
36
  },
34
37
  returnResult({reject, resolve}, cbk));
@@ -0,0 +1,69 @@
1
+ const blockSize = 512;
2
+ const {byteLength} = Buffer;
3
+ const {ceil} = Math;
4
+ const checksumOffset = 148;
5
+ const {concat} = Buffer;
6
+ const fileMode = '0000644\0';
7
+ const maxPathLength = 100;
8
+ const modeOffset = 100;
9
+ const mtimeOffset = 136;
10
+ const octal = (n, digits) => `${n.toString(8).padStart(digits, '0')}\0`;
11
+ const regularFileType = '0';
12
+ const relativePath = path => path.replace(/^\/+/, '');
13
+ const sizeOffset = 124;
14
+ const typeOffset = 156;
15
+ const ustarMagic = 'ustar\x0000';
16
+ const ustarOffset = 257;
17
+
18
+ /** Make a tar archive of files, to copy them into a container
19
+
20
+ {
21
+ files: [{
22
+ data: <File Contents Buffer Object>
23
+ path: <File Path String>
24
+ }]
25
+ }
26
+
27
+ @throws
28
+ <Error>
29
+
30
+ @returns
31
+ {
32
+ tar: <Tar Archive Buffer Object>
33
+ }
34
+ */
35
+ module.exports = args => {
36
+ const entries = args.files.map(({data, path}) => {
37
+ // Paths are relative to the directory the archive is copied into
38
+ const entryPath = relativePath(path);
39
+
40
+ // Paths are written as UTF-8, so their size is counted in bytes
41
+ if (!entryPath || byteLength(entryPath) > maxPathLength) {
42
+ throw new Error('ExpectedFilePathOfAtMost100BytesForTarArchive');
43
+ }
44
+
45
+ const header = Buffer.alloc(blockSize);
46
+ const padded = Buffer.alloc(ceil(data.length / blockSize) * blockSize);
47
+
48
+ header.write(entryPath, 0, 'utf8');
49
+ header.write(fileMode, modeOffset, 'ascii');
50
+ header.write(octal(data.length, 11), sizeOffset, 'ascii');
51
+ header.write(octal(0, 11), mtimeOffset, 'ascii');
52
+ header.write(regularFileType, typeOffset, 'ascii');
53
+ header.write(ustarMagic, ustarOffset, 'ascii');
54
+
55
+ // The checksum is summed with spaces in place of the checksum
56
+ header.fill(' ', checksumOffset, checksumOffset + 8);
57
+
58
+ const checksum = header.reduce((sum, byte) => sum + byte, 0);
59
+
60
+ header.write(octal(checksum, 6), checksumOffset, 'ascii');
61
+
62
+ data.copy(padded);
63
+
64
+ return concat([header, padded]);
65
+ });
66
+
67
+ // An archive ends with two blocks of zeros
68
+ return {tar: concat(entries.concat(Buffer.alloc(blockSize * 2)))};
69
+ };
@@ -1,13 +1,19 @@
1
1
  const asyncAuto = require('async/auto');
2
+ const asyncify = require('async/asyncify');
2
3
  const asyncRetry = require('async/retry');
3
4
  const Dockerode = require('dockerode');
4
5
  const {returnResult} = require('asyncjs-util');
5
6
 
6
7
  const getFile = require('./get_file');
7
8
  const killDocker = require('./kill_docker');
9
+ const makeTar = require('./make_tar');
8
10
 
11
+ const copyPath = '/';
9
12
  const {isArray} = Array;
13
+ const isFile = n => !!n && Buffer.isBuffer(n.data) && isString(n.path);
14
+ const isString = n => typeof n === 'string';
10
15
  const {keys} = Object;
16
+ const localhostIp = '127.0.0.1';
11
17
  const stopAfterRuntimeSeconds = 60;
12
18
 
13
19
  /** Spawn a new docker image
@@ -17,6 +23,10 @@ const stopAfterRuntimeSeconds = 60;
17
23
  {
18
24
  arguments: [<Command Arguments String>]
19
25
  [expose]: [<Expose Port String>]
26
+ [files]: [{
27
+ data: <File Contents Buffer Object>
28
+ path: <Absolute Path in Container String>
29
+ }]
20
30
  image: <Image Name String>
21
31
  ports: {
22
32
  <Internal Port String>: <External Port Number>
@@ -33,24 +43,32 @@ const stopAfterRuntimeSeconds = 60;
33
43
  }
34
44
  }
35
45
  */
36
- module.exports = ({arguments, expose, image, ports}, cbk) => {
46
+ module.exports = (args, cbk) => {
37
47
  return new Promise((resolve, reject) => {
38
48
  return asyncAuto({
39
49
  // Check arguments
40
50
  validate: cbk => {
41
- if (!isArray(arguments)) {
51
+ if (!isArray(args.arguments)) {
42
52
  return cbk([400, 'ExpectedArgumentsListToSpawnDockerImage']);
43
53
  }
44
54
 
45
- if (!!expose && !isArray(expose)) {
55
+ if (!!args.expose && !isArray(args.expose)) {
46
56
  return cbk([400, 'ExpectedExposedPortsArrayToSpawnDockerImage']);
47
57
  }
48
58
 
49
- if (!image) {
59
+ if (!!args.files && !isArray(args.files)) {
60
+ return cbk([400, 'ExpectedArrayOfFilesToSpawnDockerImage']);
61
+ }
62
+
63
+ if (!!args.files && args.files.some(n => !isFile(n))) {
64
+ return cbk([400, 'ExpectedFileDataAndPathToSpawnDockerImage']);
65
+ }
66
+
67
+ if (!args.image) {
50
68
  return cbk([400, 'ExpectedImageRepoNameToSpawnDockerImage']);
51
69
  }
52
70
 
53
- if (!ports) {
71
+ if (!args.ports) {
54
72
  return cbk([400, 'ExpectedPortBindingsToSpawnDockerImage']);
55
73
  }
56
74
 
@@ -61,20 +79,23 @@ module.exports = ({arguments, expose, image, ports}, cbk) => {
61
79
  docker: ['validate', ({}, cbk) => cbk(null, new Dockerode({}))],
62
80
 
63
81
  // Check if the image is cached
64
- getImage: ['docker', async ({docker}) => {
65
- try {
66
- return await docker.getImage(image).inspect();
67
- } catch (err) {
68
- if (err.statusCode === 404) {
69
- return;
82
+ getImage: ['docker', ({docker}, cbk) => {
83
+ return docker.getImage(args.image).inspect((err, res) => {
84
+ // Exit early when the image is not cached
85
+ if (!!err && err.statusCode === 404) {
86
+ return cbk();
70
87
  }
71
88
 
72
- if (err.code === 'ECONNREFUSED') {
73
- throw [503, 'CannotConnectToDockerDaemon'];
89
+ if (!!err && err.code === 'ECONNREFUSED') {
90
+ return cbk([503, 'CannotConnectToDockerDaemon']);
74
91
  }
75
92
 
76
- throw [503, 'UnexpectedErrorGettingDockerImage', {err}];
77
- }
93
+ if (!!err) {
94
+ return cbk([503, 'UnexpectedErrorGettingDockerImage', {err}]);
95
+ }
96
+
97
+ return cbk(null, res);
98
+ });
78
99
  }],
79
100
 
80
101
  // Pull the docker image
@@ -84,7 +105,7 @@ module.exports = ({arguments, expose, image, ports}, cbk) => {
84
105
  return cbk();
85
106
  }
86
107
 
87
- return docker.pull(image, (err, stream) => {
108
+ return docker.pull(args.image, (err, stream) => {
88
109
  if (!!err) {
89
110
  return cbk([503, 'FailedToPullImageToSpawnDockerImage', {err}]);
90
111
  }
@@ -99,11 +120,11 @@ module.exports = ({arguments, expose, image, ports}, cbk) => {
99
120
  });
100
121
  }],
101
122
 
102
- // Create the container
103
- container: ['docker', 'pull', ({docker}, cbk) => {
123
+ // Create the container after archiving, so a throw leaves no container
124
+ container: ['archive', 'docker', 'pull', ({docker}, cbk) => {
104
125
  return docker.createContainer({
105
- Cmd: arguments,
106
- ExposedPorts: (expose || []).reduce((sum, port) => {
126
+ Cmd: args.arguments,
127
+ ExposedPorts: (args.expose || []).reduce((sum, port) => {
107
128
  sum[port.toString()] = {};
108
129
 
109
130
  return sum;
@@ -111,15 +132,21 @@ module.exports = ({arguments, expose, image, ports}, cbk) => {
111
132
  {}),
112
133
  HostConfig: {
113
134
  AutoRemove: true,
114
- PortBindings: keys(ports).reduce((sum, port) => {
115
- // Docker picks a free host port when none is specified
116
- sum[port] = [{HostPort: !ports[port] ? '' : `${ports[port]}`}];
135
+ PortBindings: keys(args.ports).reduce((sum, port) => {
136
+ const hostPort = args.ports[port];
137
+
138
+ // Ports are published on localhost only, not other interfaces
139
+ sum[port] = [{
140
+ HostIp: localhostIp,
141
+ // Docker picks a free host port when none is specified
142
+ HostPort: !hostPort ? '' : `${hostPort}`,
143
+ }];
117
144
 
118
145
  return sum;
119
146
  },
120
147
  {}),
121
148
  },
122
- Image: image,
149
+ Image: args.image,
123
150
  StopTimeout: stopAfterRuntimeSeconds,
124
151
  },
125
152
  (err, container) => {
@@ -131,8 +158,40 @@ module.exports = ({arguments, expose, image, ports}, cbk) => {
131
158
  });
132
159
  }],
133
160
 
161
+ // Archive the files to copy in, which checks that their paths fit
162
+ archive: ['validate', asyncify(({}) => {
163
+ // Exit early when there are no files to copy
164
+ if (!args.files || !args.files.length) {
165
+ return;
166
+ }
167
+
168
+ return makeTar({files: args.files}).tar;
169
+ })],
170
+
171
+ // Copy files into the container before it starts
172
+ copy: ['archive', 'container', ({archive, container}, cbk) => {
173
+ // Exit early when there are no files to copy
174
+ if (!archive) {
175
+ return cbk();
176
+ }
177
+
178
+ return container.putArchive(archive, {path: copyPath}, err => {
179
+ // Exit early when the files were copied
180
+ if (!err) {
181
+ return cbk();
182
+ }
183
+
184
+ // Remove the container so a failed copy does not leave it behind
185
+ return container.remove({force: true}, () => cbk([
186
+ 503,
187
+ 'UnexpectedErrorCopyingFilesToDockerContainer',
188
+ {err},
189
+ ]));
190
+ });
191
+ }],
192
+
134
193
  // Start the image
135
- start: ['container', 'docker', ({container, docker}, cbk) => {
194
+ start: ['container', 'copy', ({container}, cbk) => {
136
195
  return asyncRetry({}, cbk => {
137
196
  return container.start(err => {
138
197
  if (!!err) {
@@ -179,9 +238,11 @@ module.exports = ({arguments, expose, image, ports}, cbk) => {
179
238
 
180
239
  return cbk(null, {
181
240
  host,
182
- getFile: ({path}, cbk) => getFile({container, path}, cbk),
241
+ getFile: (args, cbk) => {
242
+ return getFile({container, path: args.path}, cbk);
243
+ },
183
244
  kill: ({}, cbk) => killDocker({container}, cbk),
184
- ports: keys(ports).reduce((sum, port) => {
245
+ ports: keys(args.ports).reduce((sum, port) => {
185
246
  const [binding] = published[port] || [];
186
247
 
187
248
  sum[port] = !binding ? undefined : Number(binding.HostPort);
package/index.js CHANGED
@@ -3,6 +3,7 @@ const {spawnBitcoindDocker} = require('./bitcoind');
3
3
  const {spawnLightningCluster} = require('./cluster');
4
4
  const {spawnLightningDocker} = require('./lnd');
5
5
  const {spawnLndDocker} = require('./lnd');
6
+ const {spawnLoopServerDocker} = require('./loop');
6
7
 
7
8
  module.exports = {
8
9
  setupChannel,
@@ -10,4 +11,5 @@ module.exports = {
10
11
  spawnLightningCluster,
11
12
  spawnLightningDocker,
12
13
  spawnLndDocker,
14
+ spawnLoopServerDocker,
13
15
  };
@@ -1,3 +1,3 @@
1
1
  {
2
- "dockerLndImage": "lightninglabs/lnd:v0.21.4-beta"
2
+ "dockerLndImage": "lightninglabs/lnd:v0.21.5-beta"
3
3
  }
@@ -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
+ };