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

View File

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