Skip to content

Commit

Permalink
feat(consensus): add new config.ts file to handle consensus configura…
Browse files Browse the repository at this point in the history
…tion

This new file includes a Config type and a readConfig function to read and parse the configuration from a file. It also includes helper functions to extract values from the configuration file and to generate the grace period end value. This change allows for more flexible and maintainable configuration handling.
  • Loading branch information
MartinMinkov committed Aug 16, 2023
1 parent 93a2735 commit 90b31ca
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/consensus/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import fs from 'fs';
export { Config, readConfig };

type Config = {
k: number;
delta: number;
slotsPerEpoch: number;
slotsPerSubWindow: number;
subWindowsPerWindow: number;
blockWindowDuration: number;
gracePeriodEnd: number;
};

function extractValue(contents: string, key: string): number {
const regex = new RegExp(`\\[%%define ${key} (?:")?(\\d+)(?:")?\\]`);
const match = contents.match(regex);
if (!match) {
throw new Error(`Key ${key} not found in config file.`);
}
return parseInt(match[1], 10);
}

function genGracePeriodEnd(
slotsPerEpoch: number,
blockWindowDuration: number
): number {
const numDays = 3;
const gracePeriodEnd = Math.min(
(numDays * 24 * 60 * 60 * 1000) / blockWindowDuration,
slotsPerEpoch
);
return gracePeriodEnd;
}

function readConfig(path: string): Config {
const contents = fs.readFileSync(path, 'utf8');

const blockWindowDuration = extractValue(contents, 'block_window_duration');
const slotsPerEpoch = extractValue(contents, 'slots_per_epoch');
const gracePeriodEnd = genGracePeriodEnd(slotsPerEpoch, blockWindowDuration);

return {
k: extractValue(contents, 'k'),
delta: extractValue(contents, 'delta'),
slotsPerEpoch,
slotsPerSubWindow: extractValue(contents, 'slots_per_sub_window'),
subWindowsPerWindow: extractValue(contents, 'sub_windows_per_window'),
blockWindowDuration,
gracePeriodEnd,
};
}

0 comments on commit 90b31ca

Please sign in to comment.