datahaven/test/utils/parameters.ts
Facundo Farall d2bf185bcc
feat: 🚀 Add deploy command to CLI (#87)
### New Features
1. Add the `deploy` command to our CLI.
1. Conditionally deploys kurtosis eth network if we're in `stagenet`
environment.
    2. Deploys DH nodes.
3. Deploys contracts (all of them). In `mainnet` and `testnet` it
shouldn't deploy EL contracts, but for now that's not implemented.
4. Configures parameters, validators and relayers in the same way as
`launch`.
5. Currently, it only deploys `beefy` and `beacon` relayers, `execution`
and `solochain` relayers are pending for a subsequent PR.
2. Add `waitFor` utility function that receives a lambda.

### Refactors
1. Several common functionalities used both by the `launch` and `deploy`
command have been moved to the `cli/handlers/common` directory, from
where both commands use them. These include
    1. Checks for installed dependencies.
    2. Common constants.
    3. The `LaunchedNetwork` class has been moved to this directory.
    4. DataHaven nodes common functions.
    5. Kurtosis common functions.
    6. Relayer common functions.
7. Kubernetes functions (although only used by `deploy`, it seemed
fitting to have it here still).
8. Remove CLI questions and separator prints from `deploy-contracts.ts`
and `set-datahaven-parameters.ts` scripts. These things should be in the
`cli/launch` folder, which consumes the functions in these scripts.
9. Remove `setParametersFromCollection` from `utils` folder and put it
in `cli`.
10. Create base snowbridge relayer configs for `local` and `stagenet` as
two separate directories.

### Fixes
1. Sets the default time of the `deploy` command to 6s as Lodestar is
slower than Lighthouse.
2. In `runShellCommandWithLogger` only print `stderr` if the command
fails.

### Additional Minor Changes
1. K8s secret key names changed from `dh-beefy-relay-eth-key` to
`dh-beefy-relay-ethereum-key` and `dh-beacon-relay-sub-key` to
`dh-beacon-relay-substrate-key`, for simplicity in the deployment
script.
11. Update suggested configs for `.vscode` configs.

---------

Co-authored-by: Steve Degosserie <723552+stiiifff@users.noreply.github.com>
2025-06-12 10:24:03 +02:00

86 lines
3.1 KiB
TypeScript

import path from "node:path";
import { $ } from "bun";
import { logger } from "utils";
import type { ParsedDataHavenParameter } from "utils/types";
// Constants for paths
export const PARAMETERS_TEMPLATE_PATH = "configs/parameters/datahaven-parameters.json";
export const PARAMETERS_OUTPUT_DIR = "tmp/configs";
export const PARAMETERS_OUTPUT_FILE = "datahaven-parameters.json";
export const PARAMETERS_OUTPUT_PATH = path.join(PARAMETERS_OUTPUT_DIR, PARAMETERS_OUTPUT_FILE);
/**
* A collection of parameters to be set in the DataHaven runtime.
* This class is used to collect parameters from different steps of the launch process
* and then generate a JSON file to be used by the setDataHavenParameters script.
*/
export class ParameterCollection {
private parameters: ParsedDataHavenParameter[] = [];
/**
* Adds a parameter to the collection
* @param param The parameter to add
*/
public addParameter(param: ParsedDataHavenParameter): void {
// Check if parameter with same name already exists
const existingIndex = this.parameters.findIndex((p) => p.name === param.name);
if (existingIndex !== -1) {
// Replace existing parameter
this.parameters[existingIndex] = param;
logger.debug(`Updated parameter: ${String(param.name)} = ${JSON.stringify(param.value)}`);
} else {
// Add new parameter
this.parameters.push(param);
logger.debug(`Added parameter: ${String(param.name)} = ${JSON.stringify(param.value)}`);
}
}
/**
* Returns the current parameters
*/
public getParameters(): ParsedDataHavenParameter[] {
return [...this.parameters];
}
/**
* Generates a JSON file with the parameters collected so far
*/
public async generateParametersFile(): Promise<string> {
logger.debug(`Ensuring output directory exists: ${PARAMETERS_OUTPUT_DIR}`);
await $`mkdir -p ${PARAMETERS_OUTPUT_DIR}`.quiet();
// If we have no parameters, load the template to get the structure
if (this.parameters.length === 0) {
logger.debug(`No parameters collected, loading template from ${PARAMETERS_TEMPLATE_PATH}`);
const templateFile = Bun.file(PARAMETERS_TEMPLATE_PATH);
if (!(await templateFile.exists())) {
throw new Error(`Template file ${PARAMETERS_TEMPLATE_PATH} does not exist`);
}
this.parameters = await templateFile.json();
}
// Write the parameters to a file
logger.debug(`Writing parameters to ${PARAMETERS_OUTPUT_PATH}`);
await Bun.write(PARAMETERS_OUTPUT_PATH, JSON.stringify(this.parameters, null, 2));
logger.debug(`Parameters file generated at ${PARAMETERS_OUTPUT_PATH}`);
return PARAMETERS_OUTPUT_PATH;
}
}
/**
* Creates a new ParameterCollection, pre-loaded with template parameters if available
*/
export const createParameterCollection = async (): Promise<ParameterCollection> => {
const collection = new ParameterCollection();
const templateFile = Bun.file(PARAMETERS_TEMPLATE_PATH);
if (await templateFile.exists()) {
const templateParams = await templateFile.json();
for (const param of templateParams) {
collection.addParameter(param);
}
}
return collection;
};