This commit is contained in:
Christopher Jeffrey 2016-04-02 23:46:49 -07:00
parent 9b4f00ccfe
commit d87e301986
4 changed files with 97 additions and 48 deletions

View File

@ -564,13 +564,15 @@ Iterator.prototype.next = function(callback) {
};
Iterator.prototype.seek = function seek(key) {
var self = this;
assert(!this.ended, 'Already ended.');
if (typeof key === 'string')
key = new Buffer(key, 'ascii');
this.index = binarySearch(this.items, key, true, function(a, b) {
return self.compare(a.key, b);
return self.tree.compare(a.key, b);
});
};

View File

@ -149,6 +149,7 @@ Mempool.prototype.destroy = function destroy(callback) {
Mempool.prototype.addBlock = function addBlock(block, callback, force) {
var self = this;
var txs = [];
var unlock = this._lock(addBlock, [block, callback], force);
if (!unlock)
return;
@ -156,20 +157,66 @@ Mempool.prototype.addBlock = function addBlock(block, callback, force) {
callback = utils.wrap(callback, unlock);
utils.forEachSerial(block.txs, function(tx, next) {
self.removeUnchecked(tx, next);
var hash = tx.hash('hex');
var copy;
self.getTX(hash, function(err, existing) {
if (err)
return callback(err);
if (!existing)
return self.removeOrphan(hash, next);
copy = tx.clone();
copy.ts = existing.ps;
copy.block = existing.block;
copy.height = existing.height;
copy.ps = existing.ps;
self.removeUnchecked(copy, function(err) {
if (err)
return next(err);
self.emit('confirmed', tx, block);
return next();
});
});
}, callback);
};
Mempool.prototype.removeBlock = function removeBlock(block, callback, force) {
var self = this;
var unlock = this._lock(removeBlock, [block, callback], force);
if (!unlock)
return;
callback = utils.wrap(callback, unlock);
utils.forEachSerial(block.txs.slice().reverse(), function(tx, next) {
self.addUnchecked(tx, next);
var copy;
self.hasTX(tx.hash('hex'), function(err, result) {
if (err)
return next(err);
if (result)
return next();
copy = tx.clone();
copy.ts = 0;
copy.block = null;
copy.height = -1;
copy.ps = utils.now();
self.addUnchecked(copy, function(err) {
if (err)
return next(err);
self.emit('unconfirmed', tx, block);
return next();
});
});
}, callback);
};
@ -179,7 +226,7 @@ Mempool.prototype.limitMempoolSize = function limitMempoolSize(callback) {
if (this.size <= Mempool.MAX_MEMPOOL_SIZE)
return callback(null, true);
this.db.getRange({
this.tx.getRange({
start: 0,
end: utils.now() - Mempool.MEMPOOL_EXPIRY
}, function(err, txs) {
@ -967,69 +1014,69 @@ Mempool.prototype.getConfidence = function getConfidence(hash, callback) {
callback = utils.asyncify(callback);
if (hash instanceof bcoin.tx) {
tx = hash;
hash = tx.hash('hex');
} else {
try {
tx = this.getTXSync(hash);
} catch (e) {
return callback(e);
}
}
function isDoubleSpend(callback) {
if (tx)
return self.isDoubleSpend(tx, callback);
return callback(null, false);
}
return isDoubleSpend(function(err, result) {
if (err)
return callback(err);
if (result)
return callback(null, constants.confidence.INCONFLICT);
return self.hasTX(hash, function(err, result) {
function done(tx, hash) {
return isDoubleSpend(function(err, result) {
if (err)
return callback(err);
if (result)
return callback(null, constants.confidence.PENDING);
return callback(null, constants.confidence.INCONFLICT);
function getBlock(callback) {
if (tx && tx.block)
return callback(null, tx.block);
return self.chain.db.getTX(hash, function(err, existing) {
if (err)
return callback(err);
if (!existing)
return callback();
return callback(null, existing.block);
});
}
return getBlock(function(err, block) {
return self.hasTX(hash, function(err, result) {
if (err)
return callback(err);
if (!block)
return callback(null, constants.confidence.UNKNOWN);
if (result)
return callback(null, constants.confidence.PENDING);
self.chain.db.isMainChain(block, function(err, result) {
function getBlock(callback) {
if (tx && tx.block)
return callback(null, tx.block);
return self.chain.db.getTX(hash, function(err, existing) {
if (err)
return callback(err);
if (!existing)
return callback();
return callback(null, existing.block);
});
}
return getBlock(function(err, block) {
if (err)
return callback(err);
if (result)
return callback(null, constants.confidence.BUILDING);
if (!block)
return callback(null, constants.confidence.UNKNOWN);
return callback(null, constants.confidence.DEAD);
self.chain.db.isMainChain(block, function(err, result) {
if (err)
return callback(err);
if (result)
return callback(null, constants.confidence.BUILDING);
return callback(null, constants.confidence.DEAD);
});
});
});
});
}
if (hash instanceof bcoin.tx)
return done(hash, hash.hash('hex'));
return this.getTX(hash, function(err, tx) {
if (err)
return callback(err);
done(tx, hash);
});
};

View File

@ -88,7 +88,7 @@ TX.prototype.clone = function clone() {
hash: this.inputs[i].prevout.hash,
index: this.inputs[i].prevout.index
},
coin: null,
coin: this.inputs[i].coin,
script: {
code: this.inputs[i].script.code.slice(),
raw: this.inputs[i].script.raw
@ -102,7 +102,7 @@ TX.prototype.clone = function clone() {
for (i = 0; i < this.outputs.length; i++) {
copy.outputs.push({
value: this.outputs[i].value.clone(),
value: this.outputs[i].value,
script: {
code: this.inputs[i].script.code.slice(),
raw: this.inputs[i].script.raw

View File

@ -6,7 +6,7 @@ var assert = utils.assert;
var opcodes = constants.opcodes;
describe('Wallet', function() {
process.env.BCOIN_DB = 'memdown';
process.env.BCOIN_DB = 'bst';
var node = new bcoin.fullnode();
node.on('error', function() {});