From fb5553ecf5b37a16c0243e7bf4a06a468bb63eb9 Mon Sep 17 00:00:00 2001 From: Dmytro Stebaiev Date: Tue, 9 Jul 2024 19:12:02 +0300 Subject: [PATCH] Add semaphore --- package.json | 3 ++- src/upgrader.ts | 20 +++++++++++++++++++- yarn.lock | 5 +++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 4ed4103..1c68913 100644 --- a/package.json +++ b/package.json @@ -43,7 +43,8 @@ "@safe-global/safe-core-sdk-types": "^5.0.1", "@skalenetwork/skale-contracts-ethers-v6": "^1.0.0", "axios": "^1.4.0", - "ethereumjs-util": "^7.1.4" + "ethereumjs-util": "^7.1.4", + "semaphore-async-await": "^1.5.1" }, "peerDependencies": { "@nomicfoundation/hardhat-ethers": "^3.0.0", diff --git a/src/upgrader.ts b/src/upgrader.ts index e70f194..09b379b 100644 --- a/src/upgrader.ts +++ b/src/upgrader.ts @@ -5,6 +5,7 @@ import {EXIT_CODES} from "./exitCodes"; import {Instance} from "@skalenetwork/skale-contracts-ethers-v6"; import {NonceProvider} from "./nonceProvider"; import {ProxyAdmin} from "../typechain-types"; +import Semaphore from 'semaphore-async-await'; import {Submitter} from "./submitters/submitter"; import {Transaction} from "ethers"; import chalk from "chalk"; @@ -30,6 +31,10 @@ interface Project { const withoutNull = (array: Array) => array. filter((element) => element !== null) as Array; +const maxSimultaneousDeployments = 10; +// 10 minutes +const deployTimeout = 60e4; + export abstract class Upgrader { instance: Instance; @@ -46,6 +51,8 @@ export abstract class Upgrader { nonceProvider?: NonceProvider; + deploySemaphore: Semaphore; + constructor ( project: Project, submitter: Submitter = new AutoSubmitter() @@ -59,6 +66,7 @@ export abstract class Upgrader { this.projectName = project.name; this.transactions = []; this.submitter = submitter; + this.deploySemaphore = new Semaphore(maxSimultaneousDeployments); } // Abstract @@ -187,12 +195,21 @@ export abstract class Upgrader { this.nonceProvider ??= await NonceProvider.createForWallet(deployer); const contracts = await Promise.all(this.contractNamesToUpgrade. map( - this.deployNewImplementation, + this.protectedDeployNewImplementation, this )); return withoutNull(contracts); } + private async protectedDeployNewImplementation (contract: string) { + await this.deploySemaphore.acquire(); + try { + return this.deployNewImplementation(contract); + } finally { + this.deploySemaphore.release(); + } + } + private async deployNewImplementation (contract: string) { const contractFactory = await getContractFactoryAndUpdateManifest( contract, @@ -210,6 +227,7 @@ export abstract class Upgrader { proxyAddress, contractFactory, { + "timeout": deployTimeout, "txOverrides": { "nonce": this.nonceProvider?.reserveNonce() }, diff --git a/yarn.lock b/yarn.lock index dda6473..28483e3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2176,6 +2176,11 @@ secp256k1@^4.0.1: node-addon-api "^2.0.0" node-gyp-build "^4.2.0" +semaphore-async-await@^1.5.1: + version "1.5.1" + resolved "https://registry.yarnpkg.com/semaphore-async-await/-/semaphore-async-await-1.5.1.tgz#857bef5e3644601ca4b9570b87e9df5ca12974fa" + integrity sha512-b/ptP11hETwYWpeilHXXQiV5UJNJl7ZWWooKRE5eBIYWoom6dZ0SluCIdCtKycsMtZgKWE01/qAw6jblw1YVhg== + semver@^7.5.4, semver@^7.6.0, semver@^7.6.1, semver@^7.6.2: version "7.6.2" resolved "https://registry.yarnpkg.com/semver/-/semver-7.6.2.tgz#1e3b34759f896e8f14d6134732ce798aeb0c6e13"