From d87e301986de65e1caa7e4c3dc3023f6eafb33d0 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sat, 2 Apr 2016 23:46:49 -0700 Subject: [PATCH] mempool. --- lib/bcoin/bst.js | 4 +- lib/bcoin/mempool.js | 135 +++++++++++++++++++++++++++++-------------- lib/bcoin/tx.js | 4 +- test/node-test.js | 2 +- 4 files changed, 97 insertions(+), 48 deletions(-) diff --git a/lib/bcoin/bst.js b/lib/bcoin/bst.js index 6b77c1ec..15cf39bd 100644 --- a/lib/bcoin/bst.js +++ b/lib/bcoin/bst.js @@ -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); }); }; diff --git a/lib/bcoin/mempool.js b/lib/bcoin/mempool.js index 06da3bbd..00455106 100644 --- a/lib/bcoin/mempool.js +++ b/lib/bcoin/mempool.js @@ -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); }); }; diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index f42318df..0e936c2e 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -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 diff --git a/test/node-test.js b/test/node-test.js index 6813bbe8..821b1fd4 100644 --- a/test/node-test.js +++ b/test/node-test.js @@ -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() {});