ln-docker-daemons 8.1.0 → 8.1.2

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
@@ -90,11 +90,15 @@ await kill({});
90
90
 
91
91
  Spawn a Bitcoin Core Docker image
92
92
 
93
+ Ports that are not specified are not published to the host, except for RPC,
94
+ which is published on a free port picked by Docker. ZMQ defaults to ports
95
+ 28332 and 28333 inside the container.
96
+
93
97
  {
94
- p2p_port: <P2P Port Number>
95
- rpc_port: <RPC Port Number>
96
- zmq_block_port: <ZMQ Blocks Port Number>
97
- zmq_tx_port: <ZMQ Transactions Port Number>
98
+ [p2p_port]: <P2P Port Number>
99
+ [rpc_port]: <RPC Port Number>
100
+ [zmq_block_port]: <ZMQ Blocks Port Number>
101
+ [zmq_tx_port]: <ZMQ Transactions Port Number>
98
102
  }
99
103
 
100
104
  @returns via cbk or Promise
@@ -102,7 +106,10 @@ Spawn a Bitcoin Core Docker image
102
106
  host: <Host String>
103
107
  kill: ({}, [cbk]) => <Kill Promise>
104
108
  rpc_pass: <RPC Password String>
109
+ rpc_port: <RPC Port Number>
105
110
  rpc_user: <RPC Username String>
111
+ zmq_block_port: <ZMQ Blocks Port Number>
112
+ zmq_tx_port: <ZMQ Transactions Port Number>
106
113
  }
107
114
 
108
115
  Example:
