From 86cf39e913400e030226b77ba2946403733fdd3e Mon Sep 17 00:00:00 2001 From: blockchainguyy Date: Mon, 1 Apr 2024 14:10:52 +0530 Subject: [PATCH 1/7] feat: add parallel flag to execute scripts concurrently wrt chain --- evm/cli-utils.js | 2 ++ evm/utils.js | 83 +++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 84 insertions(+), 1 deletion(-) diff --git a/evm/cli-utils.js b/evm/cli-utils.js index 59b98df5..8c43accd 100644 --- a/evm/cli-utils.js +++ b/evm/cli-utils.js @@ -13,6 +13,8 @@ const addBaseOptions = (program, options = {}) => { .env('ENV'), ); program.addOption(new Option('-y, --yes', 'skip deployment prompt confirmation').env('YES')); + program.addOption(new Option('--parallel', 'run script parallely wrt chains')); + program.addOption(new Option('--saveChainSeparately', 'save chain info separately')); program.addOption(new Option('--gasOptions ', 'gas options cli override')); if (!options.ignoreChainNames) { diff --git a/evm/utils.js b/evm/utils.js index de43c27c..7ed41107 100644 --- a/evm/utils.js +++ b/evm/utils.js @@ -26,6 +26,7 @@ const { const { CosmWasmClient } = require('@cosmjs/cosmwasm-stargate'); const CreateDeploy = require('@axelar-network/axelar-gmp-sdk-solidity/artifacts/contracts/deploy/CreateDeploy.sol/CreateDeploy.json'); const IDeployer = require('@axelar-network/axelar-gmp-sdk-solidity/interfaces/IDeployer.json'); +const { exec } = require('child_process'); const { verifyContract } = require(`${__dirname}/../axelar-chains-config`); const getSaltFromKey = (key) => { @@ -668,10 +669,18 @@ function loadConfig(env) { return require(`${__dirname}/../axelar-chains-config/info/${env}.json`); } +function loadSeparateConfig(env, chain) { + return require(`${__dirname}/../chains-info/${env}-${chain}.json`); +} + function saveConfig(config, env) { writeJSON(config, `${__dirname}/../axelar-chains-config/info/${env}.json`); } +function saveSeparateConfig(config, env, chain) { + writeJSON(config, `${__dirname}/../chains-info/${env}-${chain}.json`); +} + async function printWalletInfo(wallet, options = {}) { let balance = 0; const address = await wallet.getAddress(); @@ -852,6 +861,74 @@ const mainProcessor = async (options, processCommand, save = true, catchErr = fa } } + if (options.parallel && chains.length > 1) { + const cmds = process.argv.filter((command) => command); + let chainCommandIndex = -1; + let skipPrompt = false; + + for (let commandIndex = 0; commandIndex < cmds.length; commandIndex++) { + const cmd = cmds[commandIndex]; + + if (cmd === '-n' || cmd === '--chainName' || cmd === '--chainNames') { + chainCommandIndex = commandIndex; + } else if (cmd === '--parallel') { + cmds[commandIndex] = '--saveChainSeparately'; + } else if (cmd === '-y' || cmd === '--yes') { + skipPrompt = true; + } + } + + if (!skipPrompt) { + cmds.push('-y'); + } + + const failedChainIndexes = []; + const successfullChainIndexes = []; + let totalChains = 0; + + for (const chainName of chains) { + const chain = config.chains[chainName.toLowerCase()]; + + if ( + chainsToSkip.includes(chain.name.toLowerCase()) || + chain.status === 'deactive' || + (chain.contracts && chain.contracts[options.contractName]?.skip) + ) { + printWarn('Skipping chain', chain.name); + continue; + } + + totalChains++; + cmds[chainCommandIndex + 1] = chainName; + + exec(cmds.join(' '), { stdio: 'inherit' }, (error, stdout) => { + printInfo(`logs for ${chainName}`, stdout); + + if (error) { + failedChainIndexes.push(chainName); + printError(`error while running script for ${chainName}`, error); + } else { + successfullChainIndexes.push(chainName); + printInfo(`finished running script for`, chainName); + } + }); + } + + while (successfullChainIndexes.length + failedChainIndexes.length < totalChains) { + await sleep(2000); + } + + for (const chainName of successfullChainIndexes) { + config.chains[chainName.toLowerCase()] = loadSeparateConfig(options.env, chainName); + } + + if (save) { + saveConfig(config, options.env); + } + + return; + } + for (const chainName of chains) { const chain = config.chains[chainName.toLowerCase()]; @@ -878,7 +955,11 @@ const mainProcessor = async (options, processCommand, save = true, catchErr = fa } if (save) { - saveConfig(config, options.env); + if (options.saveChainSeparately) { + saveSeparateConfig(config.chains[chainName.toLowerCase()], options.env, chainName); + } else { + saveConfig(config, options.env); + } } } }; From 680fd0c6c89c27401b3038a2bd420a682d3739ba Mon Sep 17 00:00:00 2001 From: blockchainguyy Date: Mon, 1 Apr 2024 14:25:31 +0530 Subject: [PATCH 2/7] chore: add chains-info to gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 7d727be9..53997b1f 100644 --- a/.gitignore +++ b/.gitignore @@ -106,6 +106,7 @@ dist build/ cache/ artifacts/ +chains-info/ local.json keys.json From 6662f73498f9c8dfc3ed2045a051679e72dffd56 Mon Sep 17 00:00:00 2001 From: blockchainguyy Date: Mon, 1 Apr 2024 18:00:05 +0530 Subject: [PATCH 3/7] chore: load config only when save is true --- evm/utils.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/evm/utils.js b/evm/utils.js index 7ed41107..78ff8522 100644 --- a/evm/utils.js +++ b/evm/utils.js @@ -918,11 +918,11 @@ const mainProcessor = async (options, processCommand, save = true, catchErr = fa await sleep(2000); } - for (const chainName of successfullChainIndexes) { - config.chains[chainName.toLowerCase()] = loadSeparateConfig(options.env, chainName); - } - if (save) { + for (const chainName of successfullChainIndexes) { + config.chains[chainName.toLowerCase()] = loadSeparateConfig(options.env, chainName); + } + saveConfig(config, options.env); } From 32d4f541b24fee81664b93af9606b74d06035f5a Mon Sep 17 00:00:00 2001 From: blockchainguyy Date: Mon, 1 Apr 2024 18:43:26 +0530 Subject: [PATCH 4/7] chore: rename functions --- evm/utils.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/evm/utils.js b/evm/utils.js index 78ff8522..9e4bca9b 100644 --- a/evm/utils.js +++ b/evm/utils.js @@ -669,7 +669,7 @@ function loadConfig(env) { return require(`${__dirname}/../axelar-chains-config/info/${env}.json`); } -function loadSeparateConfig(env, chain) { +function loadParallelExecutionConfig(env, chain) { return require(`${__dirname}/../chains-info/${env}-${chain}.json`); } @@ -677,7 +677,7 @@ function saveConfig(config, env) { writeJSON(config, `${__dirname}/../axelar-chains-config/info/${env}.json`); } -function saveSeparateConfig(config, env, chain) { +function saveParallelExecutionConfig(config, env, chain) { writeJSON(config, `${__dirname}/../chains-info/${env}-${chain}.json`); } @@ -920,7 +920,7 @@ const mainProcessor = async (options, processCommand, save = true, catchErr = fa if (save) { for (const chainName of successfullChainIndexes) { - config.chains[chainName.toLowerCase()] = loadSeparateConfig(options.env, chainName); + config.chains[chainName.toLowerCase()] = loadParallelExecutionConfig(options.env, chainName); } saveConfig(config, options.env); @@ -956,7 +956,7 @@ const mainProcessor = async (options, processCommand, save = true, catchErr = fa if (save) { if (options.saveChainSeparately) { - saveSeparateConfig(config.chains[chainName.toLowerCase()], options.env, chainName); + saveParallelExecutionConfig(config.chains[chainName.toLowerCase()], options.env, chainName); } else { saveConfig(config, options.env); } From 0f0d37585f4d16d7e06bfb49e718c1b0922bcd20 Mon Sep 17 00:00:00 2001 From: blockchainguyy Date: Mon, 1 Apr 2024 20:35:15 +0530 Subject: [PATCH 5/7] refactor: use promises instead of await for script execution --- evm/utils.js | 46 +++++++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/evm/utils.js b/evm/utils.js index 9e4bca9b..267215af 100644 --- a/evm/utils.js +++ b/evm/utils.js @@ -882,11 +882,9 @@ const mainProcessor = async (options, processCommand, save = true, catchErr = fa cmds.push('-y'); } - const failedChainIndexes = []; - const successfullChainIndexes = []; - let totalChains = 0; + const successfullChains = []; - for (const chainName of chains) { + const executeChain = (chainName) => { const chain = config.chains[chainName.toLowerCase()]; if ( @@ -895,31 +893,37 @@ const mainProcessor = async (options, processCommand, save = true, catchErr = fa (chain.contracts && chain.contracts[options.contractName]?.skip) ) { printWarn('Skipping chain', chain.name); - continue; + return Promise.resolve(); } - totalChains++; - cmds[chainCommandIndex + 1] = chainName; + return new Promise((resolve) => { + cmds[chainCommandIndex + 1] = chainName; - exec(cmds.join(' '), { stdio: 'inherit' }, (error, stdout) => { - printInfo(`logs for ${chainName}`, stdout); + exec(cmds.join(' '), { stdio: 'inherit' }, (error, stdout) => { + printInfo('-------------------------------------------------------'); + printInfo(`Logs for ${chainName}`, stdout); - if (error) { - failedChainIndexes.push(chainName); - printError(`error while running script for ${chainName}`, error); - } else { - successfullChainIndexes.push(chainName); - printInfo(`finished running script for`, chainName); - } + if (error) { + printError(`Error while running script for ${chainName}`, error); + } else { + successfullChains.push(chainName); + printInfo(`Finished running script for chain`, chainName); + } + + resolve(); + }); }); - } + }; - while (successfullChainIndexes.length + failedChainIndexes.length < totalChains) { - await sleep(2000); - } + const executeAllChains = async () => { + const chainPromises = chains.map((chainName) => executeChain(chainName)); + await Promise.all(chainPromises); + }; + + await executeAllChains(); if (save) { - for (const chainName of successfullChainIndexes) { + for (const chainName of successfullChains) { config.chains[chainName.toLowerCase()] = loadParallelExecutionConfig(options.env, chainName); } From f1b79de7c76d425dd8f6d55a2c602224d5f505cc Mon Sep 17 00:00:00 2001 From: blockchainguyy Date: Mon, 1 Apr 2024 20:43:49 +0530 Subject: [PATCH 6/7] chore: update executeAllChains function --- evm/utils.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/evm/utils.js b/evm/utils.js index 267215af..9309078c 100644 --- a/evm/utils.js +++ b/evm/utils.js @@ -915,12 +915,10 @@ const mainProcessor = async (options, processCommand, save = true, catchErr = fa }); }; - const executeAllChains = async () => { + await (async () => { const chainPromises = chains.map((chainName) => executeChain(chainName)); await Promise.all(chainPromises); - }; - - await executeAllChains(); + })(); if (save) { for (const chainName of successfullChains) { From 669f794d52215bd50efb22ea830cb33c31ef5870 Mon Sep 17 00:00:00 2001 From: blockchainguyy Date: Thu, 4 Apr 2024 14:07:27 +0530 Subject: [PATCH 7/7] chore: resolving promises for different chains --- evm/utils.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/evm/utils.js b/evm/utils.js index 9309078c..b8ad21ff 100644 --- a/evm/utils.js +++ b/evm/utils.js @@ -915,10 +915,7 @@ const mainProcessor = async (options, processCommand, save = true, catchErr = fa }); }; - await (async () => { - const chainPromises = chains.map((chainName) => executeChain(chainName)); - await Promise.all(chainPromises); - })(); + await Promise.all(chains.map(executeChain)); if (save) { for (const chainName of successfullChains) {