add cache to pubkeyring test. Speedup 2x

This commit is contained in:
Matias Alejo Garcia 2014-07-30 11:47:43 -03:00
parent c19eac6a4e
commit 4e6d572de0
1 changed files with 49 additions and 36 deletions

View File

@ -23,8 +23,7 @@ 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 || {};
@ -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];
@ -233,7 +234,9 @@ PublicKeyRing.prototype.getCosigner = function(pubKey) {
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,8 +258,7 @@ 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) {
@ -286,10 +288,21 @@ PublicKeyRing.prototype.getAddressesInfoForIndex = function(index, opts, copayer
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');
};