ln-docker-daemons 8.1.0 → 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/bitcoinrpc/check_generated.js +222 -0
- package/bitcoinrpc/generate_to_address.js +19 -12
- package/bitcoinrpc/mine_blocks.js +121 -0
- package/bitcoinrpc/rpc.js +110 -23
- package/bitcoinrpc/unknown_generated_error.js +22 -0
- package/cluster/generate_blocks.js +82 -0
- package/cluster/spawn_lightning_cluster.js +9 -28
- package/lnd/spawn_lightning_docker.js +3 -2
- package/package.json +2 -2
|
@@ -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
|
|
5
|
+
const mineBlocks = require('./mine_blocks');
|
|
5
6
|
|
|
6
|
-
const
|
|
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
|
-
//
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
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: '
|
|
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
|
@@ -2,8 +2,14 @@ const asyncAuto = require('async/auto');
|
|
|
2
2
|
const {returnResult} = require('asyncjs-util');
|
|
3
3
|
|
|
4
4
|
const defaultTimeout = 1000 * 30;
|
|
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;
|
|
5
10
|
let requests = 0;
|
|
6
11
|
const {stringify} = JSON;
|
|
12
|
+
const unsentCodes = ['ECONNREFUSED', 'UND_ERR_CONNECT_TIMEOUT'];
|
|
7
13
|
|
|
8
14
|
/** Call JSON RPC
|
|
9
15
|
|
|
@@ -13,13 +19,18 @@ const {stringify} = JSON;
|
|
|
13
19
|
params: [<Parameter Object>]
|
|
14
20
|
pass: <Password String>
|
|
15
21
|
port: <Port Number>
|
|
22
|
+
[timeout]: <Request Timeout Milliseconds Number>
|
|
16
23
|
user: <Username String>
|
|
17
24
|
}
|
|
18
25
|
|
|
26
|
+
ConnectionToBitcoindRpcServiceFailed means the request never reached the
|
|
27
|
+
server. ConnectionToBitcoindRpcServiceLost and TimedOutWaitingForRpcResponse
|
|
28
|
+
mean the server may have executed the command.
|
|
29
|
+
|
|
19
30
|
@returns via cbk or Promise
|
|
20
31
|
<Result Object>
|
|
21
32
|
*/
|
|
22
|
-
module.exports = ({cmd, host, params, pass, port, user}, cbk) => {
|
|
33
|
+
module.exports = ({cmd, host, params, pass, port, timeout, user}, cbk) => {
|
|
23
34
|
return new Promise((resolve, reject) => {
|
|
24
35
|
return asyncAuto({
|
|
25
36
|
// Check arguments
|
|
@@ -40,6 +51,10 @@ module.exports = ({cmd, host, params, pass, port, user}, cbk) => {
|
|
|
40
51
|
return cbk([400, 'ExpectedRpcPasswordToExecuteRpcCall']);
|
|
41
52
|
}
|
|
42
53
|
|
|
54
|
+
if (timeout !== undefined && !(isInteger(timeout) && timeout > 0)) {
|
|
55
|
+
return cbk([400, 'ExpectedPositiveTimeoutToExecuteRpcCall']);
|
|
56
|
+
}
|
|
57
|
+
|
|
43
58
|
if (!user) {
|
|
44
59
|
return cbk([400, 'ExpectedRpcUsernameToExecuteRpcCall']);
|
|
45
60
|
}
|
|
@@ -47,39 +62,111 @@ module.exports = ({cmd, host, params, pass, port, user}, cbk) => {
|
|
|
47
62
|
return cbk();
|
|
48
63
|
},
|
|
49
64
|
|
|
50
|
-
// Send request to the server
|
|
65
|
+
// Send request to the server, failures are returned rather than thrown
|
|
51
66
|
request: ['validate', async ({}) => {
|
|
52
67
|
const credentials = Buffer.from(`${user}:${pass}`);
|
|
53
68
|
|
|
54
69
|
try {
|
|
55
|
-
const response = await fetch(
|
|
56
|
-
|
|
57
|
-
{
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
method: cmd,
|
|
61
|
-
params: params,
|
|
62
|
-
}),
|
|
63
|
-
headers: {
|
|
64
|
-
'Authorization': `Basic ${credentials.toString('base64')}`,
|
|
65
|
-
'Content-Type': 'application/json',
|
|
66
|
-
},
|
|
67
|
-
method: 'POST',
|
|
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',
|
|
68
75
|
},
|
|
69
|
-
|
|
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;
|
|
70
89
|
|
|
71
|
-
|
|
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}]);
|
|
97
|
+
}
|
|
98
|
+
|
|
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
|
+
}
|
|
72
118
|
|
|
73
|
-
|
|
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]);
|
|
125
|
+
}],
|
|
126
|
+
|
|
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;
|
|
131
|
+
|
|
132
|
+
try {
|
|
133
|
+
body = parse(request.body);
|
|
74
134
|
} catch (err) {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
}
|
|
135
|
+
return cbk([503, 'FailedToParseRpcServiceResponse', details]);
|
|
136
|
+
}
|
|
78
137
|
|
|
79
|
-
|
|
138
|
+
if (!body || typeof body !== 'object') {
|
|
139
|
+
return cbk([503, 'UnexpectedRpcServiceResponse', details]);
|
|
80
140
|
}
|
|
141
|
+
|
|
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
|
+
}
|
|
157
|
+
|
|
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
|
+
]);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
return cbk(null, parseBody.result);
|
|
81
168
|
}],
|
|
82
169
|
},
|
|
83
|
-
returnResult({reject, resolve, of: '
|
|
170
|
+
returnResult({reject, resolve, of: 'result'}, cbk));
|
|
84
171
|
});
|
|
85
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
|
+
};
|
|
@@ -4,12 +4,11 @@ const asyncMap = require('async/map');
|
|
|
4
4
|
const asyncMapSeries = require('async/mapSeries');
|
|
5
5
|
const asyncRetry = require('async/retry');
|
|
6
6
|
const {authenticatedLndGrpc} = require('lightning');
|
|
7
|
-
const {createChainAddress} = require('lightning');
|
|
8
7
|
const {findFreePorts} = require('find-free-ports');
|
|
9
|
-
const {getUtxos} = require('lightning');
|
|
10
8
|
const {getIdentity} = require('lightning');
|
|
11
9
|
const {returnResult} = require('asyncjs-util');
|
|
12
10
|
|
|
11
|
+
const generateBlocks = require('./generate_blocks');
|
|
13
12
|
const {setupChannel} = require('./../setup');
|
|
14
13
|
const {spawnLightningDocker} = require('./../lnd');
|
|
15
14
|
|
|
@@ -21,12 +20,9 @@ const generateAddress = '2N8hwP1WmJrFF5QWABn38y63uYLhnJYJYTF';
|
|
|
21
20
|
const interval = 10;
|
|
22
21
|
const {isInteger} = Number;
|
|
23
22
|
const line = arr => arr.slice(1).map((_, i) => [i, i + 1]);
|
|
24
|
-
const makeAddress = ({lnd}) => createChainAddress({lnd});
|
|
25
|
-
const maturity = 100;
|
|
26
23
|
const pairs = n => n.map((x, i) => n.slice(i + 1).map(y => [x, y])).flat();
|
|
27
24
|
const portsPerLnd = 7;
|
|
28
25
|
const startPort = 1025;
|
|
29
|
-
const times = 3000;
|
|
30
26
|
|
|
31
27
|
/** Spawn a cluster of nodes
|
|
32
28
|
|
|
@@ -129,29 +125,14 @@ module.exports = (args, cbk) => {
|
|
|
129
125
|
getBlockInfo: lightningDocker.get_block_info,
|
|
130
126
|
socket: lightningDocker.chain_socket,
|
|
131
127
|
},
|
|
132
|
-
generate: ({address, count}) => {
|
|
133
|
-
return
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
return resolve();
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
await asyncRetry({interval, times}, async () => {
|
|
144
|
-
const [utxo] = (await getUtxos({lnd})).utxos;
|
|
145
|
-
|
|
146
|
-
if (!utxo) {
|
|
147
|
-
throw new Error('ExpectedUtxoInUtxos');
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
return utxo;
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
return resolve();
|
|
154
|
-
});
|
|
128
|
+
generate: ({address, count}, cbk) => {
|
|
129
|
+
return generateBlocks({
|
|
130
|
+
address,
|
|
131
|
+
count,
|
|
132
|
+
lnd,
|
|
133
|
+
generate: lightningDocker.generate,
|
|
134
|
+
},
|
|
135
|
+
cbk);
|
|
155
136
|
},
|
|
156
137
|
kill: lightningDocker.kill,
|
|
157
138
|
public_key: id,
|
|
@@ -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
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"asyncjs-util": "1.2.12",
|
|
17
17
|
"dockerode": "5.0.1",
|
|
18
18
|
"find-free-ports": "3.1.1",
|
|
19
|
-
"lightning": "13.1.
|
|
19
|
+
"lightning": "13.1.4"
|
|
20
20
|
},
|
|
21
21
|
"description": "Spawn and control LN Docker daemons",
|
|
22
22
|
"engines": {
|
|
@@ -45,5 +45,5 @@
|
|
|
45
45
|
"scripts": {
|
|
46
46
|
"test": "node --experimental-test-coverage --test"
|
|
47
47
|
},
|
|
48
|
-
"version": "8.1.
|
|
48
|
+
"version": "8.1.1"
|
|
49
49
|
}
|