Added rudimentry mempool.

This commit is contained in:
Chris Kleeschulte 2017-01-24 09:00:19 -05:00
parent 64c997817f
commit d4783dabde
3 changed files with 59 additions and 11 deletions

View File

@ -221,7 +221,7 @@ AddressService.prototype.blockHandler = function(block, connectBlock, callback)
return next();
}
self.node.services.transaction.getTransaction(input.prevTxId.toString('hex'), function(err, tx) {
self.node.services.transaction.getTransaction(input.prevTxId.toString('hex'), {}, function(err, tx) {
if(err) {
return next(err);
}
@ -1073,7 +1073,7 @@ AddressService.prototype.getAddressHistory = function(addresses, options, callba
});
}, function(err) {
async.mapLimit(txids, self.concurrency, function(txid, next) {
self.node.services.transaction.getTransaction(txid, function(err, tx) {
self.node.services.transaction.getTransaction(txid, options, function(err, tx) {
if(err) {
return next(err);
}

View File

@ -2082,6 +2082,15 @@ Bitcoin.prototype.getInfo = function(callback) {
});
};
Bitcoin.prototype.getMempool = function(callback) {
this.client.getRawMemPool(function(err, res) {
if(err) {
return callback(err);
}
callback(null, res.result);
});
};
Bitcoin.prototype.generateBlock = function(num, callback) {
var self = this;
this.client.generate(num, function(err, response) {

View File

@ -5,9 +5,18 @@ var BaseService = require('../service');
var inherits = require('util').inherits;
var bitcore = require('bitcore-lib');
/**
* The Transaction Service builds upon the Database Service and the Bitcoin Service to add additional
* functionality for getting information by bitcoin transaction hash/id. This includes the current
* bitcoin memory pool as validated by a trusted bitcoind instance.
* @param {Object} options
* @param {Node} options.node - An instance of the node
* @param {String} options.name - An optional name of the service
*/
function TransactionService(options) {
BaseService.call(this, options);
this.concurrency = options.concurrency || 20;
this._mempool = {};
this.currentTransactions = {};
}
@ -21,16 +30,40 @@ TransactionService.dependencies = [
TransactionService.prototype.start = function(callback) {
var self = this;
this.store = this.node.services.db.store;
self.store = this.node.services.db.store;
this.node.services.db.getPrefix(this.name, function(err, prefix) {
if(err) {
return callback(err);
}
var bus = self.node.openBus();
bus.subscribe('bitcoind/rawtransaction');
self.prefix = prefix;
bus.on('bitcoind/rawtransaction', function(txHex) {
var tx = new bitcore.Transaction(txHex);
self._mempool[tx.id] = tx;
});
callback();
async.series([
function(next) {
self.node.services.bitcoind.getMempool(function(err, txs) {
if(err) {
return next(err);
}
for(var i = 0; i < txs.length; i++) {
self._mempool[txs[i]] = true;
}
next();
});
}, function(next) {
self.node.services.db.getPrefix(self.name, function(err, prefix) {
if(err) {
return callback(err);
}
self.prefix = prefix;
next();
});
}], function(err) {
if(err) {
return callback(err);
}
callback();
});
};
@ -100,7 +133,7 @@ TransactionService.prototype._getInputValues = function(tx, callback) {
}
async.mapLimit(tx.inputs, this.concurrency, function(input, next) {
self.getTransaction(input.prevTxId.toString('hex'), function(err, prevTx) {
self.getTransaction(input.prevTxId.toString('hex'), {}, function(err, prevTx) {
if(err) {
return next(err);
}
@ -113,7 +146,7 @@ TransactionService.prototype._getInputValues = function(tx, callback) {
}, callback);
};
TransactionService.prototype.getTransaction = function(txid, callback) {
TransactionService.prototype.getTransaction = function(txid, options, callback) {
var self = this;
if(self.currentTransactions[txid]) {
@ -122,6 +155,12 @@ TransactionService.prototype.getTransaction = function(txid, callback) {
});
}
if (options.queryMempool && self._mempool[txid]) {
return setImmediate(function() {
callback(null, self._mempool[txid]);
});
}
var key = self._encodeTransactionKey(txid);
self.node.services.db.store.get(key, function(err, buffer) {