Skip to content

Commit

Permalink
test: Improve tests performance (#1121)
Browse files Browse the repository at this point in the history
* test: fix transaction retry interval fallback for devnet tests

* test: remove test.only
  • Loading branch information
lukasaric authored May 6, 2024
1 parent ba35b6c commit d002bae
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 21 deletions.
25 changes: 8 additions & 17 deletions __tests__/config/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@ import fs from 'node:fs';
import path from 'node:path';

import { Account, Provider, ProviderInterface, RpcProvider, json } from '../../src';
import {
CompiledSierra,
CompiledSierraCasm,
LegacyCompiledContract,
waitForTransactionOptions,
} from '../../src/types';
import { CompiledSierra, CompiledSierraCasm, LegacyCompiledContract } from '../../src/types';
import { ETransactionVersion } from '../../src/types/api';
import { toHex } from '../../src/utils/num';

Expand Down Expand Up @@ -73,22 +68,18 @@ export const compiledTestRejectSierra = readContractSierra('cairo/testReject/tes
export const compiledTestRejectCasm = readContractSierraCasm('cairo/testReject/test_reject');
export const compiledSidMulticall = readContractSierra('starknetId/multicall/multicall.sierra');
export const compiledSidMulticallCasm = readContractSierraCasm('starknetId/multicall/multicall');

export function getTestProvider(isProvider?: true): ProviderInterface;
export function getTestProvider(isProvider?: false): RpcProvider;
export function getTestProvider(isProvider: boolean = true): ProviderInterface | RpcProvider {
const provider = isProvider
? new Provider({ nodeUrl: process.env.TEST_RPC_URL })
: new RpcProvider({ nodeUrl: process.env.TEST_RPC_URL });
const isDevnet = process.env.IS_DEVNET === 'true';

if (process.env.IS_DEVNET === 'true') {
const providerOptions = {
nodeUrl: process.env.TEST_RPC_URL,
// accelerate the tests when running locally
const originalWaitForTransaction = provider.waitForTransaction.bind(provider);
provider.waitForTransaction = (txHash: string, options: waitForTransactionOptions = {}) => {
return originalWaitForTransaction(txHash, { retryInterval: 1000, ...options });
};
}

return provider;
...(isDevnet && { transactionRetryIntervalFallback: 1000 }),
};
return isProvider ? new Provider(providerOptions) : new RpcProvider(providerOptions);
}

export const TEST_TX_VERSION = process.env.TX_VERSION === 'v3' ? ETransactionVersion.V3 : undefined;
Expand Down
21 changes: 18 additions & 3 deletions src/channel/rpc_0_7.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,21 @@ export class RpcChannel {

private specVersion?: string;

private transactionRetryIntervalFallback?: number;

readonly waitMode: Boolean; // behave like web2 rpc and return when tx is processed

constructor(optionsOrProvider?: RpcProviderOptions) {
const { nodeUrl, retries, headers, blockIdentifier, chainId, specVersion, waitMode } =
optionsOrProvider || {};
const {
nodeUrl,
retries,
headers,
blockIdentifier,
chainId,
specVersion,
waitMode,
transactionRetryIntervalFallback,
} = optionsOrProvider || {};
if (Object.values(NetworkName).includes(nodeUrl as NetworkName)) {
this.nodeUrl = getDefaultNodeUrl(nodeUrl as NetworkName, optionsOrProvider?.default);
} else if (nodeUrl) {
Expand All @@ -69,6 +79,11 @@ export class RpcChannel {
this.specVersion = specVersion;
this.waitMode = waitMode || false;
this.requestId = 0;
this.transactionRetryIntervalFallback = transactionRetryIntervalFallback;
}

private get transactionRetryIntervalDefault() {
return this.transactionRetryIntervalFallback ?? 5000;
}

public setChainId(chainId: StarknetChainId) {
Expand Down Expand Up @@ -250,7 +265,7 @@ export class RpcChannel {
let { retries } = this;
let onchain = false;
let isErrorState = false;
const retryInterval = options?.retryInterval ?? 5000;
const retryInterval = options?.retryInterval ?? this.transactionRetryIntervalDefault;
const errorStates: any = options?.errorStates ?? [
RPC.ETransactionStatus.REJECTED,
// TODO: commented out to preserve the long-standing behavior of "reverted" not being treated as an error by default
Expand Down
1 change: 1 addition & 0 deletions src/provider/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ export class RpcProvider implements ProviderInterface {
txHash,
options
)) as GetTxReceiptResponseWithoutHelper;

return new ReceiptTx(receiptWoHelper) as GetTransactionReceiptResponse;
}

Expand Down
1 change: 1 addition & 0 deletions src/types/provider/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface ProviderOptions extends RpcProviderOptions {}
export type RpcProviderOptions = {
nodeUrl?: string | NetworkName;
retries?: number;
transactionRetryIntervalFallback?: number;
headers?: object;
blockIdentifier?: BlockIdentifier;
chainId?: StarknetChainId;
Expand Down
2 changes: 1 addition & 1 deletion src/utils/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export function isUrl(s?: string): boolean {
* const defaultPath = "/";
* const urlOrPath = "/docs";
* const result = buildUrl(baseUrl, defaultPath, urlOrPath);
*
*
* result = "https://starknetjs.com/docs"
*/
export function buildUrl(baseUrl: string, defaultPath: string, urlOrPath?: string) {
Expand Down

0 comments on commit d002bae

Please sign in to comment.