Skip to content

Commit

Permalink
indexer: simplify addrindex query
Browse files Browse the repository at this point in the history
  • Loading branch information
braydonf committed Apr 15, 2019
1 parent 6e59b4f commit 0f23529
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 63 deletions.
81 changes: 22 additions & 59 deletions lib/indexer/addrindexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,18 +186,22 @@ 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}[].
*/

async getHashesByAddress(addr, options = {}) {
const txs = [];

const {reverse} = options;
const {after, reverse} = options;
let {limit} = options;

if (!limit)
Expand All @@ -209,74 +213,33 @@ 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),
const opts = {
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 [];
if (after) {
const raw = await this.db.get(layout.c.encode(after));
if (!raw)
return [];

const count = Count.fromRaw(raw);
const {height, index} = count;
const count = Count.fromRaw(raw);
const {height, index} = count;

const opts = {
limit,
reverse,
parse: (key) => {
const [,, height, index] = layout.A.decode(key);
txs.push([height, index]);
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);
}
};

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);
opts.lte = layout.A.max(prefix, hash);
}

await this.db.keys(opts);
Expand Down
8 changes: 4 additions & 4 deletions test/indexer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,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);

Expand All @@ -150,8 +150,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);

Expand Down

0 comments on commit 0f23529

Please sign in to comment.