mempool: optimize coin view.

This commit is contained in:
Christopher Jeffrey 2016-12-08 01:57:26 -08:00
parent b2065cc84d
commit c86311029c
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 39 additions and 2 deletions

View File

@ -854,7 +854,7 @@ ChainDB.prototype.hasCoins = function hasCoins(hash) {
* @returns {Promise} - Returns {@link CoinView}.
*/
ChainDB.prototype.getCoinView = co(function* getCoinView(tx, callback) {
ChainDB.prototype.getCoinView = co(function* getCoinView(tx) {
var view = new CoinView();
var prevout = tx.getPrevout();
var i, hash, coins;

View File

@ -24,6 +24,8 @@ var Outpoint = require('../primitives/outpoint');
var TX = require('../primitives/tx');
var Coin = require('../primitives/coin');
var MempoolEntry = require('./mempoolentry');
var CoinView = require('../blockchain/coinview');
var Coins = require('../blockchain/coins');
/**
* Represents a mempool.
@ -1656,7 +1658,7 @@ Mempool.prototype.fillAllCoins = co(function* fillAllCoins(tx) {
* @returns {Promise} - Returns {@link CoinView}.
*/
Mempool.prototype.getCoinView = co(function* getCoinView(tx) {
Mempool.prototype.getCoinViewSlow = co(function* getCoinViewSlow(tx) {
var view = yield this.chain.db.getCoinView(tx);
var entries = view.toArray();
var i, coins, entry;
@ -1678,6 +1680,41 @@ Mempool.prototype.getCoinView = co(function* getCoinView(tx) {
return view;
});
/**
* Get coin viewpoint.
* @param {TX} tx
* @returns {Promise} - Returns {@link CoinView}.
*/
Mempool.prototype.getCoinView = co(function* getCoinView(tx) {
var view = new CoinView();
var prevout = tx.getPrevout();
var i, entry, hash, coins;
for (i = 0; i < prevout.length; i++) {
hash = prevout[i];
entry = this.getEntry(hash);
if (entry) {
view.addTX(entry.tx, -1);
continue;
}
coins = yield this.chain.db.getCoins(hash);
if (!coins) {
coins = new Coins();
coins.hash = hash;
view.add(coins);
continue;
}
view.add(coins);
}
return view;
});
/**
* Get a snapshot of all transaction hashes in the mempool. Used
* for generating INV packets in response to MEMPOOL packets.