Skip to content

Commit

Permalink
Fix race condition on proxy server (#374)
Browse files Browse the repository at this point in the history
* [skip testnet]

---------

Co-authored-by: FabijanC <fabijan.corak@gmail.com>
  • Loading branch information
Nathan-SL and FabijanC committed Jun 7, 2023
1 parent 0857f2e commit 1221c19
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 3 deletions.
37 changes: 37 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"dependencies": {
"@nomiclabs/hardhat-docker": "^2.0.2",
"axios": "^1.0.0",
"axios-retry": "^3.5.0",
"exit-hook": "2.2.1",
"form-data": "^4.0.0",
"glob": "^10.0.0",
Expand Down
16 changes: 15 additions & 1 deletion src/external-server/external-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { StarknetPluginError } from "../starknet-plugin-error";
import { IntegratedDevnetLogger } from "./integrated-devnet-logger";
import { StringMap } from "../types";
import { HardhatRuntimeEnvironment } from "hardhat/types";
import axiosRetry from "axios-retry";

function sleep(amountMillis: number): Promise<void> {
return new Promise((resolve) => {
Expand Down Expand Up @@ -46,6 +47,7 @@ export async function getFreePort(): Promise<string> {
export abstract class ExternalServer {
protected childProcess: ChildProcess;
private connected = false;
private connecting = false; // Flag indicating whether the proxy server is currently in the process of connecting
private lastError: string = null;
private _isDockerDesktop: boolean = null;

Expand Down Expand Up @@ -177,8 +179,11 @@ export abstract class ExternalServer {
}

public async post<T>(data: StringMap): Promise<T> {
await this.ensurePort();
await this.ensureStarted();

// The default value of retries, which is 3, does not work on CircleCI
axiosRetry(axios, { retries: 5, retryDelay: axiosRetry.exponentialDelay });
const hre: HardhatRuntimeEnvironment = await import("hardhat");

try {
Expand All @@ -193,10 +198,19 @@ export abstract class ExternalServer {
}
}

private async ensurePort(): Promise<void> {
if (this.port) {
return;
}
this.port = await getFreePort();
}

private async ensureStarted(): Promise<void> {
if (this.connected) {
if (this.connected || this.connecting) {
return;
}
this.connecting = true;
await this.start();
this.connecting = false;
}
}
2 changes: 0 additions & 2 deletions src/starknet-venv-proxy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ChildProcess, spawn } from "child_process";
import { ExternalServer } from "./external-server";
import { getFreePort } from "./external-server/external-server";

import path from "path";

Expand All @@ -10,7 +9,6 @@ export class StarknetVenvProxy extends ExternalServer {
}

protected async spawnChildProcess(): Promise<ChildProcess> {
this.port = await getFreePort();
const proxyServerPath = path.join(__dirname, "starknet_cli_wrapper.py");
return spawn(this.pythonPath, [proxyServerPath, this.port]);
}
Expand Down
12 changes: 12 additions & 0 deletions test/integrated-devnet-tests/predeployed-accounts-test/check.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { hardhatStarknetTest } from "../../utils/cli-functions";
import { checkDevnetIsNotRunning } from "../../utils/utils";

// Tests race condition on proxy server by making multiple calls to the
// function getAccountFromAddress and that the proxy server
// handles simultaneous requests correctly and that the port assignment
// on the integrated-devnet environment is implemented correctly
(async () => {
await checkDevnetIsNotRunning();
hardhatStarknetTest("--no-compile test/get-predeployed-accounts.test.ts".split(" "));
await checkDevnetIsNotRunning();
})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import "@shardlabs/starknet-hardhat-plugin";

module.exports = {
starknet: {
network: process.env.NETWORK
},
networks: {
integratedDevnet: {
venv: "active",
url: "http://127.0.0.1:5050"
}
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"$schema": "../../network.schema",
"integrated-devnet": true
}

0 comments on commit 1221c19

Please sign in to comment.