diff --git a/bin/node b/bin/node index 3ed9a5ee..4e1f9a31 100755 --- a/bin/node +++ b/bin/node @@ -28,8 +28,7 @@ const node = new FullNode({ logFile: true, logConsole: true, logLevel: 'debug', - db: 'leveldb', - persistent: true, + memory: false, workers: true, listen: true, loader: require diff --git a/lib/blockchain/chain.js b/lib/blockchain/chain.js index 8e41f03c..f98dc92a 100644 --- a/lib/blockchain/chain.js +++ b/lib/blockchain/chain.js @@ -2655,7 +2655,7 @@ class ChainOptions { this.prefix = null; this.location = null; - this.db = 'memory'; + this.memory = true; this.maxFiles = 64; this.cacheSize = 32 << 20; this.compression = true; @@ -2716,9 +2716,9 @@ class ChainOptions { this.location = options.location; } - if (options.db != null) { - assert(typeof options.db === 'string'); - this.db = options.db; + if (options.memory != null) { + assert(typeof options.memory === 'boolean'); + this.memory = options.memory; } if (options.maxFiles != null) { diff --git a/lib/mempool/mempool.js b/lib/mempool/mempool.js index f4bd0481..d2469b0b 100644 --- a/lib/mempool/mempool.js +++ b/lib/mempool/mempool.js @@ -1970,7 +1970,7 @@ class MempoolOptions { this.prefix = null; this.location = null; - this.db = 'memory'; + this.memory = true; this.maxFiles = 64; this.cacheSize = 32 << 20; this.compression = true; @@ -2091,9 +2091,9 @@ class MempoolOptions { this.location = options.location; } - if (options.db != null) { - assert(typeof options.db === 'string'); - this.db = options.db; + if (options.memory != null) { + assert(typeof options.memory === 'boolean'); + this.memory = options.memory; } if (options.maxFiles != null) { @@ -2505,7 +2505,7 @@ class MempoolCache { async init(hash) { 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')); await batch.write(); } @@ -2557,7 +2557,7 @@ class MempoolCache { for (const key of keys) 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.del(layout.F.build()); @@ -2629,6 +2629,12 @@ function useDesc(a) { return y > x; } +function fromU32(num) { + const data = Buffer.allocUnsafe(4); + data.writeUInt32LE(num, 0, true); + return data; +} + /* * Expose */ diff --git a/lib/net/hostlist.js b/lib/net/hostlist.js index ae54131f..64bd0dc5 100644 --- a/lib/net/hostlist.js +++ b/lib/net/hostlist.js @@ -131,7 +131,7 @@ class HostList { */ start() { - if (!this.options.persistent) + if (this.options.memory) return; if (!this.options.filename) @@ -146,7 +146,7 @@ class HostList { */ stop() { - if (!this.options.persistent) + if (this.options.memory) return; if (!this.options.filename) @@ -194,7 +194,7 @@ class HostList { if (fs.unsupported) return; - if (!this.options.persistent) + if (this.options.memory) return; if (!filename) @@ -226,7 +226,7 @@ class HostList { if (fs.unsupported) return; - if (!this.options.persistent) + if (this.options.memory) return; if (!filename) @@ -1496,7 +1496,7 @@ class HostListOptions { this.prefix = null; this.filename = null; - this.persistent = false; + this.memory = true; this.flushInterval = 120000; if (options) @@ -1590,9 +1590,9 @@ class HostListOptions { this.maxEntries = options.maxEntries; } - if (options.persistent != null) { - assert(typeof options.persistent === 'boolean'); - this.persistent = options.persistent; + if (options.memory != null) { + assert(typeof options.memory === 'boolean'); + this.memory = options.memory; } if (options.prefix != null) { diff --git a/lib/net/pool.js b/lib/net/pool.js index db44dc72..5d22a047 100644 --- a/lib/net/pool.js +++ b/lib/net/pool.js @@ -3635,7 +3635,7 @@ class PoolOptions { this.blockMode = 0; this.services = common.LOCAL_SERVICES; this.requiredServices = common.REQUIRED_SERVICES; - this.persistent = false; + this.memory = true; this.fromOptions(options); } @@ -3866,9 +3866,9 @@ class PoolOptions { this.blockMode = options.blockMode; } - if (options.persistent != null) { - assert(typeof options.persistent === 'boolean'); - this.persistent = options.persistent; + if (options.memory != null) { + assert(typeof options.memory === 'boolean'); + this.memory = options.memory; } if (this.spv) { diff --git a/lib/node/fullnode.js b/lib/node/fullnode.js index e30788e5..24b36ccc 100644 --- a/lib/node/fullnode.js +++ b/lib/node/fullnode.js @@ -45,7 +45,7 @@ class FullNode extends Node { network: this.network, logger: this.logger, workers: this.workers, - db: this.config.str('db'), + memory: this.config.bool('memory'), prefix: this.config.prefix, maxFiles: this.config.uint('max-files'), cacheSize: this.config.mb('cache-size'), @@ -71,7 +71,7 @@ class FullNode extends Node { workers: this.workers, chain: this.chain, fees: this.fees, - db: this.config.str('db'), + memory: this.config.bool('memory'), prefix: this.config.prefix, persistent: this.config.bool('persistent-mempool'), maxSize: this.config.mb('mempool-size'), @@ -110,7 +110,7 @@ class FullNode extends Node { host: this.config.str('host'), port: this.config.uint('port'), listen: this.config.bool('listen'), - persistent: this.config.bool('persistent') + memory: this.config.bool('memory') }); // Miner needs access to the chain and mempool. diff --git a/lib/node/node.js b/lib/node/node.js index 2adc456d..a9007171 100644 --- a/lib/node/node.js +++ b/lib/node/node.js @@ -47,6 +47,7 @@ class Node extends EventEmitter { this.config.open(config); this.network = Network.get(this.config.getSuffix()); + this.memory = this.config.bool('memory', true); this.startTime = -1; this.bound = []; this.plugins = Object.create(null); @@ -81,7 +82,7 @@ class Node extends EventEmitter { logger = config.obj('logger'); logger.set({ - filename: config.bool('log-file') + filename: !this.memory && config.bool('log-file') ? config.location(file) : null, level: config.str('log-level'), @@ -131,6 +132,9 @@ class Node extends EventEmitter { if (fs.unsupported) return undefined; + if (this.memory) + return undefined; + return fs.mkdirp(this.config.prefix); } diff --git a/lib/node/spvnode.js b/lib/node/spvnode.js index 29c589dd..fa477f18 100644 --- a/lib/node/spvnode.js +++ b/lib/node/spvnode.js @@ -45,8 +45,8 @@ class SPVNode extends Node { this.chain = new Chain({ network: this.network, logger: this.logger, - db: this.config.str('db'), prefix: this.config.prefix, + memory: this.config.bool('memory'), maxFiles: this.config.uint('max-files'), cacheSize: this.config.mb('cache-size'), entryCache: this.config.uint('entry-cache'), @@ -73,7 +73,7 @@ class SPVNode extends Node { identityKey: this.config.buf('identity-key'), maxOutbound: this.config.uint('max-outbound'), createSocket: this.config.func('create-socket'), - persistent: this.config.bool('persistent'), + memory: this.config.bool('memory'), selfish: true, listen: false }); diff --git a/lib/wallet/plugin.js b/lib/wallet/plugin.js index 47f31cd8..b2fbb870 100644 --- a/lib/wallet/plugin.js +++ b/lib/wallet/plugin.js @@ -47,7 +47,7 @@ class Plugin extends EventEmitter { workers: node.workers, client: this.client, prefix: config.prefix, - db: config.str(['wallet-db', 'db']), + memory: config.bool(['wallet-memory', 'memory']), maxFiles: config.uint('wallet-max-files'), cacheSize: config.mb('wallet-cache-size'), witness: config.bool('wallet-witness'), diff --git a/lib/wallet/server.js b/lib/wallet/server.js index a252c5a9..2cd97f21 100644 --- a/lib/wallet/server.js +++ b/lib/wallet/server.js @@ -45,7 +45,7 @@ class WalletNode extends Node { workers: this.workers, client: this.client, prefix: this.config.prefix, - db: this.config.str('db'), + memory: this.config.bool('memory'), maxFiles: this.config.uint('max-files'), cacheSize: this.config.mb('cache-size'), witness: this.config.bool('witness'), diff --git a/lib/wallet/walletdb.js b/lib/wallet/walletdb.js index 32f30995..b259bbf8 100644 --- a/lib/wallet/walletdb.js +++ b/lib/wallet/walletdb.js @@ -239,7 +239,7 @@ class WalletDB extends EventEmitter { this.unregister(wallet); } - await this.db.close(); + return this.db.close(); } /** @@ -1891,7 +1891,8 @@ class WalletDB extends EventEmitter { 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) { // We let blocks of the same height @@ -2137,7 +2138,7 @@ class WalletOptions { this.prefix = null; this.location = null; - this.db = 'memory'; + this.memory = true; this.maxFiles = 64; this.cacheSize = 16 << 20; this.compression = true; @@ -2186,7 +2187,7 @@ class WalletOptions { if (options.prefix != null) { assert(typeof options.prefix === 'string'); this.prefix = options.prefix; - this.location = path.join(this.prefix, 'walletdb'); + this.location = path.join(this.prefix, 'wallet'); } if (options.location != null) { @@ -2194,9 +2195,9 @@ class WalletOptions { this.location = options.location; } - if (options.db != null) { - assert(typeof options.db === 'string'); - this.db = options.db; + if (options.memory != null) { + assert(typeof options.memory === 'boolean'); + this.memory = options.memory; } if (options.maxFiles != null) { diff --git a/migrate/chaindb2to3.js b/migrate/chaindb2to3.js index 94853469..3e685d22 100644 --- a/migrate/chaindb2to3.js +++ b/migrate/chaindb2to3.js @@ -29,7 +29,6 @@ const Block = require('../lib/primitives/block'); const LRU = require('../lib/utils/lru'); const consensus = require('../lib/protocol/consensus'); -const file = process.argv[2].replace(/\.ldb\/?$/, ''); const shouldPrune = process.argv.indexOf('--prune') !== -1; let hasIndex = false; @@ -37,8 +36,7 @@ let hasPruned = false; let hasSPV = false; const db = bdb.create({ - location: file, - db: 'leveldb', + location: process.argv[2], compression: true, cacheSize: 32 << 20, createIfMissing: false @@ -77,12 +75,12 @@ async function readJournal() { if (!data) return [STATE_VERSION, consensus.NULL_HASH]; - if (data[0] !== MIGRATION_ID) - throw new Error('Bad migration id.'); - if (data.length !== 34) throw new Error('Bad migration length.'); + if (data[0] !== MIGRATION_ID) + throw new Error('Bad migration id.'); + const state = data.readUInt8(1, true); const hash = data.toString('hex', 2, 34); @@ -125,6 +123,7 @@ async function updateVersion() { async function reserializeUndo(hash) { let tip = await getTip(); + const height = tip.height; if (hash !== consensus.NULL_HASH) @@ -643,7 +642,7 @@ reserializeEntries; (async () => { await db.open(); - console.log('Opened %s.', file); + console.log('Opened %s.', process.argv[2]); if (await isSPV()) hasSPV = true; @@ -690,7 +689,7 @@ reserializeEntries; assert(state === STATE_DONE); - console.log('Closing %s.', file); + console.log('Closing %s.', process.argv[2]); await db.close(); diff --git a/migrate/chaindb3to4.js b/migrate/chaindb3to4.js index 5abcf609..3712a672 100644 --- a/migrate/chaindb3to4.js +++ b/migrate/chaindb3to4.js @@ -9,16 +9,12 @@ const layout = require('../lib/blockchain/layout'); // deployment table v->D // C/T key format -let file = process.argv[2]; +assert(process.argv.length > 2, 'Please pass in a database path.'); + let parent = null; -assert(typeof file === 'string', 'Please pass in a database path.'); - -file = file.replace(/\.ldb\/?$/, ''); - const db = bdb.create({ - location: file, - db: 'leveldb', + location: process.argv[2], compression: true, cacheSize: 32 << 20, createIfMissing: false @@ -144,7 +140,7 @@ function parseC(key) { (async () => { await db.open(); - console.log('Opened %s.', file); + console.log('Opened %s.', process.argv[2]); parent = db.batch(); diff --git a/migrate/walletdb5to6.js b/migrate/walletdb5to6.js index fec21ea6..3757ef64 100644 --- a/migrate/walletdb5to6.js +++ b/migrate/walletdb5to6.js @@ -4,23 +4,19 @@ const assert = require('assert'); const bdb = require('bdb'); const bio = require('bufio'); -let file = process.argv[2]; +assert(process.argv.length > 2, 'Please pass in a database path.'); + let batch; -assert(typeof file === 'string', 'Please pass in a database path.'); - -file = file.replace(/\.ldb\/?$/, ''); - const db = bdb.create({ - location: file, - db: 'leveldb', + location: process.argv[2], compression: true, cacheSize: 32 << 20, createIfMissing: false }); 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.'); @@ -214,7 +210,7 @@ async function updateLookahead() { const db = new WalletDB({ network: process.argv[3], db: 'leveldb', - location: file, + location: process.argv[2], witness: false, useCheckpoints: false, maxFiles: 64, @@ -248,7 +244,7 @@ async function unstate() { (async () => { await db.open(); batch = db.batch(); - console.log('Opened %s.', file); + console.log('Opened %s.', process.argv[2]); await updateVersion(); await wipeTXDB(); await patchAccounts(); diff --git a/migrate/walletdb6to7.js b/migrate/walletdb6to7.js index b2372658..2c19b3fd 100644 --- a/migrate/walletdb6to7.js +++ b/migrate/walletdb6to7.js @@ -24,23 +24,19 @@ const tlayout = layouts.txdb; // depth - counter record // hash/ascii - variable length key prefixes -let file = process.argv[2]; let parent = null; -assert(typeof file === 'string', 'Please pass in a database path.'); - -file = file.replace(/\.ldb\/?$/, ''); +assert(process.argv.length > 2, 'Please pass in a database path.'); const db = bdb.create({ - location: file, - db: 'leveldb', + location: process.argv[2], compression: true, cacheSize: 32 << 20, createIfMissing: false }); 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.'); @@ -906,7 +902,7 @@ function parsei(key) { // i[wid][name] (async () => { await db.open(); - console.log('Opened %s.', file); + console.log('Opened %s.', process.argv[2]); parent = db.batch();