refactor: rename network address.

This commit is contained in:
Christopher Jeffrey 2016-12-20 12:45:11 -08:00
parent 3baaeadd14
commit 127a52aaf1
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
7 changed files with 83 additions and 85 deletions

View File

@ -16,7 +16,7 @@ var assert = require('assert');
var constants = require('../protocol/constants');
var ec = require('../crypto/ec');
var Amount = require('../btc/amount');
var NetworkAddress = require('../primitives/netaddress');
var NetAddress = require('../primitives/netaddress');
var Script = require('../script/script');
var Address = require('../primitives/address');
var Block = require('../primitives/block');
@ -395,7 +395,7 @@ RPC.prototype.addnode = co(function* addnode(args) {
node = toString(args[0]);
cmd = toString(args[1]);
addr = NetworkAddress.fromHostname(node, this.network);
addr = NetAddress.fromHostname(node, this.network);
switch (cmd) {
case 'add':
@ -428,7 +428,7 @@ RPC.prototype.disconnectnode = co(function* disconnectnode(args) {
throw new RPCError('disconnectnode "node"');
node = toString(args[0]);
addr = NetworkAddress.fromHostname(node, this.network);
addr = NetAddress.fromHostname(node, this.network);
peer = this.pool.peers.get(addr);
if (peer)
@ -446,7 +446,7 @@ RPC.prototype.getaddednodeinfo = co(function* getaddednodeinfo(args) {
if (args.length === 2) {
host = toString(args[1]);
addr = NetworkAddress.fromHostname(host, this.network);
addr = NetAddress.fromHostname(host, this.network);
peer = this.pool.peers.get(addr);
if (!peer)
throw new RPCError('Node has not been added.');
@ -558,7 +558,7 @@ RPC.prototype.setban = co(function* setban(args) {
}
host = toString(args[0]);
ip = NetworkAddress.fromHostname(host, this.network);
ip = NetAddress.fromHostname(host, this.network);
switch (args[1]) {
case 'add':

View File

@ -14,7 +14,7 @@ var crypto = require('../crypto/crypto');
var ec = require('../crypto/ec');
var Bloom = require('../utils/bloom');
var bip152 = require('./bip152');
var NetworkAddress = require('../primitives/netaddress');
var NetAddress = require('../primitives/netaddress');
var Coin = require('../primitives/coin');
var Headers = require('../primitives/headers');
var InvItem = require('../primitives/invitem');
@ -136,8 +136,8 @@ Packet.prototype.fromRaw = function fromRaw(data) {
* @param {Number} options.version - Protocol version.
* @param {Number} options.services - Service bits.
* @param {Number} options.ts - Timestamp of discovery.
* @param {NetworkAddress} options.local - Our address.
* @param {NetworkAddress} options.remote - Their address.
* @param {NetAddress} options.local - Our address.
* @param {NetAddress} options.remote - Their address.
* @param {BN} options.nonce
* @param {String} options.agent - User agent string.
* @param {Number} options.height - Chain height.
@ -146,8 +146,8 @@ Packet.prototype.fromRaw = function fromRaw(data) {
* @property {Number} version - Protocol version.
* @property {Number} services - Service bits.
* @property {Number} ts - Timestamp of discovery.
* @property {NetworkAddress} local - Our address.
* @property {NetworkAddress} remote - Their address.
* @property {NetAddress} local - Our address.
* @property {NetAddress} remote - Their address.
* @property {BN} nonce
* @property {String} agent - User agent string.
* @property {Number} height - Chain height.
@ -164,8 +164,8 @@ function VersionPacket(options) {
this.version = constants.VERSION;
this.services = constants.LOCAL_SERVICES;
this.ts = util.now();
this.recv = new NetworkAddress();
this.from = new NetworkAddress();
this.recv = new NetAddress();
this.from = new NetAddress();
this.nonce = constants.ZERO_U64;
this.agent = constants.USER_AGENT;
this.height = 0;
@ -1006,8 +1006,8 @@ GetAddrPacket.fromRaw = function fromRaw(data, enc) {
* Represents a `addr` packet.
* @exports AddrPacket
* @constructor
* @param {(NetworkAddress[])?} items
* @property {NetworkAddress[]} items
* @param {(NetAddress[])?} items
* @property {NetAddress[]} items
*/
function AddrPacket(items) {
@ -1077,7 +1077,7 @@ AddrPacket.prototype.fromRaw = function fromRaw(data) {
count = br.readVarint();
for (i = 0; i < count; i++)
this.items.push(NetworkAddress.fromReader(br, true));
this.items.push(NetAddress.fromReader(br, true));
return this;
};

View File

@ -36,7 +36,7 @@ var VerifyResult = errors.VerifyResult;
* @exports Peer
* @constructor
* @param {Pool} pool
* @param {NetworkAddress} addr
* @param {NetAddress} addr
* @param {net.Socket?} socket
* @property {Pool} pool
* @property {net.Socket?} socket

View File

@ -15,7 +15,7 @@ var IP = require('../utils/ip');
var co = require('../utils/co');
var constants = require('../protocol/constants');
var errors = require('../btc/errors');
var NetworkAddress = require('../primitives/netaddress');
var NetAddress = require('../primitives/netaddress');
var Address = require('../primitives/address');
var BIP150 = require('./bip150');
var Bloom = require('../utils/bloom');
@ -125,7 +125,7 @@ function Pool(options) {
this.feeRate = -1;
this.address = new NetworkAddress();
this.address = new NetAddress();
this.peers = new PeerList(this);
this.hosts = new HostList(this);
@ -479,7 +479,7 @@ Pool.prototype.handleInbound = function handleInbound(socket) {
return;
}
addr = NetworkAddress.fromSocket(socket, this.network);
addr = NetAddress.fromSocket(socket, this.network);
if (this.peers.inbound >= this.maxInbound) {
this.logger.debug('Ignoring leech: too many inbound (%s).', addr.hostname);
@ -974,7 +974,7 @@ Pool.prototype.handleVersion = function handleVersion(version, peer) {
/**
* Handle peer addr event.
* @private
* @param {NetworkAddress[]} addrs
* @param {NetAddress[]} addrs
* @param {Peer} peer
*/
@ -1951,7 +1951,7 @@ Pool.prototype.increaseBan = function increaseBan(peer, score) {
/**
* Ban a peer.
* @param {NetworkAddress} addr
* @param {NetAddress} addr
*/
Pool.prototype.ban = function ban(addr) {
@ -1966,7 +1966,7 @@ Pool.prototype.ban = function ban(addr) {
/**
* Unban a peer.
* @param {String|NetworkAddress} addr
* @param {String|NetAddress} addr
*/
Pool.prototype.unban = function unban(addr) {
@ -1975,7 +1975,7 @@ Pool.prototype.unban = function unban(addr) {
/**
* Test whether the host is banned.
* @param {NetworkAddress} addr
* @param {NetAddress} addr
* @returns {Boolean}
*/
@ -2167,7 +2167,7 @@ function HostList(pool) {
/**
* Get head element.
* @returns {NetworkAddress}
* @returns {NetAddress}
*/
HostList.prototype.head = function head() {
@ -2176,7 +2176,7 @@ HostList.prototype.head = function head() {
/**
* Get tail element.
* @returns {NetworkAddress}
* @returns {NetAddress}
*/
HostList.prototype.tail = function tail() {
@ -2211,7 +2211,7 @@ HostList.prototype.clear = function clear() {
/**
* Allocate a new loader host.
* @returns {NetworkAddress}
* @returns {NetAddress}
*/
HostList.prototype.getHost = function getHost() {
@ -2223,7 +2223,7 @@ HostList.prototype.getHost = function getHost() {
/**
* Add host to host list.
* @param {NetworkAddress} addr
* @param {NetAddress} addr
* @returns {Boolean}
*/
@ -2242,7 +2242,7 @@ HostList.prototype.add = function add(addr) {
/**
* Remove host from host list.
* @param {NetworkAddress} addr
* @param {NetAddress} addr
* @returns {Boolean}
*/
@ -2260,7 +2260,7 @@ HostList.prototype.remove = function remove(addr) {
/**
* Mark a peer as banned.
* @param {NetworkAddress} addr
* @param {NetAddress} addr
*/
HostList.prototype.ban = function ban(addr) {
@ -2270,7 +2270,7 @@ HostList.prototype.ban = function ban(addr) {
/**
* Unban host.
* @param {NetworkAddress} addr
* @param {NetAddress} addr
*/
HostList.prototype.unban = function unban(addr) {
@ -2279,7 +2279,7 @@ HostList.prototype.unban = function unban(addr) {
/**
* Test whether the host is banned.
* @param {NetworkAddress} addr
* @param {NetAddress} addr
* @returns {Boolean}
*/
@ -2299,7 +2299,7 @@ HostList.prototype.isBanned = function isBanned(addr) {
/**
* Ignore peer.
* @param {NetworkAddress} addr
* @param {NetAddress} addr
*/
HostList.prototype.ignore = function ignore(addr) {
@ -2369,7 +2369,7 @@ HostList.prototype.populate = co(function* populate(seed) {
var i, addr, hosts, host;
if (seed.version !== -1) {
addr = NetworkAddress.fromHost(seed.host, seed.port, this.network);
addr = NetAddress.fromHost(seed.host, seed.port, this.network);
this.add(addr);
return;
}
@ -2385,7 +2385,7 @@ HostList.prototype.populate = co(function* populate(seed) {
for (i = 0; i < hosts.length; i++) {
host = hosts[i];
addr = NetworkAddress.fromHost(host, seed.port, this.network);
addr = NetAddress.fromHost(host, seed.port, this.network);
this.add(addr);
}
});

View File

@ -11,7 +11,7 @@ exports.KeyRing = require('./keyring');
exports.MemBlock = require('./memblock');
exports.MerkleBlock = require('./merkleblock');
exports.MTX = require('./mtx');
exports.NetworkAddress = require('./netaddress');
exports.NetAddress = require('./netaddress');
exports.Outpoint = require('./outpoint');
exports.Output = require('./output');
exports.TX = require('./tx');

View File

@ -16,7 +16,7 @@ var BufferReader = require('../utils/reader');
/**
* Represents a network address.
* @exports NetworkAddress
* @exports NetAddress
* @constructor
* @param {Object} options
* @param {Number?} options.ts - Timestamp.
@ -29,9 +29,9 @@ var BufferReader = require('../utils/reader');
* @property {Number} ts
*/
function NetworkAddress(options) {
if (!(this instanceof NetworkAddress))
return new NetworkAddress(options);
function NetAddress(options) {
if (!(this instanceof NetAddress))
return new NetAddress(options);
this.host = '0.0.0.0';
this.port = 0;
@ -52,7 +52,7 @@ function NetworkAddress(options) {
* @param {Object} options
*/
NetworkAddress.prototype.fromOptions = function fromOptions(options) {
NetAddress.prototype.fromOptions = function fromOptions(options) {
assert(typeof options.host === 'string');
assert(typeof options.port === 'number');
@ -79,11 +79,11 @@ NetworkAddress.prototype.fromOptions = function fromOptions(options) {
/**
* Instantiate network address from options.
* @param {Object} options
* @returns {NetworkAddress}
* @returns {NetAddress}
*/
NetworkAddress.fromOptions = function fromOptions(options) {
return new NetworkAddress().fromOptions(options);
NetAddress.fromOptions = function fromOptions(options) {
return new NetAddress().fromOptions(options);
};
/**
@ -91,7 +91,7 @@ NetworkAddress.fromOptions = function fromOptions(options) {
* @returns {Boolean}
*/
NetworkAddress.prototype.hasNetwork = function hasNetwork() {
NetAddress.prototype.hasNetwork = function hasNetwork() {
return (this.services & constants.services.NETWORK) !== 0;
};
@ -100,7 +100,7 @@ NetworkAddress.prototype.hasNetwork = function hasNetwork() {
* @returns {Boolean}
*/
NetworkAddress.prototype.hasBloom = function hasBloom() {
NetAddress.prototype.hasBloom = function hasBloom() {
return (this.services & constants.services.BLOOM) !== 0;
};
@ -109,7 +109,7 @@ NetworkAddress.prototype.hasBloom = function hasBloom() {
* @returns {Boolean}
*/
NetworkAddress.prototype.hasUTXO = function hasUTXO() {
NetAddress.prototype.hasUTXO = function hasUTXO() {
return (this.services & constants.services.GETUTXO) !== 0;
};
@ -118,7 +118,7 @@ NetworkAddress.prototype.hasUTXO = function hasUTXO() {
* @returns {Boolean}
*/
NetworkAddress.prototype.hasWitness = function hasWitness() {
NetAddress.prototype.hasWitness = function hasWitness() {
return (this.services & constants.services.WITNESS) !== 0;
};
@ -127,7 +127,7 @@ NetworkAddress.prototype.hasWitness = function hasWitness() {
* @param {String} host
*/
NetworkAddress.prototype.setHost = function setHost(host) {
NetAddress.prototype.setHost = function setHost(host) {
this.host = host;
this.hostname = IP.hostname(host, this.port);
};
@ -137,7 +137,7 @@ NetworkAddress.prototype.setHost = function setHost(host) {
* @param {Number} port
*/
NetworkAddress.prototype.setPort = function setPort(port) {
NetAddress.prototype.setPort = function setPort(port) {
this.port = port;
this.hostname = IP.hostname(this.host, port);
};
@ -150,16 +150,14 @@ NetworkAddress.prototype.setPort = function setPort(port) {
* @param {(Network|NetworkType)?} network
*/
NetworkAddress.prototype.fromHost = function fromHost(host, port, network) {
NetAddress.prototype.fromHost = function fromHost(host, port, network) {
network = Network.get(network);
assert(IP.version(host) !== -1);
this.host = host;
this.port = port || network.port;
this.services = constants.services.NETWORK
| constants.services.BLOOM
| constants.services.WITNESS;
this.services = constants.services.NETWORK | constants.services.WITNESS;
this.ts = network.now();
this.hostname = IP.hostname(this.host, this.port);
@ -173,11 +171,11 @@ NetworkAddress.prototype.fromHost = function fromHost(host, port, network) {
* @param {String} host
* @param {Number} port
* @param {(Network|NetworkType)?} network
* @returns {NetworkAddress}
* @returns {NetAddress}
*/
NetworkAddress.fromHost = function fromHost(host, port, network) {
return new NetworkAddress().fromHost(host, port, network);
NetAddress.fromHost = function fromHost(host, port, network) {
return new NetAddress().fromHost(host, port, network);
};
/**
@ -187,7 +185,7 @@ NetworkAddress.fromHost = function fromHost(host, port, network) {
* @param {(Network|NetworkType)?} network
*/
NetworkAddress.prototype.fromHostname = function fromHostname(hostname, network) {
NetAddress.prototype.fromHostname = function fromHostname(hostname, network) {
var addr;
network = Network.get(network);
@ -202,11 +200,11 @@ NetworkAddress.prototype.fromHostname = function fromHostname(hostname, network)
* from a hostname (i.e. 127.0.0.1:8333).
* @param {String} hostname
* @param {(Network|NetworkType)?} network
* @returns {NetworkAddress}
* @returns {NetAddress}
*/
NetworkAddress.fromHostname = function fromHostname(hostname, network) {
return new NetworkAddress().fromHostname(hostname, network);
NetAddress.fromHostname = function fromHostname(hostname, network) {
return new NetAddress().fromHostname(hostname, network);
};
/**
@ -215,7 +213,7 @@ NetworkAddress.fromHostname = function fromHostname(hostname, network) {
* @param {net.Socket} socket
*/
NetworkAddress.prototype.fromSocket = function fromSocket(socket, network) {
NetAddress.prototype.fromSocket = function fromSocket(socket, network) {
var host = socket.remoteAddress;
var port = socket.remotePort;
assert(typeof host === 'string');
@ -227,11 +225,11 @@ NetworkAddress.prototype.fromSocket = function fromSocket(socket, network) {
* Instantiate a network address
* from a socket.
* @param {net.Socket} socket
* @returns {NetworkAddress}
* @returns {NetAddress}
*/
NetworkAddress.fromSocket = function fromSocket(hostname, network) {
return new NetworkAddress().fromSocket(hostname, network);
NetAddress.fromSocket = function fromSocket(hostname, network) {
return new NetAddress().fromSocket(hostname, network);
};
/**
@ -241,7 +239,7 @@ NetworkAddress.fromSocket = function fromSocket(hostname, network) {
* @param {Boolean?} full - Include timestamp.
*/
NetworkAddress.prototype.fromReader = function fromReader(br, full) {
NetAddress.prototype.fromReader = function fromReader(br, full) {
this.ts = full ? br.readU32() : 0;
this.services = br.readU53();
this.host = IP.toString(br.readBytes(16, true));
@ -257,7 +255,7 @@ NetworkAddress.prototype.fromReader = function fromReader(br, full) {
* @param {Boolean?} full - Include timestamp.
*/
NetworkAddress.prototype.fromRaw = function fromRaw(data, full) {
NetAddress.prototype.fromRaw = function fromRaw(data, full) {
return this.fromReader(new BufferReader(data), full);
};
@ -265,22 +263,22 @@ NetworkAddress.prototype.fromRaw = function fromRaw(data, full) {
* Insantiate a network address from buffer reader.
* @param {BufferReader} br
* @param {Boolean?} full - Include timestamp.
* @returns {NetworkAddress}
* @returns {NetAddress}
*/
NetworkAddress.fromReader = function fromReader(br, full) {
return new NetworkAddress().fromReader(br, full);
NetAddress.fromReader = function fromReader(br, full) {
return new NetAddress().fromReader(br, full);
};
/**
* Insantiate a network address from serialized data.
* @param {Buffer} data
* @param {Boolean?} full - Include timestamp.
* @returns {NetworkAddress}
* @returns {NetAddress}
*/
NetworkAddress.fromRaw = function fromRaw(data, full) {
return new NetworkAddress().fromRaw(data, full);
NetAddress.fromRaw = function fromRaw(data, full) {
return new NetAddress().fromRaw(data, full);
};
/**
@ -290,7 +288,7 @@ NetworkAddress.fromRaw = function fromRaw(data, full) {
* @returns {Buffer}
*/
NetworkAddress.prototype.toWriter = function toWriter(bw, full) {
NetAddress.prototype.toWriter = function toWriter(bw, full) {
if (full)
bw.writeU32(this.ts);
@ -306,7 +304,7 @@ NetworkAddress.prototype.toWriter = function toWriter(bw, full) {
* @returns {Number}
*/
NetworkAddress.prototype.getSize = function getSize(full) {
NetAddress.prototype.getSize = function getSize(full) {
return 26 + (full ? 4 : 0);
};
@ -316,7 +314,7 @@ NetworkAddress.prototype.getSize = function getSize(full) {
* @returns {Buffer}
*/
NetworkAddress.prototype.toRaw = function toRaw(full) {
NetAddress.prototype.toRaw = function toRaw(full) {
var size = this.getSize(full);
return this.toWriter(new StaticWriter(size), full).render();
};
@ -326,8 +324,8 @@ NetworkAddress.prototype.toRaw = function toRaw(full) {
* @returns {Object}
*/
NetworkAddress.prototype.inspect = function inspect() {
return '<NetworkAddress:'
NetAddress.prototype.inspect = function inspect() {
return '<NetAddress:'
+ ' id=' + this.id
+ ' hostname=' + IP.hostname(this.host, this.port)
+ ' services=' + this.services.toString(2)
@ -339,4 +337,4 @@ NetworkAddress.prototype.inspect = function inspect() {
* Expose
*/
module.exports = NetworkAddress;
module.exports = NetAddress;

View File

@ -6,7 +6,7 @@ var constants = require('../lib/protocol/constants');
var Network = require('../lib/protocol/network');
var util = require('../lib/utils/util');
var BufferReader = require('../lib/utils/reader');
var NetworkAddress = require('../lib/primitives/netaddress');
var NetAddress = require('../lib/primitives/netaddress');
var TX = require('../lib/primitives/tx');
var Framer = require('../lib/net/framer');
var Parser = require('../lib/net/parser');
@ -41,8 +41,8 @@ describe('Protocol', function() {
version: constants.VERSION,
services: constants.LOCAL_SERVICES,
ts: network.now(),
remote: new NetworkAddress(),
local: new NetworkAddress(),
remote: new NetAddress(),
local: new NetAddress(),
nonce: util.nonce(),
agent: constants.USER_AGENT,
height: 0,
@ -60,8 +60,8 @@ describe('Protocol', function() {
version: constants.VERSION,
services: constants.LOCAL_SERVICES,
ts: network.now(),
remote: new NetworkAddress(),
local: new NetworkAddress(),
remote: new NetAddress(),
local: new NetAddress(),
nonce: util.nonce(),
agent: constants.USER_AGENT,
height: 10,
@ -79,13 +79,13 @@ describe('Protocol', function() {
});
hosts = [
new NetworkAddress({
new NetAddress({
services: constants.LOCAL_SERVICES,
host: '127.0.0.1',
port: 8333,
ts: util.now()
}),
new NetworkAddress({
new NetAddress({
services: constants.LOCAL_SERVICES,
host: '::123:456:789a',
port: 18333,