balanceofsatoshis 11.40.0 → 11.41.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/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Versions
2
2
 
3
+ ## 11.41.0
4
+
5
+ - `remove-peer`: Add interactive mode to select channels to close
6
+
3
7
  ## 11.40.0
4
8
 
5
9
  - `peers`: Add `DISK_USAGE_MB` filter to `--filter` formulas for est disk usage
package/bos CHANGED
@@ -1396,6 +1396,7 @@ prog
1396
1396
  .option('--outpoint', 'Only remove specific channel with funding txid:vout')
1397
1397
  .option('--private', 'Peer is privately connected')
1398
1398
  .option('--public', 'Peer is publicly connected')
1399
+ .option('--select-channels', 'Select channels to remove interactively')
1399
1400
  .action((args, options, logger) => {
1400
1401
  return new Promise(async (resolve, reject) => {
1401
1402
  try {
@@ -1405,6 +1406,7 @@ prog
1405
1406
  lnd,
1406
1407
  logger,
1407
1408
  address: options.address,
1409
+ ask: (n, cbk) => inquirer.prompt([n]).then(n => cbk(n)),
1408
1410
  chain_fee_rate: options.feeRate || undefined,
1409
1411
  fs: {getFile: readFile},
1410
1412
  idle_days: options.idleDays,
@@ -1415,6 +1417,7 @@ prog
1415
1417
  is_offline: !!options.offline,
1416
1418
  is_private: !!options.private,
1417
1419
  is_public: !!options.public,
1420
+ is_selecting_channels: options.selectChannels || undefined,
1418
1421
  omit: flatten([options.omit].filter(n => !!n)),
1419
1422
  outbound_liquidity_below: options.outboundBelow,
1420
1423
  outpoints: flatten([options.outpoint].filter(n => !!n)),
@@ -236,6 +236,7 @@ module.exports = ({lnd, query}, cbk) => {
236
236
  .map(chan => {
237
237
  const [height] = chan.id.split('x');
238
238
  const local = formatTokens({tokens: chan.local_balance});
239
+ const pending = chan.pending_payments;
239
240
  const remote = formatTokens({tokens: chan.remote_balance});
240
241
 
241
242
  const inbound = `in: ${balance(remote)}`;
@@ -248,6 +249,7 @@ module.exports = ({lnd, query}, cbk) => {
248
249
  capacity: formatTokens({tokens: chan.capacity}).display,
249
250
  funding: `${chan.transaction_id} ${chan.transaction_vout}`,
250
251
  peer_created: chan.is_partner_initiated || undefined,
252
+ pending_payments: !!pending.length ? pending : undefined,
251
253
  };
252
254
  }),
253
255
  }
@@ -1,6 +1,7 @@
1
1
  const asyncAuto = require('async/auto');
2
2
  const asyncEachSeries = require('async/eachSeries');
3
3
  const {closeChannel} = require('ln-service');
4
+ const {decodeChanId} = require('bolt07');
4
5
  const {getChainFeeRate} = require('ln-service');
5
6
  const {getChannels} = require('ln-service');
6
7
  const {getNetwork} = require('ln-sync');
@@ -11,20 +12,25 @@ const getPeers = require('./get_peers');
11
12
 
12
13
  const arrayWithEntries = arr => !!arr.length ? arr : undefined;
13
14
  const asOutpoint = n => `${n.transaction_id}:${n.transaction_vout}`;
15
+ const estimateDisk = n => Math.round(n * 500 / 1e6 * 10) / 10;
14
16
  const fastConf = 6;
15
17
  const {floor} = Math;
16
18
  const defaultDays = 365 * 2;
17
19
  const getMempoolRetries = 10;
20
+ const iconDisabled = channel => !channel.is_active ? '💀 ' : '';
21
+ const iconPending = channel => channel.pending_payments.length ? '💸 ' : ''
18
22
  const {isArray} = Array;
19
23
  const isPublicKey = n => !!n && /^0[2-3][0-9A-F]{64}$/i.test(n);
20
24
  const maxMempoolSize = 2e6;
21
25
  const regularConf = 72;
22
26
  const slowConf = 144;
27
+ const tokensAsBigUnit = tokens => (tokens / 1e8).toFixed(8);
23
28
 
24
29
  /** Close out channels with a peer and disconnect them
25
30
 
26
31
  {
27
32
  [address]: <Close Out Funds to On-Chain Address String>
33
+ ask: <Ask Function>
28
34
  [chain_fee_rate]: <Chain Fee Per VByte Number>
29
35
  fs: {
30
36
  getFile: <Read File Contents Function> (path, cbk) => {}
@@ -37,6 +43,7 @@ const slowConf = 144;
37
43
  [is_offline]: <Peer Is Disconnected Bool>
38
44
  [is_private]: <Peer is Privately Connected Bool>
39
45
  [is_public]: <Peer is Publicly Connected Bool>
46
+ [is_selecting_channels]: <Interactively Select Channels to Remove Bool>
40
47
  lnd: <Authenticated LND API Object>
41
48
  logger: <Winston Logger Object>
42
49
  [omit]: [<Avoid Peer With Public Key String>]
@@ -53,10 +60,18 @@ module.exports = (args, cbk) => {
53
60
  return asyncAuto({
54
61
  // Check arguments
55
62
  validate: cbk => {
63
+ if (!args.ask) {
64
+ return cbk([400, 'ExpectedAskFunctionToRemovePeer']);
65
+ }
66
+
56
67
  if (!args.fs) {
57
68
  return cbk([400, 'ExpectedFsMethodsToRemovePeer']);
58
69
  }
59
70
 
71
+ if (!!args.is_selecting_channels && !args.public_key) {
72
+ return cbk([400, 'ExpectedPeerToRemoveWhenSelectingChannels']);
73
+ }
74
+
60
75
  if (!args.lnd) {
61
76
  return cbk([400, 'LndIsRequiredToRemovePeer']);
62
77
  }
@@ -69,6 +84,10 @@ module.exports = (args, cbk) => {
69
84
  return cbk([400, 'ExpectedSpecificOutpointsToRemoveFromPeer']);
70
85
  }
71
86
 
87
+ if (!!args.outpoints.length && !args.public_key) {
88
+ return cbk([400, 'ExpectedPeerToRemoteWhenOutpointsSpecified']);
89
+ }
90
+
72
91
  if (!!args.public_key && !isPublicKey(args.public_key)) {
73
92
  return cbk([400, 'ExpectedPublicKeyOfPeerToRemove']);
74
93
  }
@@ -137,8 +156,94 @@ module.exports = (args, cbk) => {
137
156
  cbk);
138
157
  }],
139
158
 
159
+ // Determine outpoints to use
160
+ outpoints: ['getChannels', ({getChannels}, cbk) => {
161
+ // Exit early when a peer is not specified
162
+ if (!args.public_key) {
163
+ return cbk(null, []);
164
+ }
165
+
166
+ const channelsWithPeer = getChannels.channels
167
+ .filter(channel => {
168
+ // Ignore channels that are not the specified public key
169
+ if (channel.partner_public_key !== args.public_key) {
170
+ return false;
171
+ }
172
+
173
+ //Return channels with the peer
174
+ return true;
175
+ })
176
+ .sort((a, b) => {
177
+ const heightA = decodeChanId({channel: a.id}).block_height;
178
+ const heightB = decodeChanId({channel: b.id}).block_height;
179
+
180
+ // Sort channels by oldest to newest
181
+ return heightA - heightB;
182
+ });
183
+
184
+ // Exit early when no channels are available
185
+ if (!channelsWithPeer.length) {
186
+ return cbk([404, 'NoChannelsToCloseWithSpecifiedPeer']);
187
+ }
188
+
189
+ // Collect any outpoints that are unable to be cooperatively closed
190
+ const blocked = channelsWithPeer
191
+ .filter(channel => {
192
+ // Channels that are inactive or have HTLCs cannot be coop-closed
193
+ return !channel.is_active || !!channel.pending_payments.length;
194
+ })
195
+ .map(channel => asOutpoint(channel));
196
+
197
+ // Find a directly referenced outpoint that is in the blocked list
198
+ const blockedOutpoint = args.outpoints.find(n => blocked.includes(n));
199
+
200
+ // Make sure we aren't trying to coop close a channel that can't be
201
+ if (!args.is_forced && !!blockedOutpoint) {
202
+ return cbk([400, 'CannotCoopClose', {outpoint: blockedOutpoint}]);
203
+ }
204
+
205
+ // Exit early if not selecting a channel
206
+ if (!args.is_selecting_channels) {
207
+ return cbk(null, args.outpoints);
208
+ }
209
+
210
+ // Interactively select outpoints to close
211
+ return args.ask({
212
+ choices: channelsWithPeer.map(channel => {
213
+ // In closing, channels are identified by their funding outpoint
214
+ const value = asOutpoint(channel);
215
+
216
+ // Channels that are inactive or have HTLCs cannot be coop-closed
217
+ const isBlocked = blocked.includes(value);
218
+
219
+ const disk = `Est disk mb: ${estimateDisk(channel.past_states)}`;
220
+ const icon = iconDisabled(channel) || iconPending(channel);
221
+ const {id} = channel;
222
+ const inbound = `in: ${tokensAsBigUnit(channel.remote_balance)}`;
223
+ const outbound = `out: ${tokensAsBigUnit(channel.local_balance)}`;
224
+
225
+ return {
226
+ value,
227
+ checked: args.outpoints.includes(value),
228
+ disabled: !args.is_forced ? isBlocked : false,
229
+ name: `${icon}${id}: ${inbound} | ${outbound}. ${disk}.`,
230
+ };
231
+ }),
232
+ loop: false,
233
+ message: `Channels to ${!!args.is_forced ? 'force ' : ''}close?`,
234
+ name: 'outpoints',
235
+ type: 'checkbox',
236
+ validate: input => !!input.length,
237
+ },
238
+ ({outpoints}) => cbk(null, outpoints));
239
+ }],
240
+
140
241
  // Check channels for peer to make sure that they can be cleanly closed
141
- checkChannels: ['getChannels', ({getChannels}, cbk) => {
242
+ checkChannels: [
243
+ 'getChannels',
244
+ 'outpoints',
245
+ ({getChannels, outpoints}, cbk) =>
246
+ {
142
247
  // Exit early when a peer is not specified or force closing is OK
143
248
  if (!args.public_key || !!args.is_forced) {
144
249
  return cbk();
@@ -151,12 +256,12 @@ module.exports = (args, cbk) => {
151
256
  }
152
257
 
153
258
  // Exit early when there are no outpoints, consider all peer channels
154
- if (!args.outpoints.length) {
259
+ if (!outpoints.length) {
155
260
  return true;
156
261
  }
157
262
 
158
263
  // Only include selected channels
159
- return args.outpoints.includes(asOutpoint(channel));
264
+ return outpoints.includes(asOutpoint(channel));
160
265
  });
161
266
 
162
267
  const costToClose = selectedChannels
@@ -279,8 +384,9 @@ module.exports = (args, cbk) => {
279
384
  'checkChannels',
280
385
  'getChannels',
281
386
  'getNormalFee',
387
+ 'outpoints',
282
388
  'selectPeer',
283
- ({getChannels, getNormalFee, selectPeer}, cbk) =>
389
+ ({getChannels, getNormalFee, outpoints, selectPeer}, cbk) =>
284
390
  {
285
391
  // Exit early when there is no peer to close out with
286
392
  if (!selectPeer) {
@@ -292,11 +398,12 @@ module.exports = (args, cbk) => {
292
398
  const toClose = getChannels.channels
293
399
  .filter(chan => chan.partner_public_key === selectPeer.public_key)
294
400
  .filter(chan => {
295
- if (!args.outpoints.length) {
401
+ // When no outpoints are specified, all channels should be closed
402
+ if (!outpoints.length) {
296
403
  return true;
297
404
  }
298
405
 
299
- return args.outpoints.includes(asOutpoint(chan));
406
+ return !!outpoints.includes(asOutpoint(chan));
300
407
  });
301
408
 
302
409
  // Exit early when there are no channels to close
package/package.json CHANGED
@@ -81,5 +81,5 @@
81
81
  "postpublish": "docker buildx build --platform linux/amd64,linux/arm64,linux/arm/v7 -t alexbosworth/balanceofsatoshis --push .",
82
82
  "test": "tap --branches=1 --functions=1 --lines=1 --statements=1 -t 60 test/arrays/*.js test/balances/*.js test/chain/*.js test/display/*.js test/encryption/*.js test/lnd/*.js test/network/*.js test/nodes/*.js test/peers/*.js test/responses/*.js test/routing/*.js test/services/*.js test/swaps/*.js test/tags/*.js test/wallets/*.js"
83
83
  },
84
- "version": "11.40.0"
84
+ "version": "11.41.0"
85
85
  }
@@ -43,7 +43,6 @@ const isPublicKey = n => /^[0-9A-F]{66}$/i.test(n);
43
43
  const legacyMaxRebalanceTokens = 4294967;
44
44
  const {max} = Math;
45
45
  const maxPaymentSize = 4294967;
46
- const maxRebalanceTokens = 16777215;
47
46
  const {min} = Math;
48
47
  const minInboundBalance = 4294967 * 2;
49
48
  const minRebalanceAmount = 5e4;
@@ -632,7 +631,7 @@ module.exports = (args, cbk) => {
632
631
  lnd,
633
632
  cltv_delta: defaultCltvDelta,
634
633
  description: 'Rebalance',
635
- tokens: min(maxRebalanceTokens, findRoute.route_maximum),
634
+ tokens: findRoute.route_maximum,
636
635
  },
637
636
  cbk);
638
637
  }],