diff --git a/lib/bcoin/txdb.js b/lib/bcoin/txdb.js index 98d7382d..ce76c119 100644 --- a/lib/bcoin/txdb.js +++ b/lib/bcoin/txdb.js @@ -1410,19 +1410,24 @@ TXDB.prototype.getCoinHashes = function getCoinHashes(account, callback) { */ TXDB.prototype.getHeightRangeHashes = function getHeightRangeHashes(account, options, callback) { + var start, end; + if (typeof account !== 'number') { callback = options; options = account; account = null; } + start = options.start || 0; + end = options.end || 0xffffffff; + this.iterate({ gte: account != null - ? layout.H(account, options.start, constants.NULL_HASH) - : layout.h(options.start, constants.NULL_HASH), + ? layout.H(account, start, constants.NULL_HASH) + : layout.h(start, constants.NULL_HASH), lte: account != null - ? layout.H(account, options.end, constants.HIGH_HASH) - : layout.h(options.end, constants.HIGH_HASH), + ? layout.H(account, end, constants.HIGH_HASH) + : layout.h(end, constants.HIGH_HASH), limit: options.limit, reverse: options.reverse, transform: function(key) { @@ -1458,18 +1463,23 @@ TXDB.prototype.getHeightHashes = function getHeightHashes(height, callback) { */ TXDB.prototype.getRangeHashes = function getRangeHashes(account, options, callback) { + var start, end; + if (typeof account === 'function') { callback = account; account = null; } + start = options.start || 0; + end = options.end || 0xffffffff; + this.iterate({ gte: account != null - ? layout.M(account, options.start, constants.NULL_HASH) - : layout.m(options.start, constants.NULL_HASH), + ? layout.M(account, start, constants.NULL_HASH) + : layout.m(start, constants.NULL_HASH), lte: account != null - ? layout.M(account, options.end, constants.HIGH_HASH) - : layout.m(options.end, constants.HIGH_HASH), + ? layout.M(account, end, constants.HIGH_HASH) + : layout.m(end, constants.HIGH_HASH), limit: options.limit, reverse: options.reverse, transform: function(key) { @@ -1487,8 +1497,8 @@ TXDB.prototype.getRangeHashes = function getRangeHashes(account, options, callba * Get transactions by timestamp range. * @param {Number?} account * @param {Object} options - * @param {Number} options.start - Start height. - * @param {Number} options.end - End height. + * @param {Number} options.start - Start time. + * @param {Number} options.end - End time. * @param {Number?} options.limit - Max number of records. * @param {Boolean?} options.reverse - Reverse order. * @param {Function} callback - Returns [Error, {@link TX}[]]. diff --git a/lib/bcoin/wallet.js b/lib/bcoin/wallet.js index 7eb2c13a..3178b2d3 100644 --- a/lib/bcoin/wallet.js +++ b/lib/bcoin/wallet.js @@ -1550,6 +1550,26 @@ Wallet.prototype.getLastTime = function getLastTime(account, callback) { }); }; +/** + * Get a range of transactions between two timestamps (accesses db). + * @param {(String|Number)?} account + * @param {Object} options + * @param {Number} options.start + * @param {Number} options.end + * @param {Function} callback - Returns [Error, {@link TX}[]]. + */ + +Wallet.prototype.getRange = function getRange(account, options, callback) { + if (typeof options === 'function') { + callback = options; + options = account; + account = null; + } + this._getIndex(account, callback, function(account, callback) { + this.tx.getRange(account, options, callback); + }); +}; + /** * Get the last N transactions (accesses db). * @param {(String|Number)?} account @@ -1568,26 +1588,6 @@ Wallet.prototype.getLast = function getLast(account, limit, callback) { }); }; -/** - * Get a range of transactions between two timestamps (accesses db). - * @param {(String|Number)?} account - * @param {Object} options - * @param {Number} options.start - * @param {Number} options.end - * @param {Function} callback - Returns [Error, {@link TX}[]]. - */ - -Wallet.prototype.getTimeRange = function getTimeRange(account, options, callback) { - if (typeof options === 'function') { - callback = options; - options = account; - account = null; - } - this._getIndex(account, callback, function(account, callback) { - this.tx.getTimeRange(account, options, callback); - }); -}; - /** * Zap stale TXs from wallet (accesses db). * @param {(Number|String)?} account diff --git a/test/wallet-test.js b/test/wallet-test.js index 36f60e02..317d1f86 100644 --- a/test/wallet-test.js +++ b/test/wallet-test.js @@ -57,6 +57,7 @@ describe('Wallet', function() { db: 'memory', verify: true }); + var lastW; it('should open walletdb', function(cb) { constants.tx.COINBASE_MATURITY = 0; @@ -818,6 +819,7 @@ describe('Wallet', function() { it('should fail to fill tx with account 1', function(cb) { walletdb.create({}, function(err, w1) { assert.ifError(err); + lastW = w1; w1.createAccount({ name: 'foo' }, function(err, acc) { assert.ifError(err); assert.equal(acc.name, 'foo'); @@ -860,6 +862,7 @@ describe('Wallet', function() { .addOutput(account.receiveAddress, 5460) .addOutput(account.receiveAddress, 5460); + t1.ps = 0xdeadbeef; t1.addInput(dummyInput); walletdb.addTX(t1, function(err) { @@ -1001,10 +1004,47 @@ describe('Wallet', function() { }); }); + it('should get range of txs', function(cb) { + var w1 = lastW; + w1.getRange({ start: 0xdeadbeef - 1000 }, function(err, txs) { + if (err) + return callback(err); + assert.equal(txs.length, 1); + cb(); + }); + }); + + it('should get range of txs from account', function(cb) { + var w1 = lastW; + w1.getRange('foo', { start: 0xdeadbeef - 1000 }, function(err, txs) { + if (err) + return callback(err); + assert.equal(txs.length, 1); + cb(); + }); + }); + + it('should not get range of txs from non-existent account', function(cb) { + var w1 = lastW; + w1.getRange('bad', { start: 0xdeadbeef - 1000 }, function(err, txs) { + assert(err); + assert.equal(err.message, 'Account not found.'); + cb(); + }); + }); + + it('should get account balance', function(cb) { + var w1 = lastW; + w1.getBalance('foo', function(err, balance) { + assert.ifError(err); + assert.equal(balance.total, 21840); + cb(); + }); + }); + it('should cleanup', function(cb) { walletdb.dump(function(err, records) { assert.ifError(err); - //utils.log(JSON.stringify(Object.keys(records), null, 2)); constants.tx.COINBASE_MATURITY = 100; cb(); });