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

View File

@ -868,6 +868,7 @@ HostList.prototype.addSeed = function addSeed(host) {
/**
* Add a priority node.
* @param {String} host
* @returns {NetAddress}
*/
HostList.prototype.addNode = function addNode(host) {
@ -887,6 +888,33 @@ HostList.prototype.addNode = function addNode(host) {
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.
* @param {String[]} seeds