rpc: improve some network rpc calls.

This commit is contained in:
Christopher Jeffrey 2017-05-13 03:51:01 -07:00
parent 4cf82c442f
commit e0c2eb122b
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 100 additions and 36 deletions

View File

@ -221,20 +221,38 @@ RPC.prototype.stop = co(function* stop(args, help) {
*/ */
RPC.prototype.getNetworkInfo = co(function* getNetworkInfo(args, help) { RPC.prototype.getNetworkInfo = co(function* getNetworkInfo(args, help) {
var hosts = this.pool.hosts;
var locals = [];
var i, keys, key, local;
if (help || args.length !== 0) if (help || args.length !== 0)
throw new RPCError(errs.MISC_ERROR, 'getnetworkinfo'); throw new RPCError(errs.MISC_ERROR, 'getnetworkinfo');
keys = hosts.local.keys();
for (i = 0; i < keys.length; i++) {
key = keys[i];
local = hosts.local.get(key);
locals.push({
address: local.addr.host,
port: local.addr.port,
score: local.score
});
}
return { return {
version: pkg.version, version: pkg.version,
subversion: this.pool.options.agent, subversion: this.pool.options.agent,
protocolversion: this.pool.options.version, protocolversion: this.pool.options.version,
localservices: util.hex32(this.pool.options.services), localservices: util.hex32(this.pool.options.services),
localrelay: true, localrelay: !this.pool.options.noRelay,
timeoffset: this.network.time.offset, timeoffset: this.network.time.offset,
networkactive: this.pool.connected,
connections: this.pool.peers.size(), connections: this.pool.peers.size(),
networks: [], networks: [],
relayfee: Amount.btc(this.network.minRelay, true), relayfee: Amount.btc(this.network.minRelay, true),
localaddresses: [], incrementalfee: 0,
localaddresses: locals,
warnings: '' warnings: ''
}; };
}); });
@ -248,20 +266,21 @@ RPC.prototype.addNode = co(function* addNode(args, help) {
if (help || args.length !== 2) if (help || args.length !== 2)
throw new RPCError(errs.MISC_ERROR, 'addnode "node" "add|remove|onetry"'); throw new RPCError(errs.MISC_ERROR, 'addnode "node" "add|remove|onetry"');
addr = NetAddress.fromHostname(node, this.network);
switch (cmd) { switch (cmd) {
case 'add': case 'add':
this.pool.hosts.add(addr); addr = this.pool.hosts.addNode(node);
break; ; // fall through
case 'remove':
this.pool.hosts.remove(addr.hostname);
break;
case 'onetry': case 'onetry':
addr = NetAddress.fromHostname(node, this.network);
if (!this.pool.peers.get(addr.hostname)) { if (!this.pool.peers.get(addr.hostname)) {
peer = this.pool.createOutbound(addr); peer = this.pool.createOutbound(addr);
this.pool.peers.add(peer); this.pool.peers.add(peer);
} }
break;
case 'remove':
this.pool.hosts.removeNode(node);
break; break;
} }
@ -286,28 +305,60 @@ RPC.prototype.disconnectNode = co(function* disconnectNode(args, help) {
}); });
RPC.prototype.getAddedNodeInfo = co(function* getAddedNodeInfo(args, help) { RPC.prototype.getAddedNodeInfo = co(function* getAddedNodeInfo(args, help) {
var hosts = this.pool.hosts;
var valid = new Validator([args]); var valid = new Validator([args]);
var addr = valid.str(1, ''); var addr = valid.str(1, '');
var out = []; var result = [];
var peer; var i, target, node, peer;
if (help || args.length < 1 || args.length > 2) if (help || args.length < 1 || args.length > 2)
throw new RPCError(errs.MISC_ERROR, 'getaddednodeinfo dummy ( "node" )'); throw new RPCError(errs.MISC_ERROR, 'getaddednodeinfo dummy ( "node" )');
if (args.length === 2) { if (args.length === 2)
addr = IP.fromHostname(addr, this.network.port); target = IP.fromHostname(addr, this.network.port);
peer = this.pool.peers.get(addr.hostname);
if (!peer) { for (i = 0; i < hosts.nodes.length; i++) {
throw new RPCError(errs.CLIENT_NODE_NOT_ADDED, node = hosts.nodes[i];
'Node has not been added.');
if (target) {
if (node.host !== target.host)
continue;
if (node.port !== target.port)
continue;
} }
return [toAddedNode(peer)];
peer = this.pool.peers.get(node.hostname);
if (!peer || !peer.connected) {
result.push({
addednode: node.hostname,
connected: false,
addresses: []
});
continue;
}
result.push({
addednode: node.hostname,
connected: peer.connected,
addresses: [
{
address: peer.hostname(),
connected: peer.outbound
? 'outbound'
: 'inbound'
}
]
});
} }
for (peer = this.pool.peers.head(); peer; peer = peer.next) if (target && result.length === 0) {
out.push(toAddedNode(peer)); throw new RPCError(errs.CLIENT_NODE_NOT_ADDED,
'Node has not been added.');
}
return out; return result;
}); });
RPC.prototype.getConnectionCount = co(function* getConnectionCount(args, help) { RPC.prototype.getConnectionCount = co(function* getConnectionCount(args, help) {
@ -2690,21 +2741,6 @@ function swap32(data) {
return data; return data;
} }
function toAddedNode(peer) {
return {
addednode: peer.hostname(),
connected: peer.connected,
addresses: [
{
address: peer.hostname(),
connected: peer.outbound
? 'outbound'
: 'inbound'
}
]
};
}
function toDeployment(id, version, status) { function toDeployment(id, version, status) {
return { return {
id: id, id: id,

View File

@ -868,6 +868,7 @@ HostList.prototype.addSeed = function addSeed(host) {
/** /**
* Add a priority node. * Add a priority node.
* @param {String} host * @param {String} host
* @returns {NetAddress}
*/ */
HostList.prototype.addNode = function addNode(host) { HostList.prototype.addNode = function addNode(host) {
@ -887,6 +888,33 @@ HostList.prototype.addNode = function addNode(host) {
return addr; return addr;
}; };
/**
* Remove a priority node.
* @param {String} host
* @returns {Boolean}
*/
HostList.prototype.removeNode = function removeNode(host) {
var addr = IP.fromHostname(host, this.network.port);
var i, node;
for (i = 0; i < this.nodes.length; i++) {
node = this.nodes[i];
if (node.host !== addr.host)
continue;
if (node.port !== addr.port)
continue;
this.nodes.splice(i, 1);
return true;
}
return false;
};
/** /**
* Set initial seeds. * Set initial seeds.
* @param {String[]} seeds * @param {String[]} seeds