Browse Source

remove hdkeycache

patch-2
Gabe Gattis 8 years ago
parent
commit
a9aeada69e
No known key found for this signature in database GPG Key ID: 441430987182732C
  1. 1
      index.js
  2. 45
      lib/hdkeycache.js
  3. 8
      lib/hdprivatekey.js
  4. 7
      lib/hdpublickey.js
  5. 52
      test/hdkeycache.js
  6. 7
      test/hdpublickey.js

1
index.js

@ -66,5 +66,4 @@ bitcore.deps.elliptic = require('elliptic');
bitcore.deps._ = require('lodash');
// Internal usage, exposed for testing/advanced tweaking
bitcore._HDKeyCache = require('./lib/hdkeycache');
bitcore.Transaction.sighash = require('./lib/transaction/sighash');

45
lib/hdkeycache.js

@ -1,45 +0,0 @@
'use strict';
module.exports = {
_cache: {},
_count: 0,
_eraseIndex: 0,
_usedList: {},
_usedIndex: {},
_CACHE_SIZE: 5000,
get: function(xkey, number, hardened, nonCompliant) {
hardened = !!hardened;
var key = (nonCompliant ? '1' : '0') + xkey + '/' + number + '/' + hardened;
if (this._cache[key]) {
this._cacheHit(key);
return this._cache[key];
}
},
set: function(xkey, number, hardened, derived, nonCompliant) {
hardened = !!hardened;
var key = (nonCompliant ? '1' : '0') + xkey + '/' + number + '/' + hardened;
this._cache[key] = derived;
this._cacheHit(key);
},
_cacheHit: function(key) {
if (this._usedIndex[key]) {
delete this._usedList[this._usedIndex[key]];
}
this._usedList[this._count] = key;
this._usedIndex[key] = this._count;
this._count++;
this._cacheRemove();
},
_cacheRemove: function() {
while (this._eraseIndex < this._count - this._CACHE_SIZE) {
if (this._usedList[this._eraseIndex]) {
var removeKey = this._usedList[this._eraseIndex];
delete this._usedIndex[removeKey];
delete this._cache[removeKey];
}
delete this._usedList[this._eraseIndex];
this._eraseIndex++;
}
}
};

8
lib/hdprivatekey.js

@ -11,7 +11,6 @@ var Base58 = require('./encoding/base58');
var Base58Check = require('./encoding/base58check');
var Hash = require('./crypto/hash');
var Network = require('./networks');
var HDKeyCache = require('./hdkeycache');
var Point = require('./crypto/point');
var PrivateKey = require('./privatekey');
var Random = require('./crypto/random');
@ -200,11 +199,6 @@ HDPrivateKey.prototype._deriveWithNumber = function(index, hardened, nonComplian
index += HDPrivateKey.Hardened;
}
var cached = HDKeyCache.get(this.xprivkey, index, hardened, nonCompliant);
if (cached) {
return cached;
}
var indexBuffer = BufferUtil.integerAsBuffer(index);
var data;
if (hardened && nonCompliant) {
@ -243,7 +237,7 @@ HDPrivateKey.prototype._deriveWithNumber = function(index, hardened, nonComplian
chainCode: chainCode,
privateKey: privateKey
});
HDKeyCache.set(this.xprivkey, index, hardened, derived, nonCompliant);
return derived;
};

7
lib/hdpublickey.js

@ -8,7 +8,6 @@ var Base58 = require('./encoding/base58');
var Base58Check = require('./encoding/base58check');
var Hash = require('./crypto/hash');
var HDPrivateKey = require('./hdprivatekey');
var HDKeyCache = require('./hdkeycache');
var Network = require('./networks');
var Point = require('./crypto/point');
var PublicKey = require('./publickey');
@ -130,10 +129,6 @@ HDPublicKey.prototype._deriveWithNumber = function(index, hardened) {
if (index < 0) {
throw new hdErrors.InvalidPath(index);
}
var cached = HDKeyCache.get(this.xpubkey, index, false);
if (cached) {
return cached;
}
var indexBuffer = BufferUtil.integerAsBuffer(index);
var data = BufferUtil.concat([this.publicKey.toBuffer(), indexBuffer]);
@ -156,7 +151,7 @@ HDPublicKey.prototype._deriveWithNumber = function(index, hardened) {
chainCode: chainCode,
publicKey: publicKey
});
HDKeyCache.set(this.xpubkey, index, false, derived);
return derived;
};

52
test/hdkeycache.js

@ -1,52 +0,0 @@
'use strict';
var _ = require('lodash');
var expect = require('chai').expect;
var bitcore = require('..');
var HDPrivateKey = bitcore.HDPrivateKey;
var xprivkey = 'xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi';
describe('HDKey cache', function() {
this.timeout(10000);
/* jshint unused: false */
var cache = bitcore._HDKeyCache;
var master = new HDPrivateKey(xprivkey);
beforeEach(function() {
cache._cache = {};
cache._count = 0;
cache._eraseIndex = 0;
cache._usedIndex = {};
cache._usedList = {};
cache._CACHE_SIZE = 3; // Reduce for quick testing
});
it('saves a derived key', function() {
var child = master.deriveChild(0);
expect(cache._cache['0' + master.xprivkey + '/0/false'].xprivkey).to.equal(child.xprivkey);
});
it('starts erasing unused keys', function() {
var child1 = master.deriveChild(0);
var child2 = child1.deriveChild(0);
var child3 = child2.deriveChild(0);
expect(cache._cache['0' + master.xprivkey + '/0/false'].xprivkey).to.equal(child1.xprivkey);
var child4 = child3.deriveChild(0);
expect(cache._cache['0' + master.xprivkey + '/0/false']).to.equal(undefined);
});
it('avoids erasing keys that get cache hits ("hot keys")', function() {
var child1 = master.deriveChild(0);
var child2 = master.deriveChild(0).deriveChild(0);
expect(cache._cache['0' + master.xprivkey + '/0/false'].xprivkey).to.equal(child1.xprivkey);
var child1_copy = master.deriveChild(0);
expect(cache._cache['0' + master.xprivkey + '/0/false'].xprivkey).to.equal(child1.xprivkey);
});
it('keeps the size of the cache small', function() {
var child1 = master.deriveChild(0);
var child2 = child1.deriveChild(0);
var child3 = child2.deriveChild(0);
var child4 = child3.deriveChild(0);
expect(_.size(cache._cache)).to.equal(3);
});
});

7
test/hdpublickey.js

@ -271,12 +271,5 @@ describe('HDPublicKey interface', function() {
valid = HDPublicKey.isValidPath(HDPublicKey.Hardened);
valid.should.equal(false);
});
it('should use the cache', function() {
var pubkey = new HDPublicKey(xpubkey);
var derived1 = pubkey.deriveChild(0);
var derived2 = pubkey.deriveChild(0);
derived1.should.equal(derived2);
});
});
});

Loading…
Cancel
Save