fixes. refactor.

This commit is contained in:
Christopher Jeffrey 2016-03-29 18:08:33 -07:00
parent dffff280f7
commit 126fb72644
2 changed files with 35 additions and 30 deletions

View File

@ -45,7 +45,7 @@ TXPool.prototype._lock = function _lock(func, args, force) {
};
TXPool.prototype.getMap = function getMap(tx, callback) {
var input, output, addresses, map;
var input, output, addresses, table, map;
if (!this.options.indexAddress)
return callback();
@ -54,22 +54,25 @@ TXPool.prototype.getMap = function getMap(tx, callback) {
output = tx.getOutputAddresses();
addresses = utils.uniqs(input.concat(output));
function cb(err, map) {
function cb(err, table) {
if (err)
return callback(err);
map.input = [];
map.output = [];
map.all = [];
map = {
table: table,
input: [],
output: [],
all: []
};
input.forEach(function(address) {
assert(map[address]);
map.input = map.input.concat(map[address]);
assert(map.table[address]);
map.input = map.input.concat(map.table[address]);
});
output.forEach(function(address) {
assert(map[address]);
map.output = map.output.concat(map[address]);
assert(map.table[address]);
map.output = map.output.concat(map.table[address]);
});
map.input = utils.uniqs(map.input);
@ -80,11 +83,11 @@ TXPool.prototype.getMap = function getMap(tx, callback) {
}
if (!this.options.mapAddress) {
map = addresses.reduce(function(out, address) {
table = addresses.reduce(function(out, address) {
out[address] = [address];
return out;
}, {});
return cb(null, map);
return cb(null, table);
}
return this.mapAddresses(addresses, cb);
@ -93,7 +96,7 @@ TXPool.prototype.getMap = function getMap(tx, callback) {
TXPool.prototype.mapAddresses = function mapAddresses(address, callback) {
var self = this;
var prefix = this.prefix + '/';
var map = {};
var table = {};
var iter;
if (Array.isArray(address)) {
@ -103,7 +106,7 @@ TXPool.prototype.mapAddresses = function mapAddresses(address, callback) {
return next(err);
assert(res[address]);
map[address] = res[address];
table[address] = res[address];
next();
});
@ -111,7 +114,7 @@ TXPool.prototype.mapAddresses = function mapAddresses(address, callback) {
if (err)
return callback(err);
return callback(null, map);
return callback(null, table);
});
}
@ -126,7 +129,7 @@ TXPool.prototype.mapAddresses = function mapAddresses(address, callback) {
callback = utils.ensure(callback);
map[address] = [];
table[address] = [];
(function next() {
iter.next(function(err, key, value) {
@ -140,12 +143,12 @@ TXPool.prototype.mapAddresses = function mapAddresses(address, callback) {
return iter.end(function(err) {
if (err)
return callback(err);
return callback(null, map);
return callback(null, table);
});
}
key = key.split('/')[3];
map[address].push(key);
table[address].push(key);
next();
});
@ -307,7 +310,7 @@ TXPool.prototype._add = function add(tx, map, callback, force) {
// Only add orphans if this input is ours.
if (self.options.mapAddress) {
if (!address || !map[address].length)
if (!address || !map.table[address].length)
return next();
}
@ -330,7 +333,7 @@ TXPool.prototype._add = function add(tx, map, callback, force) {
updated = true;
if (self.options.indexAddress && address) {
map[address].forEach(function(id) {
map.table[address].forEach(function(id) {
batch.del(prefix + 'u/a/' + id + '/' + key);
});
}
@ -402,7 +405,7 @@ TXPool.prototype._add = function add(tx, map, callback, force) {
// Do not add unspents for outputs that aren't ours.
if (self.options.mapAddress) {
if (!address || !map[address].length)
if (!address || !map.table[address].length)
return next();
}
@ -465,7 +468,7 @@ TXPool.prototype._add = function add(tx, map, callback, force) {
if (!orphans) {
if (self.options.indexAddress && address) {
map[address].forEach(function(id) {
map.table[address].forEach(function(id) {
batch.put(
prefix + 'u/a/' + id
+ '/' + hash + '/' + i,
@ -631,7 +634,7 @@ TXPool.prototype._confirm = function _confirm(tx, map, callback, force) {
// Only update coins if this output is ours.
if (self.options.mapAddress) {
if (!address || !map[address].length)
if (!address || !map.table[address].length)
return next();
}
@ -775,12 +778,12 @@ TXPool.prototype._remove = function remove(tx, map, callback, force) {
return;
if (self.options.mapAddress) {
if (!address || !map[address].length)
if (!address || !map.table[address].length)
return;
}
if (self.options.indexAddress && address) {
map[address].forEach(function(id) {
map.table[address].forEach(function(id) {
batch.put(prefix + 'u/a/' + id
+ '/' + input.prevout.hash
+ '/' + input.prevout.index,
@ -804,12 +807,12 @@ TXPool.prototype._remove = function remove(tx, map, callback, force) {
var address = output.getAddress();
if (self.options.mapAddress) {
if (!address || !map[address].length)
if (!address || !map.table[address].length)
return;
}
if (self.options.indexAddress && address) {
map[address].forEach(function(id) {
map.table[address].forEach(function(id) {
batch.del(prefix + 'u/a/' + id + '/' + hash + '/' + i);
});
}

View File

@ -1,5 +1,6 @@
var assert = require('assert');
var bcoin = require('../');
var constants = bcoin.protocol.constants;
var utils = bcoin.utils;
describe('Protocol', function() {
@ -26,14 +27,14 @@ describe('Protocol', function() {
}
packetTest('version', {}, function(payload) {
assert.equal(payload.version, 70002);
assert.equal(payload.version, constants.version);
assert.equal(payload.agent, agent);
assert.equal(payload.height, 0);
assert.equal(payload.relay, false);
});
packetTest('version', { relay: true, height: 10 }, function(payload) {
assert.equal(payload.version, 70002);
assert.equal(payload.version, constants.version);
assert.equal(payload.agent, agent);
assert.equal(payload.height, 10);
assert.equal(payload.relay, true);
@ -65,6 +66,7 @@ describe('Protocol', function() {
});
addr._ipv6 = addr.ipv6;
addr.ipv6 = new Buffer(addr.ipv6.replace(/:/g, ''), 'hex');
addr.services = constants.localServices;
});
packetTest('addr', peers, function(payload) {
@ -72,13 +74,13 @@ describe('Protocol', function() {
assert.equal(payload.length, 2);
assert.equal(typeof payload[0].ts, 'number');
assert.equal(payload[0].services, bcoin.protocol.constants.bcoinServices);
assert.equal(payload[0].services, constants.localServices);
assert.equal(payload[0].ipv6, peers[0]._ipv6);
assert.equal(payload[0].ipv4, peers[0]._ipv4);
assert.equal(payload[0].port, peers[0].port);
assert.equal(typeof payload[1].ts, 'number');
assert.equal(payload[1].services, bcoin.protocol.constants.bcoinServices);
assert.equal(payload[1].services, constants.localServices);
assert.equal(payload[1].ipv6, peers[1]._ipv6);
assert.equal(payload[1].ipv4, peers[1]._ipv4);
assert.equal(payload[1].port, peers[1].port);