Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow axios timeout configuration via hardhat config #322

Merged
merged 8 commits into from
Feb 23, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ export const VOYAGER_MAINNET_VERIFIED_URL = "https://voyager.online/contract/";

export const CHECK_STATUS_TIMEOUT = 5000; // ms
export const CHECK_STATUS_RECOVER_TIMEOUT = 10000; // ms
export const REQUEST_TIMEOUT = 30000; // ms

export const LEN_SUFFIX = "_len";

Expand Down
3 changes: 1 addition & 2 deletions src/devnet-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { StarknetPluginError } from "./starknet-plugin-error";
import { Devnet, HardhatRuntimeEnvironment } from "hardhat/types";

import { Block, MintResponse, L2ToL1Message } from "./starknet-types";
import { REQUEST_TIMEOUT } from "./constants";
import { hash } from "starknet";
import { numericToHexString } from "./utils";
import { Numeric } from "./types";
Expand Down Expand Up @@ -79,7 +78,7 @@ export interface PredeployedAccount {
export class DevnetUtils implements Devnet {
private axiosInstance = axios.create({
baseURL: this.endpoint,
timeout: REQUEST_TIMEOUT,
timeout: this.hre.config.axios.timeout,
timeoutErrorMessage: "Request timed out"
});

Expand Down
2 changes: 2 additions & 0 deletions src/external-server/create-devnet-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export function createIntegratedDevnet(hre: HardhatRuntimeEnvironment): External
devnetNetwork.venv,
hostname,
port,
hre,
devnetNetwork?.args,
devnetNetwork?.stdout,
devnetNetwork?.stderr,
Expand All @@ -52,6 +53,7 @@ export function createIntegratedDevnet(hre: HardhatRuntimeEnvironment): External
},
hostname,
port,
hre,
devnetNetwork?.args,
devnetNetwork?.stdout,
devnetNetwork?.stderr,
Expand Down
4 changes: 3 additions & 1 deletion src/external-server/docker-devnet.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Image } from "@nomiclabs/hardhat-docker";
import { DockerServer } from "./docker-server";
import { HardhatRuntimeEnvironment } from "hardhat/types";

