From 0e15723acd36312bb9488c5aa0a11bf7d206a0dc Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sun, 20 Mar 2016 10:55:37 -0700 Subject: [PATCH] refactor. fixes. --- lib/bcoin/block.js | 2 ++ lib/bcoin/chain.js | 45 +++++++++++++++++++++++++------------------- lib/bcoin/chaindb.js | 8 ++++---- lib/bcoin/ldb.js | 11 +++++------ lib/bcoin/tx.js | 8 ++++---- 5 files changed, 41 insertions(+), 33 deletions(-) diff --git a/lib/bcoin/block.js b/lib/bcoin/block.js index c3c112bd..96187772 100644 --- a/lib/bcoin/block.js +++ b/lib/bcoin/block.js @@ -10,6 +10,8 @@ var utils = require('./utils'); var assert = utils.assert; var constants = bcoin.protocol.constants; var network = bcoin.protocol.network; +var BufferWriter = require('./writer'); +var BufferReader = require('./reader'); /** * Block diff --git a/lib/bcoin/chain.js b/lib/bcoin/chain.js index 5a3f1c4e..05ba3202 100644 --- a/lib/bcoin/chain.js +++ b/lib/bcoin/chain.js @@ -684,9 +684,9 @@ Chain.prototype._checkInputs = function _checkInputs(block, prev, flags, callbac utils.debug('Signature Hash v1: %s', utils.toHex(tx.signatureHash(j, input.output.script, 'all', 1))); utils.debug('Raw Script: %s', - utils.toHex(input.output.script._raw || [])); + utils.toHex(input.output.script.encode())); utils.debug('Reserialized Script: %s', - utils.toHex(bcoin.script.encode(input.output.script))); + utils.toHex(input.output.script.clone().encode())); if (height < network.checkpoints.lastHeight) throw new Error('BUG: Bad inputs in historical data!'); return callback(null, false); @@ -903,7 +903,12 @@ Chain.prototype.reset = function reset(height, callback, force) { callback = utils.ensure(callback); - function done(err, result) { + this.db.reset(height, function(err, result) { + if (err) { + unlock(); + return callback(err); + } + // Reset the orphan map completely. There may // have been some orphans on a forked chain we // no longer need. @@ -911,14 +916,7 @@ Chain.prototype.reset = function reset(height, callback, force) { self.purgePending(); unlock(); - callback(err, result); - } - - this.db.reset(height, function(err) { - if (err) - return done(err); - - return done(); + callback(null, result); }); }; @@ -1127,8 +1125,9 @@ Chain.prototype.add = function add(initial, peer, callback, force) { } } - // Update the block height - // IMPORTANT!!!!! + // Update the block height early + // Some things in verifyContext may + // need access to height on txs. block.height = height; block.txs.forEach(function(tx) { tx.height = height; @@ -1140,6 +1139,15 @@ Chain.prototype.add = function add(initial, peer, callback, force) { self._verifyContext(block, prev, function(err, verified) { var entry; + // Couldn't verify block. + // Revert the height. + if (err || !verified) { + block.height = -1; + block.txs.forEach(function(tx) { + tx.height = -1; + }); + } + if (err) return done(err); @@ -1310,7 +1318,7 @@ Chain.prototype.has = function has(hash, callback) { Chain.prototype.byTime = function byTime(ts, callback, force) { var self = this; var start = 0; - var end = this.height + 1; + var end = this.height; var pos, delta; var unlock = this._lock(byTime, [ts, callback], force); @@ -1346,7 +1354,7 @@ Chain.prototype.byTime = function byTime(ts, callback, force) { if (start >= end) return done(); - pos = (start + end) >> 1; + pos = (start + end) >>> 1; self.db.get(pos, function(err, entry) { if (err) @@ -1357,11 +1365,10 @@ Chain.prototype.byTime = function byTime(ts, callback, force) { if (delta <= 60 * 60) return done(null, entry); - if (ts < entry.ts) { - end = pos; - } else { + if (ts < entry.ts) + end = pos - 1; + else start = pos + 1; - } next(); }); diff --git a/lib/bcoin/chaindb.js b/lib/bcoin/chaindb.js index a296bf09..40d59eb5 100644 --- a/lib/bcoin/chaindb.js +++ b/lib/bcoin/chaindb.js @@ -557,11 +557,11 @@ ChainDB.prototype.saveBlock = function saveBlock(block, batch, callback) { if (this.options.spv) return callback(); - // batch.put('b/b/' + block.hash('hex'), block.toCompact()); + batch.put('b/b/' + block.hash('hex'), block.toCompact()); - // block.txs.forEach(function(tx) { - // batch.put('t/t/' + tx.hash('hex'), tx.toExtended()); - // }); + block.txs.forEach(function(tx) { + batch.put('t/t/' + tx.hash('hex'), tx.toExtended()); + }); this.connectBlock(block, batch, callback); }; diff --git a/lib/bcoin/ldb.js b/lib/bcoin/ldb.js index c1840a96..591e798f 100644 --- a/lib/bcoin/ldb.js +++ b/lib/bcoin/ldb.js @@ -13,12 +13,6 @@ var db = {}; module.exports = function ldb(name, options) { var file = bcoin.prefix + '/' + name + '-' + network.type + '.db'; var backend = process.env.BCOIN_DB; - var promise; - - bcoin.ensurePrefix(); - - if (!options) - options = {}; if (!db[file]) { if (bcoin.isBrowser) { @@ -34,6 +28,11 @@ module.exports = function ldb(name, options) { backend = require(backend); } + if (!options) + options = {}; + + bcoin.ensurePrefix(); + db[file] = new LowlevelUp(file, { keyEncoding: 'ascii', valueEncoding: 'binary', diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index 16c43e67..4dc3c919 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -1056,11 +1056,11 @@ TX.prototype.toExtended = function toExtended(saveCoins) { p.writeUIntv(this.inputs.length); this.inputs.forEach(function(input) { if (!input.output) { - p.writeVarBytes(new Buffer([])); + p.writeUIntv(0); return; } - bcoin.protocol.framer.coin(input.output, false, p); + p.writeVarBytes(bcoin.protocol.framer.coin(input.output, false)); }); } @@ -1076,7 +1076,7 @@ TX._fromExtended = function _fromExtended(buf, saveCoins) { tx = bcoin.protocol.parser.parseTX(p); tx.height = p.readU32(); - tx.block = p.readHash().toString('hex'); + tx.block = p.readHash('hex'); tx.index = p.readU32(); tx.ts = p.readU32(); tx.ps = p.readU32(); @@ -1104,7 +1104,7 @@ TX._fromExtended = function _fromExtended(buf, saveCoins) { coin.hash = tx.inputs[i].prevout.hash; coin.index = tx.inputs[i].prevout.index; coin.spent = false; - tx.inputs[i].output = coin; + tx.inputs[i].output = new bcoin.coin(coin); } }