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