Skip to content

Commit

Permalink
chain-test: change initial change migration to be selfcontained.
Browse files Browse the repository at this point in the history
  • Loading branch information
nodech committed Jul 13, 2024
1 parent 30f9de7 commit a550b13
Show file tree
Hide file tree
Showing 7 changed files with 515 additions and 153 deletions.
2 changes: 1 addition & 1 deletion lib/blockchain/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -3967,8 +3967,8 @@ class ChainOptions {
}

if (options.prune != null) {
assert(!options.spv, 'Can not prune in spv mode.');
assert(typeof options.prune === 'boolean');
assert(!options.prune || !options.spv, 'Can not prune in spv mode.');
this.prune = options.prune;
}

Expand Down
57 changes: 49 additions & 8 deletions lib/blockchain/migrations.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@

const assert = require('bsert');
const Logger = require('blgr');
const {encoding} = require('bufio');
const bio = require('bufio');
const {encoding} = bio;
const bdb = require('bdb');
const Network = require('../protocol/network');
const rules = require('../covenants/rules');
const Block = require('../primitives/block');
const CoinView = require('../coins/coinview');
const UndoCoins = require('../coins/undocoins');
const layout = require('./layout');
const MigrationState = require('../migrations/state');
const AbstractMigration = require('../migrations/migration');
const {
Migrator,
Expand All @@ -40,6 +41,7 @@ class MigrateMigrations extends AbstractMigration {
this.logger = options.logger.context('chain-migration-migrate');
this.db = options.db;
this.ldb = options.ldb;
this.layout = MigrateMigrations.layout();
}

async check() {
Expand All @@ -54,8 +56,11 @@ class MigrateMigrations extends AbstractMigration {

async migrate(b) {
this.logger.info('Migrating migrations..');
const state = new MigrationState();
state.nextMigration = 1;

const oldLayout = this.layout.oldLayout.wdb;
const newLayout = this.layout.newLayout.wdb;
let nextMigration = 1;
const skipped = [];

const oldMigrations = await this.ldb.keys({
gte: oldLayout.M.min(),
Expand All @@ -67,15 +72,36 @@ class MigrateMigrations extends AbstractMigration {
b.del(oldLayout.M.encode(id));

if (id === 1) {
if (this.options.prune)
state.skipped.push(1);
if (this.options.prune) {
skipped.push(1);
}

state.nextMigration = 2;
nextMigration = 2;
}
}

this.db.writeVersion(b, 2);
b.put(layout.M.encode(), state.encode());
b.put(newLayout.M.encode(),
this.encodeMigrationState(nextMigration, skipped));
}

encodeMigrationState(nextMigration, skipped) {
let size = 4;
size += encoding.sizeVarint(nextMigration);
size += encoding.sizeVarint(skipped.length);

for (const id of skipped)
size += encoding.sizeVarint(id);

const bw = bio.write(size);
bw.writeU32(0);
bw.writeVarint(nextMigration);
bw.writeVarint(skipped.length);

for (const id of skipped)
bw.writeVarint(id);

return bw.render();
}

static info() {
Expand All @@ -84,6 +110,21 @@ class MigrateMigrations extends AbstractMigration {
description: 'ChainDB migration layout has changed.'
};
}

static layout() {
return {
oldLayout: {
wdb: {
M: bdb.key('M', ['uint32'])
}
},
newLayout: {
wdb: {
M: bdb.key('M')
}
}
};
}
}

/**
Expand Down
Loading

0 comments on commit a550b13

Please sign in to comment.