From d842b5639bede44363721ea2100d2f30b24b2e1f Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sat, 1 Oct 2016 21:37:56 -0700 Subject: [PATCH] chain: refactor deps. --- lib/chain/chain.js | 25 ++++++++++++++---------- lib/chain/chaindb.js | 42 +++++++++++++++++++++++------------------ lib/chain/chainentry.js | 12 +++++++----- lib/chain/coins.js | 17 +++++++++-------- lib/chain/coinview.js | 8 ++++---- 5 files changed, 59 insertions(+), 45 deletions(-) diff --git a/lib/chain/chain.js b/lib/chain/chain.js index 8cb01ffe..24d365d3 100644 --- a/lib/chain/chain.js +++ b/lib/chain/chain.js @@ -7,13 +7,18 @@ 'use strict'; -var bcoin = require('../env'); var AsyncObject = require('../utils/async'); -var constants = bcoin.constants; +var Network = require('../protocol/network'); +var Logger = require('../node/logger'); +var ChainDB = require('./chaindb'); +var constants = require('../protocol/constants'); var utils = require('../utils/utils'); +var Locker = require('../utils/locker'); +var ChainEntry = require('./chainentry'); var assert = utils.assert; -var VerifyError = bcoin.errors.VerifyError; +var VerifyError = require('../utils/errors').VerifyError; var VerifyResult = utils.VerifyResult; +var time = require('../net/timedata'); var spawn = require('../utils/spawn'); var co = spawn.co; @@ -67,13 +72,13 @@ function Chain(options) { this.options = options; - this.network = bcoin.network.get(options.network); - this.logger = options.logger || bcoin.defaultLogger; - this.db = new bcoin.chaindb(this); + this.network = Network.get(options.network); + this.logger = options.logger || Logger.global; + this.db = new ChainDB(this); this.total = 0; this.currentBlock = null; this.orphanLimit = options.orphanLimit || (20 << 20); - this.locker = new bcoin.locker(true); + this.locker = new Locker(true); this.invalid = {}; this.bestHeight = -1; this.tip = null; @@ -1173,7 +1178,7 @@ Chain.prototype._add = co(function* add(block) { block.setHeight(height); // Create a new chain entry. - entry = bcoin.chainentry.fromBlock(this, block, prev); + entry = ChainEntry.fromBlock(this, block, prev); // The block is on a alternate chain if the // chainwork is less than or equal to @@ -1658,7 +1663,7 @@ Chain.prototype.getTarget = function getTarget(block, prev, ancestors) { if ((prev.height + 1) % this.network.pow.retargetInterval !== 0) { if (this.network.pow.difficultyReset) { // Special behavior for testnet: - ts = block ? (block.ts || block) : bcoin.now(); + ts = block ? (block.ts || block) : time.now(); if (ts > prev.ts + this.network.pow.targetSpacing * 2) return this.network.pow.bits; @@ -1945,7 +1950,7 @@ Chain.prototype.checkFinal = co(function* checkFinal(prev, tx, flags) { return tx.isFinal(height, ts); } - return tx.isFinal(height, bcoin.now()); + return tx.isFinal(height, time.now()); }); /** diff --git a/lib/chain/chaindb.js b/lib/chain/chaindb.js index 519d1e2c..b1cb12bc 100644 --- a/lib/chain/chaindb.js +++ b/lib/chain/chaindb.js @@ -7,9 +7,8 @@ 'use strict'; -var bcoin = require('../env'); var AsyncObject = require('../utils/async'); -var constants = bcoin.constants; +var constants = require('../protocol/constants'); var utils = require('../utils/utils'); var assert = utils.assert; var DUMMY = new Buffer([0]); @@ -18,6 +17,13 @@ var BufferReader = require('../utils/reader'); var spawn = require('../utils/spawn'); var CoinView = require('./coinview'); var Coins = require('./coins'); +var ldb = require('../db/ldb'); +var LRU = require('../utils/lru'); +var Block = require('../primitives/block'); +var Coin = require('../primitives/coin'); +var TX = require('../primitives/tx'); +var Address = require('../primitives/address'); +var ChainEntry = require('./chainentry'); var co = spawn.co; /* @@ -164,7 +170,7 @@ function ChainDB(chain) { this.logger = chain.logger; this.network = chain.network; - this.db = bcoin.ldb({ + this.db = ldb({ location: this.options.location, db: this.options.db, maxOpenFiles: this.options.maxFiles, @@ -191,12 +197,12 @@ function ChainDB(chain) { // We want to keep the last 5 blocks of unspents in memory. this.coinWindow = 25 << 20; - this.coinCache = new bcoin.lru.nil(); - this.cacheHash = new bcoin.lru(this.cacheWindow); - this.cacheHeight = new bcoin.lru(this.cacheWindow); + this.coinCache = new LRU.nil(); + this.cacheHash = new LRU(this.cacheWindow); + this.cacheHeight = new LRU(this.cacheWindow); if (this.options.coinCache) - this.coinCache = new bcoin.lru(this.coinWindow, getSize); + this.coinCache = new LRU(this.coinWindow, getSize); } utils.inherits(ChainDB, AsyncObject); @@ -231,9 +237,9 @@ ChainDB.prototype._open = co(function* open() { } else { // Otherwise write the genesis block. // (We assume this database is fresh). - block = bcoin.block.fromRaw(this.network.genesisBlock, 'hex'); + block = Block.fromRaw(this.network.genesisBlock, 'hex'); block.setHeight(0); - entry = bcoin.chainentry.fromBlock(this.chain, block); + entry = ChainEntry.fromBlock(this.chain, block); yield this.save(entry, block, new CoinView()); } @@ -514,7 +520,7 @@ ChainDB.prototype.getEntry = co(function* getEntry(hash) { if (!entry) return; - return bcoin.chainentry.fromRaw(this.chain, entry); + return ChainEntry.fromRaw(this.chain, entry); }); /** @@ -703,7 +709,7 @@ ChainDB.prototype.getNextHash = co(function* getNextHash(hash) { ChainDB.prototype.isMainChain = co(function* isMainChain(hash) { var query, height, existing; - if (hash instanceof bcoin.chainentry) { + if (hash instanceof ChainEntry) { query = hash.height; hash = hash.hash; } else { @@ -1071,7 +1077,7 @@ ChainDB.prototype.fillHistory = co(function* fillHistory(tx) { ptx = yield this.getTX(input.prevout.hash); if (ptx) - input.coin = bcoin.coin.fromTX(ptx, input.prevout.index); + input.coin = Coin.fromTX(ptx, input.prevout.index); } return tx; @@ -1200,7 +1206,7 @@ ChainDB.prototype.getTX = co(function* getTX(hash) { if (!data) return; - return bcoin.tx.fromExtended(data); + return TX.fromExtended(data); }); /** @@ -1233,7 +1239,7 @@ ChainDB.prototype.getCoinsByAddress = co(function* getCoinsByAddress(addresses) for (i = 0; i < addresses.length; i++) { address = addresses[i]; - hash = bcoin.address.getHash(address); + hash = Address.getHash(address); if (!hash) continue; @@ -1267,7 +1273,7 @@ ChainDB.prototype.getEntries = function getEntries() { gte: layout.e(constants.ZERO_HASH), lte: layout.e(constants.MAX_HASH), parse: function(key, value) { - return bcoin.chainentry.fromRaw(self.chain, value); + return ChainEntry.fromRaw(self.chain, value); } }); }; @@ -1287,7 +1293,7 @@ ChainDB.prototype.getHashesByAddress = co(function* getHashesByAddress(addresses for (i = 0; i < addresses.length; i++) { address = addresses[i]; - hash = bcoin.address.getHash(address); + hash = Address.getHash(address); if (!hash) continue; @@ -1410,7 +1416,7 @@ ChainDB.prototype.getUndoCoins = co(function* getUndoCoins(hash) { coins = []; while (p.left()) - coins.push(bcoin.coin.fromRaw(p)); + coins.push(Coin.fromRaw(p)); return coins; }); @@ -1472,7 +1478,7 @@ ChainDB.prototype.getBlock = co(function* getBlock(hash) { if (!data) return; - block = bcoin.block.fromRaw(data); + block = Block.fromRaw(data); block.setHeight(height); return block; diff --git a/lib/chain/chainentry.js b/lib/chain/chainentry.js index 9d92a4b8..2ca956af 100644 --- a/lib/chain/chainentry.js +++ b/lib/chain/chainentry.js @@ -7,14 +7,16 @@ 'use strict'; -var bcoin = require('../env'); var bn = require('bn.js'); -var constants = bcoin.constants; +var Network = require('../protocol/network'); +var constants = require('../protocol/constants'); var utils = require('../utils/utils'); var crypto = require('../crypto/crypto'); var assert = utils.assert; var BufferWriter = require('../utils/writer'); var BufferReader = require('../utils/reader'); +var Header = require('../primitives/headers'); +var InvItem = require('../primitives/invitem'); var spawn = require('../utils/spawn'); var co = spawn.co; @@ -48,7 +50,7 @@ function ChainEntry(chain, options, prev) { return new ChainEntry(chain, options, prev); this.chain = chain; - this.network = chain ? chain.network : bcoin.network.get(); + this.network = chain ? chain.network : Network.primary; this.hash = constants.NULL_HASH; this.version = 1; @@ -608,7 +610,7 @@ ChainEntry.fromJSON = function fromJSON(chain, json) { */ ChainEntry.prototype.toHeaders = function toHeaders() { - return bcoin.headers.fromEntry(this); + return Headers.fromEntry(this); }; /** @@ -617,7 +619,7 @@ ChainEntry.prototype.toHeaders = function toHeaders() { */ ChainEntry.prototype.toInv = function toInv() { - return new bcoin.invitem(constants.inv.BLOCK, this.hash); + return new InvItem(constants.inv.BLOCK, this.hash); }; /** diff --git a/lib/chain/coins.js b/lib/chain/coins.js index dbd15d4d..2e734e50 100644 --- a/lib/chain/coins.js +++ b/lib/chain/coins.js @@ -7,10 +7,11 @@ 'use strict'; -var bcoin = require('../env'); -var utils = bcoin.utils; +var utils = require('../utils/utils'); var assert = utils.assert; -var constants = bcoin.constants; +var constants = require('../protocol/constants'); +var Coin = require('../primitives/coin'); +var ec = require('../crypto/ec'); var BufferReader = require('../utils/reader'); var BufferWriter = require('../utils/writer'); @@ -449,7 +450,7 @@ Coins.prototype.fromTX = function fromTX(tx) { this.outputs.push(null); continue; } - this.outputs.push(bcoin.coin.fromTX(tx, i)); + this.outputs.push(Coin.fromTX(tx, i)); } return this; @@ -502,7 +503,7 @@ function CompressedCoin(offset, size, raw) { CompressedCoin.prototype.toCoin = function toCoin(coins, index) { var p = new BufferReader(this.raw); - var coin = new bcoin.coin(); + var coin = new Coin(); // Load in all necessary properties // from the parent Coins object. @@ -681,7 +682,7 @@ function compressKey(key) { var out; // We can't compress it if it's not valid. - if (!bcoin.ec.publicKeyVerify(key)) + if (!ec.publicKeyVerify(key)) return; switch (key[0]) { @@ -694,7 +695,7 @@ function compressKey(key) { case 0x06: case 0x07: // Compress the key normally. - out = bcoin.ec.publicKeyConvert(key, true); + out = ec.publicKeyConvert(key, true); // Store the original format (which // may be a hybrid byte) in the hi // 3 bits so we can restore it later. @@ -733,7 +734,7 @@ function decompressKey(key) { // low bits so publicKeyConvert // actually understands it. key[0] &= 0x03; - out = bcoin.ec.publicKeyConvert(key, false); + out = ec.publicKeyConvert(key, false); // Reset the hi bits so as not to // mutate the original buffer. diff --git a/lib/chain/coinview.js b/lib/chain/coinview.js index 61d4f57f..0c2dd8ae 100644 --- a/lib/chain/coinview.js +++ b/lib/chain/coinview.js @@ -7,9 +7,9 @@ 'use strict'; -var bcoin = require('../env'); -var utils = bcoin.utils; +var utils = require('../utils/utils'); var assert = utils.assert; +var Coins = require('./coins'); /** * A collections of {@link Coins} objects. @@ -43,7 +43,7 @@ CoinView.prototype.add = function add(coins) { CoinView.prototype.addCoin = function addCoin(coin) { assert(typeof coin.hash === 'string'); if (!this.coins[coin.hash]) - this.coins[coin.hash] = new bcoin.coins(); + this.coins[coin.hash] = new Coins(); this.coins[coin.hash].add(coin); }; @@ -53,7 +53,7 @@ CoinView.prototype.addCoin = function addCoin(coin) { */ CoinView.prototype.addTX = function addTX(tx) { - this.add(bcoin.coins.fromTX(tx)); + this.add(Coins.fromTX(tx)); }; /**