migrate: better walletdb v4 migration.

This commit is contained in:
Christopher Jeffrey 2016-10-18 19:23:36 -07:00
parent 519280e1bd
commit 321f4f6df6
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -48,67 +48,47 @@ var updateVersion = co(function* updateVersion() {
batch.put('V', ver); batch.put('V', ver);
}); });
var updateState = co(function* updateState(wid) { var updateTXDB = co(function* updateTXDB() {
var total = 0; var txs = {};
var unconfirmed = 0; var i, keys, key, hash, tx, walletdb;
var confirmed = 0;
var txs = 0;
var coins = 0;
var p, keys;
keys = yield db.keys({ keys = yield db.keys({
gte: tlayout.prefix(wid, tlayout.t(constants.NULL_HASH)), gte: new Buffer([0x00]),
lte: tlayout.prefix(wid, tlayout.t(constants.HIGH_HASH)) lte: new Buffer([0xff])
}); });
txs += keys.length; for (i = 0; i < keys.length; i++) {
key = keys[i];
yield db.range({ if (key[0] === 0x74 && key[5] === 0x74) {
gte: tlayout.prefix(wid, tlayout.c(constants.NULL_HASH, 0x00000000)), tx = yield db.get(key);
lte: tlayout.prefix(wid, tlayout.c(constants.HIGH_HASH, 0xffffffff)), tx = bcoin.tx.fromExtended(tx);
parse: function(key, data) { hash = tx.hash('hex');
var height = data.readUInt32LE(4, true); txs[hash] = tx;
var value = utils.read64N(data, 8);
total += value;
if (height === 0x7fffffff)
unconfirmed += value;
else
confirmed += value;
coins += 1;
} }
}); if (key[0] === 0x74)
batch.del(key);
p = new BufferWriter();
p.writeU64(txs);
p.writeU64(coins);
p.writeU64(unconfirmed);
p.writeU64(confirmed);
batch.put(tlayout.prefix(wid, tlayout.R), p.render());
});
var updateStates = co(function* updateStates() {
var i, wallets, wid;
wallets = yield db.keys({
gte: layout.w(0),
lte: layout.w(0xffffffff),
parse: function(key) {
return key.readUInt32LE(1, true);
}
});
console.log('Updating states...');
for (i = 0; i < wallets.length; i++) {
wid = wallets[i];
yield updateState(wid);
} }
console.log('Updated %d states.', wallets.length); txs = utils.values(txs);
yield batch.write();
yield db.close();
walletdb = new WalletDB({
location: file,
db: 'leveldb',
resolution: true,
verify: false
});
yield walletdb.open();
for (i = 0; i < txs.length; i++) {
tx = txs[i];
yield walletdb.addTX(tx);
}
yield walletdb.close();
}); });
co.spawn(function* () { co.spawn(function* () {
@ -116,8 +96,7 @@ co.spawn(function* () {
batch = db.batch(); batch = db.batch();
console.log('Opened %s.', file); console.log('Opened %s.', file);
yield updateVersion(); yield updateVersion();
yield updateStates(); yield updateTXDB();
yield batch.write();
}).then(function() { }).then(function() {
console.log('Migration complete.'); console.log('Migration complete.');
process.exit(0); process.exit(0);