From a9aeada69e1ae335b1282d34700b7d5f9f3376ec Mon Sep 17 00:00:00 2001 From: Gabe Gattis Date: Tue, 6 Dec 2016 18:23:35 -0500 Subject: [PATCH] remove hdkeycache --- index.js | 3 +-- lib/hdkeycache.js | 45 --------------------------------------- lib/hdprivatekey.js | 8 +------ lib/hdpublickey.js | 7 +----- test/hdkeycache.js | 52 --------------------------------------------- test/hdpublickey.js | 7 ------ 6 files changed, 3 insertions(+), 119 deletions(-) delete mode 100644 lib/hdkeycache.js delete mode 100644 test/hdkeycache.js diff --git a/index.js b/index.js index 9aa78d9..eaa6107 100644 --- a/index.js +++ b/index.js @@ -6,7 +6,7 @@ var bitcore = module.exports; bitcore.version = 'v' + require('./package.json').version; bitcore.versionGuard = function(version) { if (version !== undefined) { - var message = 'More than one instance of bitcore-lib found. ' + + var message = 'More than one instance of bitcore-lib found. ' + 'Please make sure to require bitcore-lib and check that submodules do' + ' not also include their own bitcore-lib dependency.'; throw new Error(message); @@ -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'); diff --git a/lib/hdkeycache.js b/lib/hdkeycache.js deleted file mode 100644 index 606e00c..0000000 --- a/lib/hdkeycache.js +++ /dev/null @@ -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++; - } - } -}; diff --git a/lib/hdprivatekey.js b/lib/hdprivatekey.js index f8e560b..8c4e2bb 100644 --- a/lib/hdprivatekey.js +++ b/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; }; diff --git a/lib/hdpublickey.js b/lib/hdpublickey.js index d28a82b..ee083b6 100644 --- a/lib/hdpublickey.js +++ b/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; }; diff --git a/test/hdkeycache.js b/test/hdkeycache.js deleted file mode 100644 index 35a5167..0000000 --- a/test/hdkeycache.js +++ /dev/null @@ -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); - }); -}); diff --git a/test/hdpublickey.js b/test/hdpublickey.js index 19f86b3..457f1bd 100644 --- a/test/hdpublickey.js +++ b/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); - }); }); });