@@ -110,13 +117,8 @@ Example:
110
117
  ```node
111
118
  const {spawnBitcoindDocker} = require('ln-docker-daemons');
112
119
 
113
- // Launch a Bitcoind Docker image
114
- const {kill} = spawnBitcoindDocker({
115
- p2p_port: 2345,
116
- rpc_port: 3456,
117
- zmq_block_port: 4567,
118
- zmq_tx_port: 5678,
119
- });
120
+ // Launch a Bitcoind Docker image, Docker picks a free port for RPC
121
+ const {kill, rpc_port} = await spawnBitcoindDocker({});
120
122
 
121
123
  // Stop the image
122
124
  await kill({});
@@ -196,15 +198,19 @@ await kill({});
196
198
 
197
199
  Spawn an LND Docker
198
200
 
201
+ Ports are optional. When RPC or tower ports are not specified, Docker picks
202
+ free host ports for them. Other ports that are not specified are only
203
+ reachable over the Docker network.
204
+
199
205
  {
200
- chain_p2p_port: <Chain Network P2P Listen Port Number>
201
- chain_rpc_port: <Chain Node RPC Port Number>
202
- chain_zmq_block_port: <Chain Node ZMQ Blocks Port Number>
203
- chain_zmq_tx_port: <Chain Node ZMQ Transactions Port Number>
206
+ [chain_p2p_port]: <Chain Network P2P Listen Port Number>
207
+ [chain_rpc_port]: <Chain Node RPC Port Number>
208
+ [chain_zmq_block_port]: <Chain Node ZMQ Blocks Port Number>
209
+ [chain_zmq_tx_port]: <Chain Node ZMQ Transactions Port Number>
204
210
  generate_address: <Generate Blocks to Address String>
205
- lightning_p2p_port: <Lightning Network P2P Listen Port Number>
206
- lightning_rpc_port: <Lightning Node RPC Port Number>
207
- lightning_tower_port: <Lightning Tower Port Number>
211
+ [lightning_p2p_port]: <Lightning Network P2P Listen Port Number>
212
+ [lightning_rpc_port]: <Lightning Node RPC Port Number>
213
+ [lightning_tower_port]: <Lightning Tower Port Number>
208
214
  [lnd_configuration]: [<LND Configuration Argument String>]
209
215
  [seed]: <Mnemonic Seed String>
210
216
  }
@@ -226,6 +232,9 @@ Spawn an LND Docker
226
232
 
227
233
  Spawn a new Docker image running LND
228
234
 
235
+ When RPC or tower ports are not specified, Docker picks free host ports for
236
+ them. A p2p port that is not specified is not published to the host.
237
+
229
238
  {
230
239
  bitcoind_rpc_host: <Bitcoin Core RPC Host String>
231
240
  bitcoind_rpc_pass: <Bitcoin Core RPC Password String>
@@ -234,9 +243,9 @@ Spawn a new Docker image running LND
234
243
  [bitcoind_zmq_block_port]: <Bitcoin Core ZMQ Block Port Number>
235
244
  [bitcoind_zmq_tx_port]: <Bitcoin Core ZMQ Transaction Port Number>
236
245
  [configuration]: [<LND Configuration Argument String>]
237
- p2p_port: <LND Peer to Peer Listen Port Number>
238
- rpc_port: <LND RPC Port Number>
239
- tower_port: <LND Tower Port Number>
246
+ [p2p_port]: <LND Peer to Peer Listen Port Number>
247
+ [rpc_port]: <LND RPC Port Number>
248
+ [tower_port]: <LND Tower Port Number>
240
249
  [seed]: <Mnemonic Seed String>
241
250
  }
242
251
 
@@ -12,18 +12,26 @@ const {rpcUser} = require('./constants');
12
12
  const {spawnDockerImage} = require('./../docker');
13
13
 
14
14
  const clean = string => string.replace(/\+/g, '-').replace(/\//g, '_');
15
+ const defaultZmqBlockPort = 28332;
16
+ const defaultZmqTxPort = 28333;
15
17
  const deriveHmac = (s, p) => createHmac('sha256', s).update(p).digest('hex');
16
18
  const generateAuthSalt = () => randomBytes(16).toString('hex');
17
19
  const generatePassword = () => randomBytes(32).toString('base64');
18
20
  const interval = 100;
19
21
  const times = 100;
20
22
  const trim = string => string.replace(/=+$/g, '');
23
+ const zmqBlockPort = args => args.zmq_block_port || defaultZmqBlockPort;
24
+ const zmqTxPort = args => args.zmq_tx_port || defaultZmqTxPort;
21
25
 
22
26
  /** Spawn a Bitcoin Core Docker image
23
27
 
28
+ Ports that are not specified are not published to the host, except for RPC,
29
+ which is published on a free port picked by Docker. ZMQ defaults to ports
30
+ 28332 and 28333 inside the container.
31
+
24
32
  {
25
- p2p_port: <P2P Port Number>
26
- rpc_port: <RPC Port Number>
33
+ [p2p_port]: <P2P Port Number>
34
+ [rpc_port]: <RPC Port Number>
27
35
  [zmq_block_port]: <ZMQ Blocks Port Number>
28
36
  [zmq_tx_port]: <ZMQ Transactions Port Number>
29
37
  }
@@ -33,27 +41,17 @@ const trim = string => string.replace(/=+$/g, '');
33
41
  host: <Host String>
34
42
  kill: ({}, [cbk]) => <Kill Promise>
35
43
  rpc_pass: <RPC Password String>
44
+ rpc_port: <RPC Port Number>
36
45
  rpc_user: <RPC Username String>
46
+ zmq_block_port: <ZMQ Blocks Port Number>
47
+ zmq_tx_port: <ZMQ Transactions Port Number>
37
48
  }
38
49
  */
39
50
  module.exports = (args, cbk) => {
40
51
  return new Promise((resolve, reject) => {
41
52
  return asyncAuto({
42
- // Check arguments
43
- validate: cbk => {
44
- if (!args.p2p_port) {
45
- return cbk([400, 'ExpectedPeerListenPortToSpawnBitcoindDocker']);
46
- }
47
-
48
- if (!args.rpc_port) {
49
- return cbk([400, 'ExpectedRpcListenPortToSpawnBitcoindDocker']);
50
- }
51
-
52
- return cbk();
53
- },
54
-
55
53
  // Generate auth
56
- generateAuth: ['validate', ({}, cbk) => {
54
+ generateAuth: cbk => {
57
55
  const pass = trim(clean(generatePassword()));
58
56
  const salt = generateAuthSalt();
59
57
 
@@ -61,7 +59,7 @@ module.exports = (args, cbk) => {
61
59
  rpc_auth: `${rpcUser}:${salt}$${deriveHmac(salt, pass)}`,
62
60
  rpc_pass: pass,
63
61
  });
64
- }],
62
+ },
65
63
 
66
64
  // Spawn the docker image
67
65
  spawnDocker: ['generateAuth', ({generateAuth}, cbk) => {
@@ -76,18 +74,25 @@ module.exports = (args, cbk) => {
76
74
  '--rpcbind=0.0.0.0',
77
75
  '--server',
78
76
  '--txindex',
77
+ `--zmqpubrawblock=tcp://*:${zmqBlockPort(args)}`,
78
+ `--zmqpubrawtx=tcp://*:${zmqTxPort(args)}`,
79
79
  ];
