db: remove backend option.

This commit is contained in:
Christopher Jeffrey 2017-12-06 17:05:00 -08:00
parent bad028ab67
commit 417b37b0c3
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
15 changed files with 70 additions and 73 deletions

View File

@ -28,8 +28,7 @@ const node = new FullNode({
logFile: true, logFile: true,
logConsole: true, logConsole: true,
logLevel: 'debug', logLevel: 'debug',
db: 'leveldb', memory: false,
persistent: true,
workers: true, workers: true,
listen: true, listen: true,
loader: require loader: require

View File

@ -2655,7 +2655,7 @@ class ChainOptions {
this.prefix = null; this.prefix = null;
this.location = null; this.location = null;
this.db = 'memory'; this.memory = true;
this.maxFiles = 64; this.maxFiles = 64;
this.cacheSize = 32 << 20; this.cacheSize = 32 << 20;
this.compression = true; this.compression = true;
@ -2716,9 +2716,9 @@ class ChainOptions {
this.location = options.location; this.location = options.location;
} }
if (options.db != null) { if (options.memory != null) {
assert(typeof options.db === 'string'); assert(typeof options.memory === 'boolean');
this.db = options.db; this.memory = options.memory;
} }
if (options.maxFiles != null) { if (options.maxFiles != null) {

View File

@ -1970,7 +1970,7 @@ class MempoolOptions {
this.prefix = null; this.prefix = null;
this.location = null; this.location = null;
this.db = 'memory'; this.memory = true;
this.maxFiles = 64; this.maxFiles = 64;
this.cacheSize = 32 << 20; this.cacheSize = 32 << 20;
this.compression = true; this.compression = true;
@ -2091,9 +2091,9 @@ class MempoolOptions {
this.location = options.location; this.location = options.location;
} }
if (options.db != null) { if (options.memory != null) {
assert(typeof options.db === 'string'); assert(typeof options.memory === 'boolean');
this.db = options.db; this.memory = options.memory;
} }
if (options.maxFiles != null) { if (options.maxFiles != null) {
@ -2505,7 +2505,7 @@ class MempoolCache {
async init(hash) { async init(hash) {
const batch = this.db.batch(); const batch = this.db.batch();
batch.put(layout.v.build(), encoding.u32(MempoolCache.VERSION)); batch.put(layout.v.build(), fromU32(MempoolCache.VERSION));
batch.put(layout.R.build(), Buffer.from(hash, 'hex')); batch.put(layout.R.build(), Buffer.from(hash, 'hex'));
await batch.write(); await batch.write();
} }
@ -2557,7 +2557,7 @@ class MempoolCache {
for (const key of keys) for (const key of keys)
batch.del(key); batch.del(key);
batch.put(layout.v.build(), encoding.u32(MempoolCache.VERSION)); batch.put(layout.v.build(), fromU32(MempoolCache.VERSION));
batch.put(layout.R.build(), Buffer.from(this.chain.tip.hash, 'hex')); batch.put(layout.R.build(), Buffer.from(this.chain.tip.hash, 'hex'));
batch.del(layout.F.build()); batch.del(layout.F.build());
@ -2629,6 +2629,12 @@ function useDesc(a) {
return y > x; return y > x;
} }
function fromU32(num) {
const data = Buffer.allocUnsafe(4);
data.writeUInt32LE(num, 0, true);
return data;
}
/* /*
* Expose * Expose
*/ */

View File

@ -131,7 +131,7 @@ class HostList {
*/ */
start() { start() {
if (!this.options.persistent) if (this.options.memory)
return; return;
if (!this.options.filename) if (!this.options.filename)
@ -146,7 +146,7 @@ class HostList {
*/ */
stop() { stop() {
if (!this.options.persistent) if (this.options.memory)
return; return;
if (!this.options.filename) if (!this.options.filename)
@ -194,7 +194,7 @@ class HostList {
if (fs.unsupported) if (fs.unsupported)
return; return;
if (!this.options.persistent) if (this.options.memory)
return; return;
if (!filename) if (!filename)
@ -226,7 +226,7 @@ class HostList {
if (fs.unsupported) if (fs.unsupported)
return; return;
if (!this.options.persistent) if (this.options.memory)
return; return;
if (!filename) if (!filename)
@ -1496,7 +1496,7 @@ class HostListOptions {
this.prefix = null; this.prefix = null;
this.filename = null; this.filename = null;
this.persistent = false; this.memory = true;
this.flushInterval = 120000; this.flushInterval = 120000;
if (options) if (options)
@ -1590,9 +1590,9 @@ class HostListOptions {
this.maxEntries = options.maxEntries; this.maxEntries = options.maxEntries;
} }
if (options.persistent != null) { if (options.memory != null) {
assert(typeof options.persistent === 'boolean'); assert(typeof options.memory === 'boolean');
this.persistent = options.persistent; this.memory = options.memory;
} }
if (options.prefix != null) { if (options.prefix != null) {

View File

@ -3635,7 +3635,7 @@ class PoolOptions {
this.blockMode = 0; this.blockMode = 0;
this.services = common.LOCAL_SERVICES; this.services = common.LOCAL_SERVICES;
this.requiredServices = common.REQUIRED_SERVICES; this.requiredServices = common.REQUIRED_SERVICES;
this.persistent = false; this.memory = true;
this.fromOptions(options); this.fromOptions(options);
} }
@ -3866,9 +3866,9 @@ class PoolOptions {
this.blockMode = options.blockMode; this.blockMode = options.blockMode;
} }
if (options.persistent != null) { if (options.memory != null) {
assert(typeof options.persistent === 'boolean'); assert(typeof options.memory === 'boolean');
this.persistent = options.persistent; this.memory = options.memory;
} }
if (this.spv) { if (this.spv) {

View File

@ -45,7 +45,7 @@ class FullNode extends Node {
network: this.network, network: this.network,
logger: this.logger, logger: this.logger,
workers: this.workers, workers: this.workers,
db: this.config.str('db'), memory: this.config.bool('memory'),
prefix: this.config.prefix, prefix: this.config.prefix,
maxFiles: this.config.uint('max-files'), maxFiles: this.config.uint('max-files'),
cacheSize: this.config.mb('cache-size'), cacheSize: this.config.mb('cache-size'),
@ -71,7 +71,7 @@ class FullNode extends Node {
workers: this.workers, workers: this.workers,
chain: this.chain, chain: this.chain,
fees: this.fees, fees: this.fees,
db: this.config.str('db'), memory: this.config.bool('memory'),
prefix: this.config.prefix, prefix: this.config.prefix,
persistent: this.config.bool('persistent-mempool'), persistent: this.config.bool('persistent-mempool'),
maxSize: this.config.mb('mempool-size'), maxSize: this.config.mb('mempool-size'),
@ -110,7 +110,7 @@ class FullNode extends Node {
host: this.config.str('host'), host: this.config.str('host'),
port: this.config.uint('port'), port: this.config.uint('port'),
listen: this.config.bool('listen'), listen: this.config.bool('listen'),
persistent: this.config.bool('persistent') memory: this.config.bool('memory')
}); });
// Miner needs access to the chain and mempool. // Miner needs access to the chain and mempool.

View File

@ -47,6 +47,7 @@ class Node extends EventEmitter {
this.config.open(config); this.config.open(config);
this.network = Network.get(this.config.getSuffix()); this.network = Network.get(this.config.getSuffix());
this.memory = this.config.bool('memory', true);
this.startTime = -1; this.startTime = -1;
this.bound = []; this.bound = [];
this.plugins = Object.create(null); this.plugins = Object.create(null);
@ -81,7 +82,7 @@ class Node extends EventEmitter {
logger = config.obj('logger'); logger = config.obj('logger');
logger.set({ logger.set({
filename: config.bool('log-file') filename: !this.memory && config.bool('log-file')
? config.location(file) ? config.location(file)
: null, : null,
level: config.str('log-level'), level: config.str('log-level'),
@ -131,6 +132,9 @@ class Node extends EventEmitter {
if (fs.unsupported) if (fs.unsupported)
return undefined; return undefined;
if (this.memory)
return undefined;
return fs.mkdirp(this.config.prefix); return fs.mkdirp(this.config.prefix);
} }

View File

@ -45,8 +45,8 @@ class SPVNode extends Node {
this.chain = new Chain({ this.chain = new Chain({
network: this.network, network: this.network,
logger: this.logger, logger: this.logger,
db: this.config.str('db'),
prefix: this.config.prefix, prefix: this.config.prefix,
memory: this.config.bool('memory'),
maxFiles: this.config.uint('max-files'), maxFiles: this.config.uint('max-files'),
cacheSize: this.config.mb('cache-size'), cacheSize: this.config.mb('cache-size'),
entryCache: this.config.uint('entry-cache'), entryCache: this.config.uint('entry-cache'),
@ -73,7 +73,7 @@ class SPVNode extends Node {
identityKey: this.config.buf('identity-key'), identityKey: this.config.buf('identity-key'),
maxOutbound: this.config.uint('max-outbound'), maxOutbound: this.config.uint('max-outbound'),
createSocket: this.config.func('create-socket'), createSocket: this.config.func('create-socket'),
persistent: this.config.bool('persistent'), memory: this.config.bool('memory'),
selfish: true, selfish: true,
listen: false listen: false
}); });

View File

@ -47,7 +47,7 @@ class Plugin extends EventEmitter {
workers: node.workers, workers: node.workers,
client: this.client, client: this.client,
prefix: config.prefix, prefix: config.prefix,
db: config.str(['wallet-db', 'db']), memory: config.bool(['wallet-memory', 'memory']),
maxFiles: config.uint('wallet-max-files'), maxFiles: config.uint('wallet-max-files'),
cacheSize: config.mb('wallet-cache-size'), cacheSize: config.mb('wallet-cache-size'),
witness: config.bool('wallet-witness'), witness: config.bool('wallet-witness'),

View File

@ -45,7 +45,7 @@ class WalletNode extends Node {
workers: this.workers, workers: this.workers,
client: this.client, client: this.client,
prefix: this.config.prefix, prefix: this.config.prefix,
db: this.config.str('db'), memory: this.config.bool('memory'),
maxFiles: this.config.uint('max-files'), maxFiles: this.config.uint('max-files'),
cacheSize: this.config.mb('cache-size'), cacheSize: this.config.mb('cache-size'),
witness: this.config.bool('witness'), witness: this.config.bool('witness'),

View File

@ -239,7 +239,7 @@ class WalletDB extends EventEmitter {
this.unregister(wallet); this.unregister(wallet);
} }
await this.db.close(); return this.db.close();
} }
/** /**
@ -1891,7 +1891,8 @@ class WalletDB extends EventEmitter {
return 0; return 0;
} }
this.logger.debug('Adding block: %d.', entry.height); if (tip.height >= this.network.block.slowHeight)
this.logger.debug('Adding block: %d.', tip.height);
if (tip.height === this.state.height) { if (tip.height === this.state.height) {
// We let blocks of the same height // We let blocks of the same height
@ -2137,7 +2138,7 @@ class WalletOptions {
this.prefix = null; this.prefix = null;
this.location = null; this.location = null;
this.db = 'memory'; this.memory = true;
this.maxFiles = 64; this.maxFiles = 64;
this.cacheSize = 16 << 20; this.cacheSize = 16 << 20;
this.compression = true; this.compression = true;
@ -2186,7 +2187,7 @@ class WalletOptions {
if (options.prefix != null) { if (options.prefix != null) {
assert(typeof options.prefix === 'string'); assert(typeof options.prefix === 'string');
this.prefix = options.prefix; this.prefix = options.prefix;
this.location = path.join(this.prefix, 'walletdb'); this.location = path.join(this.prefix, 'wallet');
} }
if (options.location != null) { if (options.location != null) {
@ -2194,9 +2195,9 @@ class WalletOptions {
this.location = options.location; this.location = options.location;
} }
if (options.db != null) { if (options.memory != null) {
assert(typeof options.db === 'string'); assert(typeof options.memory === 'boolean');
this.db = options.db; this.memory = options.memory;
} }
if (options.maxFiles != null) { if (options.maxFiles != null) {

View File

@ -29,7 +29,6 @@ const Block = require('../lib/primitives/block');
const LRU = require('../lib/utils/lru'); const LRU = require('../lib/utils/lru');
const consensus = require('../lib/protocol/consensus'); const consensus = require('../lib/protocol/consensus');
const file = process.argv[2].replace(/\.ldb\/?$/, '');
const shouldPrune = process.argv.indexOf('--prune') !== -1; const shouldPrune = process.argv.indexOf('--prune') !== -1;
let hasIndex = false; let hasIndex = false;
@ -37,8 +36,7 @@ let hasPruned = false;
let hasSPV = false; let hasSPV = false;
const db = bdb.create({ const db = bdb.create({
location: file, location: process.argv[2],
db: 'leveldb',
compression: true, compression: true,
cacheSize: 32 << 20, cacheSize: 32 << 20,
createIfMissing: false createIfMissing: false
@ -77,12 +75,12 @@ async function readJournal() {
if (!data) if (!data)
return [STATE_VERSION, consensus.NULL_HASH]; return [STATE_VERSION, consensus.NULL_HASH];
if (data[0] !== MIGRATION_ID)
throw new Error('Bad migration id.');
if (data.length !== 34) if (data.length !== 34)
throw new Error('Bad migration length.'); throw new Error('Bad migration length.');
if (data[0] !== MIGRATION_ID)
throw new Error('Bad migration id.');
const state = data.readUInt8(1, true); const state = data.readUInt8(1, true);
const hash = data.toString('hex', 2, 34); const hash = data.toString('hex', 2, 34);
@ -125,6 +123,7 @@ async function updateVersion() {
async function reserializeUndo(hash) { async function reserializeUndo(hash) {
let tip = await getTip(); let tip = await getTip();
const height = tip.height; const height = tip.height;
if (hash !== consensus.NULL_HASH) if (hash !== consensus.NULL_HASH)
@ -643,7 +642,7 @@ reserializeEntries;
(async () => { (async () => {
await db.open(); await db.open();
console.log('Opened %s.', file); console.log('Opened %s.', process.argv[2]);
if (await isSPV()) if (await isSPV())
hasSPV = true; hasSPV = true;
@ -690,7 +689,7 @@ reserializeEntries;
assert(state === STATE_DONE); assert(state === STATE_DONE);
console.log('Closing %s.', file); console.log('Closing %s.', process.argv[2]);
await db.close(); await db.close();

View File

@ -9,16 +9,12 @@ const layout = require('../lib/blockchain/layout');
// deployment table v->D // deployment table v->D
// C/T key format // C/T key format
let file = process.argv[2]; assert(process.argv.length > 2, 'Please pass in a database path.');
let parent = null; let parent = null;
assert(typeof file === 'string', 'Please pass in a database path.');
file = file.replace(/\.ldb\/?$/, '');
const db = bdb.create({ const db = bdb.create({
location: file, location: process.argv[2],
db: 'leveldb',
compression: true, compression: true,
cacheSize: 32 << 20, cacheSize: 32 << 20,
createIfMissing: false createIfMissing: false
@ -144,7 +140,7 @@ function parseC(key) {
(async () => { (async () => {
await db.open(); await db.open();
console.log('Opened %s.', file); console.log('Opened %s.', process.argv[2]);
parent = db.batch(); parent = db.batch();

View File

@ -4,23 +4,19 @@ const assert = require('assert');
const bdb = require('bdb'); const bdb = require('bdb');
const bio = require('bufio'); const bio = require('bufio');
let file = process.argv[2]; assert(process.argv.length > 2, 'Please pass in a database path.');
let batch; let batch;
assert(typeof file === 'string', 'Please pass in a database path.');
file = file.replace(/\.ldb\/?$/, '');
const db = bdb.create({ const db = bdb.create({
location: file, location: process.argv[2],
db: 'leveldb',
compression: true, compression: true,
cacheSize: 32 << 20, cacheSize: 32 << 20,
createIfMissing: false createIfMissing: false
}); });
async function updateVersion() { async function updateVersion() {
const bak = `${process.env.HOME}/walletdb-bak-${Date.now()}.ldb`; const bak = `${process.env.HOME}/wallet-bak-${Date.now()}`;
console.log('Checking version.'); console.log('Checking version.');
@ -214,7 +210,7 @@ async function updateLookahead() {
const db = new WalletDB({ const db = new WalletDB({
network: process.argv[3], network: process.argv[3],
db: 'leveldb', db: 'leveldb',
location: file, location: process.argv[2],
witness: false, witness: false,
useCheckpoints: false, useCheckpoints: false,
maxFiles: 64, maxFiles: 64,
@ -248,7 +244,7 @@ async function unstate() {
(async () => { (async () => {
await db.open(); await db.open();
batch = db.batch(); batch = db.batch();
console.log('Opened %s.', file); console.log('Opened %s.', process.argv[2]);
await updateVersion(); await updateVersion();
await wipeTXDB(); await wipeTXDB();
await patchAccounts(); await patchAccounts();

View File

@ -24,23 +24,19 @@ const tlayout = layouts.txdb;
// depth - counter record // depth - counter record
// hash/ascii - variable length key prefixes // hash/ascii - variable length key prefixes
let file = process.argv[2];
let parent = null; let parent = null;
assert(typeof file === 'string', 'Please pass in a database path.'); assert(process.argv.length > 2, 'Please pass in a database path.');
file = file.replace(/\.ldb\/?$/, '');
const db = bdb.create({ const db = bdb.create({
location: file, location: process.argv[2],
db: 'leveldb',
compression: true, compression: true,
cacheSize: 32 << 20, cacheSize: 32 << 20,
createIfMissing: false createIfMissing: false
}); });
async function updateVersion() { async function updateVersion() {
const bak = `${process.env.HOME}/walletdb-bak-${Date.now()}.ldb`; const bak = `${process.env.HOME}/wallet-bak-${Date.now()}`;
console.log('Checking version.'); console.log('Checking version.');
@ -906,7 +902,7 @@ function parsei(key) { // i[wid][name]
(async () => { (async () => {
await db.open(); await db.open();
console.log('Opened %s.', file); console.log('Opened %s.', process.argv[2]);
parent = db.batch(); parent = db.batch();