Skip to content

Commit

Permalink
feat: simplify node spawning to avoid forgotten nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
cytadela8 committed Oct 11, 2024
1 parent 1c6de49 commit b03433b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 28 deletions.
3 changes: 2 additions & 1 deletion core/tests/ts-integration/src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ async function loadTestEnvironmentFromFile(fileConfig: FileConfig): Promise<Test
baseTokenAddress: contracts.l1.base_token_addr
});

l2Node = await mainNodeSpawner.spawnMainNode();
await mainNodeSpawner.killAndSpawnMainNode();
l2Node = mainNodeSpawner.mainNode;
}

const l2Provider = new zksync.Provider(l2NodeUrl);
Expand Down
33 changes: 21 additions & 12 deletions core/tests/ts-integration/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,20 @@ export class Node<TYPE extends NodeType> {
}
}

interface MainNodeOptions {
newL1GasPrice?: bigint;
newPubdataPrice?: bigint;
customBaseToken?: boolean;
externalPriceApiClientForcedNumerator?: number;
externalPriceApiClientForcedDenominator?: number;
externalPriceApiClientForcedFluctuation?: number;
baseTokenPricePollingIntervalMs?: number;
baseTokenAdjusterL1UpdateDeviationPercentage?: number;
}
export class NodeSpawner {
private readonly generalConfigPath: string | undefined;
private readonly originalConfig: string | undefined;
public mainNode: Node<NodeType.MAIN> | null;

public constructor(
private readonly pathToHome: string,
Expand All @@ -110,6 +121,7 @@ export class NodeSpawner {
private readonly options: MainNodeSpawnOptions,
private env?: ProcessEnvOptions['env']
) {
this.mainNode = null;
if (fileConfig.loadFromFile) {
this.generalConfigPath = getConfigPath({
pathToHome,
Expand All @@ -121,18 +133,15 @@ export class NodeSpawner {
}
}

public async spawnMainNode(
overrides: {
newL1GasPrice?: bigint;
newPubdataPrice?: bigint;
customBaseToken?: boolean;
externalPriceApiClientForcedNumerator?: number;
externalPriceApiClientForcedDenominator?: number;
externalPriceApiClientForcedFluctuation?: number;
baseTokenPricePollingIntervalMs?: number;
baseTokenAdjusterL1UpdateDeviationPercentage?: number;
} | null = null
): Promise<Node<NodeType.MAIN>> {
public async killAndSpawnMainNode(configOverrides: MainNodeOptions | null = null): Promise<void> {
if (this.mainNode != null) {
await this.mainNode.killAndWaitForShutdown();
this.mainNode = null;
}
this.mainNode = await this.spawnMainNode(configOverrides);
}

private async spawnMainNode(overrides: MainNodeOptions | null): Promise<Node<NodeType.MAIN>> {
const env = this.env ?? process.env;
const { fileConfig, pathToHome, options, logs } = this;

Expand Down
24 changes: 9 additions & 15 deletions core/tests/ts-integration/tests/fees.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ testFees('Test fees', function () {
let ethClientWeb3Url: string;
let apiWeb3JsonRpcHttpUrl: string;
let mainNodeSpawner: NodeSpawner;
let mainNode: Node<NodeType.MAIN>;

const fileConfig = shouldLoadConfigFromFile();
const pathToHome = path.join(__dirname, '../../../..');
Expand Down Expand Up @@ -124,7 +123,7 @@ testFees('Test fees', function () {
baseTokenAddress
});

mainNode = await mainNodeSpawner.spawnMainNode();
await mainNodeSpawner.killAndSpawnMainNode();

alice = testMaster.mainAccount();
tokenDetails = testMaster.environment().erc20Token;
Expand Down Expand Up @@ -210,8 +209,7 @@ testFees('Test fees', function () {
];
for (const gasPrice of L1_GAS_PRICES_TO_TEST) {
// For the sake of simplicity, we'll use the same pubdata price as the L1 gas price.
await mainNode.killAndWaitForShutdown();
mainNode = await mainNodeSpawner.spawnMainNode({
await mainNodeSpawner.killAndSpawnMainNode({
newL1GasPrice: gasPrice,
newPubdataPrice: gasPrice
});
Expand Down Expand Up @@ -252,9 +250,7 @@ testFees('Test fees', function () {
test('Test gas price expected value', async () => {
const receiver = ethers.Wallet.createRandom().address;
const l1GasPrice = 2_000_000_000n; /// set to 2 gwei

await mainNode.killAndWaitForShutdown();
mainNode = await mainNodeSpawner.spawnMainNode({
await mainNodeSpawner.killAndSpawnMainNode({
newL1GasPrice: l1GasPrice,
newPubdataPrice: l1GasPrice
});
Expand Down Expand Up @@ -287,18 +283,18 @@ testFees('Test fees', function () {

console.log('feeParams', feeParams);
console.log('receipt', receipt);
console.log('gas price', await alice._providerL2().getGasPrice());
console.log('gas price');
console.log(await alice._providerL2().getFeeParams());
expect(receipt.gasPrice).toBe(BigInt(expectedConvertedGasPrice));
expect(await alice._providerL2().getGasPrice()).toBe(BigInt(expectedConvertedGasPrice));
});

test('Test base token ratio fluctuations', async () => {
const l1GasPrice = 2_000_000_000n; /// set to 2 gwei

if (isETHBasedChain) return;

await mainNode.killAndWaitForShutdown();
mainNode = await mainNodeSpawner.spawnMainNode({
await mainNodeSpawner.killAndSpawnMainNode({
newL1GasPrice: l1GasPrice,
newPubdataPrice: l1GasPrice,
externalPriceApiClientForcedNumerator: 300,
Expand Down Expand Up @@ -369,8 +365,7 @@ testFees('Test fees', function () {
// that the gasLimit is indeed over u32::MAX, which is the most important tested property.
const requiredPubdataPrice = minimalL2GasPrice * 100_000n;

await mainNode.killAndWaitForShutdown();
mainNode = await mainNodeSpawner.spawnMainNode({
await mainNodeSpawner.killAndSpawnMainNode({
newL1GasPrice: requiredPubdataPrice,
newPubdataPrice: requiredPubdataPrice
});
Expand Down Expand Up @@ -415,11 +410,10 @@ testFees('Test fees', function () {

afterAll(async () => {
await testMaster.deinitialize();
await mainNode.killAndWaitForShutdown();
// Returning the pubdata price to the default one
// Spawning with no options restores defaults.
mainNode = await mainNodeSpawner.spawnMainNode();
__ZKSYNC_TEST_CONTEXT_OWNER__.setL2NodePid(mainNode.proc.pid!);
await mainNodeSpawner.killAndSpawnMainNode();
__ZKSYNC_TEST_CONTEXT_OWNER__.setL2NodePid(mainNodeSpawner.mainNode!.proc.pid!);
});
});

Expand Down

0 comments on commit b03433b

Please sign in to comment.