80
80
 
81
81
  const image = dockerBitcoindImage;
82
- const ports = {'18443/tcp': args.rpc_port, '18444/tcp': args.p2p_port};
82
+
83
+ // Docker picks a free host port for RPC when no port is specified
84
+ const ports = {'18443/tcp': args.rpc_port};
85
+
86
+ // Other ports are reached over the Docker network unless specified
87
+ if (!!args.p2p_port) {
88
+ ports['18444/tcp'] = args.p2p_port;
89
+ }
83
90
 
84
91
  if (!!args.zmq_block_port) {
85
- arguments.push('--zmqpubrawblock=tcp://*:' + args.zmq_block_port);
86
92
  ports[`${args.zmq_block_port}/tcp`] = args.zmq_block_port;
87
93
  }
88
94
 
89
95
  if (!!args.zmq_tx_port) {
90
- arguments.push('--zmqpubrawtx=tcp://*:' + args.zmq_tx_port);
91
96
  ports[`${args.zmq_tx_port}/tcp`] = args.zmq_tx_port;
92
97
  }
93
98
 
@@ -95,16 +100,28 @@ module.exports = (args, cbk) => {
95
100
  }],
96
101
 
97
102
  // Wait for the image to respond to a query
98
- checkReady: ['generateAuth', 'spawnDocker', ({generateAuth}, cbk) => {
103
+ checkReady: [
104
+ 'generateAuth',
105
+ 'spawnDocker',
106
+ ({generateAuth, spawnDocker}, cbk) =>
107
+ {
99
108
  return asyncRetry({interval, times}, cbk => {
100
109
  return getBlockchainInfo({
101
110
  pass: generateAuth.rpc_pass,
102
- port: args.rpc_port,
111
+ port: spawnDocker.ports['18443/tcp'],
103
112
  user: rpcUser,
104
113
  },
105
114
  cbk);
106
115
  },
107
- cbk);
116
+ err => {
117
+ // Exit early when bitcoind is responding
118
+ if (!err) {
119
+ return cbk();
120
+ }
121
+
122
+ // Stop the container when bitcoind never responds
123
+ return spawnDocker.kill({}, () => cbk(err));
124
+ });
108
125
  }],
109
126
 
110
127
  // Final docker object
@@ -118,7 +135,10 @@ module.exports = (args, cbk) => {
118
135
  host: spawnDocker.host,
119
136
  kill: spawnDocker.kill,
120
137
  rpc_pass: generateAuth.rpc_pass,
138
+ rpc_port: spawnDocker.ports['18443/tcp'],
121
139
  rpc_user: rpcUser,
140
+ zmq_block_port: zmqBlockPort(args),
141
+ zmq_tx_port: zmqTxPort(args),
122
142
  });
123
143
  }],
124
144
  },
@@ -4,11 +4,16 @@ const {returnResult} = require('asyncjs-util');
4
4
  const rpc = require('./rpc');
5
5
 
6
6
  const action = 'add';
7
+ const alreadyAdded = -23;
7
8
  const cmd = 'addnode';
8
9
  const host = 'localhost';
10
+ const isAlreadyAdded = err => isRpcError(err) && err[2].code === alreadyAdded;
11
+ const isRpcError = err => err[1] === 'UnexpectedErrorFromRpcCommand';
9
12
 
10
13
  /** Add a node as a peer
11
14
 
15
+ Adding a node that is already added is not an error
16
+
12
17
  {
13
18
  pass: <RPC Password String>
14
19
  port: <RPC Port Number>
@@ -46,7 +51,14 @@ module.exports = ({pass, port, socket, user}, cbk) => {
46
51
  request: ['validate', ({}, cbk) => {
47
52
  const params = [socket, action];
48
53
 
49
- return rpc({cmd, host, pass, params, port, user}, cbk);
54
+ return rpc({cmd, host, pass, params, port, user}, err => {
55
+ // Exit early when the node was already added
56
+ if (!!err && isAlreadyAdded(err)) {
57
+ return cbk();
58
+ }
59
+
60
+ return cbk(err);
61
+ });
50
62
  }],
51
63
  },
52
64
  returnResult({reject, resolve}, cbk));
@@ -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
+ };