network object.

This commit is contained in:
Christopher Jeffrey 2016-06-29 00:03:16 -07:00
parent 4a8ef06154
commit 7821d65db4
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -10,7 +10,7 @@
var utils = require('./utils');
var assert = utils.assert;
var constants = require('./protocol/constants');
var network = require('./protocol/network');
var networks = require('./protocol/network');
var PolicyEstimator = require('./fees');
/**
@ -27,13 +27,7 @@ function Network(options) {
if (!(this instanceof Network))
return new Network(options);
if (typeof options === 'string')
options = network[options];
assert(options, 'Unknown network.');
if (Network[options.type])
return Network[options.type];
assert(!Network[options.type], 'Cannot create two networks.');
this.type = options.type;
this.height = options.height;
@ -64,12 +58,6 @@ function Network(options) {
this.requestMempool = options.requestMempool;
this.fees = new PolicyEstimator(constants.tx.MIN_RELAY, this);
if (!Network[this.type])
Network[this.type] = this;
if (!Network.primary)
Network.primary = this.type;
}
/**
@ -79,6 +67,10 @@ function Network(options) {
Network.primary = null;
/*
* Networks (to avoid hash table mode).
*/
Network.main = null;
Network.testnet = null;
Network.regtest = null;
@ -138,6 +130,33 @@ Network.prototype.getRate = function getRate() {
return Math.min(this.feeRate, this.maxRate);
};
/**
* Create a network. Get existing network if possible.
* @param {NetworkType|Object} options
* @returns {Network}
*/
Network.create = function create(options) {
var net;
if (typeof options === 'string')
options = networks[options];
assert(options, 'Unknown network.');
if (Network[options.type])
return Network[options.type];
net = new Network(options);
Network[net.type] = net;
if (!Network.primary)
Network.primary = net;
return net;
};
/**
* Set the default network. This network will be used
* if nothing is passed as the `network` option for
@ -148,27 +167,27 @@ Network.prototype.getRate = function getRate() {
Network.set = function set(type) {
assert(typeof type === 'string', 'Bad network.');
Network.primary = type;
return Network(network[type]);
Network.primary = Network.get(type);
return Network.primary;
};
/**
* Get a network with a string or a Network object.
* @param {NetworkType|Network} options - Network type.
* @param {NetworkType|Network} type - Network type.
* @returns {Network}
*/
Network.get = function get(options) {
if (!options) {
Network.get = function get(type) {
if (!type) {
assert(Network.primary, 'No default network.');
return Network[Network.primary];
return Network.primary;
}
if (options instanceof Network)
return options;
if (type instanceof Network)
return type;
if (typeof options === 'string')
return Network(network[options]);
if (typeof type === 'string')
return Network.create(type);
assert(false, 'Unknown network.');
};
@ -181,13 +200,13 @@ Network.get = function get(options) {
Network.fromMagic = function fromMagic(magic) {
var i, type;
for (i = 0; i < network.types.length; i++) {
type = network.types[i];
if (magic === network[type].magic)
for (i = 0; i < networks.types.length; i++) {
type = networks.types[i];
if (magic === networks[type].magic)
break;
}
assert(i < network.types.length, 'Network not found.');
assert(i < networks.types.length, 'Network not found.');
return Network.get(type);
};