addrindexer: index by address prefix

This commit is contained in:
Javed Khan 2019-03-28 16:58:51 +05:30 committed by Braydon Fuller
parent aa3f02d585
commit e2a6a92ebc
No known key found for this signature in database
GPG Key ID: F24F232D108B3AD4
2 changed files with 28 additions and 20 deletions

View File

@ -16,8 +16,10 @@ const Indexer = require('./indexer');
/* /*
* AddrIndexer Database Layout: * AddrIndexer Database Layout:
* A[addr-hash][height][index][hash] -> dummy (tx by address, height and index) * A[addr-prefix][addr-hash][height][index][hash] ->
* a[addr-hash][hash] -> (tx height and index by address and tx hash) * dummy (tx by address, height and index)
* a[addr-prefix][addr-hash][hash] ->
* (tx height and index by address and tx hash)
* *
* The database layout is organized so that transactions are sorted in * The database layout is organized so that transactions are sorted in
* the same order as the blocks (e.g. chronological order) using the block * the same order as the blocks (e.g. chronological order) using the block
@ -32,8 +34,8 @@ const Indexer = require('./indexer');
*/ */
Object.assign(layout, { Object.assign(layout, {
A: bdb.key('A', ['hash', 'uint32', 'uint32', 'hash256']), A: bdb.key('A', ['uint8', 'hash', 'uint32', 'uint32', 'hash256']),
a: bdb.key('a', ['hash', 'hash256']) a: bdb.key('a', ['uint8', 'hash', 'hash256'])
}); });
/** /**
@ -132,11 +134,13 @@ class AddrIndexer extends Indexer {
const tx = block.txs[i]; const tx = block.txs[i];
const hash = tx.hash(); const hash = tx.hash();
for (const addr of tx.getHashes(view)) { for (const addr of tx.getAddresses(view)) {
const prefix = addr.getPrefix();
const addrHash = addr.getHash();
const count = new Count(height, i); const count = new Count(height, i);
b.put(layout.A.encode(addr, height, i, hash), null); b.put(layout.A.encode(prefix, addrHash, height, i, hash), null);
b.put(layout.a.encode(addr, hash), count.toRaw()); b.put(layout.a.encode(prefix, addrHash, hash), count.toRaw());
} }
} }
@ -159,9 +163,11 @@ class AddrIndexer extends Indexer {
const tx = block.txs[i]; const tx = block.txs[i];
const hash = tx.hash(); const hash = tx.hash();
for (const addr of tx.getHashes(view)) { for (const addr of tx.getAddresses(view)) {
b.del(layout.A.encode(addr, height, i, hash)); const prefix = addr.getPrefix();
b.del(layout.a.encode(addr, hash)); const addrHash = addr.getHash();
b.del(layout.A.encode(prefix, addrHash, height, i, hash));
b.del(layout.a.encode(prefix, addrHash, hash));
} }
} }
@ -190,14 +196,15 @@ 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();
await this.db.keys({ await this.db.keys({
gte: layout.A.min(hash), gte: layout.A.min(prefix, hash),
lte: layout.A.max(hash), lte: layout.A.max(prefix, hash),
limit, limit,
reverse, reverse,
parse: (key) => { parse: (key) => {
const [,,, txid] = layout.A.decode(key); const [,,,, txid] = layout.A.decode(key);
set.add(txid); set.add(txid);
} }
}); });
@ -220,6 +227,7 @@ class AddrIndexer extends Indexer {
const set = new BufferSet(); const set = new BufferSet();
const hash = Address.getHash(addr); const hash = Address.getHash(addr);
const prefix = addr.getPrefix();
const {txid, reverse} = options; const {txid, reverse} = options;
let {limit} = options; let {limit} = options;
@ -230,7 +238,7 @@ class AddrIndexer extends Indexer {
if (limit > this.maxTxs) if (limit > this.maxTxs)
throw new Error('Limit above max of ${this.maxTxs}.'); throw new Error('Limit above max of ${this.maxTxs}.');
const raw = await this.db.get(layout.a.encode(hash, txid)); const raw = await this.db.get(layout.a.encode(prefix, hash, txid));
if (!raw) if (!raw)
return []; return [];
@ -242,17 +250,17 @@ class AddrIndexer extends Indexer {
limit, limit,
reverse, reverse,
parse: (key) => { parse: (key) => {
const [,,, txid] = layout.A.decode(key); const [,,,, txid] = layout.A.decode(key);
set.add(txid); set.add(txid);
} }
}; };
if (!reverse) { if (!reverse) {
opts.gt = layout.A.min(hash, height, index, txid); opts.gt = layout.A.min(prefix, hash, height, index, txid);
opts.lte = layout.A.max(hash); opts.lte = layout.A.max(prefix, hash);
} else { } else {
opts.gte = layout.A.min(hash); opts.gte = layout.A.min(prefix, hash);
opts.lt = layout.A.max(hash, height, index, txid); opts.lt = layout.A.max(prefix, hash, height, index, txid);
} }
await this.db.keys(opts); await this.db.keys(opts);

View File

@ -208,7 +208,7 @@ describe('Indexer', function() {
const vectors = [ const vectors = [
// Secret for the vectors: // Secret for the vectors:
// cVDJUtDjdaM25yNVVDLLX3hcHUfth4c7tY3rSc4hy9e8ibtCuj6G // cVDJUtDjdaM25yNVVDLLX3hcHUfth4c7tY3rSc4hy9e8ibtCuj6G
// {addr: 'bcrt1qngw83fg8dz0k749cg7k3emc7v98wy0c7azaa6h', amount: 19.99}, {addr: 'bcrt1qngw83fg8dz0k749cg7k3emc7v98wy0c7azaa6h', amount: 19.99},
{addr: 'muZpTpBYhxmRFuCjLc7C6BBDF32C8XVJUi', amount: 1.99} {addr: 'muZpTpBYhxmRFuCjLc7C6BBDF32C8XVJUi', amount: 1.99}
]; ];