migrate: walletdb 4 to 5.
This commit is contained in:
parent
3b333c07cd
commit
d2a9352c9c
@ -42,8 +42,9 @@ var U32 = utils.U32;
|
|||||||
* a[wid][index] -> account
|
* a[wid][index] -> account
|
||||||
* i[wid][name] -> account index
|
* i[wid][name] -> account index
|
||||||
* t[wid]* -> txdb
|
* t[wid]* -> txdb
|
||||||
* R -> tip
|
* R -> tip height
|
||||||
* b[hash] -> wallet block
|
* c[height] -> chain header
|
||||||
|
* b[height] -> block->wid map
|
||||||
* e[hash] -> tx->wid map
|
* e[hash] -> tx->wid map
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ -228,7 +229,7 @@ WalletDB.prototype._init = function _init() {
|
|||||||
|
|
||||||
WalletDB.prototype._open = co(function* open() {
|
WalletDB.prototype._open = co(function* open() {
|
||||||
yield this.db.open();
|
yield this.db.open();
|
||||||
yield this.db.checkVersion('V', 4);
|
yield this.db.checkVersion('V', 5);
|
||||||
yield this.writeGenesis();
|
yield this.writeGenesis();
|
||||||
|
|
||||||
if (this.options.wipeNoReally)
|
if (this.options.wipeNoReally)
|
||||||
@ -261,6 +262,7 @@ WalletDB.prototype._close = co(function* close() {
|
|||||||
yield wallet.destroy();
|
yield wallet.destroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// yield this.client.disconnect();
|
||||||
yield this.db.close();
|
yield this.db.close();
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -295,7 +297,7 @@ WalletDB.prototype._connect = co(function* connect() {
|
|||||||
|
|
||||||
hashes = yield this.getHashes();
|
hashes = yield this.getHashes();
|
||||||
|
|
||||||
yield this.watchAddress(hashes);
|
yield this.client.watchAddress(hashes);
|
||||||
|
|
||||||
if (this.options.noScan) {
|
if (this.options.noScan) {
|
||||||
tip = yield this.client.getTip();
|
tip = yield this.client.getTip();
|
||||||
@ -351,7 +353,7 @@ WalletDB.prototype._rescan = co(function* rescan(height) {
|
|||||||
|
|
||||||
hashes = yield this.getHashes();
|
hashes = yield this.getHashes();
|
||||||
|
|
||||||
yield this.watchAddress(hashes);
|
yield this.client.watchAddress(hashes);
|
||||||
|
|
||||||
yield this.scan(height, hashes);
|
yield this.scan(height, hashes);
|
||||||
});
|
});
|
||||||
|
|||||||
81
migrate/walletdb4to5.js
Normal file
81
migrate/walletdb4to5.js
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
var assert = require('assert');
|
||||||
|
var bcoin = require('../');
|
||||||
|
var constants = require('../lib/protocol/constants');
|
||||||
|
var BufferWriter = require('../lib/utils/writer');
|
||||||
|
var BufferReader = require('../lib/utils/reader');
|
||||||
|
var utils = require('../lib/utils/utils');
|
||||||
|
var co = bcoin.co;
|
||||||
|
var file = process.argv[2];
|
||||||
|
var db, batch;
|
||||||
|
|
||||||
|
assert(typeof file === 'string', 'Please pass in a database path.');
|
||||||
|
|
||||||
|
file = file.replace(/\.ldb\/?$/, '');
|
||||||
|
|
||||||
|
db = bcoin.ldb({
|
||||||
|
location: file,
|
||||||
|
db: 'leveldb',
|
||||||
|
compression: true,
|
||||||
|
cacheSize: 16 << 20,
|
||||||
|
writeBufferSize: 8 << 20,
|
||||||
|
createIfMissing: false,
|
||||||
|
bufferKeys: true
|
||||||
|
});
|
||||||
|
|
||||||
|
var updateVersion = co(function* updateVersion() {
|
||||||
|
var bak = process.env.HOME + '/walletdb-bak-' + Date.now() + '.ldb';
|
||||||
|
var data, ver;
|
||||||
|
|
||||||
|
console.log('Checking version.');
|
||||||
|
|
||||||
|
data = yield db.get('V');
|
||||||
|
assert(data, 'No version.');
|
||||||
|
|
||||||
|
ver = data.readUInt32LE(0, true);
|
||||||
|
|
||||||
|
if (ver !== 4)
|
||||||
|
throw Error('DB is version ' + ver + '.');
|
||||||
|
|
||||||
|
console.log('Backing up DB to: %s.', bak);
|
||||||
|
|
||||||
|
yield db.backup(bak);
|
||||||
|
|
||||||
|
ver = new Buffer(4);
|
||||||
|
ver.writeUInt32LE(5, 0, true);
|
||||||
|
batch.put('V', ver);
|
||||||
|
});
|
||||||
|
|
||||||
|
var updateTXDB = co(function* updateTXDB() {
|
||||||
|
var i, keys, key;
|
||||||
|
|
||||||
|
keys = yield db.keys({
|
||||||
|
gte: new Buffer([0x00]),
|
||||||
|
lte: new Buffer([0xff])
|
||||||
|
});
|
||||||
|
|
||||||
|
for (i = 0; i < keys.length; i++) {
|
||||||
|
key = keys[i];
|
||||||
|
switch (key[0]) {
|
||||||
|
case 0x62: // b
|
||||||
|
case 0x63: // c
|
||||||
|
case 0x65: // e
|
||||||
|
case 0x74: // t
|
||||||
|
batch.del(key);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
yield batch.write();
|
||||||
|
});
|
||||||
|
|
||||||
|
co.spawn(function* () {
|
||||||
|
yield db.open();
|
||||||
|
batch = db.batch();
|
||||||
|
console.log('Opened %s.', file);
|
||||||
|
yield updateVersion();
|
||||||
|
yield updateTXDB();
|
||||||
|
yield db.close();
|
||||||
|
}).then(function() {
|
||||||
|
console.log('Migration complete.');
|
||||||
|
process.exit(0);
|
||||||
|
});
|
||||||
Loading…
Reference in New Issue
Block a user