migrate: pertxout migration improvements.
This commit is contained in:
parent
5602001348
commit
ca81520875
@ -39,7 +39,7 @@ const STATE_ENTRY = 3;
|
|||||||
const STATE_FINAL = 4;
|
const STATE_FINAL = 4;
|
||||||
const STATE_DONE = 5;
|
const STATE_DONE = 5;
|
||||||
|
|
||||||
const heightCache = new Map();
|
const metaCache = new Map();
|
||||||
|
|
||||||
function writeJournal(batch, state, hash) {
|
function writeJournal(batch, state, hash) {
|
||||||
let data = Buffer.allocUnsafe(34);
|
let data = Buffer.allocUnsafe(34);
|
||||||
@ -166,7 +166,7 @@ async function reserializeUndo(hash) {
|
|||||||
data.writeUInt32LE(version, 0, true);
|
data.writeUInt32LE(version, 0, true);
|
||||||
data.writeUInt32LE(height, 4, true);
|
data.writeUInt32LE(height, 4, true);
|
||||||
batch.put(pair(0x01, prevout.hash), data);
|
batch.put(pair(0x01, prevout.hash), data);
|
||||||
heightCache.set(prevout.hash, [version, height]);
|
metaCache.set(prevout.hash, [version, height]);
|
||||||
}
|
}
|
||||||
|
|
||||||
undo.items.push(item);
|
undo.items.push(item);
|
||||||
@ -175,11 +175,11 @@ async function reserializeUndo(hash) {
|
|||||||
|
|
||||||
batch.put(pair('u', tip.hash), undo.toRaw());
|
batch.put(pair('u', tip.hash), undo.toRaw());
|
||||||
|
|
||||||
if (++total % 10000 === 0) {
|
if (++total % 1000 === 0) {
|
||||||
console.log('Reserialized %d undo coins.', total);
|
console.log('Reserialized %d undo coins.', total);
|
||||||
writeJournal(batch, STATE_UNDO, tip.prevBlock);
|
writeJournal(batch, STATE_UNDO, tip.prevBlock);
|
||||||
await batch.write();
|
await batch.write();
|
||||||
heightCache.clear();
|
metaCache.clear();
|
||||||
batch = db.batch();
|
batch = db.batch();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -189,7 +189,7 @@ async function reserializeUndo(hash) {
|
|||||||
writeJournal(batch, STATE_CLEANUP);
|
writeJournal(batch, STATE_CLEANUP);
|
||||||
await batch.write();
|
await batch.write();
|
||||||
|
|
||||||
heightCache.clear();
|
metaCache.clear();
|
||||||
|
|
||||||
console.log('Reserialized %d undo coins.', total);
|
console.log('Reserialized %d undo coins.', total);
|
||||||
|
|
||||||
@ -216,7 +216,7 @@ async function cleanupIndex() {
|
|||||||
|
|
||||||
batch.del(item.key);
|
batch.del(item.key);
|
||||||
|
|
||||||
if (++total % 100000 === 0) {
|
if (++total % 10000 === 0) {
|
||||||
console.log('Cleaned up %d undo records.', total);
|
console.log('Cleaned up %d undo records.', total);
|
||||||
writeJournal(batch, STATE_CLEANUP);
|
writeJournal(batch, STATE_CLEANUP);
|
||||||
await batch.write();
|
await batch.write();
|
||||||
@ -284,7 +284,7 @@ async function reserializeCoins(hash) {
|
|||||||
|
|
||||||
batch.put(bpair('c', hash, i), item.toRaw());
|
batch.put(bpair('c', hash, i), item.toRaw());
|
||||||
|
|
||||||
if (++total % 100000 === 0)
|
if (++total % 10000 === 0)
|
||||||
update = true;
|
update = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -382,7 +382,7 @@ async function getMeta(coin, prevout) {
|
|||||||
if (coin.height !== -1)
|
if (coin.height !== -1)
|
||||||
return [coin.version, coin.height, true];
|
return [coin.version, coin.height, true];
|
||||||
|
|
||||||
item = heightCache.get(prevout.hash);
|
item = metaCache.get(prevout.hash);
|
||||||
|
|
||||||
if (item) {
|
if (item) {
|
||||||
let [version, height] = item;
|
let [version, height] = item;
|
||||||
@ -428,6 +428,12 @@ async function isPruned() {
|
|||||||
return (data.readUInt32LE(4) & 4) !== 0;
|
return (data.readUInt32LE(4) & 4) !== 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function isSPV() {
|
||||||
|
let data = await db.get('O');
|
||||||
|
assert(data);
|
||||||
|
return (data.readUInt32LE(4) & 1) !== 0;
|
||||||
|
}
|
||||||
|
|
||||||
async function isMainChain(entry, tip) {
|
async function isMainChain(entry, tip) {
|
||||||
if (entry.hash === tip)
|
if (entry.hash === tip)
|
||||||
return true;
|
return true;
|
||||||
@ -506,6 +512,9 @@ function bpair(prefix, hash, index) {
|
|||||||
|
|
||||||
console.log('Opened %s.', file);
|
console.log('Opened %s.', file);
|
||||||
|
|
||||||
|
if (await isSPV())
|
||||||
|
throw new Error('Cannot migrate SPV database.');
|
||||||
|
|
||||||
console.log('Starting migration in 3 seconds...');
|
console.log('Starting migration in 3 seconds...');
|
||||||
console.log('If you crash you can start over.');
|
console.log('If you crash you can start over.');
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user