Skip to content

Commit

Permalink
Merge pull request #1478 from ExchangeUnion/rpc/close-channel
Browse files Browse the repository at this point in the history
  • Loading branch information
sangaman authored Apr 29, 2020
2 parents 6e6a4c8 + f69fa7c commit 7c5c86d
Show file tree
Hide file tree
Showing 13 changed files with 1,048 additions and 319 deletions.
32 changes: 30 additions & 2 deletions docs/api.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions lib/cli/commands/closechannel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Arguments } from 'yargs';
import { CloseChannelRequest } from '../../proto/xudrpc_pb';
import { callback, loadXudClient } from '../command';

export const command = 'closechannel <node_identifier> <currency> [--force]';

export const describe = 'close any payment channels with a peer';

export const builder = {
node_identifier: {
description: 'the node key or alias of the connected peer to close the channel with',
type: 'string',
},
currency: {
description: 'the ticker symbol for the currency',
type: 'string',
},
force: {
type: 'boolean',
description: 'whether to force close if the peer is offline',
},
};

export const handler = async (argv: Arguments<any>) => {
const request = new CloseChannelRequest();
request.setNodeIdentifier(argv.node_identifier);
request.setCurrency(argv.currency.toUpperCase());
if (argv.force) {
request.setForce(argv.force);
}
(await loadXudClient(argv)).closeChannel(request, callback(argv));
};
16 changes: 16 additions & 0 deletions lib/grpc/GrpcService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,22 @@ class GrpcService {
}
}

/**
* See [[Service.closeChannel]]
*/
public closeChannel: grpc.handleUnaryCall<xudrpc.CloseChannelRequest, xudrpc.CloseChannelResponse> = async (call, callback) => {
if (!this.isReady(this.service, callback)) {
return;
}
try {
await this.service.closeChannel(call.request.toObject());
const response = new xudrpc.CloseChannelResponse();
callback(null, response);
} catch (err) {
callback(getGrpcError(err), null);
}
}

/**
* See [[Service.removeOrder]]
*/
Expand Down
64 changes: 40 additions & 24 deletions lib/lndclient/LndClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1027,30 +1027,46 @@ class LndClient extends SwapClient {
/**
* Attempts to close an open channel.
*/
public closeChannel = (fundingTxId: string, outputIndex: number, force: boolean): void => {
if (!this.lightning) {
throw(errors.UNAVAILABLE(this.currency, this.status));
}
const request = new lndrpc.CloseChannelRequest();
const channelPoint = new lndrpc.ChannelPoint();
channelPoint.setFundingTxidStr(fundingTxId);
channelPoint.setOutputIndex(outputIndex);
request.setChannelPoint(channelPoint);
request.setForce(force);
this.lightning.closeChannel(request, this.meta)
// TODO: handle close channel events
.on('data', (message: string) => {
this.logger.info(`closeChannel update: ${message}`);
})
.on('end', () => {
this.logger.info('closeChannel ended');
})
.on('status', (status: string) => {
this.logger.debug(`closeChannel status: ${JSON.stringify(status)}`);
})
.on('error', (error: any) => {
this.logger.error(`closeChannel error: ${error}`);
});
public closeChannel = (fundingTxId: string, outputIndex: number, force: boolean): Promise<void> => {
return new Promise<void>((resolve, reject) => {
if (!this.lightning) {
throw(errors.UNAVAILABLE(this.currency, this.status));
}
const request = new lndrpc.CloseChannelRequest();
const channelPoint = new lndrpc.ChannelPoint();
channelPoint.setFundingTxidStr(fundingTxId);
channelPoint.setOutputIndex(outputIndex);
request.setChannelPoint(channelPoint);
request.setForce(force);

this.lightning.closeChannel(request, this.meta)
.on('data', (message: lndrpc.CloseStatusUpdate) => {
if (message.hasClosePending()) {
const txId = base64ToHex(message.getClosePending()!.getTxid_asB64());
if (txId) {
this.logger.info(`channel closed with tx id ${txId}`);
resolve();
}
}
})
.on('end', () => {
this.logger.debug('closeChannel ended');
// we should receeive a channel close update above before the end event
// if we don't, assume the call has failed and reject
// if we have already resolved the promise this line will do nothing
reject('channel close ended unexpectedly');
})
.on('status', (status: grpc.StatusObject) => {
this.logger.debug(`closeChannel status: ${status.code} ${status.details}`);
if (status.code !== grpc.status.OK) {
reject(status.details);
}
})
.on('error', (err: any) => {
this.logger.error(`closeChannel error: ${err}`);
reject(err);
});
});
}

/** Lnd specific procedure to disconnect from the server. */
Expand Down
20 changes: 20 additions & 0 deletions lib/proto/xudrpc.swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions lib/proto/xudrpc_grpc_pb.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions lib/proto/xudrpc_grpc_pb.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions lib/proto/xudrpc_pb.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 7c5c86d

Please sign in to comment.