diff --git a/README.md b/README.md index 7db5abf1..2c496a1c 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ Docs need to be rewritten. They're coming soon. I promise. ### High-level usage for Node object ``` js -var bcoin = require('bcoin'); +var bcoin = require('bcoin')('main'); var node = bcoin.fullnode({ prune: false, @@ -136,8 +136,7 @@ $ node bin/bcoin-cli mempool ### Creating a blockchain and mempool ``` js -var bcoin = require('bcoin'); -bcoin.protocol.network.set('regtest'); +var bcoin = require('bcoin')('regtest'); var chain = new bcoin.chain({ db: 'memory' }); var mempool = new bcoin.mempool({ chain: chain, db: 'memory' }); var miner = new bcoin.miner({ chain: chain, mempool: mempool }); diff --git a/lib/bcoin/ldb.js b/lib/bcoin/ldb.js index 7f1edadb..98f694dc 100644 --- a/lib/bcoin/ldb.js +++ b/lib/bcoin/ldb.js @@ -36,6 +36,7 @@ function ldb(options) { maxOpenFiles: options.maxOpenFiles || 8192, filterBits: 0, paranoidChecks: false, + memory: false, // For LMDB if we decide to use it: sync: options.sync || false, diff --git a/lib/bcoin/miner.js b/lib/bcoin/miner.js index 540a163f..ae2450f3 100644 --- a/lib/bcoin/miner.js +++ b/lib/bcoin/miner.js @@ -110,25 +110,22 @@ Miner.prototype._init = function _init() { stat.best); }); - this.chain.open(function(err) { + function done(err) { if (err) return self.emit('error', err); self.loaded = true; self.emit('open'); - }); + } + + if (this.mempool) + this.mempool.open(done); + else + this.chain.open(done); }; Miner.prototype.start = function start() { var self = this; - // Wait for `tip`. - if (!this.chain.tip) { - this.chain.on('tip', function(tip) { - self.start(); - }); - return; - } - this.stop(); this.running = true; @@ -189,6 +186,17 @@ Miner.prototype.createBlock = function createBlock(callback) { var ts = Math.max(utils.now(), this.chain.tip.ts + 1); var attempt; + if (!this.loaded) { + this.open(function(err) { + if (err) + return callback(err); + self.createBlock(callback); + }); + return; + } + + assert(this.chain.tip); + // Find target this.chain.getTargetAsync(this.chain.tip, ts, function(err, target) { if (err) @@ -520,7 +528,6 @@ function rcmp(a, b) { * Expose */ -var exports = Miner; -exports.minerblock = MinerBlock; -return exports; +Miner.minerblock = MinerBlock; +return Miner; }; diff --git a/lib/bcoin/mtx.js b/lib/bcoin/mtx.js index 98f90986..73299e75 100644 --- a/lib/bcoin/mtx.js +++ b/lib/bcoin/mtx.js @@ -37,7 +37,7 @@ function MTX(options) { this.ts = 0; this.block = null; this.index = -1; - this.ps = this.ts === 0 ? utils.now() : 0; + this.ps = options.ps != null ? options.ps : utils.now(); this.changeIndex = options.changeIndex != null ? options.changeIndex : -1; this.height = -1; diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index 05ac2110..da026bf9 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -43,6 +43,13 @@ function Pool(options) { options.relay = true; } + if (options.headers == null) { + if (options.spv) + options.headers = true; + else + options.headers = false; + } + seeds = (options.seeds || network.seeds).slice(); if (process.env.BCOIN_SEED) @@ -59,14 +66,6 @@ function Pool(options) { this.size = options.size || 8; this.connected = false; - if (options.spv) { - if (options.headers == null) - options.headers = true; - } else { - if (options.headers == null) - options.headers = false; - } - this.syncing = false; this.synced = false; this._scheduled = false; @@ -140,7 +139,7 @@ function Pool(options) { timeout: options.invTimeout || 60000 }; - this.chain.open(function(err) { + function done(err) { if (err) return self.emit('error', err); @@ -148,7 +147,12 @@ function Pool(options) { self.emit('open'); self._init(); - }); + } + + if (this.mempool) + this.mempool.open(done); + else + this.chain.open(done); } utils.inherits(Pool, EventEmitter); diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index 959d57d2..6240cfb0 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -40,7 +40,7 @@ function TX(data, block, index) { this.ts = data.ts || 0; this.block = data.block || null; this.index = data.index != null ? data.index : -1; - this.ps = this.ts === 0 ? utils.now() : 0; + this.ps = this.ts === 0 ? (data.ps != null ? data.ps : utils.now()) : 0; this.height = data.height != null ? data.height : -1; this._hash = null; @@ -1223,7 +1223,9 @@ TX.prototype.inspect = function inspect() { date: new Date(this.ts * 1000).toISOString(), block: this.block ? utils.revHex(this.block) : null, ts: this.ts, + ps: this.ps, index: this.index, + changeIndex: this.changeIndex || -1, version: this.version, inputs: this.inputs, outputs: this.outputs,