indexer: simplify addrindex query

This commit is contained in:
Braydon Fuller 2019-04-15 15:56:05 -07:00
parent ebc40a58d0
commit 06ef0e3615
No known key found for this signature in database
GPG Key ID: F24F232D108B3AD4
2 changed files with 27 additions and 64 deletions

View File

@ -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);

View File

@ -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);