From 6655e57053d757c63280640a0e1315d99f6a881d Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Tue, 29 Mar 2016 19:20:10 -0700 Subject: [PATCH] refactor maps. --- lib/bcoin/input.js | 16 ++++------ lib/bcoin/output.js | 16 ++++------ lib/bcoin/peer.js | 2 +- lib/bcoin/pool.js | 2 +- lib/bcoin/protocol/constants.js | 33 +++++---------------- lib/bcoin/protocol/network.js | 52 ++++++++------------------------- lib/bcoin/tx.js | 44 ++++++++++------------------ lib/bcoin/utils.js | 17 ++++++++--- 8 files changed, 62 insertions(+), 120 deletions(-) diff --git a/lib/bcoin/input.js b/lib/bcoin/input.js index 2ab08238..db791e16 100644 --- a/lib/bcoin/input.js +++ b/lib/bcoin/input.js @@ -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; diff --git a/lib/bcoin/output.js b/lib/bcoin/output.js index de62379e..6a7ca59a 100644 --- a/lib/bcoin/output.js +++ b/lib/bcoin/output.js @@ -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; diff --git a/lib/bcoin/peer.js b/lib/bcoin/peer.js index c3ec7bef..7ff83e79 100644 --- a/lib/bcoin/peer.js +++ b/lib/bcoin/peer.js @@ -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.'); diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index d066d83c..87f0c895 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -109,7 +109,7 @@ function Pool(node, options) { bestHash: null, type: !options.spv ? constants.inv.block - : constants.inv.filtered + : constants.inv.filteredblock }; this.tx = { diff --git a/lib/bcoin/protocol/constants.js b/lib/bcoin/protocol/constants.js index 5154d435..ab6515b6 100644 --- a/lib/bcoin/protocol/constants.js +++ b/lib/bcoin/protocol/constants.js @@ -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, diff --git a/lib/bcoin/protocol/network.js b/lib/bcoin/protocol/network.js index 7aca7c7b..7507fe7e 100644 --- a/lib/bcoin/protocol/network.js +++ b/lib/bcoin/protocol/network.js @@ -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' }; diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index 1272d303..0d041030 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -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; } diff --git a/lib/bcoin/utils.js b/lib/bcoin/utils.js index 03bce93f..a64069ed 100644 --- a/lib/bcoin/utils.js +++ b/lib/bcoin/utils.js @@ -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) {