refactor: more restructuring.

This commit is contained in:
Christopher Jeffrey 2016-08-23 23:26:50 -07:00
parent d350338c98
commit a3c24938b9
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
19 changed files with 1276 additions and 1275 deletions

View File

@ -124,13 +124,13 @@ function Environment() {
this.rbt = require('./db/rbt');
this.lowlevelup = require('./db/lowlevelup');
this.uri = require('./utils/uri');
this.logger = require('./logger');
this.config = require('./config');
this.logger = require('./node/logger');
this.config = require('./node/config');
this.protocol = require('./protocol');
this.packets = require('./net/packets');
this.network = require('./network');
this.errors = require('./errors');
this.network = require('./protocol/network');
this.errors = require('./utils/errors');
this.ldb = require('./db/ldb');
this.timedata = require('./timedata');
this.script = require('./primitives/script');
@ -155,14 +155,14 @@ function Environment() {
this.block = require('./primitives/block');
this.merkleblock = require('./primitives/merkleblock');
this.headers = require('./primitives/headers');
this.fees = require('./fees');
this.node = require('./node');
this.spvnode = require('./spvnode');
this.fullnode = require('./fullnode');
this.fees = require('./mempool/fees');
this.node = require('./node/node');
this.spvnode = require('./node/spvnode');
this.fullnode = require('./node/fullnode');
this.chainentry = require('./chain/chainentry');
this.chaindb = require('./chain/chaindb');
this.chain = require('./chain/chain');
this.mempool = require('./mempool');
this.mempool = require('./mempool/mempool');
this.mempoolentry = this.mempool.MempoolEntry;
this.keyring = require('./primitives/keyring');
this.hd = require('./primitives/hd');

View File

@ -7,7 +7,7 @@
'use strict';
var Network = require('../network');
var Network = require('../protocol/network');
var AsyncObject = require('../utils/async');
var RPCClient = require('./rpcclient');
var utils = require('../utils/utils');

View File

@ -6,7 +6,7 @@
'use strict';
var Network = require('../network');
var Network = require('../protocol/network');
var request = require('./request');
/**

View File

@ -7,7 +7,7 @@
'use strict';
var Network = require('../network');
var Network = require('../protocol/network');
var EventEmitter = require('events').EventEmitter;
var utils = require('../utils/utils');

View File

@ -1,267 +0,0 @@
/*!
* network.js - network object for bcoin
* Copyright (c) 2014-2015, Fedor Indutny (MIT License)
* Copyright (c) 2014-2016, Christopher Jeffrey (MIT License).
* https://github.com/bcoin-org/bcoin
*/
'use strict';
var utils = require('./utils/utils');
var assert = utils.assert;
var networks = require('./protocol/network');
/**
* Represents a network.
* @exports Network
* @constructor
* @param {Object|NetworkType} options - See {@link module:network}.
* @property {Number} height
* @property {Rate} feeRate
* @property {Rate} minRelay
* @property {PolicyEstimator} fees
*/
function Network(options) {
if (!(this instanceof Network))
return new Network(options);
assert(!Network[options.type], 'Cannot create two networks.');
this.type = options.type;
this.height = options.height;
this.seeds = options.seeds;
this.magic = options.magic;
this.port = options.port;
this.alertKey = options.alertKey;
this.checkpoints = options.checkpoints;
this.halvingInterval = options.halvingInterval;
this.genesis = options.genesis;
this.genesisBlock = options.genesisBlock;
this.pow = options.pow;
this.block = options.block;
this.witness = options.witness;
this.oldWitness = options.oldWitness;
this.activationThreshold = options.activationThreshold;
this.minerWindow = options.minerWindow;
this.deployments = options.deployments;
this.keyPrefix = options.keyPrefix;
this.addressPrefix = options.addressPrefix;
this.requireStandard = options.requireStandard;
this.rpcPort = options.rpcPort;
this.minRelay = options.minRelay;
this.feeRate = options.feeRate;
this.minRate = options.minRate;
this.maxRate = options.maxRate;
this.selfConnect = options.selfConnect;
this.requestMempool = options.requestMempool;
this.batchSize = options.batchSize;
}
/**
* Default network.
* @type {String}
*/
Network.primary = null;
/*
* Networks (to avoid hash table mode).
*/
Network.main = null;
Network.testnet = null;
Network.regtest = null;
Network.segnet3 = null;
Network.segnet4 = null;
/**
* Update the height of the network.
* @param {Number} height
*/
Network.prototype.updateHeight = function updateHeight(height) {
this.height = height;
};
/**
* Update the estimated fee rate of the network.
* @param {Rate} rate
*/
Network.prototype.updateRate = function updateRate(rate) {
this.feeRate = rate;
};
/**
* Update the minimum relay rate (reject rate) of the network.
* @param {Rate} rate
*/
Network.prototype.updateMinRelay = function updateMinRelay(rate) {
this.minRelay = rate;
};
/**
* Calculate the minimum relay rate. If the network is
* inactive (height=-1), return the default minimum relay.
* @return {Rate} Rate
*/
Network.prototype.getMinRelay = function getMinRelay() {
if (this.height === -1)
return this.minRate;
return Math.min(this.minRelay, this.maxRate);
};
/**
* Calculate the normal relay rate. If the network is
* inactive (height=-1), return the default rate.
* @return {Rate} Rate
*/
Network.prototype.getRate = function getRate() {
if (this.height === -1)
return this.maxRate;
return Math.min(this.feeRate, this.maxRate);
};
/**
* Determine how many blocks to request
* based on current height of the chain.
* @param {Number} height
* @returns {Number}
*/
Network.prototype.getBatchSize = function getBatchSize(height) {
var batch = this.batchSize;
var last = batch.length - 1;
var i;
for (i = 0; i < last; i++) {
if (height <= batch[i][0])
return batch[i][1];
}
return batch[last][0];
};
/**
* Create a network. Get existing network if possible.
* @param {NetworkType|Object} options
* @returns {Network}
*/
Network.create = function create(options) {
var network;
if (typeof options === 'string')
options = networks[options];
assert(options, 'Unknown network.');
if (Network[options.type])
return Network[options.type];
network = new Network(options);
Network[network.type] = network;
if (!Network.primary)
Network.primary = network;
return network;
};
/**
* Set the default network. This network will be used
* if nothing is passed as the `network` option for
* certain objects.
* @param {NetworkType} type - Network type.
* @returns {Network}
*/
Network.set = function set(type) {
assert(typeof type === 'string', 'Bad network.');
Network.primary = Network.get(type);
return Network.primary;
};
/**
* Get a network with a string or a Network object.
* @param {NetworkType|Network} type - Network type.
* @returns {Network}
*/
Network.get = function get(type) {
if (!type) {
assert(Network.primary, 'No default network.');
return Network.primary;
}
if (type instanceof Network)
return type;
if (typeof type === 'string')
return Network.create(type);
assert(false, 'Unknown network.');
};
/**
* Get a network by its magic number.
* @returns {Network}
*/
Network.fromMagic = function fromMagic(magic) {
var i, type;
for (i = 0; i < networks.types.length; i++) {
type = networks.types[i];
if (magic === networks[type].magic)
break;
}
assert(i < networks.types.length, 'Network not found.');
return Network.get(type);
};
/**
* Convert the network to a string.
* @returns {String}
*/
Network.prototype.toString = function toString() {
return this.type;
};
/**
* Inspect the network.
* @returns {String}
*/
Network.prototype.inspect = function inspect() {
return '<Network: ' + this.type + '>';
};
/**
* Test an object to see if it is a Network.
* @param {Object} obj
* @returns {Boolean}
*/
Network.isNetwork = function isNetwork(obj) {
return obj
&& typeof obj.getMinRelay === 'function'
&& typeof obj.genesisBlock === 'string'
&& typeof obj.pow === 'object';
};
/*
* Expose
*/
module.exports = Network;

View File

@ -6,8 +6,8 @@
'use strict';
var Network = require('./network');
var utils = require('./utils/utils');
var Network = require('../protocol/network');
var utils = require('../utils/utils');
var assert = utils.assert;
var fs;

View File

@ -7,9 +7,9 @@
'use strict';
var bcoin = require('./env');
var bcoin = require('../env');
var constants = bcoin.protocol.constants;
var utils = require('./utils/utils');
var utils = require('../utils/utils');
var assert = utils.assert;
var Node = bcoin.node;

View File

@ -6,7 +6,7 @@
'use strict';
var utils = require('./utils/utils');
var utils = require('../utils/utils');
var assert = require('assert');
var fs;

View File

@ -7,9 +7,9 @@
'use strict';
var bcoin = require('./env');
var AsyncObject = require('./utils/async');
var utils = require('./utils/utils');
var bcoin = require('../env');
var AsyncObject = require('../utils/async');
var utils = require('../utils/utils');
/**
* Base class from which every other

View File

@ -7,8 +7,8 @@
'use strict';
var bcoin = require('./env');
var utils = require('./utils/utils');
var bcoin = require('../env');
var utils = require('../utils/utils');
var assert = utils.assert;
var Node = bcoin.node;

View File

@ -8,7 +8,7 @@
'use strict';
var bcoin = require('../env');
var networks = bcoin.protocol.network;
var networks = bcoin.protocol.networks;
var constants = bcoin.protocol.constants;
var utils = require('../utils/utils');
var assert = utils.assert;

View File

@ -83,7 +83,7 @@ var utils = require('../utils/utils');
var ec = require('../crypto/ec');
var assert = utils.assert;
var constants = bcoin.protocol.constants;
var networks = bcoin.protocol.network;
var networks = bcoin.protocol.networks;
var KeyRing = bcoin.keyring;
var LRU = require('../utils/lru');
var BufferWriter = require('../utils/writer');

View File

@ -11,7 +11,7 @@ var bcoin = require('../env');
var constants = bcoin.protocol.constants;
var utils = bcoin.utils;
var assert = utils.assert;
var network = bcoin.protocol.network;
var networks = bcoin.protocol.networks;
var BufferReader = require('../utils/reader');
var BufferWriter = require('../utils/writer');
var scriptTypes = constants.scriptTypes;
@ -253,14 +253,14 @@ KeyRing.prototype.fromSecret = function fromSecret(data) {
version = p.readU8();
for (i = 0; i < network.types.length; i++) {
type = network.types[i];
prefix = network[type].keyPrefix.privkey;
for (i = 0; i < networks.types.length; i++) {
type = networks.types[i];
prefix = networks[type].keyPrefix.privkey;
if (version === prefix)
break;
}
assert(i < network.types.length, 'Network not found.');
assert(i < networks.types.length, 'Network not found.');
key = p.readBytes(32);

View File

@ -8,4 +8,5 @@
'use strict';
exports.constants = require('./constants');
exports.networks = require('./networks');
exports.network = require('./network');

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -7,7 +7,7 @@
'use strict';
var bcoin = require('./env');
var bcoin = require('../env');
var utils = bcoin.utils;
var constants = bcoin.protocol.constants;