mempool: refactor addr index.

This commit is contained in:
Christopher Jeffrey 2016-11-04 17:40:50 -07:00
parent 212c633d44
commit 6dfe8c59eb
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -1579,7 +1579,7 @@ Mempool.prototype.trackEntry = function trackEntry(entry) {
this.tx[hash] = entry; this.tx[hash] = entry;
if (this.options.indexAddress) if (this.options.indexAddress)
this.indexTX.addTX(tx); this.txIndex.addTX(tx);
assert(!tx.isCoinbase()); assert(!tx.isCoinbase());
@ -1784,30 +1784,12 @@ Mempool.prototype.getSize = function getSize() {
function AddressIndex(mempool) { function AddressIndex(mempool) {
this.mempool = mempool; this.mempool = mempool;
this.index = {};
this.map = {}; this.map = {};
} }
AddressIndex.prototype.getCoins = function getCoins(address) {
var items = this.map[address];
var out = [];
var i, item, outpoint, coin;
if (!items)
return out;
for (i = 0; i < items.length; i++) {
item = items[i];
outpoint = Outpoint.fromRaw(item);
coin = this.mempool.getCoin(outpoint.hash, outpoint.index);
assert(coin);
out.push(coin);
}
return out;
};
AddressIndex.prototype.getTX = function getTX(address) { AddressIndex.prototype.getTX = function getTX(address) {
var items = this.map[address]; var items = this.index[address];
var out = []; var out = [];
var i, hash, tx; var i, hash, tx;
@ -1831,11 +1813,11 @@ AddressIndex.prototype.addTX = function addTX(tx) {
for (i = 0; i < hashes.length; i++) { for (i = 0; i < hashes.length; i++) {
hash = hashes[i]; hash = hashes[i];
items = this.map[hash]; items = this.index[hash];
if (!items) { if (!items) {
items = []; items = [];
this.map[hash] = items; this.index[hash] = items;
} }
utils.binaryInsert(items, tx.hash(), utils.cmp); utils.binaryInsert(items, tx.hash(), utils.cmp);
@ -1854,7 +1836,7 @@ AddressIndex.prototype.removeTX = function removeTX(tx) {
for (i = 0; i < hashes.length; i++) { for (i = 0; i < hashes.length; i++) {
hash = hashes[i]; hash = hashes[i];
items = this.map[hash]; items = this.index[hash];
if (!items) if (!items)
continue; continue;
@ -1862,12 +1844,31 @@ AddressIndex.prototype.removeTX = function removeTX(tx) {
utils.binaryRemove(items, tx.hash(), utils.cmp); utils.binaryRemove(items, tx.hash(), utils.cmp);
if (items.length === 0) if (items.length === 0)
delete this.map[hash]; delete this.index[hash];
} }
delete this.map[key]; delete this.map[key];
}; };
AddressIndex.prototype.getCoins = function getCoins(address) {
var items = this.index[address];
var out = [];
var i, item, outpoint, coin;
if (!items)
return out;
for (i = 0; i < items.length; i++) {
item = items[i];
outpoint = Outpoint.fromRaw(item);
coin = this.mempool.getCoin(outpoint.hash, outpoint.index);
assert(coin);
out.push(coin);
}
return out;
};
AddressIndex.prototype.addCoin = function addCoin(coin) { AddressIndex.prototype.addCoin = function addCoin(coin) {
var key = coin.hash + coin.index; var key = coin.hash + coin.index;
var hash = coin.getHash('hex'); var hash = coin.getHash('hex');
@ -1876,11 +1877,11 @@ AddressIndex.prototype.addCoin = function addCoin(coin) {
if (!hash) if (!hash)
return; return;
items = this.map[hash]; items = this.index[hash];
if (!items) { if (!items) {
items = []; items = [];
this.map[hash] = items; this.index[hash] = items;
} }
outpoint = Outpoint(coin.hash, coin.index).toRaw(); outpoint = Outpoint(coin.hash, coin.index).toRaw();
@ -1897,7 +1898,7 @@ AddressIndex.prototype.removeCoin = function removeCoin(coin) {
if (!hash) if (!hash)
return; return;
items = this.map[hash]; items = this.index[hash];
if (!items) if (!items)
return; return;
@ -1906,7 +1907,7 @@ AddressIndex.prototype.removeCoin = function removeCoin(coin) {
utils.binaryRemove(items, outpoint, utils.cmp); utils.binaryRemove(items, outpoint, utils.cmp);
if (items.length === 0) if (items.length === 0)
delete this.map[hash]; delete this.index[hash];
delete this.map[key]; delete this.map[key];
}; };