refactor maps.

This commit is contained in:
Christopher Jeffrey 2016-03-29 19:20:10 -07:00
parent 126fb72644
commit 6655e57053
8 changed files with 62 additions and 120 deletions

View File

@ -136,23 +136,19 @@ Input.prototype.isCoinbase = function isCoinbase() {
return +this.prevout.hash === 0;
};
Input.prototype.test = function test(addressTable) {
Input.prototype.test = function test(addressMap) {
var address = this.getAddress();
if (!address)
return false;
if (typeof addressTable === 'string')
addressTable = [addressTable];
if (typeof addressMap === 'string')
return address === addressMap;
if (Array.isArray(addressTable)) {
addressTable = addressTable.reduce(function(out, address) {
out[address] = true;
return out;
}, {});
}
if (Array.isArray(addressMap))
return addressMap.indexOf(address) !== -1;
if (addressTable[address] != null)
if (addressMap[address] != null)
return true;
return false;

View File

@ -74,23 +74,19 @@ Output.prototype.getAddress = function getAddress() {
return address;
};
Output.prototype.test = function test(addressTable) {
Output.prototype.test = function test(addressMap) {
var address = this.getAddress();
if (!address)
return false;
if (typeof addressTable === 'string')
addressTable = [addressTable];
if (typeof addressMap === 'string')
return address === addressMap;
if (Array.isArray(addressTable)) {
addressTable = addressTable.reduce(function(out, address) {
out[address] = true;
return out;
}, {});
}
if (Array.isArray(addressMap))
return addressMap.indexOf(address) !== -1;
if (addressTable[address] != null)
if (addressMap[address] != null)
return true;
return false;

View File

@ -258,7 +258,7 @@ Peer.prototype.broadcast = function broadcast(items) {
packetType = 'block';
else if (type === constants.inv.tx)
packetType = 'tx';
else if (type === constants.inv.filtered)
else if (type === constants.inv.filteredblock)
packetType = 'merkleblock';
else
assert(false, 'Bad type.');

View File

@ -109,7 +109,7 @@ function Pool(node, options) {
bestHash: null,
type: !options.spv
? constants.inv.block
: constants.inv.filtered
: constants.inv.filteredblock
};
this.tx = {

View File

@ -5,6 +5,7 @@
*/
var bn = require('bn.js');
var utils = require('../utils');
exports.minVersion = 70001;
exports.version = 70012;
@ -25,22 +26,14 @@ exports.inv = {
error: 0,
tx: 1,
block: 2,
filtered: 3,
filteredblock: 3,
witnesstx: 1 | (1 << 30),
witnessblock: 2 | (1 << 30),
witnessfiltered: 3 | (1 << 30)
witnessfilteredblock: 3 | (1 << 30)
};
exports.invByVal = {
0: 'error',
1: 'tx',
2: 'block',
3: 'filtered'
};
exports.invByVal = utils.revMap(exports.inv);
exports.invByVal[1 | (1 << 30)] = 'witnesstx';
exports.invByVal[2 | (1 << 30)] = 'witnessblock';
exports.invByVal[3 | (1 << 30)] = 'witnessfiltered';
exports.invWitnessMask = 1 << 30;
exports.filterFlags = {
@ -184,13 +177,7 @@ exports.opcodes = {
OP_INVALIDOPCODE: 0xff
};
Object.freeze(exports.opcodes);
exports.opcodesByVal = new Array(256);
Object.keys(exports.opcodes).forEach(function(name) {
var val = exports.opcodes[name];
exports.opcodesByVal[val] = name;
});
exports.opcodesByVal = utils.revMap(exports.opcodes);
exports.coin = new bn(10000000).muln(10);
exports.cent = new bn(1000000);
@ -203,10 +190,7 @@ exports.hashType = {
anyonecanpay: 0x80
};
exports.hashTypeByVal = Object.keys(exports.hashType).reduce(function(out, type) {
out[exports.hashType[type]] = type;
return out;
}, {});
exports.hashTypeByVal = utils.revMap(exports.hashType);
exports.block = {
maxSize: 1000000,
@ -257,10 +241,7 @@ exports.reject = {
conflict: 0x102
};
exports.rejectByVal = Object.keys(exports.reject).reduce(function(out, name) {
out[exports.reject[name]] = name;
return out;
}, {});
exports.rejectByVal = utils.revMap(exports.reject);
exports.hd = {
hardened: 0x80000000,

View File

@ -46,15 +46,8 @@ main.address = {
}
};
main.address.prefixesByVal = Object.keys(main.address.prefixes).reduce(function(out, name) {
out[main.address.prefixes[name]] = name;
return out;
}, {});
main.address.versionsByVal = Object.keys(main.address.versions).reduce(function(out, name) {
out[main.address.versions[name]] = name;
return out;
}, {});
main.address.prefixesByVal = utils.revMap(main.address.prefixes);
main.address.versionsByVal = utils.revMap(main.address.versions);
main.type = 'main';
@ -188,15 +181,8 @@ testnet.address = {
}
};
testnet.address.prefixesByVal = Object.keys(testnet.address.prefixes).reduce(function(out, name) {
out[testnet.address.prefixes[name]] = name;
return out;
}, {});
testnet.address.versionsByVal = Object.keys(testnet.address.versions).reduce(function(out, name) {
out[testnet.address.versions[name]] = name;
return out;
}, {});
testnet.address.prefixesByVal = utils.revMap(testnet.address.prefixes);
testnet.address.versionsByVal = utils.revMap(testnet.address.versions);
testnet.seeds = [
'testnet-seed.alexykot.me',
@ -308,15 +294,8 @@ regtest.address = {
}
};
regtest.address.prefixesByVal = Object.keys(regtest.address.prefixes).reduce(function(out, name) {
out[regtest.address.prefixes[name]] = name;
return out;
}, {});
regtest.address.versionsByVal = Object.keys(regtest.address.versions).reduce(function(out, name) {
out[regtest.address.versions[name]] = name;
return out;
}, {});
regtest.address.prefixesByVal = utils.revMap(regtest.address.prefixes);
regtest.address.versionsByVal = utils.revMap(regtest.address.versions);
regtest.seeds = [
'127.0.0.1'
@ -411,15 +390,8 @@ segnet.address = {
}
};
segnet.address.prefixesByVal = Object.keys(segnet.address.prefixes).reduce(function(out, name) {
out[segnet.address.prefixes[name]] = name;
return out;
}, {});
segnet.address.versionsByVal = Object.keys(segnet.address.versions).reduce(function(out, name) {
out[segnet.address.versions[name]] = name;
return out;
}, {});
segnet.address.prefixesByVal = utils.revMap(segnet.address.prefixes);
segnet.address.versionsByVal = utils.revMap(segnet.address.versions);
segnet.seeds = [
'104.243.38.34',
@ -498,8 +470,8 @@ network.xprivkeys = {
'76066276': 'main',
'70615956': 'testnet',
'87393172': 'segnet',
xprv: 'main',
tprv: 'testnet',
'xprv': 'main',
'tprv': 'testnet',
'2791': 'segnet'
};
@ -507,7 +479,7 @@ network.xpubkeys = {
'76067358': 'main',
'70617039': 'testnet',
'87394255': 'segnet',
xpub: 'main',
tpub: 'testnet',
'xpub': 'main',
'tpub': 'testnet',
'2793': 'segnet'
};

View File

@ -530,64 +530,52 @@ TX.prototype.getAddresses = function getAddresses() {
return input;
};
TX.prototype.testInputs = function testInputs(addressTable, index) {
var i, input;
TX.prototype.testInputs = function testInputs(addressMap, index) {
var i;
if (typeof addressTable === 'string')
addressTable = [addressTable];
if (typeof addressMap === 'string')
addressMap = [addressMap];
if (Array.isArray(addressTable)) {
addressTable = addressTable.reduce(function(out, address) {
out[address] = true;
return out;
}, {});
}
if (Array.isArray(addressMap))
addressMap = utils.toMap(addressMap);
if (index && typeof index === 'object')
index = this.inputs.indexOf(index);
if (index != null)
assert(this.inputs[index]);
return this.inputs[index].test(addressMap);
for (i = 0; i < this.inputs.length; i++) {
if (index != null && i !== index)
continue;
input = this.inputs[i];
if (input.test(addressTable))
if (this.inputs[i].test(addressMap))
return true;
}
return false;
};
TX.prototype.testOutputs = function testOutputs(addressTable, index) {
var i, output;
TX.prototype.testOutputs = function testOutputs(addressMap, index) {
var i;
if (typeof addressTable === 'string')
addressTable = [addressTable];
if (typeof addressMap === 'string')
addressMap = [addressMap];
if (Array.isArray(addressTable)) {
addressTable = addressTable.reduce(function(out, address) {
out[address] = true;
return out;
}, {});
}
if (Array.isArray(addressMap))
addressMap = utils.toMap(addressMap);
if (index && typeof index === 'object')
index = this.outputs.indexOf(index);
if (index != null)
assert(this.outputs[index]);
return this.outputs[index].test(addressMap);
for (i = 0; i < this.outputs.length; i++) {
if (index != null && i !== index)
continue;
output = this.outputs[i];
if (output.test(addressTable))
if (this.outputs[i].test(addressMap))
return true;
}

View File

@ -1955,10 +1955,19 @@ utils.serial = function serial(stack, callback) {
};
utils.toMap = function toMap(arr) {
return arr.reduce(function(out, value) {
out[value] = true;
return out;
}, {});
var map = {};
arr.forEach(function(value) {
map[value] = true;
});
return map;
};
utils.revMap = function revMap(map) {
var reversed = {};
Object.keys(map).forEach(function(key) {
reversed[map[key]] = key;
});
return reversed;
};
if (utils.isBrowser) {