From 4e6d572de05150c38d8773cb2fb6bc1ce865c923 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Wed, 30 Jul 2014 11:47:43 -0300 Subject: [PATCH] add cache to pubkeyring test. Speedup 2x --- js/models/core/PublicKeyRing.js | 85 +++++++++++++++++++-------------- 1 file changed, 49 insertions(+), 36 deletions(-) diff --git a/js/models/core/PublicKeyRing.js b/js/models/core/PublicKeyRing.js index ad7fe6f15..2a25010a1 100644 --- a/js/models/core/PublicKeyRing.js +++ b/js/models/core/PublicKeyRing.js @@ -23,14 +23,13 @@ function PublicKeyRing(opts) { this.copayersHK = opts.copayersHK || []; - this.indexes = opts.indexes ? HDParams.fromList(opts.indexes) - : HDParams.init(this.totalCopayers); + this.indexes = opts.indexes ? HDParams.fromList(opts.indexes) : HDParams.init(this.totalCopayers); - this.publicKeysCache = opts.publicKeysCache || {}; - this.nicknameFor = opts.nicknameFor || {}; - this.copayerIds = []; - this.copayersBackup = opts.copayersBackup || []; - this.addressToPath = {}; + this.publicKeysCache = opts.publicKeysCache || {}; + this.nicknameFor = opts.nicknameFor || {}; + this.copayerIds = []; + this.copayersBackup = opts.copayersBackup || []; + this.addressToPath = {}; } PublicKeyRing.fromObj = function(data) { @@ -192,7 +191,9 @@ PublicKeyRing.prototype.getAddress = function(index, isChange, id) { // Overloaded to receive a PubkeyString or a consigner index PublicKeyRing.prototype.getHDParams = function(id) { var copayerIndex = this.getCosigner(id); - var index = this.indexes.filter(function(i) { return i.copayerIndex == copayerIndex }); + var index = this.indexes.filter(function(i) { + return i.copayerIndex == copayerIndex + }); if (index.length != 1) throw new Error('no index for copayerIndex'); return index[0]; @@ -231,9 +232,11 @@ PublicKeyRing.prototype.getCosigner = function(pubKey) { if (typeof pubKey == 'undefined') return HDPath.SHARED_INDEX; if (typeof pubKey == 'number') return pubKey; - var sorted = this.copayersHK.map(function(h, i){ + var sorted = this.copayersHK.map(function(h, i) { return h.eckey.public.toString('hex'); - }).sort(function(h1, h2){ return h1.localeCompare(h2); }); + }).sort(function(h1, h2) { + return h1.localeCompare(h2); + }); var index = sorted.indexOf(pubKey); if (index == -1) throw new Error('no public key in ring'); @@ -255,41 +258,51 @@ PublicKeyRing.prototype.getAddressesInfo = function(opts, pubkey) { PublicKeyRing.prototype.getAddressesInfoForIndex = function(index, opts, copayerIndex) { opts = opts || {}; - var isOwned = index.copayerIndex == HDPath.SHARED_INDEX - || index.copayerIndex == copayerIndex; + var isOwned = index.copayerIndex == HDPath.SHARED_INDEX || index.copayerIndex == copayerIndex; - var ret = []; - if (!opts.excludeChange) { - for (var i = 0; i < index.changeIndex; i++) { - var a = this.getAddress(i, true, index.copayerIndex); - ret.unshift({ - address: a, - addressStr: a.toString(), - isChange: true, - owned: isOwned - }); - } + var ret = []; + if (!opts.excludeChange) { + for (var i = 0; i < index.changeIndex; i++) { + var a = this.getAddress(i, true, index.copayerIndex); + ret.unshift({ + address: a, + addressStr: a.toString(), + isChange: true, + owned: isOwned + }); } + } - if (!opts.excludeMain) { - for (var i = 0; i < index.receiveIndex; i++) { - var a = this.getAddress(i, false, index.copayerIndex); - ret.unshift({ - address: a, - addressStr: a.toString(), - isChange: false, - owned: isOwned - }); - } + if (!opts.excludeMain) { + for (var i = 0; i < index.receiveIndex; i++) { + var a = this.getAddress(i, false, index.copayerIndex); + ret.unshift({ + address: a, + addressStr: a.toString(), + isChange: false, + owned: isOwned + }); } + } - return ret; + return ret; }; +PublicKeyRing.prototype.getForPaths = function(paths) { + return paths.map(this.getForPath.bind(this)); +}; + +PublicKeyRing.prototype.getForPath = function(path) { + var p = HDPath.indexesForPath(path); + var pubKeys = this.getPubKeys(p.addressIndex, p.isChange, p.copayerIndex); + return pubKeys; +}; + + // TODO this could be cached PublicKeyRing.prototype._addScriptMap = function(map, path) { - var p = HDPath.indicesForPath(path); - var script = this.getRedeemScript(p.index, p.isChange, p.copayerIndex); + var p = HDPath.indexesForPath(path); + var script = this.getRedeemScript(p.addressIndex, p.isChange, p.copayerIndex); map[Address.fromScript(script, this.network.name).toString()] = script.getBuffer().toString('hex'); };