From a31acf3da233280462d7c74c790b493680f6b39b Mon Sep 17 00:00:00 2001 From: Jordi Date: Thu, 1 Feb 2024 11:04:25 +0100 Subject: [PATCH] Optimize code --- core/tests/ts-integration/src/helpers.ts | 24 +++++++++++++++++++ .../ts-integration/tests/contracts.test.ts | 19 ++------------- .../tests/ts-integration/tests/system.test.ts | 23 ++++-------------- 3 files changed, 31 insertions(+), 35 deletions(-) diff --git a/core/tests/ts-integration/src/helpers.ts b/core/tests/ts-integration/src/helpers.ts index 1b6d1dff477..ecb03f37cb1 100644 --- a/core/tests/ts-integration/src/helpers.ts +++ b/core/tests/ts-integration/src/helpers.ts @@ -105,3 +105,27 @@ export async function scaledGasPrice(wallet: ethers.Wallet | zksync.Wallet): Pro // Increase by 40%. return gasPrice.mul(140).div(100); } + +/** + * Returns if it is runnning in Validium Mode. + * + * @returns Boolean that indicates whether it is Validium mode. + */ +export async function getIsValidium(): Promise { + const filePath = `${process.env.ZKSYNC_HOME}/etc/env/dev.env`; + let isValidium: Boolean = false; + try { + const fileContent = await fs.promises.readFile(filePath, 'utf-8'); + const keyValuePairs = fileContent.split('\n').map((line) => line.trim().split('=')); + const configObject: { [key: string]: string } = {}; + keyValuePairs.forEach((pair) => { + if (pair.length === 2) { + configObject[pair[0]] = pair[1]; + } + }); + isValidium = configObject.VALIDIUM_MODE === 'true'; + } catch (error) { + console.error(`Error reading or parsing the config file ${filePath}:`, error); + } + return isValidium; +} diff --git a/core/tests/ts-integration/tests/contracts.test.ts b/core/tests/ts-integration/tests/contracts.test.ts index 2384fa27e3a..effba2c3d89 100644 --- a/core/tests/ts-integration/tests/contracts.test.ts +++ b/core/tests/ts-integration/tests/contracts.test.ts @@ -7,7 +7,7 @@ */ import { TestMaster } from '../src/index'; -import { deployContract, getTestContract, waitForNewL1Batch } from '../src/helpers'; +import { deployContract, getIsValidium, getTestContract, waitForNewL1Batch } from '../src/helpers'; import { shouldOnlyTakeFee } from '../src/modifiers/balance-checker'; import * as ethers from 'ethers'; @@ -36,25 +36,10 @@ describe('Smart contract behavior checks', () => { // Contracts shared in several tests. let counterContract: zksync.Contract; - let is_validium: Boolean; beforeAll(async () => { testMaster = TestMaster.getInstance(__filename); alice = testMaster.mainAccount(); - const filePath = `${process.env.ZKSYNC_HOME}/etc/env/dev.env`; - try { - const fileContent = await fs.readFile(filePath, 'utf-8'); - const keyValuePairs = fileContent.split('\n').map((line) => line.trim().split('=')); - const configObject: { [key: string]: string } = {}; - keyValuePairs.forEach((pair) => { - if (pair.length === 2) { - configObject[pair[0]] = pair[1]; - } - }); - is_validium = configObject.VALIDIUM_MODE === 'true'; - } catch (error) { - console.error(`Error reading or parsing the config file ${filePath}:`, error); - } }); test('Should deploy & call a contract', async () => { @@ -336,7 +321,7 @@ describe('Smart contract behavior checks', () => { }); // If it is running in validium mode, there is no pubdata and the transaction will not be rejected. - if (is_validium) { + if (await getIsValidium()) { await expect( alice.sendTransaction({ to: alice.address, diff --git a/core/tests/ts-integration/tests/system.test.ts b/core/tests/ts-integration/tests/system.test.ts index 9cd7a24bc01..2663cbe3513 100644 --- a/core/tests/ts-integration/tests/system.test.ts +++ b/core/tests/ts-integration/tests/system.test.ts @@ -14,7 +14,7 @@ import * as ethers from 'ethers'; import { BigNumberish, BytesLike } from 'ethers'; import { serialize, hashBytecode } from 'zksync-web3/build/src/utils'; import { deployOnAnyLocalAddress, ForceDeployment } from '../src/system'; -import { getTestContract } from '../src/helpers'; +import { getIsValidium, getTestContract } from '../src/helpers'; import { promises as fs } from 'fs'; const contracts = { @@ -25,25 +25,10 @@ const contracts = { describe('System behavior checks', () => { let testMaster: TestMaster; let alice: zksync.Wallet; - let is_validium: Boolean; beforeAll(async () => { testMaster = TestMaster.getInstance(__filename); alice = testMaster.mainAccount(); - const filePath = `${process.env.ZKSYNC_HOME}/etc/env/dev.env`; - try { - const fileContent = await fs.readFile(filePath, 'utf-8'); - const keyValuePairs = fileContent.split('\n').map((line) => line.trim().split('=')); - const configObject: { [key: string]: string } = {}; - keyValuePairs.forEach((pair) => { - if (pair.length === 2) { - configObject[pair[0]] = pair[1]; - } - }); - is_validium = configObject.VALIDIUM_MODE === 'true'; - } catch (error) { - console.error(`Error reading or parsing the config file ${filePath}:`, error); - } }); test('Network should be supporting Cancun+Deneb', async () => { @@ -89,10 +74,12 @@ describe('System behavior checks', () => { }); test('Should accept transactions with small gasPerPubdataByte', async () => { + const isValidium = await getIsValidium(); // The number "10" was chosen because we have a different error for lesser `smallGasPerPubdata`. // In validium mode, this minimum value is "55" - const smallGasPerPubdata = is_validium ? 55 : 10; - const senderNonce = is_validium ? undefined : await alice.getTransactionCount(); + const smallGasPerPubdata = isValidium ? 55 : 10; + // In validium mode, the nonce is not required. + const senderNonce = isValidium ? undefined : await alice.getTransactionCount(); // This tx should be accepted by the server, but would never be executed, so we don't wait for the receipt. await alice.sendTransaction({