From fd3bd9fac9501a135643760daee4a8bc732864be Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Tue, 1 Mar 2016 15:18:14 -0800 Subject: [PATCH] walletdb --- lib/bcoin/walletdb.js | 178 ++++++------------------------------------ 1 file changed, 26 insertions(+), 152 deletions(-) diff --git a/lib/bcoin/walletdb.js b/lib/bcoin/walletdb.js index 674f6cff..de23686f 100644 --- a/lib/bcoin/walletdb.js +++ b/lib/bcoin/walletdb.js @@ -34,20 +34,12 @@ function WalletDB(node, options) { this.node = node; this.options = options; this.file = options.file; - this.dir = options.dir; - this.type = options.type; bcoin.ensurePrefix(); if (!this.file) this.file = bcoin.prefix + '/wallet-' + network.type + '.db'; - if (!this.dir) - this.dir = bcoin.prefix + '/wallet-' + network.type; - - if (!this.type) - this.type = 'leveldb'; - WalletDB.global = this; this._init(); @@ -60,56 +52,27 @@ WalletDB._db = {}; WalletDB.prototype._init = function _init() { var levelup; - if (this.type === 'file' && !bcoin.fs) { - this.type = 'leveldb'; - utils.debug('`fs` module not available. Falling back to leveldb.'); + if (!WalletDB._db[this.file]) { + // Some lazy loading + levelup = require('levelup'); + WalletDB._db[this.file] = new levelup(this.file, { + keyEncoding: 'ascii', + valueEncoding: 'binary', + createIfMissing: true, + errorIfExists: false, + compression: true, + cacheSize: 1 * 1024 * 1024, + writeBufferSize: 1 * 1024 * 1024, + // blockSize: 4 * 1024, + maxOpenFiles: 1024, + // blockRestartInterval: 16, + db: bcoin.isBrowser + ? require('level-js') + : require('level' + 'down') + }); } - if (this.type === 'file') { - if (bcoin.fs) { - try { - bcoin.fs.statSync(this.dir); - } catch (e) { - bcoin.fs.mkdirSync(this.dir, 0750); - } - } - if (+process.env.BCOIN_FRESH === 1) { - try { - bcoin.fs.readdirSync(this.dir).forEach(function(file) { - bcoin.fs.unlinkSync(this.dir + '/' + file); - }, this); - } catch (e) { - ; - } - } - return; - } - - if (this.type === 'leveldb') { - if (!WalletDB._db[this.file]) { - // Some lazy loading - levelup = require('levelup'); - WalletDB._db[this.file] = new levelup(this.file, { - keyEncoding: 'ascii', - valueEncoding: 'utf8', - createIfMissing: true, - errorIfExists: false, - compression: true, - cacheSize: 1 * 1024 * 1024, - writeBufferSize: 1 * 1024 * 1024, - // blockSize: 4 * 1024, - maxOpenFiles: 1024, - // blockRestartInterval: 16, - db: bcoin.isBrowser - ? require('level-js') - : require('level' + 'down') - }); - } - this.db = WalletDB._db[this.file]; - return; - } - - throw new Error('Unknown storage type: ' + this.type); + this.db = WalletDB._db[this.file]; }; WalletDB.prototype.getJSON = function getJSON(id, callback) { @@ -118,13 +81,7 @@ WalletDB.prototype.getJSON = function getJSON(id, callback) { callback = utils.ensure(callback); - if (this.type === 'leveldb') - return this._getDB(id, callback); - - if (this.type === 'file') - return this._getFile(id, callback); - - throw new Error('Unknown storage type: ' + this.type); + return this._getDB(id, callback); }; WalletDB.prototype.saveJSON = function saveJSON(id, json, callback) { @@ -152,7 +109,7 @@ WalletDB.prototype.saveJSON = function saveJSON(id, json, callback) { if (json && self.type === 'leveldb') { batch = self.db.batch(); Object.keys(json.addressMap).forEach(function(address) { - batch.put('w/a/' + address + '/' + json.id, ''); + batch.put('w/a/' + address + '/' + json.id, new Buffer([])); }); return batch.write(function(err) { if (err) @@ -164,13 +121,7 @@ WalletDB.prototype.saveJSON = function saveJSON(id, json, callback) { return callback(null, json); } - if (this.type === 'leveldb') - return this._saveDB(id, json, cb); - - if (this.type === 'file') - return this._saveFile(id, json, cb); - - throw new Error('Unknown storage type: ' + this.type); + return this._saveDB(id, json, cb); }; WalletDB.prototype.removeJSON = function removeJSON(id, callback) { @@ -202,13 +153,7 @@ WalletDB.prototype.removeJSON = function removeJSON(id, callback) { return callback(null, json); } - if (this.type === 'leveldb') - return this._removeDB(id, cb); - - if (this.type === 'file') - return this._removeFile(id, cb); - - throw new Error('Unknown storage type: ' + this.type); + return this._removeDB(id, cb); }; WalletDB.prototype.createJSON = function createJSON(id, options, callback) { @@ -225,31 +170,6 @@ WalletDB.prototype.createJSON = function createJSON(id, options, callback) { }); }; -WalletDB.prototype._getFile = function _getFile(id, callback) { - var self = this; - var file; - - callback = utils.ensure(callback); - - file = this.dir + '/' + id + '.json'; - - fs.readFile(file, 'utf8', function(err, json) { - if (err && err.code === 'ENOENT') - return callback(); - - if (err) - return callback(err); - - try { - json = JSON.parse(json); - } catch (e) { - return callback(e); - } - - return callback(null, json); - }); -}; - WalletDB.prototype._getDB = function _getDB(id, callback) { var self = this; var key; @@ -266,7 +186,7 @@ WalletDB.prototype._getDB = function _getDB(id, callback) { return callback(err); try { - json = JSON.parse(json); + json = JSON.parse(json.toString('utf8')); } catch (e) { return callback(e); } @@ -281,7 +201,7 @@ WalletDB.prototype._saveDB = function _saveDB(id, json, callback) { callback = utils.ensure(callback); - data = JSON.stringify(json); + data = new Buffer(JSON.stringify(json), 'utf8'); this.db.put(key, data, function(err) { if (err) @@ -291,27 +211,6 @@ WalletDB.prototype._saveDB = function _saveDB(id, json, callback) { }); }; -WalletDB.prototype._saveFile = function _saveFile(id, json, callback) { - var file = this.dir + '/' + id + '.json'; - var options, data; - - callback = utils.ensure(callback); - - data = JSON.stringify(json, null, 2); - - options = { - encoding: 'utf8', - mode: 0600 - }; - - fs.writeFile(file, data, options, function(err) { - if (err) - return callback(err); - - return callback(null, json); - }); -}; - WalletDB.prototype._removeDB = function _removeDB(id, callback) { var self = this; var key = 'w/w/' + id; @@ -331,24 +230,6 @@ WalletDB.prototype._removeDB = function _removeDB(id, callback) { }); }; -WalletDB.prototype._removeFile = function _removeFile(id, callback) { - var file = this.dir + '/' + id + '.json'; - - callback = utils.ensure(callback); - - this._getFile(id, function(err, json) { - if (err) - return callback(err); - - fs.unlink(file, function(err) { - if (err && err.code !== 'ENOENT') - return callback(err); - - return callback(null, json); - }); - }); -}; - WalletDB.prototype.get = function get(id, passphrase, callback) { if (typeof passphrase === 'function') { callback = passphrase; @@ -424,15 +305,11 @@ WalletDB.prototype.create = function create(options, callback) { WalletDB.prototype.saveAddress = function saveAddress(id, address, callback) { callback = utils.ensure(callback); - if (this.type !== 'leveldb') - return utils.nextTick(callback); - this.db.put('w/a/' + address + '/' + id, '', callback); + this.db.put('w/a/' + address + '/' + id, new Buffer([]), callback); }; WalletDB.prototype.removeAddress = function removeAddress(id, address, callback) { callback = utils.ensure(callback); - if (this.type !== 'leveldb') - return utils.nextTick(callback); this.db.del('w/a/' + address + '/' + id, callback); }; @@ -477,9 +354,6 @@ WalletDB.prototype._getIDs = function _getIDs(address, callback) { WalletDB.prototype.test = function test(addresses, callback) { var self = this; - if (this.type !== 'leveldb') - return utils.nextTick(callback); - utils.forEachSerial(addresses, function(address, next) { self._getIDs(address, function(err, ids) { if (err)