Skip to content

Commit

Permalink
refactor configLine into struct
Browse files Browse the repository at this point in the history
  • Loading branch information
toni-calvin committed Feb 26, 2024
1 parent ba00a14 commit 629c325
Showing 1 changed file with 98 additions and 30 deletions.
128 changes: 98 additions & 30 deletions infrastructure/zk/src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,86 +140,154 @@ export async function submoduleUpdate() {
await utils.exec('git submodule update');
}

function updateConfigFile(path: string, modeConstantValues: Record<string, number | string | null>) {
interface ConfigLine {
key: string;
value: string | number | null;
section: string | null;
lineIndex?: number | null;
}

function updateConfigFile(path: string, modeConstantValues: ConfigLine[]) {
let content = fs.readFileSync(path, 'utf-8');
let lines = content.split('\n');
let addedContent: string | undefined;
const lineIndices: Record<string, number> = {};

// Iterate through each line in the file
for (let i = 0; i < lines.length; i++) {
const line = lines[i];

// Check if the line does not start with '#' (comment)
if (!line.startsWith('#')) {
// Using regex to match key-value pairs in the line
const match = line.match(/([^=]+)=(.*)/);

if (match) {
// Extract the key and trim any leading/trailing whitespaces
const key = match[1].trim();

// Store the index of the line where the key is found
lineIndices[key] = i;
}
}
}

for (const [key, value] of Object.entries(modeConstantValues)) {
const lineIndex = lineIndices[key];


// Iterate through each key-value pair in modeConstantValues
modeConstantValues.forEach((configLine) =>{
// Get the line index for the current key from the record
const lineIndex = lineIndices[configLine.key];

// Check if [key, value] to append/remove is found in the file
if (lineIndex !== undefined) {
if (value !== null) {
lines.splice(lineIndex, 1, `${key}=${value}`);
// If the value to insert is not null, update the line with the new value
if (configLine.value !== null) {
lines.splice(lineIndex, 1, `${configLine.key}=${configLine.value}`);
} else {
// If the value is null, remove the line and update line indices
lines.splice(lineIndex, 1);

// Update line indices for keys that appear after the removed line
for (const [k, index] of Object.entries(lineIndices)) {
if (index > lineIndex) {
lineIndices[k] = index - 1;
}
}
}
} else {
if (value !== null) {
addedContent = `${key}=${value}\n`;
// If it is not found, append the key-value pair to the end of the file content
if (configLine.value !== null) {
addedContent = `${configLine.key}=${configLine.value}\n`;
}
}
}
});

// Join the lines back into a single string with line breaks
content = lines.join('\n');

// Append the additional content (if any) to the end of the file content
if (addedContent) {
content += addedContent;
}

// Write the modified content back to the file
fs.writeFileSync(path, content);

}


function updateChainConfig(validiumMode: boolean) {
const modeConstantValues = {
compute_overhead_part: validiumMode
? constants.VALIDIUM_COMPUTE_OVERHEAD_PART
: constants.ROLLUP_COMPUTE_OVERHEAD_PART,
pubdata_overhead_part: validiumMode
? constants.VALIDIUM_PUBDATA_OVERHEAD_PART
: constants.ROLLUP_PUBDATA_OVERHEAD_PART,
batch_overhead_l1_gas: validiumMode
? constants.VALIDIUM_BATCH_OVERHEAD_L1_GAS
: constants.ROLLUP_BATCH_OVERHEAD_L1_GAS,
max_pubdata_per_batch: validiumMode
? constants.VALIDIUM_MAX_PUBDATA_PER_BATCH
: constants.ROLLUP_MAX_PUBDATA_PER_BATCH,
l1_batch_commit_data_generator_mode: validiumMode
? constants.VALIDIUM_L1_BATCH_COMMIT_DATA_GENERATOR_MODE
: constants.ROLLUP_L1_BATCH_COMMIT_DATA_GENERATOR_MODE
};

const modeConstantValues: ConfigLine[] = [
{
key: 'compute_overhead_part',
value: validiumMode
? constants.VALIDIUM_COMPUTE_OVERHEAD_PART
: constants.ROLLUP_COMPUTE_OVERHEAD_PART,
section: null,
},
{
key: 'pubdata_overhead_part',
value: validiumMode
? constants.VALIDIUM_PUBDATA_OVERHEAD_PART
: constants.ROLLUP_PUBDATA_OVERHEAD_PART,
section: null,
},
{
key: 'batch_overhead_l1_gas',
value: validiumMode
? constants.VALIDIUM_BATCH_OVERHEAD_L1_GAS
: constants.ROLLUP_BATCH_OVERHEAD_L1_GAS,
section: null,
},
{
key: 'max_pubdata_per_batch',
value: validiumMode
? constants.VALIDIUM_MAX_PUBDATA_PER_BATCH
: constants.ROLLUP_MAX_PUBDATA_PER_BATCH,
section: null,
},
{
key: 'l1_batch_commit_data_generator_mode',
value: validiumMode
? constants.VALIDIUM_L1_BATCH_COMMIT_DATA_GENERATOR_MODE
: constants.ROLLUP_L1_BATCH_COMMIT_DATA_GENERATOR_MODE,
section: null,
},
];
updateConfigFile(CHAIN_CONFIG_PATH, modeConstantValues);
}
function updateEthSenderConfig(validiumMode: boolean) {
// This constant is used in validium mode and is deleted in rollup mode
// In order to pass the existing integration tests
const modeConstantValues = {
l1_gas_per_pubdata_byte: validiumMode
? constants.VALIDIUM_L1_GAS_PER_PUBDATA_BYTE
: constants.ROLLUP_L1_GAS_PER_PUBDATA_BYTE
};
const modeConstantValues: ConfigLine[] = [
{
key: 'l1_gas_per_pubdata_byte',
value: validiumMode
? constants.VALIDIUM_L1_GAS_PER_PUBDATA_BYTE
: constants.ROLLUP_L1_GAS_PER_PUBDATA_BYTE,
section: 'en',
},
];
updateConfigFile(ETH_SENDER_PATH, modeConstantValues);
}

function updateExtNodeConfig(validiumMode: boolean) {
const modeConstantValues: ConfigLine[] = [
{
key: 'l1_batch_commit_data_generator_mode',
value: validiumMode ? 'Validium' : null,
section: 'en',
},
];
updateConfigFile('etc/env/ext-node.toml', modeConstantValues);
}

function updateConfig(validiumMode: boolean) {
updateChainConfig(validiumMode);
updateEthSenderConfig(validiumMode);
updateExtNodeConfig(validiumMode);
config.compileConfig();
let envFileContent = fs.readFileSync(process.env.ENV_FILE!).toString();
envFileContent += `VALIDIUM_MODE=${validiumMode}\n`;
Expand Down

0 comments on commit 629c325

Please sign in to comment.