export class DockerDevnet extends DockerServer {
private vmLang?: string;
Expand All @@ -8,12 +9,13 @@ export class DockerDevnet extends DockerServer {
image: Image,
host: string,
port: string,
hre: HardhatRuntimeEnvironment,
private devnetArgs?: string[],
stdout?: string,
stderr?: string,
vmLang?: string
) {
super(image, host, port, "is_alive", "integrated-devnet", devnetArgs, stdout, stderr);
super(image, host, port, "is_alive", "integrated-devnet", hre, devnetArgs, stdout, stderr);
this.vmLang = vmLang;
}

Expand Down
4 changes: 3 additions & 1 deletion src/external-server/docker-server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { HardhatDocker, Image } from "@nomiclabs/hardhat-docker";
import { ChildProcess, spawn, spawnSync } from "child_process";
import { ExternalServer } from "./external-server";
import { HardhatRuntimeEnvironment } from "hardhat/types";

export abstract class DockerServer extends ExternalServer {
private docker: HardhatDocker;
Expand All @@ -12,13 +13,14 @@ export abstract class DockerServer extends ExternalServer {
externalPort: string,
isAliveURL: string,
containerName: string,
hre: HardhatRuntimeEnvironment,
protected args?: string[],
stdout?: string,
stderr?: string
) {
// to make name unique and allow multiple simultaneous instances
containerName += "-" + Math.random().toString().slice(2);
super(host, externalPort, isAliveURL, containerName, stdout, stderr);
super(host, externalPort, isAliveURL, containerName, hre, stdout, stderr);
this.containerName = containerName;
}

Expand Down
5 changes: 3 additions & 2 deletions src/external-server/external-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ChildProcess } from "child_process";
import { StarknetPluginError } from "../starknet-plugin-error";
import { IntegratedDevnetLogger } from "./integrated-devnet-logger";
import { StringMap } from "../types";
import { REQUEST_TIMEOUT } from "../constants";
import { HardhatRuntimeEnvironment } from "hardhat/types";

function sleep(amountMillis: number): Promise<void> {
return new Promise((resolve) => {
Expand Down Expand Up @@ -53,6 +53,7 @@ export abstract class ExternalServer {
protected port: string | null,
private isAliveURL: string,
protected processName: string,
protected hre: HardhatRuntimeEnvironment,
protected stdout?: string,
protected stderr?: string
) {
Expand Down Expand Up @@ -163,7 +164,7 @@ export abstract class ExternalServer {

try {
const response = await axios.post<T>(this.url, data, {
timeout: REQUEST_TIMEOUT,
timeout: this.hre.config.axios.timeout,
notV4l marked this conversation as resolved.
Show resolved Hide resolved
timeoutErrorMessage: "Request timed out"
});
return response.data;
Expand Down
4 changes: 3 additions & 1 deletion src/external-server/venv-devnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ChildProcess, spawn } from "child_process";

import { getPrefixedCommand, normalizeVenvPath } from "../utils/venv";
import { ExternalServer } from "./external-server";
import { HardhatRuntimeEnvironment } from "hardhat/types";

export class VenvDevnet extends ExternalServer {
private command: string;
Expand All @@ -12,12 +13,13 @@ export class VenvDevnet extends ExternalServer {
venvPath: string,
host: string,
port: string,
hre: HardhatRuntimeEnvironment,
args?: string[],
stdout?: string,
stderr?: string,
vmLang?: string
) {
super(host, port, "is_alive", "integrated-devnet", stdout, stderr);
super(host, port, "is_alive", "integrated-devnet", hre, stdout, stderr);

this.command = "starknet-devnet";
this.args = args;
Expand Down
12 changes: 12 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,18 @@ extendConfig((config: HardhatConfig, userConfig: Readonly<HardhatUserConfig>) =>
}
});

// copy all user-defined axios settings
extendConfig((config: HardhatConfig, userConfig: Readonly<HardhatUserConfig>) => {
if (userConfig.axios) {
config.axios = JSON.parse(JSON.stringify(userConfig.axios));
}
if (!config.axios) {
config.axios = {
notV4l marked this conversation as resolved.
Show resolved Hide resolved
timeout: 30_000
};
}
});

// add sources path
extendConfig((config: HardhatConfig, userConfig: Readonly<HardhatUserConfig>) => {
let newPath: string;
Expand Down
6 changes: 4 additions & 2 deletions src/starknet-docker-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Image } from "@nomiclabs/hardhat-docker";
import path from "path";
import { DockerServer } from "./external-server/docker-server";
import { getFreePort } from "./external-server/external-server";
import { HardhatRuntimeEnvironment } from "hardhat/types";

const PROXY_SERVER_FILE = "starknet_cli_wrapper.py";
const PROXY_SERVER_HOST_PATH = path.join(__dirname, PROXY_SERVER_FILE);
Expand All @@ -22,9 +23,10 @@ export class StarknetDockerProxy extends DockerServer {
image: Image,
private rootPath: string,
private accountPaths: string[],
private cairoPaths: string[]
private cairoPaths: string[],
hre: HardhatRuntimeEnvironment
) {
super(image, "127.0.0.1", null, "", "starknet-docker-proxy");
super(image, "127.0.0.1", null, "", "starknet-docker-proxy", hre);
}

protected async getDockerArgs(): Promise<string[]> {
Expand Down
6 changes: 4 additions & 2 deletions src/starknet-venv-proxy.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { ChildProcess, spawn } from "child_process";
import { ExternalServer } from "./external-server";
import { getFreePort } from "./external-server/external-server";
import { HardhatRuntimeEnvironment } from "hardhat/types";

import path from "path";

export class StarknetVenvProxy extends ExternalServer {
constructor(private pythonPath: string) {
super("127.0.0.1", null, "", "starknet-venv-proxy");
constructor(private pythonPath: string, hre: HardhatRuntimeEnvironment) {
super("127.0.0.1", null, "", "starknet-venv-proxy", hre);
}

protected async spawnChildProcess(): Promise<ChildProcess> {
Expand Down
4 changes: 2 additions & 2 deletions src/starknet-wrappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ export class DockerWrapper extends StarknetWrapper {
cairoPaths: string[],
hre: HardhatRuntimeEnvironment
) {
super(new StarknetDockerProxy(image, rootPath, accountPaths, cairoPaths), hre);
super(new StarknetDockerProxy(image, rootPath, accountPaths, cairoPaths, hre), hre);
console.log(
`${PLUGIN_NAME} plugin using dockerized environment (${getFullImageName(image)})`
);
Expand Down Expand Up @@ -478,7 +478,7 @@ export class VenvWrapper extends StarknetWrapper {
pythonPath = getPrefixedCommand(venvPath, "python3");
}

super(new StarknetVenvProxy(pythonPath), hre);
super(new StarknetVenvProxy(pythonPath, hre), hre);
}

protected override get gatewayUrl(): string {
Expand Down
6 changes: 6 additions & 0 deletions src/type-extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ declare module "hardhat/types/config" {

export interface HardhatConfig {
starknet: StarknetTypes.StarknetConfig;
axios: HardhatAxiosConfig;
}

export interface HardhatUserConfig {
starknet?: StarknetTypes.StarknetConfig;
axios?: HardhatAxiosConfig;
}

export interface NetworksConfig {
Expand Down Expand Up @@ -69,6 +71,10 @@ declare module "hardhat/types/config" {
vmLang?: VmLang;
}

export interface HardhatAxiosConfig {
timeout?: number;
}

type VmLang = "python" | "rust" | "";
}

Expand Down
14 changes: 14 additions & 0 deletions www/docs/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,20 @@ module.exports = {
};
```

### Axios

Default timeout is 30s. It can be changed using the following configuration.
You may need to increase the timeout value in some situation (declaring large smart contract).

```javascript
module.exports = {
axios: {
timeout: 90_000, // 90s
},
...
};
```

### Installing third-party libraries

If you want to install a third-party Cairo library and be able to import it in your Cairo files, use the following pattern:
Expand Down