Skip to content

Commit

Permalink
feat(rpc): add GetNodeInfo call (#570)
Browse files Browse the repository at this point in the history
This creates a new rpc call named `GetNodeInfo` which is designed to
retrieve information about a particular node. Currently the only
information returned is the node's reputation score.

Closes #529.
  • Loading branch information
ImmanuelSegol authored and sangaman committed Oct 26, 2018
1 parent 7286897 commit 1a038a2
Show file tree
Hide file tree
Showing 13 changed files with 589 additions and 22 deletions.
Empty file added bin/{
Empty file.
34 changes: 34 additions & 0 deletions docs/api.md

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

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

export const command = 'getnodeinfo <node_pub_key>';

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

export const builder = {
node_pub_key: {
type: 'string',
description: 'nodePubKey of node to get reputation',
},
};

export const handler = (argv: Arguments) => {
const request = new GetNodeInfoRequest();
request.setNodePubKey(argv.node_pub_key);
loadXudClient(argv).getNodeInfo(request, callback);
};
1 change: 1 addition & 0 deletions lib/grpc/GrpcServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class GrpcServer {
ban: grpcService.ban,
unban: grpcService.unban,
getInfo: grpcService.getInfo,
getNodeInfo: grpcService.getNodeInfo,
getOrders: grpcService.getOrders,
listCurrencies: grpcService.listCurrencies,
listPairs: grpcService.listPairs,
Expand Down
15 changes: 15 additions & 0 deletions lib/grpc/GrpcService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,21 @@ class GrpcService {
}
}

/**
* See [[Service.getNodeInfo]]
*/
public getNodeInfo: grpc.handleUnaryCall<xudrpc.GetNodeInfoRequest, xudrpc.GetNodeInfoResponse> = async (call, callback) => {
try {
const { banned, reputationScore } = await this.service.getNodeInfo(call.request.toObject());
const response = new xudrpc.GetNodeInfoResponse();
response.setBanned(banned);
response.setReputationscore(reputationScore);
callback(null, response);
} catch (err) {
callback(this.getGrpcError(err), null);
}
}

/**
* See [[Service.getOrders]]
*/
Expand Down
25 changes: 25 additions & 0 deletions lib/p2p/Pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import addressUtils from '../utils/addressUtils';
import { getExternalIp, UriParts } from '../utils/utils';
import assert from 'assert';
import { ReputationEvent } from '../types/enums';
import { ReputationEventInstance } from '../types/db';

type PoolConfig = {
/** Whether or not to automatically detect and share current external ip address on startup. */
Expand All @@ -36,6 +37,11 @@ type PoolConfig = {
addresses: string[];
};

type NodeReputationInfo = {
reputationScore: ReputationEvent;
banned: boolean;
};

interface Pool {
on(event: 'packet.order', listener: (order: StampedPeerOrder) => void): this;
on(event: 'packet.getOrders', listener: (peer: Peer, reqId: string, pairIds: string[]) => void): this;
Expand Down Expand Up @@ -281,6 +287,25 @@ class Pool extends EventEmitter {
return false;
}

/**
* Gets a node's reputation score and whether it is banned
* @param nodePubKey The node pub key of the node for which to get reputation information
* @return true if the specified node exists and the event was added, false otherwise
*/
public getNodeReputation = async (nodePubKey: string): Promise<NodeReputationInfo> => {
const node = await this.repository.getNode(nodePubKey);
if (node) {
const { reputationScore, banned } = node;
return {
reputationScore,
banned,
};
} else {
this.logger.warn(`node ${nodePubKey} not found`);
throw errors.NODE_UNKNOWN(nodePubKey);
}
}

/**
* Attempt to add an outbound peer by connecting to a given socket address.
* Throws an error if a connection to a node with the given nodePubKey exists or
Expand Down
78 changes: 58 additions & 20 deletions lib/proto/xudrpc.swagger.json

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

Loading

0 comments on commit 1a038a2

Please sign in to comment.