Skip to content

Commit

Permalink
chore: fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
npty committed Oct 27, 2023
1 parent d718d04 commit 52127d1
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 58 deletions.
11 changes: 3 additions & 8 deletions packages/axelar-local-dev-cosmos/jest/jest.global-setup.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import fs from "fs";
import { setLogger } from "@axelar-network/axelar-local-dev";
import { start, startAll } from "../src/docker";

setLogger(() => undefined);
import { stopAll, startAll } from "../src/docker";

export default async () => {
const config = await start("wasm").catch((e) => console.log(e));
await new Promise((resolve) => setTimeout(resolve, 5000));
// fs.writeFileSync("./config.json", JSON.stringify(config));
await startAll();
// await new Promise((resolve) => setTimeout(resolve, 3000));
};
42 changes: 21 additions & 21 deletions packages/axelar-local-dev-cosmos/src/__tests__/docker.spec.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
import { setLogger } from "@axelar-network/axelar-local-dev";
import { getOwnerAccount, start, stop } from "../docker";
import { CosmosClient } from "../CosmosClient";
import { getOwnerAccount } from "../docker";
import { CosmosClient } from "../";
import fetch from "node-fetch";

setLogger(() => undefined);

describe("docker", () => {
it("should start Cosmos container successfully", async () => {
const response = await fetch("http://localhost:26657/health");
expect(response.status).toBe(200);
});
it("should start containers successfully", async () => {
const testLcd = "cosmos/base/tendermint/v1beta1/node_info";
const healthAxelarRpc = await fetch("http://localhost/axelar-rpc");
const healthAxelarLcd = await fetch(
`http://localhost/axelar-lcd/${testLcd}`
);
const healthWasmRpc = await fetch("http://localhost/wasm-rpc");
const healthWasmLcd = await fetch(`http://localhost/wasm-lcd/${testLcd}`);

// it('should start Cosmos container with default denom "udemo"', async () => {
// const owner = await getOwnerAccount();
// const cosmosClient = await CosmosClient.create({
// owner,
// });
expect(healthAxelarRpc.status).toBe(200);
expect(healthAxelarLcd.status).toBe(200);
expect(healthWasmRpc.status).toBe(200);
expect(healthWasmLcd.status).toBe(200);
});

// const balance = await cosmosClient.getBalance(owner.address);
it("should have some balance in the owner account", async () => {
const owner = await getOwnerAccount("wasm");
const cosmosClient = await CosmosClient.create();

// expect(parseInt(balance)).toBeGreaterThan(1);
// expect(cosmosClient.getChainInfo().denom).toBe(defaultDenom);
// });
const balance = await cosmosClient.getBalance(owner.address);

// it.skip("should stop Cosmos container gracefully", async () => {
// await stop();
// await fetch("http://localhost:1317/").catch((e) => {
// expect(e.message).toContain("ECONNREFUSED");
// });
// });
expect(parseInt(balance)).toBeGreaterThan(1);
});
});
1 change: 1 addition & 0 deletions packages/axelar-local-dev-cosmos/src/axelar/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export const defaultConfig: ChainConfig = {
healthcheckEndpoint: "health",
dockerPath: path.join(__dirname, "../../docker/axelar"),
};
export const getOwnerAccount = () => {};
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { DirectSecp256k1HdWallet } from "@cosmjs/proto-signing";
import { GasPrice } from "@cosmjs/stargate";
import { SigningCosmWasmClient } from "@cosmjs/cosmwasm-stargate";
import { Decimal } from "@cosmjs/math";
import { CosmosChainInfo } from "./types";
import { CosmosChainInfo } from "../types";
import { getOwnerAccount } from "../docker";

