refactor. fixes.
This commit is contained in:
parent
6136434959
commit
0e15723acd
@ -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
|
||||
|
||||
@ -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();
|
||||
});
|
||||
|
||||
@ -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);
|
||||
};
|
||||
|
||||
@ -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',
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user