Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: chain sync projection primitives #520

Merged
merged 16 commits into from
Nov 18, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix(golden-test-generator)!: preserve BigInts by replacing serializer…
… to use toSerializableObj util

add metadata for supplied blockHeights option

export getBlockHeaderAndHash
  • Loading branch information
mkazlauskas committed Nov 18, 2022
commit d951df2c2d852a66f1392b4903c0d588b3916b3b
1 change: 0 additions & 1 deletion packages/golden-test-generator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
"commander": "^8.0.0",
"fs-extra": "^10.0.0",
"git-last-commit": "^1.0.0",
"json-bigint": "^1.0.0",
"object-hash": "^2.2.0",
"ts-log": "^2.2.3"
},
Expand Down
14 changes: 9 additions & 5 deletions packages/golden-test-generator/src/ChainSync/chainSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ export type RollForward = { type: 'rollForward'; block: Ogmios.Schema.Block };
export type RollBackward = { type: 'rollBackward'; rollback: Ogmios.Schema.TipOrOrigin };
export type ChainSyncEvent = RollForward | RollBackward;

export type GetBlocksResponse = GeneratorMetadata & {
type CardanoMetadata = Pick<GeneratorMetadata['metadata'], 'cardano'>;

export type GetBlocksResponse = {
events: ChainSyncEvent[];
metadata: CardanoMetadata
};

type RequestedBlocks = { [blockHeight: number]: Ogmios.Schema.Block };

const getBlockHeaderAndHash = (block: Ogmios.Schema.Block) => {
export const getBlockHeaderAndHash = (block: Ogmios.Schema.Block) => {
let header:
| (Ogmios.Schema.StandardBlock
| Ogmios.Schema.BlockShelley
Expand Down Expand Up @@ -78,14 +81,15 @@ export const getBlocks = async (
let currentBlock: number;
// Required to ensure existing messages in the pipe are not processed after the completion condition is met
let draining = false;
const metadata: GeneratorMetadata['metadata'] = {
const metadata: CardanoMetadata = {
cardano: {
compactGenesis: await Ogmios.StateQuery.genesisConfig(
await Ogmios.createInteractionContext(reject, logger.info, { connection: options.ogmiosConnectionConfig })
),
intersection: undefined as unknown as Ogmios.ChainSync.Intersection
}
},
};
const maxHeight = Math.max(...blockHeights);
try {
const syncClient = await Ogmios.createChainSyncClient(
await Ogmios.createInteractionContext(reject, logger.info, { connection: options.ogmiosConnectionConfig }),
Expand All @@ -103,7 +107,7 @@ export const getBlocks = async (
}
if (blockHeights.includes(currentBlock)) {
requestedBlocks[currentBlock] = block;
if (blockHeights[blockHeights.length - 1] === currentBlock) {
if (maxHeight === currentBlock) {
draining = true;
await syncClient.shutdown();
return resolve({
Expand Down
2 changes: 1 addition & 1 deletion packages/golden-test-generator/src/Content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export type Metadata = {
};
};

export type GeneratorMetadata = { metadata: { cardano: Metadata['cardano'] } };
export type GeneratorMetadata = { metadata: { cardano: Metadata['cardano']; options?: { blockHeights: string } } };

export const prepareContent = async <Body>(
metadata: Omit<Metadata, 'software'>,
Expand Down
36 changes: 22 additions & 14 deletions packages/golden-test-generator/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { Command } from 'commander';
import { GetBlocksResponse, getBlocks as chainSync } from './ChainSync';
import { Options, SingleBar } from 'cli-progress';
import { ensureDir, writeFile } from 'fs-extra';
import { prepareContent } from './Content';
import { GeneratorMetadata, prepareContent } from './Content';
import { createLogger } from 'bunyan';
import JSONBig from 'json-bigint';
import chalk from 'chalk';
import hash from 'object-hash';
import path from 'path';
import { toSerializableObject } from '@cardano-sdk/util';

const clear = require('clear');
const packageJson = require('../../package.json');
Expand Down Expand Up @@ -70,20 +70,15 @@ program
const fileName = path.join(outDir, `address-balances-${hash(content)}.json`);

logger.info(`Writing ${fileName}`);
await writeFile(fileName, JSONBig.stringify(content, undefined, 2));
await writeFile(fileName, JSON.stringify(toSerializableObject(content), undefined, 2));
process.exit(0);
} catch (error) {
console.error(error);
process.exit(1);
}
});

program
.command('chain-sync')
.description('Dump the requested blocks (rollForward) in their raw structure and simulate rollbacks')
.argument('[blockHeights]', `Comma-separated sorted list of blocks by number.
Use "-" for rollback to a block, e.g. 10,11,-10,11
Use ".." for block ranges (inclusive), e.g. 0..9`, (blockHeights) =>
const mapBlockHeights = (blockHeights: string) =>
blockHeights
.split(',')
.filter((b) => b !== '')
Expand All @@ -98,13 +93,20 @@ program
result.push(blockHeight)
}
return result;
})
)
});

program
.command('chain-sync')
rhyslbw marked this conversation as resolved.
Show resolved Hide resolved
.description('Dump the requested blocks (rollForward) in their raw structure and simulate rollbacks')
.argument('[blockHeights]', `Comma-separated sorted list of blocks by number.
Use "-" for rollback to a block, e.g. 10,11,-10,11
Use ".." for block ranges (inclusive), e.g. 0..9`)
.requiredOption('--out-dir [outDir]', 'File path to write results to')
.option('--log-level [logLevel]', 'Minimum log level', 'info')
.action(async (blockHeights: number[], { logLevel, outDir }) => {
.action(async (blockHeightsInput: string, { logLevel, outDir }) => {
try {
const { ogmiosHost, ogmiosPort, ogmiosTls } = program.opts();
const blockHeights = mapBlockHeights(blockHeightsInput);
const lastblockHeight = blockHeights[blockHeights.length - 1];
const logger = createLogger({ level: logLevel, name: 'chain-sync' });
const progress = createProgressBar(lastblockHeight);
Expand All @@ -117,12 +119,18 @@ program
progress.update(blockHeight);
}
});
const fullMetadata: GeneratorMetadata['metadata'] = {
...metadata,
options: {
blockHeights: blockHeightsInput
}
}
progress.stop();
const content = await prepareContent<GetBlocksResponse['events']>(metadata, data);
const content = await prepareContent<GetBlocksResponse['events']>(fullMetadata, data);
const fileName = path.join(outDir, `blocks-${hash(content)}.json`);

logger.info(`Writing ${fileName}`);
await writeFile(fileName, JSONBig.stringify(content, undefined, 2));
await writeFile(fileName, JSON.stringify(toSerializableObject(content), undefined, 2));
process.exit(0);
} catch (error) {
console.error(error);
Expand Down
Loading