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

Commit

Permalink
Subscribe to new block event
Browse files Browse the repository at this point in the history
  • Loading branch information
nagdahimanshu committed Sep 19, 2023
1 parent ee0dc3d commit 3a72e5c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,29 @@
*
* Removal or modification of this copyright notice is prohibited.
*/
import { resolve } from 'path';
import { createIPCClient, APIClient } from '@liskhq/lisk-api-client';
import { Block } from '@liskhq/lisk-chain';
import { NEW_BLOCK_EVENT_NAME } from './constants';
import { write } from './utils/fs';

export const getAPIClient = async (liskCorePath: string): Promise<APIClient> => {
const client = await createIPCClient(liskCorePath);
return client;
};

export const subscribeToNewBlockEvent = (
client: APIClient,
snapshotHeight: number,
outputDir: string,
) => {
client.subscribe(NEW_BLOCK_EVENT_NAME, async data => {
const { block: encodedBlock } = (data as unknown) as Record<string, string>;
const newBlock = client.block.decode(Buffer.from(encodedBlock, 'hex')) as Block;
if (newBlock.header.height === snapshotHeight) {
const forgingStatus = await client.invoke('app:getForgingStatus');
const forgingStatusJsonFilepath = resolve(outputDir, 'forgingStatus.json');
await write(forgingStatusJsonFilepath, JSON.stringify(forgingStatus));
}
});
};
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,4 @@ export const DEFAULT_LISK_CORE_PATH = '~/.lisk/lisk-core';
export const LEGACY_DB_PATH = `${DEFAULT_LISK_CORE_PATH}/${DEFAULT_DATA_DIR}/legacy.db`;

export const DEFAULT_VERSION = '0.1.0';
export const NEW_BLOCK_EVENT_NAME = 'app:block:new';
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
SNAPSHOT_TIME_GAP,
LEGACY_DB_PATH,
} from './constants';
import { getAPIClient } from './client';
import { getAPIClient, subscribeToNewBlockEvent } from './client';
import {
getConfig,
migrateUserConfig,
Expand Down Expand Up @@ -140,6 +140,8 @@ class LiskMigrator extends Command {
const networkConstant = NETWORK_CONSTANT[networkIdentifier] as NetworkConfigLocal;
const outputDir = `${outputPath}/${networkIdentifier}`;

subscribeToNewBlockEvent(client, snapshotHeight, outputDir);

if (autoStartLiskCoreV4) {
if (!networkConstant) {
this.error(
Expand Down
10 changes: 10 additions & 0 deletions src/utils/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,13 @@ export const copyDir = async (src: string, dest: string) => {
: await fs.promises.copyFile(srcPath, destPath);
}
};

export const write = async (filePath: string, content: string): Promise<boolean | Error> =>
new Promise((resolve, reject) => {
fs.writeFile(filePath, content, err => {
if (err) {
return reject(err);
}
return resolve(true);
});
});

0 comments on commit 3a72e5c

Please sign in to comment.