From 0f6ef910b0f2e65d257dc8ea496c77e5900dec47 Mon Sep 17 00:00:00 2001 From: Nodar Chkuaselidze Date: Fri, 17 May 2019 20:23:40 +0400 Subject: [PATCH 1/2] addrindexer: use network for getPrefix. --- lib/indexer/addrindexer.js | 6 +++--- lib/mempool/addrindexer.js | 7 +++++-- lib/mempool/mempool.js | 2 +- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/lib/indexer/addrindexer.js b/lib/indexer/addrindexer.js index ce2478dd..d864be6b 100644 --- a/lib/indexer/addrindexer.js +++ b/lib/indexer/addrindexer.js @@ -141,7 +141,7 @@ class AddrIndexer extends Indexer { let hasAddress = false; for (const addr of tx.getAddresses(view)) { - const prefix = addr.getPrefix(); + const prefix = addr.getPrefix(this.network); if (prefix < 0) continue; @@ -178,7 +178,7 @@ class AddrIndexer extends Indexer { let hasAddress = false; for (const addr of tx.getAddresses(view)) { - const prefix = addr.getPrefix(); + const prefix = addr.getPrefix(this.network); if (prefix < 0) continue; @@ -223,7 +223,7 @@ class AddrIndexer extends Indexer { throw new Error('Limit above max of ${this.maxTxs}.'); const hash = Address.getHash(addr); - const prefix = addr.getPrefix(); + const prefix = addr.getPrefix(this.network); const opts = { limit, diff --git a/lib/mempool/addrindexer.js b/lib/mempool/addrindexer.js index e28353b6..f2d4b34f 100644 --- a/lib/mempool/addrindexer.js +++ b/lib/mempool/addrindexer.js @@ -20,9 +20,12 @@ class AddrIndexer { /** * Create TX address index. * @constructor + * @param {Network} network */ - constructor() { + constructor(network) { + this.network = network; + // Map of addr->entries. this.index = new BufferMap(); @@ -36,7 +39,7 @@ class AddrIndexer { } getKey(addr) { - const prefix = addr.getPrefix(); + const prefix = addr.getPrefix(this.network); if (prefix < 0) return null; diff --git a/lib/mempool/mempool.js b/lib/mempool/mempool.js index 5492a9c1..fbf3ab8a 100644 --- a/lib/mempool/mempool.js +++ b/lib/mempool/mempool.js @@ -73,7 +73,7 @@ class Mempool extends EventEmitter { this.spents = new BufferMap(); this.rejects = new RollingFilter(120000, 0.000001); - this.addrindex = new AddrIndexer(); + this.addrindex = new AddrIndexer(this.network); } /** From cdca51a8445e3a5c582fd58afc88aefb5637fff1 Mon Sep 17 00:00:00 2001 From: Nodar Chkuaselidze Date: Fri, 17 May 2019 20:35:21 +0400 Subject: [PATCH 2/2] 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 d864be6b..92531591 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 f2d4b34f..311dbd3e 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; } /**