migrate: walletdb 4 to 5.

This commit is contained in:
Christopher Jeffrey 2016-10-24 15:42:58 -07:00
parent 3b333c07cd
commit d2a9352c9c
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 88 additions and 5 deletions

View File

@ -42,8 +42,9 @@ var U32 = utils.U32;
* a[wid][index] -> account
* i[wid][name] -> account index
* t[wid]* -> txdb
* R -> tip
* b[hash] -> wallet block
* R -> tip height
* c[height] -> chain header
* b[height] -> block->wid map
* e[hash] -> tx->wid map
*/
@ -228,7 +229,7 @@ WalletDB.prototype._init = function _init() {
WalletDB.prototype._open = co(function* open() {
yield this.db.open();
yield this.db.checkVersion('V', 4);
yield this.db.checkVersion('V', 5);
yield this.writeGenesis();
if (this.options.wipeNoReally)
@ -261,6 +262,7 @@ WalletDB.prototype._close = co(function* close() {
yield wallet.destroy();
}
// yield this.client.disconnect();
yield this.db.close();
});
@ -295,7 +297,7 @@ WalletDB.prototype._connect = co(function* connect() {
hashes = yield this.getHashes();
yield this.watchAddress(hashes);
yield this.client.watchAddress(hashes);
if (this.options.noScan) {
tip = yield this.client.getTip();
@ -351,7 +353,7 @@ WalletDB.prototype._rescan = co(function* rescan(height) {
hashes = yield this.getHashes();
yield this.watchAddress(hashes);
yield this.client.watchAddress(hashes);
yield this.scan(height, hashes);
});

81
migrate/walletdb4to5.js Normal file
View 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);
});