Skip to content

Commit

Permalink
feat(rpc/cli): node aliases (#1362)
Browse files Browse the repository at this point in the history
  • Loading branch information
hatmer authored Mar 31, 2020
1 parent b60f49c commit 73e5665
Show file tree
Hide file tree
Showing 26 changed files with 355 additions and 162 deletions.
11 changes: 6 additions & 5 deletions docs/api.md

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

8 changes: 4 additions & 4 deletions lib/cli/commands/ban.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ import { callback, loadXudClient } from '../command';
import { Arguments } from 'yargs';
import { BanRequest } from '../../proto/xudrpc_pb';

export const command = 'ban <node_key>';
export const command = 'ban <node_identifier>';

export const describe = 'ban a remote node';

export const builder = {
node_key: {
description: 'the node key of the remote node to ban',
node_identifier: {
description: 'the node key or alias of the remote node to ban',
type: 'string',
},
};

export const handler = (argv: Arguments<any>) => {
const request = new BanRequest();
request.setNodePubKey(argv.node_key);
request.setNodeIdentifier(argv.node_identifier);
loadXudClient(argv).ban(request, callback(argv));
};
8 changes: 4 additions & 4 deletions lib/cli/commands/discovernodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ import { callback, loadXudClient } from '../command';
import { Arguments } from 'yargs';
import { DiscoverNodesRequest } from '../../proto/xudrpc_pb';

export const command = 'discovernodes <node_key>';
export const command = 'discovernodes <node_identifier>';

export const describe = 'discover nodes from a specific peer';

export const builder = {
node_key: {
description: 'the node key of the connected peer to discover nodes from',
node_identifier: {
description: 'the node key or alias of the connected peer to discover nodes from',
type: 'string',
},
};

export const handler = (argv: Arguments<any>) => {
const request = new DiscoverNodesRequest();
request.setPeerPubKey(argv.node_key);
request.setNodeIdentifier(argv.node_identifier);
loadXudClient(argv).discoverNodes(request, callback(argv));
};
8 changes: 4 additions & 4 deletions lib/cli/commands/getnodeinfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ const displayNodeInfo = (node: GetNodeInfoResponse.AsObject) => {
console.log(table.toString());
};

export const command = 'getnodeinfo <node_key>';
export const command = 'getnodeinfo <node_identifier>';

export const describe = 'get general information about a peer';

export const builder = {
node_key: {
node_identifier: {
type: 'string',
description: 'the node key of the connected peer to get general information from',
description: 'the node key or alias of the connected peer to get general information from',
},
};

export const handler = (argv: Arguments<any>) => {
const request = new GetNodeInfoRequest();
request.setNodePubKey(argv.node_key);
request.setNodeIdentifier(argv.node_identifier);
loadXudClient(argv).getNodeInfo(request, callback(argv, displayNodeInfo));
};
2 changes: 1 addition & 1 deletion lib/cli/commands/listpeers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const formatPeers = (peers: ListPeersResponse.AsObject) => {
@${peer.address}` ;

const details = [
`Alias:
`Alias: ${peer.alias}
Address ([node_key]@[host]:[port]):
${address}`,
Expand Down
8 changes: 4 additions & 4 deletions lib/cli/commands/openchannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import { callback, loadXudClient } from '../command';
import { OpenChannelRequest } from '../../proto/xudrpc_pb';
import { coinsToSats } from '../utils';

export const command = 'openchannel <node_key> <currency> <amount>';
export const command = 'openchannel <node_identifier> <currency> <amount>';

export const describe = 'open a payment channel with a peer';

export const builder = {
node_key: {
description: 'the node key of the connected peer to open the channel with',
node_identifier: {
description: 'the node key or alias of the connected peer to open the channel with',
type: 'string',
},
currency: {
Expand All @@ -24,7 +24,7 @@ export const builder = {

export const handler = (argv: Arguments<any>) => {
const request = new OpenChannelRequest();
request.setNodePubKey(argv.node_key);
request.setNodeIdentifier(argv.node_identifier);
request.setCurrency(argv.currency.toUpperCase());
request.setAmount(coinsToSats(argv.amount));
loadXudClient(argv).openChannel(request, callback(argv));
Expand Down
8 changes: 4 additions & 4 deletions lib/cli/commands/unban.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { callback, loadXudClient } from '../command';
import { Arguments } from 'yargs';
import { UnbanRequest } from '../../proto/xudrpc_pb';

export const command = 'unban <node_key> [reconnect]';
export const command = 'unban <node_identifier> [reconnect]';

export const describe = 'unban a previously banned remote node';

export const builder = {
node_key: {
description: 'the node key of the remote node to unban',
node_identifier: {
description: 'the node key or alias of the remote node to unban',
type: 'string',
},
reconnect: {
Expand All @@ -20,7 +20,7 @@ export const builder = {

export const handler = (argv: Arguments<any>) => {
const request = new UnbanRequest();
request.setNodePubKey(argv.node_key);
request.setNodeIdentifier(argv.node_identifier);
request.setReconnect(argv.reconnect);
loadXudClient(argv).unban(request, callback(argv));
};
3 changes: 3 additions & 0 deletions lib/constants/wordlist.ts

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions lib/grpc/GrpcService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,7 @@ class GrpcService {
grpcPeer.setAddress(peer.address);
grpcPeer.setInbound(peer.inbound);
grpcPeer.setNodePubKey(peer.nodePubKey || '');
grpcPeer.setAlias(peer.alias || '');
if (peer.lndPubKeys) {
const map = grpcPeer.getLndPubKeysMap();
for (const key in peer.lndPubKeys) {
Expand Down
1 change: 1 addition & 0 deletions lib/grpc/getGrpcError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const getGrpcError = (err: any) => {
break;
case orderErrorCodes.PAIR_DOES_NOT_EXIST:
case p2pErrorCodes.NODE_UNKNOWN:
case p2pErrorCodes.UNKNOWN_ALIAS:
case orderErrorCodes.LOCAL_ID_DOES_NOT_EXIST:
case orderErrorCodes.ORDER_NOT_FOUND:
code = status.NOT_FOUND;
Expand Down
13 changes: 7 additions & 6 deletions lib/orderbook/OrderBook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Pool from '../p2p/Pool';
import Peer from '../p2p/Peer';
import Logger from '../Logger';
import { derivePairId, ms, setTimeoutPromise } from '../utils/utils';
import { getAlias } from '../utils/aliasUtils';
import { Models } from '../db/DB';
import Swaps from '../swaps/Swaps';
import { limits, maxLimits } from '../constants/limits';
Expand Down Expand Up @@ -483,18 +484,19 @@ class OrderBook extends EventEmitter {
} else {
// this is a match with a peer order which cannot be considered executed until after a
// successful swap, which is an asynchronous process that can fail for numerous reasons
this.logger.debug(`matched with peer ${maker.peerPubKey}, executing swap on taker ${taker.id} and maker ${maker.id} for ${maker.quantity}`);
const alias = getAlias(maker.peerPubKey);
this.logger.debug(`matched with peer ${maker.peerPubKey} (${alias}), executing swap on taker ${taker.id} and maker ${maker.id} for ${maker.quantity}`);
try {
const swapResult = await this.executeSwap(maker, taker);
if (swapResult.quantity < maker.quantity) {
// swap was only partially completed
portion.quantity = swapResult.quantity;
const rejectedQuantity = maker.quantity - swapResult.quantity;
this.logger.info(`match partially executed on taker ${taker.id} and maker ${maker.id} for ${swapResult.quantity} ` +
`with peer ${maker.peerPubKey}, ${rejectedQuantity} quantity not accepted and will repeat matching routine`);
`with peer ${maker.peerPubKey} (${alias}), ${rejectedQuantity} quantity not accepted and will repeat matching routine`);
await retryFailedSwap(rejectedQuantity);
} else {
this.logger.info(`match executed on taker ${taker.id} and maker ${maker.id} for ${maker.quantity} with peer ${maker.peerPubKey}`);
this.logger.info(`match executed on taker ${taker.id} and maker ${maker.id} for ${maker.quantity} with peer ${maker.peerPubKey} (${alias})`);
}
swapSuccesses.push(swapResult);
onUpdate && onUpdate({ type: PlaceOrderEventType.SwapSuccess, payload: swapResult });
Expand Down Expand Up @@ -767,8 +769,7 @@ class OrderBook extends EventEmitter {
for (const pairId of this.pairInstances.keys()) {
this.removePeerPair(peerPubKey, pairId);
}

this.logger.debug(`removed all orders for peer ${peerPubKey}`);
this.logger.debug(`removed all orders for peer ${peerPubKey} (${getAlias(peerPubKey)})`);
}

private removePeerPair = (peerPubKey: string, pairId: string) => {
Expand Down Expand Up @@ -911,7 +912,7 @@ class OrderBook extends EventEmitter {
const removeResult = this.removePeerOrder(oi.id, oi.pairId, peerPubKey, oi.quantity);
this.emit('peerOrder.invalidation', removeResult.order);
} catch {
this.logger.error(`failed to remove order (${oi.id}) of peer ${peerPubKey}`);
this.logger.error(`failed to remove order (${oi.id}) of peer ${peerPubKey} (${getAlias(peerPubKey)})`);
// TODO: Penalize peer
}
}
Expand Down
14 changes: 14 additions & 0 deletions lib/p2p/NodeList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { NodeFactory, NodeInstance, ReputationEventInstance } from '../db/types'
import addressUtils from '../utils/addressUtils';
import P2PRepository from './P2PRepository';
import { Address } from './types';
import { getAlias } from '../utils/aliasUtils';

export const reputationEventWeight = {
[ReputationEvent.ManualBan]: Number.NEGATIVE_INFINITY,
Expand Down Expand Up @@ -51,6 +52,19 @@ class NodeList extends EventEmitter {
this.nodes.forEach(callback);
}

/**
* Return list of public keys of all banned nodes with a given alias.
*/
public getBannedPubKeys = (alias: string): string[] => {
const keys: string[] = [];
for (const pubKey of this.bannedNodes.keys()) {
if (getAlias(pubKey).toLowerCase() === alias.toLowerCase()) {
keys.push(pubKey);
}
}
return keys;
}

/**
* Ban a node by nodePubKey.
* @returns true if the node was banned, false otherwise
Expand Down
Loading

0 comments on commit 73e5665

Please sign in to comment.