refactor. fixes.

This commit is contained in:
Christopher Jeffrey 2016-03-20 10:55:37 -07:00
parent 6136434959
commit 0e15723acd
5 changed files with 41 additions and 33 deletions

View File

@ -10,6 +10,8 @@ var utils = require('./utils');
var assert = utils.assert; var assert = utils.assert;
var constants = bcoin.protocol.constants; var constants = bcoin.protocol.constants;
var network = bcoin.protocol.network; var network = bcoin.protocol.network;
var BufferWriter = require('./writer');
var BufferReader = require('./reader');
/** /**
* Block * Block

View File

@ -684,9 +684,9 @@ Chain.prototype._checkInputs = function _checkInputs(block, prev, flags, callbac
utils.debug('Signature Hash v1: %s', utils.debug('Signature Hash v1: %s',
utils.toHex(tx.signatureHash(j, input.output.script, 'all', 1))); utils.toHex(tx.signatureHash(j, input.output.script, 'all', 1)));
utils.debug('Raw Script: %s', utils.debug('Raw Script: %s',
utils.toHex(input.output.script._raw || [])); utils.toHex(input.output.script.encode()));
utils.debug('Reserialized Script: %s', 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) if (height < network.checkpoints.lastHeight)
throw new Error('BUG: Bad inputs in historical data!'); throw new Error('BUG: Bad inputs in historical data!');
return callback(null, false); return callback(null, false);
@ -903,7 +903,12 @@ Chain.prototype.reset = function reset(height, callback, force) {
callback = utils.ensure(callback); 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 // Reset the orphan map completely. There may
// have been some orphans on a forked chain we // have been some orphans on a forked chain we
// no longer need. // no longer need.
@ -911,14 +916,7 @@ Chain.prototype.reset = function reset(height, callback, force) {
self.purgePending(); self.purgePending();
unlock(); unlock();
callback(err, result); callback(null, result);
}
this.db.reset(height, function(err) {
if (err)
return done(err);
return done();
}); });
}; };
@ -1127,8 +1125,9 @@ Chain.prototype.add = function add(initial, peer, callback, force) {
} }
} }
// Update the block height // Update the block height early
// IMPORTANT!!!!! // Some things in verifyContext may
// need access to height on txs.
block.height = height; block.height = height;
block.txs.forEach(function(tx) { block.txs.forEach(function(tx) {
tx.height = height; tx.height = height;
@ -1140,6 +1139,15 @@ Chain.prototype.add = function add(initial, peer, callback, force) {
self._verifyContext(block, prev, function(err, verified) { self._verifyContext(block, prev, function(err, verified) {
var entry; 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) if (err)
return done(err); return done(err);
@ -1310,7 +1318,7 @@ Chain.prototype.has = function has(hash, callback) {
Chain.prototype.byTime = function byTime(ts, callback, force) { Chain.prototype.byTime = function byTime(ts, callback, force) {
var self = this; var self = this;
var start = 0; var start = 0;
var end = this.height + 1; var end = this.height;
var pos, delta; var pos, delta;
var unlock = this._lock(byTime, [ts, callback], force); var unlock = this._lock(byTime, [ts, callback], force);
@ -1346,7 +1354,7 @@ Chain.prototype.byTime = function byTime(ts, callback, force) {
if (start >= end) if (start >= end)
return done(); return done();
pos = (start + end) >> 1; pos = (start + end) >>> 1;
self.db.get(pos, function(err, entry) { self.db.get(pos, function(err, entry) {
if (err) if (err)
@ -1357,11 +1365,10 @@ Chain.prototype.byTime = function byTime(ts, callback, force) {
if (delta <= 60 * 60) if (delta <= 60 * 60)
return done(null, entry); return done(null, entry);
if (ts < entry.ts) { if (ts < entry.ts)
end = pos; end = pos - 1;
} else { else
start = pos + 1; start = pos + 1;
}
next(); next();
}); });

View File

@ -557,11 +557,11 @@ ChainDB.prototype.saveBlock = function saveBlock(block, batch, callback) {
if (this.options.spv) if (this.options.spv)
return callback(); 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) { block.txs.forEach(function(tx) {
// batch.put('t/t/' + tx.hash('hex'), tx.toExtended()); batch.put('t/t/' + tx.hash('hex'), tx.toExtended());
// }); });
this.connectBlock(block, batch, callback); this.connectBlock(block, batch, callback);
}; };

View File

@ -13,12 +13,6 @@ var db = {};
module.exports = function ldb(name, options) { module.exports = function ldb(name, options) {
var file = bcoin.prefix + '/' + name + '-' + network.type + '.db'; var file = bcoin.prefix + '/' + name + '-' + network.type + '.db';
var backend = process.env.BCOIN_DB; var backend = process.env.BCOIN_DB;
var promise;
bcoin.ensurePrefix();
if (!options)
options = {};
if (!db[file]) { if (!db[file]) {
if (bcoin.isBrowser) { if (bcoin.isBrowser) {
@ -34,6 +28,11 @@ module.exports = function ldb(name, options) {
backend = require(backend); backend = require(backend);
} }
if (!options)
options = {};
bcoin.ensurePrefix();
db[file] = new LowlevelUp(file, { db[file] = new LowlevelUp(file, {
keyEncoding: 'ascii', keyEncoding: 'ascii',
valueEncoding: 'binary', valueEncoding: 'binary',

View File

@ -1056,11 +1056,11 @@ TX.prototype.toExtended = function toExtended(saveCoins) {
p.writeUIntv(this.inputs.length); p.writeUIntv(this.inputs.length);
this.inputs.forEach(function(input) { this.inputs.forEach(function(input) {
if (!input.output) { if (!input.output) {
p.writeVarBytes(new Buffer([])); p.writeUIntv(0);
return; 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 = bcoin.protocol.parser.parseTX(p);
tx.height = p.readU32(); tx.height = p.readU32();
tx.block = p.readHash().toString('hex'); tx.block = p.readHash('hex');
tx.index = p.readU32(); tx.index = p.readU32();
tx.ts = p.readU32(); tx.ts = p.readU32();
tx.ps = p.readU32(); tx.ps = p.readU32();
@ -1104,7 +1104,7 @@ TX._fromExtended = function _fromExtended(buf, saveCoins) {
coin.hash = tx.inputs[i].prevout.hash; coin.hash = tx.inputs[i].prevout.hash;
coin.index = tx.inputs[i].prevout.index; coin.index = tx.inputs[i].prevout.index;
coin.spent = false; coin.spent = false;
tx.inputs[i].output = coin; tx.inputs[i].output = new bcoin.coin(coin);
} }
} }