indexer: cleanup and check pruned and index options

This commit is contained in:
Braydon Fuller 2019-04-23 13:42:21 -07:00
parent 5d18f9ba2e
commit cede31d86f
No known key found for this signature in database
GPG Key ID: F24F232D108B3AD4
5 changed files with 43 additions and 38 deletions

View File

@ -2687,8 +2687,6 @@ class ChainOptions {
this.bip91 = false;
this.bip148 = false;
this.prune = false;
this.indexTX = false;
this.indexAddress = false;
this.forceFlags = false;
this.entryCache = 5000;
@ -2766,16 +2764,6 @@ class ChainOptions {
this.compression = options.compression;
}
if (options.indexTX != null) {
assert(typeof options.indexTX === 'boolean');
this.indexTX = options.indexTX;
}
if (options.indexAddress != null) {
assert(typeof options.indexAddress === 'boolean');
this.indexAddress = options.indexAddress;
}
if (options.prune != null) {
assert(typeof options.prune === 'boolean');
this.prune = options.prune;

View File

@ -571,12 +571,6 @@ class ChainDB {
if (!options.prune && flags.prune)
throw new Error('Cannot retroactively unprune.');
if (options.prune && options.indexTX && !flags.indexTX)
throw new Error('Cannot retroactively enable TX indexing.');
if (options.prune && options.indexAddress && !flags.indexAddress)
throw new Error('Cannot retroactively enable address indexing.');
if (needsSave) {
await this.logger.info('Rewriting chain flags.');
await this.saveFlags();
@ -1801,8 +1795,6 @@ class ChainFlags {
this.bip91 = false;
this.bip148 = false;
this.prune = false;
this.indexTX = false;
this.indexAddress = false;
if (options)
this.fromOptions(options);
@ -1831,16 +1823,6 @@ class ChainFlags {
this.prune = options.prune;
}
if (options.indexTX != null) {
assert(typeof options.indexTX === 'boolean');
this.indexTX = options.indexTX;
}
if (options.indexAddress != null) {
assert(typeof options.indexAddress === 'boolean');
this.indexAddress = options.indexAddress;
}
return this;
}
@ -1862,12 +1844,6 @@ class ChainFlags {
if (this.prune)
flags |= 1 << 2;
if (this.indexTX)
flags |= 1 << 3;
if (this.indexAddress)
flags |= 1 << 4;
if (this.bip91)
flags |= 1 << 5;
@ -1891,8 +1867,6 @@ class ChainFlags {
this.spv = (flags & 1) !== 0;
this.witness = (flags & 2) !== 0;
this.prune = (flags & 4) !== 0;
this.indexTX = (flags & 8) !== 0;
this.indexAddress = (flags & 16) !== 0;
this.bip91 = (flags & 32) !== 0;
this.bip148 = (flags & 64) !== 0;

View File

@ -612,6 +612,7 @@ class IndexOptions {
'Indexer requires a blockstore.');
assert(options.chain && typeof options.chain === 'object',
'Indexer requires chain.');
assert(!options.prune, 'Can not index while pruned.');
this.blocks = options.blocks;
this.chain = options.chain;

View File

@ -164,6 +164,7 @@ class FullNode extends Node {
logger: this.logger,
blocks: this.blocks,
chain: this.chain,
prune: this.config.bool('prune'),
memory: this.config.bool('memory'),
prefix: this.config.filter('index').str('prefix') || this.config.prefix
});
@ -175,6 +176,7 @@ class FullNode extends Node {
logger: this.logger,
blocks: this.blocks,
chain: this.chain,
prune: this.config.bool('prune'),
memory: this.config.bool('memory'),
prefix: this.config.filter('index').str('prefix') || this.config.prefix,
maxTxs: this.config.uint('max-txs')

View File

@ -17,6 +17,7 @@ const TXIndexer = require('../lib/indexer/txindexer');
const AddrIndexer = require('../lib/indexer/addrindexer');
const BlockStore = require('../lib/blockstore/level');
const FullNode = require('../lib/node/fullnode');
const SPVNode = require('../lib/node/spvnode');
const Network = require('../lib/protocol/network');
const network = Network.get('regtest');
const {NodeClient, WalletClient} = require('bclient');
@ -639,6 +640,45 @@ describe('Indexer', function() {
await node.close();
}
});
it('will not index if pruned', async () => {
let err = null;
try {
new FullNode({
prefix: prefix,
network: 'regtest',
apiKey: 'foo',
memory: false,
prune: true,
indexTX: true,
indexAddress: true,
port: ports.p2p,
httpPort: ports.node
});
} catch (e) {
err = e;
}
assert(err);
assert.equal(err.message, 'Can not index while pruned.');
});
it('will not index if spv', async () => {
const node = new SPVNode({
prefix: prefix,
network: 'regtest',
apiKey: 'foo',
memory: false,
indexTX: true,
indexAddress: true,
port: ports.p2p,
httpPort: ports.node
});
assert.equal(node.txindex, undefined);
assert.equal(node.addrindex, undefined);
});
});
});