Skip to content

Commit

Permalink
Add semaphore
Browse files Browse the repository at this point in the history
  • Loading branch information
DimaStebaev committed Jul 9, 2024
1 parent 9fab9c2 commit fb5553e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
20 changes: 19 additions & 1 deletion src/upgrader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -30,6 +31,10 @@ interface Project {
const withoutNull = <T>(array: Array<T | null>) => array.
filter((element) => element !== null) as Array<T>;

const maxSimultaneousDeployments = 10;
// 10 minutes
const deployTimeout = 60e4;


export abstract class Upgrader {
instance: Instance;
Expand All @@ -46,6 +51,8 @@ export abstract class Upgrader {

nonceProvider?: NonceProvider;

deploySemaphore: Semaphore;

constructor (
project: Project,
submitter: Submitter = new AutoSubmitter()
Expand All @@ -59,6 +66,7 @@ export abstract class Upgrader {
this.projectName = project.name;
this.transactions = [];
this.submitter = submitter;
this.deploySemaphore = new Semaphore(maxSimultaneousDeployments);
}

// Abstract
Expand Down Expand Up @@ -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,
Expand All @@ -210,6 +227,7 @@ export abstract class Upgrader {
proxyAddress,
contractFactory,
{
"timeout": deployTimeout,
"txOverrides": {
"nonce": this.nonceProvider?.reserveNonce()
},
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit fb5553e

Please sign in to comment.