Skip to content

Commit

Permalink
Optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
jorbush committed Feb 1, 2024
1 parent 5d88fab commit a31acf3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 35 deletions.
24 changes: 24 additions & 0 deletions core/tests/ts-integration/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Boolean> {
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;
}
19 changes: 2 additions & 17 deletions core/tests/ts-integration/tests/contracts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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,
Expand Down
23 changes: 5 additions & 18 deletions core/tests/ts-integration/tests/system.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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 () => {
Expand Down Expand Up @@ -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({
Expand Down

0 comments on commit a31acf3

Please sign in to comment.