From e442cd81fe50b201d838ac452e1300d92f596b60 Mon Sep 17 00:00:00 2001 From: David Murdoch Date: Tue, 21 Mar 2023 14:40:19 -0400 Subject: [PATCH 1/2] optimize --- src/packages/cli/src/initialize/ethereum.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/packages/cli/src/initialize/ethereum.ts b/src/packages/cli/src/initialize/ethereum.ts index 2a460cc72f..bce9ccb9bf 100644 --- a/src/packages/cli/src/initialize/ethereum.ts +++ b/src/packages/cli/src/initialize/ethereum.ts @@ -16,21 +16,21 @@ export default function (provider: EthereumProvider, cliSettings: CliSettings) { const liveOptions = provider.getOptions(); const accounts = provider.getInitialAccounts(); - const addresses = Object.keys(accounts); + const addresses = Object.entries(accounts); const logs = []; logs.push(""); logs.push("Available Accounts"); logs.push("=================="); if (addresses.length > 0) { - addresses.forEach(function (address, index) { - const balance = accounts[address].balance; + addresses.forEach(([address, account], index) => { + const balance = account.balance; const strBalance = balance / WEI; const about = balance % WEI === 0n ? "" : "~"; let line = `(${index}) ${toChecksumAddress( address )} (${about}${strBalance} ETH)`; - if (!accounts[address].unlocked) { + if (!account.unlocked) { line += " 🔒"; } @@ -41,8 +41,8 @@ export default function (provider: EthereumProvider, cliSettings: CliSettings) { logs.push("Private Keys"); logs.push("=================="); - addresses.forEach(function (address, index) { - logs.push(`(${index}) ${accounts[address].secretKey}`); + addresses.forEach(([_, account], index) => { + logs.push(`(${index}) ${account.secretKey}`); }); if (liveOptions.wallet.accountKeysPath != null) { From e3476b41a800a690030c4561d3de99e35a35bebb Mon Sep 17 00:00:00 2001 From: jeffsmale90 <6363749+jeffsmale90@users.noreply.github.com> Date: Wed, 21 Jun 2023 10:11:38 +1200 Subject: [PATCH 2/2] perf: minor optimisations to account iteration (#4446) --- src/packages/cli/src/initialize/ethereum.ts | 103 ++++++++++---------- 1 file changed, 53 insertions(+), 50 deletions(-) diff --git a/src/packages/cli/src/initialize/ethereum.ts b/src/packages/cli/src/initialize/ethereum.ts index 7be89379fb..dd55d7ab25 100644 --- a/src/packages/cli/src/initialize/ethereum.ts +++ b/src/packages/cli/src/initialize/ethereum.ts @@ -17,16 +17,18 @@ export default function (provider: EthereumProvider, cliSettings: CliSettings) { const accounts = provider.getInitialAccounts(); const addresses = Object.entries(accounts); - const logs = []; - logs.push(""); - logs.push("Available Accounts"); - logs.push("=================="); + let log = "\n"; + const appendLog = line => (log += line + "\n"); + + appendLog("Available Accounts"); + appendLog("=================="); if (addresses.length > 0) { - addresses.forEach(([address, account], index) => { + let index = 0; + for (const [address, account] of addresses) { const balance = account.balance; const strBalance = balance / WEI; const about = balance % WEI === 0n ? "" : "~"; - let line = `(${index}) ${toChecksumAddress( + let line = `(${index++}) ${toChecksumAddress( address )} (${about}${strBalance} ETH)`; @@ -34,33 +36,34 @@ export default function (provider: EthereumProvider, cliSettings: CliSettings) { line += " 🔒"; } - logs.push(line); - }); + appendLog(line); + } - logs.push(""); - logs.push("Private Keys"); - logs.push("=================="); + appendLog(""); + appendLog("Private Keys"); + appendLog("=================="); - addresses.forEach(([_, account], index) => { - logs.push(`(${index}) ${account.secretKey}`); - }); + index = 0; + for (const [address, account] of addresses) { + appendLog(`(${index++}) ${account.secretKey}`); + } if (liveOptions.wallet.accountKeysPath != null) { - logs.push(""); - logs.push( + appendLog(""); + appendLog( `Accounts and keys saved to ${liveOptions.wallet.accountKeysPath}` ); } } else { - logs.push("(no accounts unlocked)"); + appendLog("(no accounts unlocked)"); } if (liveOptions.wallet.accounts == null) { - logs.push(""); - logs.push("HD Wallet"); - logs.push("=================="); - logs.push(`Mnemonic: ${color(liveOptions.wallet.mnemonic)}`); - logs.push( + appendLog(""); + appendLog("HD Wallet"); + appendLog("=================="); + appendLog(`Mnemonic: ${color(liveOptions.wallet.mnemonic)}`); + appendLog( `Base HD Path: ${color( liveOptions.wallet.hdPath.join("/") + "/{account_index}" )}` @@ -68,30 +71,30 @@ export default function (provider: EthereumProvider, cliSettings: CliSettings) { } if (liveOptions.miner.defaultGasPrice) { - logs.push(""); - logs.push("Default Gas Price"); - logs.push("=================="); - logs.push(color(liveOptions.miner.defaultGasPrice.toBigInt().toString())); + appendLog(""); + appendLog("Default Gas Price"); + appendLog("=================="); + appendLog(color(liveOptions.miner.defaultGasPrice.toBigInt().toString())); } if (liveOptions.miner.blockGasLimit) { - logs.push(""); - logs.push("BlockGas Limit"); - logs.push("=================="); - logs.push(color(liveOptions.miner.blockGasLimit.toBigInt().toString())); + appendLog(""); + appendLog("BlockGas Limit"); + appendLog("=================="); + appendLog(color(liveOptions.miner.blockGasLimit.toBigInt().toString())); } if (liveOptions.miner.callGasLimit) { - logs.push(""); - logs.push("Call Gas Limit"); - logs.push("=================="); - logs.push(color(liveOptions.miner.callGasLimit.toBigInt().toString())); + appendLog(""); + appendLog("Call Gas Limit"); + appendLog("=================="); + appendLog(color(liveOptions.miner.callGasLimit.toBigInt().toString())); } if (liveOptions.fork.network || liveOptions.fork.url) { - logs.push(""); - logs.push("Forked Chain"); - logs.push("=================="); + appendLog(""); + appendLog("Forked Chain"); + appendLog("=================="); let location: string; if (liveOptions.fork.network) { location = `Ethereum ${capitalizeFirstLetter( @@ -101,17 +104,17 @@ export default function (provider: EthereumProvider, cliSettings: CliSettings) { location = (liveOptions.fork.url as any).toString(); } - logs.push(`Location: ${color(location)}`); - logs.push( + appendLog(`Location: ${color(location)}`); + appendLog( `Block: ${color(liveOptions.fork.blockNumber.toString())}` ); - logs.push( + appendLog( `Network ID: ${color(liveOptions.chain.networkId.toString())}` ); - logs.push(`Time: ${color(liveOptions.chain.time.toString())}`); + appendLog(`Time: ${color(liveOptions.chain.time.toString())}`); if (liveOptions.fork.requestsPerSecond !== 0) { - logs.push( + appendLog( `Requests/Second: ${color( liveOptions.fork.requestsPerSecond.toString() )}` @@ -119,13 +122,13 @@ export default function (provider: EthereumProvider, cliSettings: CliSettings) { } } - logs.push(""); - logs.push("Chain"); - logs.push("=================="); - logs.push(`Hardfork: ${color(liveOptions.chain.hardfork)}`); - logs.push(`Id: ${color(liveOptions.chain.chainId.toString())}`); + appendLog(""); + appendLog("Chain"); + appendLog("=================="); + appendLog(`Hardfork: ${color(liveOptions.chain.hardfork)}`); + appendLog(`Id: ${color(liveOptions.chain.chainId.toString())}`); - logs.push(""); - logs.push("RPC Listening on " + cliSettings.host + ":" + cliSettings.port); - console.log(logs.join("\n")); + appendLog(""); + appendLog("RPC Listening on " + cliSettings.host + ":" + cliSettings.port); + console.log(log); }