Skip to content

Commit

Permalink
addrindexer: minor.
Browse files Browse the repository at this point in the history
* allocate one buffer istead of concat.
* return results instead of mutating array.
  • Loading branch information
nodech committed May 17, 2019
1 parent 0f6ef91 commit cdca51a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
7 changes: 2 additions & 5 deletions lib/indexer/addrindexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,6 @@ class AddrIndexer extends Indexer {
*/

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

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

Expand All @@ -230,7 +228,7 @@ class AddrIndexer extends Indexer {
reverse,
parse: (key) => {
const [,, height, index] = layout.A.decode(key);
txs.push([height, index]);
return [height, index];
}
};

Expand Down Expand Up @@ -265,8 +263,7 @@ class AddrIndexer extends Indexer {
opts.lte = layout.A.max(prefix, hash);
}

await this.db.keys(opts);

const txs = await this.db.keys(opts);
const hashes = [];

for (const [height, index] of txs)
Expand Down
11 changes: 8 additions & 3 deletions lib/mempool/addrindexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,15 @@ class AddrIndexer {
if (prefix < 0)
return null;

const raw = Buffer.allocUnsafe(1);
raw.writeUInt8(prefix);
const hash = addr.getHash();
const size = hash.length + 1;
const raw = Buffer.allocUnsafe(size);

return Buffer.concat([raw, addr.getHash()]);
let written = raw.writeUInt8(prefix);
written += hash.copy(raw, 1);
assert(written === size);

return raw;
}

/**
Expand Down

0 comments on commit cdca51a

Please sign in to comment.