From cdca51a8445e3a5c582fd58afc88aefb5637fff1 Mon Sep 17 00:00:00 2001 From: Nodar Chkuaselidze Date: Fri, 17 May 2019 20:35:21 +0400 Subject: [PATCH] addrindexer: minor. * allocate one buffer istead of concat. * return results instead of mutating array. --- lib/indexer/addrindexer.js | 7 ++----- lib/mempool/addrindexer.js | 11 ++++++++--- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/lib/indexer/addrindexer.js b/lib/indexer/addrindexer.js index d864be6b9..925315915 100644 --- a/lib/indexer/addrindexer.js +++ b/lib/indexer/addrindexer.js @@ -211,8 +211,6 @@ class AddrIndexer extends Indexer { */ async getHashesByAddress(addr, options = {}) { - const txs = []; - const {after, reverse} = options; let {limit} = options; @@ -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]; } }; @@ -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) diff --git a/lib/mempool/addrindexer.js b/lib/mempool/addrindexer.js index f2d4b34f3..311dbd3e9 100644 --- a/lib/mempool/addrindexer.js +++ b/lib/mempool/addrindexer.js @@ -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; } /**