From ea1c727face221b431cd39dd5ed9dba3834d59df Mon Sep 17 00:00:00 2001 From: Jordi <146965181+jordibonet-lambdaclass@users.noreply.github.com> Date: Thu, 1 Feb 2024 12:51:19 +0100 Subject: [PATCH] fix: tests using the new env var (#85) * Fix tests using the new env var * Fix file used * Update validium environment * Optimize code * Remove unused imports * update isValidium method --------- Co-authored-by: toni-calvin --- core/tests/ts-integration/src/helpers.ts | 23 +++++++++++++++++++ .../ts-integration/tests/contracts.test.ts | 13 ++++------- .../tests/ts-integration/tests/system.test.ts | 11 ++++----- 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/core/tests/ts-integration/src/helpers.ts b/core/tests/ts-integration/src/helpers.ts index 1b6d1dff477b..2268c12864f6 100644 --- a/core/tests/ts-integration/src/helpers.ts +++ b/core/tests/ts-integration/src/helpers.ts @@ -105,3 +105,26 @@ 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`; + + try { + const fileContent = await fs.promises.readFile(filePath, 'utf-8'); + + const isValidiumMode = fileContent + .split('\n') + .map((line) => line.trim().split('=')) + .find((pair) => pair[0] === 'VALIDIUM_MODE'); + + return isValidiumMode ? isValidiumMode[1] === 'true' : false; + } catch (error) { + console.error(`Error reading or parsing the config file ${filePath}:`, error); + return false; // Return a default value or handle the error as needed + } +} \ No newline at end of file diff --git a/core/tests/ts-integration/tests/contracts.test.ts b/core/tests/ts-integration/tests/contracts.test.ts index 6ec7e5d836aa..face3cd85048 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'; @@ -15,8 +15,6 @@ import * as zksync from 'zksync-web3'; import { Provider } from 'zksync-web3'; import { RetryProvider } from '../src/retry-provider'; -const SYSTEM_CONFIG = require(`${process.env.ZKSYNC_HOME}/contracts/SystemConfig.json`); - // TODO: Leave only important ones. const contracts = { counter: getTestContract('Counter'), @@ -321,9 +319,8 @@ describe('Smart contract behavior checks', () => { data: '0x' }); - // If L1_GAS_PER_PUBDATA_BYTE is zero it is assumed to be running in validium mode, - // there is no pubdata and the transaction will not be rejected. - if (SYSTEM_CONFIG['L1_GAS_PER_PUBDATA_BYTE'] > 0) { + // If it is running in validium mode, there is no pubdata and the transaction will not be rejected. + if (await getIsValidium()) { await expect( alice.sendTransaction({ to: alice.address, @@ -332,7 +329,7 @@ describe('Smart contract behavior checks', () => { factoryDeps: [bytecode] } }) - ).toBeRejected('not enough gas to publish compressed bytecodes'); + ); } else { await expect( alice.sendTransaction({ @@ -342,7 +339,7 @@ describe('Smart contract behavior checks', () => { factoryDeps: [bytecode] } }) - ); + ).toBeRejected('not enough gas to publish compressed bytecodes'); } }); diff --git a/core/tests/ts-integration/tests/system.test.ts b/core/tests/ts-integration/tests/system.test.ts index 13990f7dd9e1..428b9bfe33ee 100644 --- a/core/tests/ts-integration/tests/system.test.ts +++ b/core/tests/ts-integration/tests/system.test.ts @@ -14,15 +14,13 @@ 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'; const contracts = { counter: getTestContract('Counter'), events: getTestContract('Emitter') }; -const SYSTEM_CONFIG = require(`${process.env.ZKSYNC_HOME}/contracts/SystemConfig.json`); - describe('System behavior checks', () => { let testMaster: TestMaster; let alice: zksync.Wallet; @@ -75,11 +73,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 = SYSTEM_CONFIG['L1_GAS_PER_PUBDATA_BYTE'] > 0 ? 10 : 55; - const senderNonce = - SYSTEM_CONFIG['L1_GAS_PER_PUBDATA_BYTE'] > 0 ? await alice.getTransactionCount() : undefined; + 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({