Merge pull request #774 from nodar-chkuaselidze/indexer-minor

Indexer getPrefix
This commit is contained in:
Braydon Fuller 2019-05-17 10:57:10 -07:00
commit 4c49b088f0
No known key found for this signature in database
GPG Key ID: F24F232D108B3AD4
3 changed files with 19 additions and 14 deletions

View File

@ -141,7 +141,7 @@ class AddrIndexer extends Indexer {
let hasAddress = false; let hasAddress = false;
for (const addr of tx.getAddresses(view)) { for (const addr of tx.getAddresses(view)) {
const prefix = addr.getPrefix(); const prefix = addr.getPrefix(this.network);
if (prefix < 0) if (prefix < 0)
continue; continue;
@ -178,7 +178,7 @@ class AddrIndexer extends Indexer {
let hasAddress = false; let hasAddress = false;
for (const addr of tx.getAddresses(view)) { for (const addr of tx.getAddresses(view)) {
const prefix = addr.getPrefix(); const prefix = addr.getPrefix(this.network);
if (prefix < 0) if (prefix < 0)
continue; continue;
@ -211,8 +211,6 @@ class AddrIndexer extends Indexer {
*/ */
async getHashesByAddress(addr, options = {}) { async getHashesByAddress(addr, options = {}) {
const txs = [];
const {after, reverse} = options; const {after, reverse} = options;
let {limit} = options; let {limit} = options;
@ -223,14 +221,14 @@ class AddrIndexer extends Indexer {
throw new Error('Limit above max of ${this.maxTxs}.'); throw new Error('Limit above max of ${this.maxTxs}.');
const hash = Address.getHash(addr); const hash = Address.getHash(addr);
const prefix = addr.getPrefix(); const prefix = addr.getPrefix(this.network);
const opts = { const opts = {
limit, limit,
reverse, reverse,
parse: (key) => { parse: (key) => {
const [,, height, index] = layout.A.decode(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); opts.lte = layout.A.max(prefix, hash);
} }
await this.db.keys(opts); const txs = await this.db.keys(opts);
const hashes = []; const hashes = [];
for (const [height, index] of txs) for (const [height, index] of txs)

View File

@ -20,9 +20,12 @@ class AddrIndexer {
/** /**
* Create TX address index. * Create TX address index.
* @constructor * @constructor
* @param {Network} network
*/ */
constructor() { constructor(network) {
this.network = network;
// Map of addr->entries. // Map of addr->entries.
this.index = new BufferMap(); this.index = new BufferMap();
@ -36,15 +39,20 @@ class AddrIndexer {
} }
getKey(addr) { getKey(addr) {
const prefix = addr.getPrefix(); const prefix = addr.getPrefix(this.network);
if (prefix < 0) if (prefix < 0)
return null; return null;
const raw = Buffer.allocUnsafe(1); const hash = addr.getHash();
raw.writeUInt8(prefix); 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;
} }
/** /**

View File

@ -73,7 +73,7 @@ class Mempool extends EventEmitter {
this.spents = new BufferMap(); this.spents = new BufferMap();
this.rejects = new RollingFilter(120000, 0.000001); this.rejects = new RollingFilter(120000, 0.000001);
this.addrindex = new AddrIndexer(); this.addrindex = new AddrIndexer(this.network);
} }
/** /**