From 06ef0e3615c434456436230ad388225ee2524684 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Mon, 15 Apr 2019 15:56:05 -0700 Subject: [PATCH] indexer: simplify addrindex query --- lib/indexer/addrindexer.js | 83 +++++++++++--------------------------- test/indexer-test.js | 8 ++-- 2 files changed, 27 insertions(+), 64 deletions(-) diff --git a/lib/indexer/addrindexer.js b/lib/indexer/addrindexer.js index 7b91a420..b6112226 100644 --- a/lib/indexer/addrindexer.js +++ b/lib/indexer/addrindexer.js @@ -186,10 +186,14 @@ class AddrIndexer extends Indexer { } /** - * Get all transaction hashes to an address. + * Get transaction hashes to an address in ascending or descending + * order. If the `after` argument is supplied, results will be given + * _after_ that transaction hash. The default order is ascending from + * oldest to latest. * @param {Address} addr * @param {Object} options - * @param {Boolean} options.limit + * @param {Buffer} options.after - A transaction hash + * @param {Number} options.limit * @param {Boolean} options.reverse * @returns {Promise} - Returns {@link Hash}[]. */ @@ -197,7 +201,7 @@ class AddrIndexer extends Indexer { async getHashesByAddress(addr, options = {}) { const txs = []; - const {reverse} = options; + const {after, reverse} = options; let {limit} = options; if (!limit) @@ -209,59 +213,6 @@ class AddrIndexer extends Indexer { const hash = Address.getHash(addr); const prefix = addr.getPrefix(); - await this.db.keys({ - gte: layout.A.min(prefix, hash), - lte: layout.A.max(prefix, hash), - limit, - reverse, - parse: (key) => { - const [,, height, index] = layout.A.decode(key); - txs.push([height, index]); - } - }); - - const hashes = []; - - for (const [height, index] of txs) - hashes.push(await this.db.get(layout.C.encode(height, index))); - - return hashes; - } - - /** - * Get all transaction hashes to an address after - * a specific txid. - * @param {Address} addr - * @param {Object} options - * @param {Buffer} options.txid - * @param {Boolean} options.limit - * @param {Boolean} options.reverse - * @returns {Promise} - Returns {@link Hash}[]. - */ - - async getHashesByAddressAfter(addr, options = {}) { - const txs = []; - - const hash = Address.getHash(addr); - const prefix = addr.getPrefix(); - - const {txid, reverse} = options; - let {limit} = options; - - if (!limit) - limit = this.maxTxs; - - if (limit > this.maxTxs) - throw new Error('Limit above max of ${this.maxTxs}.'); - - const raw = await this.db.get(layout.c.encode(txid)); - - if (!raw) - return []; - - const count = Count.fromRaw(raw); - const {height, index} = count; - const opts = { limit, reverse, @@ -271,12 +222,24 @@ class AddrIndexer extends Indexer { } }; - if (!reverse) { - opts.gt = layout.A.min(prefix, hash, height, index); - opts.lte = layout.A.max(prefix, hash); + if (after) { + const raw = await this.db.get(layout.c.encode(after)); + if (!raw) + return []; + + const count = Count.fromRaw(raw); + const {height, index} = count; + + if (!reverse) { + opts.gt = layout.A.min(prefix, hash, height, index); + opts.lte = layout.A.max(prefix, hash); + } else { + opts.gte = layout.A.min(prefix, hash); + opts.lt = layout.A.max(prefix, hash, height, index); + } } else { opts.gte = layout.A.min(prefix, hash); - opts.lt = layout.A.max(prefix, hash, height, index); + opts.lte = layout.A.max(prefix, hash); } await this.db.keys(opts); diff --git a/test/indexer-test.js b/test/indexer-test.js index 839bc3cd..86b9cb9f 100644 --- a/test/indexer-test.js +++ b/test/indexer-test.js @@ -129,8 +129,8 @@ describe('Indexer', function() { const txid = hashes[4]; - const next = await addrindexer.getHashesByAddressAfter( - addr, {txid: txid, limit: 5}); + const next = await addrindexer.getHashesByAddress( + addr, {after: txid, limit: 5}); assert.strictEqual(next.length, 5); @@ -149,8 +149,8 @@ describe('Indexer', function() { const txid = hashes[4]; - const next = await addrindexer.getHashesByAddressAfter( - addr, {txid: txid, limit: 5, reverse: true}); + const next = await addrindexer.getHashesByAddress( + addr, {after: txid, limit: 5, reverse: true}); assert.strictEqual(next.length, 5);