Skip to content

Commit

Permalink
fix: exit on all errors
Browse files Browse the repository at this point in the history
Signed-off-by: Norman Meier <[email protected]>
  • Loading branch information
n0izn0iz committed Jan 19, 2024
1 parent 495fcda commit b0755cb
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 83 deletions.
37 changes: 23 additions & 14 deletions packages/scripts/integration-testing/cosmos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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"),
Expand All @@ -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);
Expand All @@ -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) {
Expand All @@ -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);

Expand All @@ -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");
}
Expand All @@ -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
Expand Down
99 changes: 52 additions & 47 deletions packages/scripts/integration-testing/upgradeTest142toDir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,61 +16,66 @@ import { deployTeritoriEcosystem } from "../network-setup/deployLib";
const repoURL = "https://github.com/TERITORI/teritori-chain.git";

const main = async () => {
program.argument(
"<repo-path>",
"Path to the repo to build latest binary from",
);
program.parse();
const [repoPath] = program.args;
try {
program.argument(
"<repo-path>",
"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();
4 changes: 4 additions & 0 deletions packages/scripts/lib.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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);
52 changes: 30 additions & 22 deletions packages/scripts/network-setup/deployLib.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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 (
Expand All @@ -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();
}
Expand Down Expand Up @@ -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("{"));
}
Expand Down Expand Up @@ -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("{"));
Expand Down Expand Up @@ -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("{"));
}
Expand All @@ -290,13 +288,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;
};

Expand Down

0 comments on commit b0755cb

Please sign in to comment.