diff --git a/packages/scripts/integration-testing/cosmos.ts b/packages/scripts/integration-testing/cosmos.ts index 995544f630..3a09a11ba4 100644 --- a/packages/scripts/integration-testing/cosmos.ts +++ b/packages/scripts/integration-testing/cosmos.ts @@ -7,7 +7,13 @@ import util from "util"; import { cloneRepo } from "./git"; import { zodTryParseJSON } from "../../utils/sanitize"; -import { mustGetAttr, replaceInFile, sleep, zodTxResult } from "../lib"; +import { + execPromise, + mustGetAttr, + replaceInFile, + sleep, + zodTxResult, +} from "../lib"; export const startCosmosLocalnet = async ( binaryPath: string, @@ -22,7 +28,7 @@ export const startCosmosLocalnet = async ( // run teritorid init --chain-id=testing testing --home=$HOME/.teritorid const initCmd = `${binaryPath} init testing --chain-id testing --home ${home}`; console.log("> " + initCmd); - child_process.execSync(initCmd, { stdio: "ignore" }); + await execPromise(initCmd); await replaceInFile( path.join(home, "config/genesis.json"), @@ -33,27 +39,27 @@ export const startCosmosLocalnet = async ( // run teritorid keys add validator --keyring-backend=test --home=$HOME/.teritorid const addValidatorCmd = `${binaryPath} keys add validator --keyring-backend=test --home ${home}`; console.log("> " + addValidatorCmd); - child_process.execSync(addValidatorCmd); + await execPromise(addValidatorCmd); // run teritorid add-genesis-account $(teritorid keys show validator -a --keyring-backend=test --home=$HOME/.teritorid) 100000000000utori,100000000000stake --home=$HOME/.teritorid const addGenesisAccountCmd = `${binaryPath} add-genesis-account $(${binaryPath} keys show validator -a --keyring-backend=test --home ${home}) 100000000000000000utori,100000000000000000stake --home ${home}`; console.log("> " + addGenesisAccountCmd); - child_process.execSync(addGenesisAccountCmd); + await execPromise(addGenesisAccountCmd); // run teritorid keys add testnet-adm --keyring-backend=test --home=$HOME/.teritorid const addTestnetAdmCmd = `${binaryPath} keys add testnet-adm --keyring-backend=test --home ${home}`; console.log("> " + addTestnetAdmCmd); - child_process.execSync(addTestnetAdmCmd); + await execPromise(addTestnetAdmCmd); // run teritorid add-genesis-account $(teritorid keys show validator -a --keyring-backend=test --home=$HOME/.teritorid) 100000000000utori,100000000000stake --home=$HOME/.teritorid const addTestnetAdmGenesisAccountCmd = `${binaryPath} add-genesis-account $(${binaryPath} keys show testnet-adm -a --keyring-backend=test --home ${home}) 100000000000000000utori,100000000000000000stake --home ${home}`; console.log("> " + addTestnetAdmGenesisAccountCmd); - child_process.execSync(addTestnetAdmGenesisAccountCmd); + await execPromise(addTestnetAdmGenesisAccountCmd); // run teritorid gentx validator 500000000stake --keyring-backend=test --home=$HOME/.teritorid --chain-id=testing const gentxCmd = `${binaryPath} gentx validator 500000000stake --keyring-backend=test --home ${home} --chain-id=testing`; console.log("> " + gentxCmd); - child_process.execSync(gentxCmd); + await execPromise(gentxCmd); // run teritorid collect-gentxs --home=$HOME/.teritorid const collectGentxsCmd = `${binaryPath} collect-gentxs --home ${home}`; console.log("> " + collectGentxsCmd); - child_process.execSync(collectGentxsCmd, { stdio: "ignore" }); + await execPromise(collectGentxsCmd); } const cmd = `${binaryPath} start --home ${home}`; console.log("> " + cmd); @@ -79,8 +85,9 @@ const waitForHeight = async (port: number, targetHeight: bigint) => { console.log("> " + statusCmd); while (true) { try { + const result = await execPromise(statusCmd); // eslint-disable-next-line no-restricted-syntax - const status = JSON.parse(child_process.execSync(statusCmd).toString()); + const status = JSON.parse(result.stdout); const height = BigInt(status.result.sync_info.latest_block_height); //console.log(`height: ${height}`); if (height < targetHeight) { @@ -102,7 +109,9 @@ export const upgradeCosmosLocalnet = async ( const statusCmd = `curl -s http://localhost:26657/status`; console.log("> " + statusCmd); // eslint-disable-next-line no-restricted-syntax - const status = JSON.parse(child_process.execSync(statusCmd).toString()); + const status = JSON.parse( + (await execPromise(statusCmd, { encoding: "utf-8" })).stdout, + ); let height = BigInt(status.result.sync_info.latest_block_height); height += BigInt(8); @@ -114,8 +123,8 @@ export const upgradeCosmosLocalnet = async ( */ const cmd = `${oldBinaryPath} tx gov submit-proposal software-upgrade "${newVersion}" --upgrade-height=${height} --title="Upgrade to ${newVersion}" --description="Upgrade to ${newVersion}" --from=${validatorWalletName} --keyring-backend=test --chain-id=testing --home=${home} --yes -b block --deposit="500000000stake" --node http://127.0.0.1:26657 -o json`; console.log("> " + cmd); - const out = child_process.execSync(cmd, { encoding: "utf-8" }); - const outObj = zodTryParseJSON(zodTxResult, out.toString()); + const { stdout: out } = await execPromise(cmd, { encoding: "utf-8" }); + const outObj = zodTryParseJSON(zodTxResult, out); if (!outObj) { throw new Error("Failed to parse submit-proposal result"); } @@ -126,14 +135,14 @@ export const upgradeCosmosLocalnet = async ( */ const voteCmd = `${oldBinaryPath} tx gov vote ${proposalId} yes --from ${validatorWalletName} --chain-id testing --home ${home} -b block -y --keyring-backend test --node http://127.0.0.1:26657`; console.log("> " + voteCmd); - child_process.execSync(voteCmd); + await execPromise(voteCmd); await waitForHeight(26657, height); // check proposal status const proposalCmd = `${oldBinaryPath} query gov proposal ${proposalId} --chain-id testing --home ${home} -o json --node http://127.0.0.1:26657`; console.log("> " + proposalCmd); - const proposalOut = child_process.execSync(proposalCmd, { + const { stdout: proposalOut } = await execPromise(proposalCmd, { encoding: "utf-8", }); // TODO: compare date diff --git a/packages/scripts/integration-testing/upgradeTest142toDir.ts b/packages/scripts/integration-testing/upgradeTest142toDir.ts index 652aeec0b5..2c2e13ebfd 100644 --- a/packages/scripts/integration-testing/upgradeTest142toDir.ts +++ b/packages/scripts/integration-testing/upgradeTest142toDir.ts @@ -16,61 +16,66 @@ import { deployTeritoriEcosystem } from "../network-setup/deployLib"; const repoURL = "https://github.com/TERITORI/teritori-chain.git"; const main = async () => { - program.argument( - "", - "Path to the repo to build latest binary from", - ); - program.parse(); - const [repoPath] = program.args; + try { + program.argument( + "", + "Path to the repo to build latest binary from", + ); + program.parse(); + const [repoPath] = program.args; - const binaries = await buildBinaries(repoURL, "teritorid", [ - "v1.4.2", - ] as const); - const latestBinaryPath = await buildCosmos(repoPath, "teritorid"); + const binaries = await buildBinaries(repoURL, "teritorid", [ + "v1.4.2", + ] as const); + const latestBinaryPath = await buildCosmos(repoPath, "teritorid"); - const { - home, - result: v142Result, - process: v142Process, - validatorWalletName, - } = await startCosmosLocalnet(binaries["v1.4.2"]); + const { + home, + result: v142Result, + process: v142Process, + validatorWalletName, + } = await startCosmosLocalnet(binaries["v1.4.2"]); - const upgradeHeight = await upgradeCosmosLocalnet( - binaries["v1.4.2"], - "v2.0.0", - validatorWalletName, - home, - ); - v142Process.kill(os.constants.signals.SIGINT); - await v142Result; + const upgradeHeight = await upgradeCosmosLocalnet( + binaries["v1.4.2"], + "v2.0.0", + validatorWalletName, + home, + ); + v142Process.kill(os.constants.signals.SIGINT); + await v142Result; - await replaceInFile( - path.join(home, "config/app.toml"), - `minimum-gas-prices = ""`, - `minimum-gas-prices = "0stake"`, - ); + await replaceInFile( + path.join(home, "config/app.toml"), + `minimum-gas-prices = ""`, + `minimum-gas-prices = "0stake"`, + ); - // start next version - const { result, process: v203Process } = await startCosmosLocalnet( - latestBinaryPath, - { - home, - height: upgradeHeight, - }, - ); + // start next version + const { result, process: v203Process } = await startCosmosLocalnet( + latestBinaryPath, + { + home, + height: upgradeHeight, + }, + ); - // test cosmwasm - await deployTeritoriEcosystem( - { binaryPath: latestBinaryPath, home }, - teritoriLocalnetNetwork.id, - "testnet-adm", - ); + // test cosmwasm + await deployTeritoriEcosystem( + { binaryPath: latestBinaryPath, home }, + teritoriLocalnetNetwork.id, + "testnet-adm", + ); - // stop - v203Process.kill(os.constants.signals.SIGINT); - await result; + // stop + v203Process.kill(os.constants.signals.SIGINT); + await result; - await fs.rm(home, { recursive: true, force: true }); + await fs.rm(home, { recursive: true, force: true }); + } catch (e) { + console.error(e); + process.exit(1); + } }; main(); diff --git a/packages/scripts/lib.ts b/packages/scripts/lib.ts index 767e8948db..e888aeb474 100644 --- a/packages/scripts/lib.ts +++ b/packages/scripts/lib.ts @@ -1,5 +1,7 @@ import { NodeHttpTransport } from "@improbable-eng/grpc-web-node-http-transport"; +import child_process from "child_process"; import fs from "fs/promises"; +import util from "util"; import { z } from "zod"; import { @@ -104,3 +106,5 @@ export const replaceInFile = async ( const newData = data.replace(match, repl); await fs.writeFile(filePath, newData); }; + +export const execPromise = util.promisify(child_process.exec); diff --git a/packages/scripts/network-setup/deployLib.ts b/packages/scripts/network-setup/deployLib.ts index 0af9adf2d6..78a5727c67 100644 --- a/packages/scripts/network-setup/deployLib.ts +++ b/packages/scripts/network-setup/deployLib.ts @@ -1,6 +1,5 @@ import { IndexedTx } from "@cosmjs/stargate"; import { bech32 } from "bech32"; -import child_process from "child_process"; import { cloneDeep } from "lodash"; import path from "path"; @@ -16,7 +15,7 @@ import { mustGetNonSigningCosmWasmClient, } from "../../networks"; import { zodTryParseJSON } from "../../utils/sanitize"; -import { injectRPCPort, retry, sleep, zodTxResult } from "../lib"; +import { execPromise, injectRPCPort, retry, sleep, zodTxResult } from "../lib"; import sqh from "../sqh"; export const deployTeritoriEcosystem = async ( @@ -31,12 +30,12 @@ export const deployTeritoriEcosystem = async ( } console.log(`Deploying to ${network.displayName}`); - let walletAddr = child_process - .execSync( + let walletAddr = ( + await execPromise( `${opts.binaryPath} keys show --keyring-backend test -a ${wallet} --home ${opts.home}`, + { encoding: "utf-8" }, ) - .toString() - .trim(); + ).stdout.trim(); if (walletAddr.startsWith("Successfully migrated")) { walletAddr = walletAddr.substring(walletAddr.indexOf("\n")).trim(); } @@ -191,12 +190,11 @@ const instantiateNameService = async ( network.rpcEndpoint, )} --yes --keyring-backend test -o json --home ${opts.home}`; console.log("> " + cmd); - let out = await retry(5, () => - child_process.execSync(cmd, { - stdio: ["inherit", "pipe", "inherit"], + let { stdout: out } = await retry(5, async () => { + return await execPromise(cmd, { encoding: "utf-8", - }), - ); + }); + }); if (!out.startsWith("{")) { out = out.substring(out.indexOf("{")); } @@ -227,11 +225,12 @@ const instantiateContract = async ( label, )} --admin ${admin} --home ${opts.home}`; console.log("> " + cmd); - let out = await retry(5, () => - child_process.execSync(cmd, { - stdio: ["inherit", "pipe", "inherit"], - encoding: "utf-8", - }), + let { stdout: out } = await retry( + 5, + async () => + await execPromise(cmd, { + encoding: "utf-8", + }), ); if (!out.startsWith("{")) { out = out.substring(out.indexOf("{")); @@ -259,12 +258,11 @@ const storeWASM = async ( network.rpcEndpoint, )} --yes --keyring-backend test -o json --home ${opts.home}`; console.log("> " + cmd); - let out = await retry(5, () => - child_process.execSync(cmd, { - stdio: ["inherit", "pipe", "inherit"], + let { stdout: out } = await retry(5, async () => { + return await execPromise(cmd, { encoding: "utf-8", - }), - ); + }); + }); if (!out.startsWith("{")) { out = out.substring(out.indexOf("{")); } @@ -283,6 +281,7 @@ const getTx = async (networkId: string, txhash: string, timeout?: number) => { let tx: IndexedTx | null = null; while (tx === null) { tx = await retry(5, async () => { + console.log("trying to get tx"); const client = await mustGetNonSigningCosmWasmClient(networkId); return client.getTx(txhash); }); @@ -290,13 +289,23 @@ const getTx = async (networkId: string, txhash: string, timeout?: number) => { return tx; }; const startTimeout = async () => { - await sleep(timeout || 50000); + await sleep(timeout || 10000); return undefined; }; const tx = await Promise.race([startTimeout(), innerGetTx()]); if (!tx) { throw new Error("Timed out waiting for tx '" + txhash + "'"); } + if (tx.code !== 0) { + throw new Error( + "Tx '" + + txhash + + "' failed with code " + + tx.code + + "\n" + + JSON.stringify(tx, null, 2), + ); + } return tx; };