Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
Add support to extract snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
nagdahimanshu committed Oct 24, 2023
1 parent 061ee4f commit da7c553
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 5 deletions.
20 changes: 16 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
FILE_NAME,
LISK_V3_BACKUP_DATA_DIR,
LEGACY_DB_PATH,
DEFAULT_DATA_DIR,
} from './constants';
import { getAPIClient } from './client';
import {
Expand Down Expand Up @@ -61,13 +62,15 @@ import {
startLiskCore,
isLiskCoreV3Running,
getLiskCoreStartCommand,
resolveSnapshotPath,
} from './utils/node';
import { resolveAbsolutePath, verifyOutputPath } from './utils/path';
import { execAsync } from './utils/process';
import { getBlockHeaderByHeight } from './utils/block';
import { MigratorException } from './utils/exception';
import { writeCommandsToExec } from './utils/commands';
import { getNetworkIdentifier } from './utils/network';
import { extractTarBall } from './utils/fs';

let configCoreV4: PartialApplicationConfig;
class LiskMigrator extends Command {
Expand Down Expand Up @@ -146,7 +149,7 @@ class LiskMigrator extends Command {
const autoMigrateUserConfig = flags['auto-migrate-config'] ?? false;
const autoStartLiskCoreV4 = flags['auto-start-lisk-core-v4'];
const pageSize = Number(flags['page-size']);
const snapshotPath = flags['snapshot-path'];
const snapshotPath = flags['snapshot-path'] as string;
const inputNetwork = flags.network as string;
const useSnapshot = !!snapshotPath || false;

Expand All @@ -163,6 +166,7 @@ class LiskMigrator extends Command {
// Ensure the output directory is present
if (!fs.existsSync(outputDir)) fs.mkdirSync(outputDir, { recursive: true });
const filePathCommandsToExec = `${outputDir}/${FILE_NAME.COMMANDS_TO_EXEC}`;
const dataDir = join(__dirname, '..', DEFAULT_DATA_DIR);

try {
if (!useSnapshot) {
Expand Down Expand Up @@ -224,16 +228,24 @@ class LiskMigrator extends Command {
delay: 500,
isFinal: true,
});
} else if (useSnapshot && snapshotPath.endsWith('.tar.gz')) {
cli.action.start(`Extracting snapshot at ${dataDir}`);
await extractTarBall(snapshotPath, dataDir);
cli.action.stop();
}

await setTokenIDLskByNetID(networkIdentifier);
await setPrevSnapshotBlockHeightByNetID(networkIdentifier);

// Create new DB instance based on the snapshot path
cli.action.start('Creating database instance');
const snapshotDirPath = useSnapshot
? (snapshotPath as string)
: join(liskCoreV3DataPath, SNAPSHOT_DIR);
const snapshotDirPath = await resolveSnapshotPath(
useSnapshot,
snapshotPath,
dataDir,
liskCoreV3DataPath,
);

const db = new Database(snapshotDirPath);
cli.action.stop();

Expand Down
10 changes: 10 additions & 0 deletions src/utils/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,13 @@ export const createTarball = async (filePath: string, outputDir: string) =>
.then(() => resolve(true))
.catch(err => reject(err));
});

export const getFiles = async (directoryPath: string, options = {}): Promise<string[] | Error> =>
new Promise((resolve, reject) => {
fs.readdir(directoryPath, options, (err, files) => {
if (err) {
return reject(err);
}
return resolve(files as string[]);
});
});
16 changes: 15 additions & 1 deletion src/utils/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { renameSync } from 'fs-extra';
import { PartialApplicationConfig } from 'lisk-framework';

import { execAsync } from './process';
import { copyDir, exists } from './fs';
import { copyDir, exists, getFiles } from './fs';
import { isPortAvailable } from './network';
import { resolveAbsolutePath } from './path';
import { Port } from '../types';
Expand Down Expand Up @@ -253,3 +253,17 @@ export const startLiskCore = async (
);
}
};

export const resolveSnapshotPath = async (
useSnapshot: boolean,
snapshotPath: string,
dataDir: string,
liskCoreV3DataPath: string,
) => {
if (!useSnapshot) return path.join(liskCoreV3DataPath, SNAPSHOT_DIR);
if (useSnapshot && !snapshotPath.endsWith('.tar.gz')) return snapshotPath;

const [snapshotDirNameExtracted] = (await getFiles(dataDir)) as string[];
const snapshotFilePathExtracted = path.join(dataDir, snapshotDirNameExtracted);
return snapshotFilePathExtracted;
};
24 changes: 24 additions & 0 deletions test/unit/utils/fs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
write,
copyFile,
read,
getFiles,
} from '../../../src/utils/fs';
import { configV3 } from '../fixtures/config';

Expand Down Expand Up @@ -143,3 +144,26 @@ describe('Test createTarball method', () => {
await expect(createTarball('', '')).rejects.toThrow();
});
});

describe('Test getFiles method', () => {
it('should get files when getFiles() method is called', async () => {
const directoryPath = join(__dirname, '../fixtures');
const files = await getFiles(directoryPath);
expect(files).toEqual(
expect.arrayContaining([
'blockchain.db.tar.gz',
'config.ts',
'customConfig.json',
'forgingStatus.json',
'genesis_assets.ts',
'genesis_block.json',
'lisk-core',
'sub-directory',
]),
);
});

it('should throw when called with empty string', async () => {
await expect(getFiles('')).rejects.toThrow();
});
});

0 comments on commit da7c553

Please sign in to comment.