@zombienet/orchestrator
Version:
ZombieNet aim to be a testing framework for substrate based blockchains, providing a simple cli tool that allow users to spawn and test ephemeral Substrate based networks
80 lines (79 loc) • 4.34 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.setupChainSpec = setupChainSpec;
exports.getChainSpecRaw = getChainSpecRaw;
const utils_1 = require("@zombienet/utils");
const fs_1 = require("fs");
const chainSpec_1 = require("../../chainSpec");
const constants_1 = require("../../constants");
const client_1 = require("../client");
const dynResourceDefinition_1 = require("./dynResourceDefinition");
const debug = require("debug")("zombie::native::chain-spec");
function setupChainSpec(namespace, chainConfig, chainName, chainFullPath) {
return __awaiter(this, void 0, void 0, function* () {
// We have two options to get the chain-spec file, neither should use the `raw` file/argument
// 1: User provide the file (we DON'T expect the raw file)
// 2: User provide the chainSpecCommand (without the --raw option)
const client = (0, client_1.getClient)();
if (chainConfig.chainSpecPath) {
// copy file to temp to use
yield fs_1.promises.copyFile(chainConfig.chainSpecPath, chainFullPath);
}
else {
if (chainConfig.chainSpecCommand) {
const { defaultImage, chainSpecCommand } = chainConfig;
const plainChainSpecOutputFilePath = client.remoteDir +
"/" +
constants_1.DEFAULT_CHAIN_SPEC.replace(/{{chainName}}/gi, chainName);
// set output of command
const fullCommand = `${chainSpecCommand.replace(/{{chainName}}/gi, chainName)} > ${plainChainSpecOutputFilePath}`;
const node = yield (0, dynResourceDefinition_1.createTempNodeDef)("temp", defaultImage, chainName, fullCommand);
const podDef = yield (0, dynResourceDefinition_1.genNodeDef)(namespace, node);
yield client.spawnFromDef(podDef);
yield fs_1.promises.copyFile(plainChainSpecOutputFilePath, chainFullPath);
}
}
});
}
function getChainSpecRaw(namespace, image, chainName, chainSpecCommand, chainFullPath) {
return __awaiter(this, void 0, void 0, function* () {
const client = (0, client_1.getClient)();
const remoteChainSpecFullPath = client.tmpDir +
"/" +
constants_1.DEFAULT_CHAIN_SPEC.replace(/{{chainName}}/, chainName);
const remoteChainSpecRawFullPath = client.tmpDir +
"/" +
constants_1.DEFAULT_CHAIN_SPEC_RAW.replace(/{{chainName}}/, chainName);
const chainSpecCommandRaw = chainSpecCommand.replace(/{{chainName}}/gi, remoteChainSpecFullPath);
const fullCommand = `${chainSpecCommandRaw} --raw > ${remoteChainSpecRawFullPath}`;
const node = yield (0, dynResourceDefinition_1.createTempNodeDef)("temp", image, chainName, fullCommand);
const podDef = yield (0, dynResourceDefinition_1.genNodeDef)(namespace, node);
const podName = podDef.metadata.name;
yield client.spawnFromDef(podDef);
// let's just wait 2 secs
yield (0, utils_1.sleep)(1000);
yield client.copyFileFromPod(podName, remoteChainSpecRawFullPath, chainFullPath, podName);
// We had some issues where the `raw` file is empty
// let's add some extra checks here to ensure we are ok.
let isValid = false;
try {
(0, chainSpec_1.readAndParseChainSpec)(chainFullPath);
isValid = true;
}
catch (e) {
debug(e);
}
if (!isValid)
throw new Error(`Invalid chain spec raw file generated.`);
yield client.putLocalMagicFile(podName, podName);
});
}