export class CosmosClient {
chainInfo: Required<CosmosChainInfo>;
Expand All @@ -20,12 +21,11 @@ export class CosmosClient {
this.client = client;
}

static async create(config: CosmosChainInfo) {
static async create(config: Omit<CosmosChainInfo, "owner"> = {}) {
const chainInfo = {
...config,
denom: config.denom || "udemo",
lcdUrl: config.lcdUrl || "http://localhost:1317",
rpcUrl: config.rpcUrl || "http://localhost:26657",
denom: config.denom || "uwasm",
lcdUrl: config.lcdUrl || "http://localhost/wasm-lcd",
rpcUrl: config.rpcUrl || "http://localhost/wasm-rpc",
};

const walletOptions = {
Expand All @@ -35,8 +35,10 @@ export class CosmosClient {
gasPrice: new GasPrice(Decimal.fromAtomics("1", 6), chainInfo.denom),
};

const { address, mnemonic } = await getOwnerAccount("wasm");

const owner = await DirectSecp256k1HdWallet.fromMnemonic(
config?.owner.mnemonic,
mnemonic,
walletOptions
);

Expand All @@ -46,7 +48,17 @@ export class CosmosClient {
clientOptions
);

return new CosmosClient(chainInfo, owner, client);
return new CosmosClient(
{
...chainInfo,
owner: {
mnemonic,
address,
},
},
owner,
client
);
}

getBalance(address: string) {
Expand All @@ -63,6 +75,22 @@ export class CosmosClient {
};
}

async fundWallet(address: string, amount: string) {
const ownerAddress = await this.getOwnerAccount();

return this.client.sendTokens(
ownerAddress,
address,
[
{
amount,
denom: this.chainInfo.denom,
},
],
"auto"
);
}

async uploadWasm(path: string) {
const wasm = fs.readFileSync(path);

Expand Down
1 change: 1 addition & 0 deletions packages/axelar-local-dev-cosmos/src/clients/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./CosmosClient";
6 changes: 4 additions & 2 deletions packages/axelar-local-dev-cosmos/src/docker/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
getChainDenom,
getOwnerAccount,
isDockerRunning,
waitForLcd,
waitForRpc,
} from "./utils";
import path from "path";
Expand Down Expand Up @@ -61,8 +62,9 @@ export async function start(
// Start docker container
await compose.upOne(chain, config);

// Wait for cosmos to start
// Wait for API servers to start
await waitForRpc(chain, options);
await waitForLcd(chain);

const rpcUrl = `http://localhost/${chain}-rpc`;
const lcdUrl = `http://localhost/${chain}-lcd`;
Expand All @@ -71,7 +73,7 @@ export async function start(
console.log(`LCD server for ${chain} is started at ${lcdUrl}`);

return {
owner: await getOwnerAccount(chain, dockerPath),
owner: await getOwnerAccount(chain),
denom: getChainDenom(chain),
lcdUrl,
rpcUrl,
Expand Down
9 changes: 3 additions & 6 deletions packages/axelar-local-dev-cosmos/src/docker/stop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ import path from "path";
import { logger } from "@axelar-network/axelar-local-dev";

export async function stopAll() {
return Promise.all([
stop("axelar", axelarConfig.dockerPath),
stop("wasm", wasmConfig.dockerPath),
stopTraefik(),
]);
return Promise.all([stop("axelar"), stop("wasm"), stopTraefik()]);
}

export async function stopTraefik() {
Expand All @@ -29,8 +25,9 @@ export async function stopTraefik() {
/**
* Stop docker container
*/
export async function stop(chain: CosmosChain, dockerPath: string) {
export async function stop(chain: CosmosChain) {
logger.log(`Stopping ${chain} container...`);
const dockerPath = path.join(__dirname, `../../docker/${chain}`);
try {
await compose.down({
cwd: dockerPath,
Expand Down
34 changes: 30 additions & 4 deletions packages/axelar-local-dev-cosmos/src/docker/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ export function createContainerEnv(chain: CosmosChain, options: ChainConfig) {
fs.writeFileSync(envPath, env);
}

export async function getOwnerAccount(chain: CosmosChain, dockerPath: string) {
export async function getOwnerAccount(chain: CosmosChain) {
// Get mnemonic and address from the container
const homedir = `./.${chain}`;
const dockerPath = path.join(__dirname, `../../docker/${chain}`);
const homePath = path.join(dockerPath, homedir);
const mnemonic = fs.readFileSync(`${homePath}/mnemonic.txt`, "utf8");
const address = await DirectSecp256k1HdWallet.fromMnemonic(mnemonic, {
Expand All @@ -49,9 +50,9 @@ export async function getOwnerAccount(chain: CosmosChain, dockerPath: string) {
* If response isn't 200 within {timeout}, throws an error.
*/
export async function waitForRpc(chain: CosmosChain, config: ChainConfig) {
const { healthcheckEndpoint, rpcPort } = config;
const { healthcheckEndpoint } = config;
const start = Date.now();
const timeout = 30000;
const timeout = 60000;
const interval = 3000;
const url = `http://localhost/${chain}-rpc/${healthcheckEndpoint}`;
let status = 0;
Expand All @@ -61,13 +62,38 @@ export async function waitForRpc(chain: CosmosChain, config: ChainConfig) {
if (status === 200) {
break;
}
console.log("status", status);
} catch (e) {
// do nothing
}
await new Promise((resolve) => setTimeout(resolve, interval));
}
if (status !== 200) {
throw new Error(`${chain} failed to start in ${timeout}ms`);
throw new Error(`${chain} rpc server failed to start in ${timeout}ms`);
}
}

export async function waitForLcd(chain: CosmosChain) {
const testUrl = "cosmos/base/tendermint/v1beta1/node_info";
const start = Date.now();
const timeout = 60000;
const interval = 3000;
const url = `http://localhost/${chain}-lcd/${testUrl}`;
let result, network;
while (Date.now() - start < timeout) {
try {
result = await fetch(url).then((res: any) => res.json());
network = result.default_node_info.network;
if (network === chain) {
break;
}
} catch (e) {
// do nothing
}
await new Promise((resolve) => setTimeout(resolve, interval));
}
if (network !== chain) {
throw new Error(`${chain} lcd server failed to start in ${timeout}ms`);
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/axelar-local-dev-cosmos/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from "./CosmosClient";
export * from "./clients";
8 changes: 0 additions & 8 deletions packages/axelar-local-dev-cosmos/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
import { IDockerComposeOptions } from "docker-compose/dist/v2";

export interface StartOptions {
// cleanStart?: boolean;
chain?: CosmosChainOptions;
composeOptions?: IDockerComposeOptions;
}

export interface CosmosChainInfo {
owner: {
mnemonic: string;
Expand Down

0 comments on commit 52127d1

Please sign in to comment.