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

Commit

Permalink
Merge pull request #185 from LiskHQ/182-export-genesis-block-tar-and-…
Browse files Browse the repository at this point in the history
…sha256-file-to-the-output-directory

Export genesis block tar and SHA256 file to the output directory
  • Loading branch information
sameersubudhi authored Sep 29, 2023
2 parents 090e5fb + 1d6c6fc commit c65977c
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 9 deletions.
12 changes: 11 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ import {
setPrevSnapshotBlockHeightByNetID,
} from './utils/chain';
import { captureForgingStatusAtSnapshotHeight } from './events';
import { copyGenesisBlock, createGenesisBlock, writeGenesisAssets } from './utils/genesis_block';
import {
copyGenesisBlock,
createGenesisBlock,
writeGenesisAssets,
writeGenesisBlock,
} from './utils/genesis_block';
import { CreateAsset } from './createAsset';
import { ApplicationConfigV3, NetworkConfigLocal, NodeInfo } from './types';
import { installLiskCore, startLiskCore } from './utils/node';
Expand Down Expand Up @@ -252,6 +257,11 @@ class LiskMigrator extends Command {
);
cli.action.stop();

cli.action.start('Creating genesis block tar and SHA256 files');
await writeGenesisBlock(outputDir);
this.log(`Genesis block tar and SHA256 files have been created at: ${outputDir}.`);
cli.action.stop();

if (autoStartLiskCoreV4) {
try {
if (!autoMigrateUserConfig) {
Expand Down
31 changes: 24 additions & 7 deletions src/utils/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import { homedir } from 'os';
import * as tar from 'tar';
import fs from 'fs';
import { join } from 'path';
import path from 'path';

export const extractTarBall = async (
srcFilePath: string,
Expand All @@ -31,9 +31,9 @@ export const extractTarBall = async (
fileStream.on('end', () => setTimeout(resolve.bind(null, true), 100));
});

export const exists = async (path: string): Promise<boolean | Error> => {
export const exists = async (inputPath: string): Promise<boolean | Error> => {
try {
await fs.promises.access(path);
await fs.promises.access(inputPath);
return true;
} catch (_) {
return false;
Expand All @@ -48,9 +48,9 @@ export const rmdir = async (directoryPath: string, options = {}): Promise<boolea
});
});

export const resolveAbsolutePath = (path: string) => {
export const resolveAbsolutePath = (inputPath: string) => {
const homeDirectory = homedir();
return homeDirectory ? path.replace(/^~(?=$|\/|\\)/, homeDirectory) : path;
return homeDirectory ? inputPath.replace(/^~(?=$|\/|\\)/, homeDirectory) : inputPath;
};

/* eslint-disable @typescript-eslint/no-unused-expressions */
Expand All @@ -59,8 +59,8 @@ export const copyDir = async (src: string, dest: string) => {
const files = await fs.promises.readdir(src, { withFileTypes: true });

for (const fileInfo of files) {
const srcPath = join(src, fileInfo.name);
const destPath = join(dest, fileInfo.name);
const srcPath = path.join(src, fileInfo.name);
const destPath = path.join(dest, fileInfo.name);

fileInfo.isDirectory()
? await copyDir(srcPath, destPath)
Expand All @@ -87,3 +87,20 @@ export const copyFile = async (src: string, dest: string): Promise<boolean | Err
return resolve(true);
});
});

export const createTarball = async (filePath: string, outputDir: string) =>
new Promise((resolve, reject) => {
const fileName = path.basename(filePath);

tar
.create(
{
gzip: true,
file: `${outputDir}/${fileName}.tar.gz`,
cwd: outputDir,
},
[fileName],
)
.then(() => resolve(true))
.catch(err => reject(err));
});
22 changes: 21 additions & 1 deletion src/utils/genesis_block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { Block as BlockVersion3 } from '@liskhq/lisk-chain';
import { SNAPSHOT_TIME_GAP } from '../constants';
import { GenesisAssetEntry } from '../types';
import { execAsync } from './process';
import { copyFile } from './fs';
import { copyFile, createTarball } from './fs';

(BigInt.prototype as any).toJSON = function () {
return this.toString();
Expand Down Expand Up @@ -75,3 +75,23 @@ export const copyGenesisBlock = async (
currGenesisBlockFilepath: string,
liskCoreV4ConfigPath: string,
): Promise<boolean | Error> => copyFile(currGenesisBlockFilepath, liskCoreV4ConfigPath);

export const writeGenesisBlock = async (outputDir: string): Promise<void> => {
const genesisBlockJsonFilepath = path.resolve(outputDir, 'genesis_block.json');
await createTarball(genesisBlockJsonFilepath, outputDir);

const genesisBlockBlobFilepath = path.resolve(outputDir, 'genesis_block.blob');
await createTarball(genesisBlockBlobFilepath, outputDir);

const genesisBlockJsonHash = await createChecksum(`${genesisBlockJsonFilepath}.tar.gz`);
fs.writeFileSync(
path.resolve(outputDir, 'genesis_block.json.tar.gz.SHA256'),
genesisBlockJsonHash,
);

const genesisBlockBlobHash = await createChecksum(`${genesisBlockBlobFilepath}.tar.gz`);
fs.writeFileSync(
path.resolve(outputDir, 'genesis_block.blob.tar.gz.SHA256'),
genesisBlockBlobHash,
);
};
26 changes: 26 additions & 0 deletions test/unit/fixtures/genesis_block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"header": {
"version": 0,
"timestamp": 1693395510,
"height": 1,
"previousBlockID": "21bf5541c661ad72167bb1238fa81695ca6df00bbb8986262551bb6f7f14d703",
"stateRoot": "f5535322e2a338f76b007b3e1831eb632b51f80971c385b5d3a172064df960eb",
"assetRoot": "523ec59f002f996ed8cfb9598df17b1e5d16f16b2d45f88a1db29d83d237f126",
"eventRoot": "85775091c4b25cbdbd46e1efc4fb3d19264d6939290d18feece714daad259d0f",
"transactionRoot": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
"validatorsHash": "652b84751db42b3767900900cf39ffe93fcf51be2a29d1b0eba2c56295305940",
"aggregateCommit": {
"height": 0,
"aggregationBits": "",
"certificateSignature": ""
},
"generatorAddress": "lskzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz5fw596",
"maxHeightPrevoted": 20220446,
"maxHeightGenerated": 0,
"impliesMaxPrevotes": true,
"signature": "",
"id": "8a4da363d192c70237d2473041f6a131298693d5b6714e0a3024e8a476923001"
},
"transactions": [],
"assets": []
}
19 changes: 19 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 {
copyDir,
write,
copyFile,
createTarball,
} from '../../../src/utils/fs';
import { configV3 } from '../fixtures/config';

Expand Down Expand Up @@ -126,3 +127,21 @@ describe('Test copyFile method', () => {
await expect(copyFile('', '')).rejects.toThrow();
});
});

describe('Test createTarball method', () => {
it('should create tar.gz file', async () => {
const filePath = join(__dirname, '../fixtures/genesis_block.json');
const outputDir = `${testDir}/fixtures`;
const expectedTarFilePath = `${testDir}/fixtures/genesis_block.json.tar.gz`;

expect(await exists(expectedTarFilePath)).toBe(false);

await createTarball(filePath, outputDir);

expect(await exists(expectedTarFilePath)).toBe(true);
});

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

0 comments on commit c65977c

Please sign in to comment.