From fbde107637e0a95040c352a596749b3b500368c6 Mon Sep 17 00:00:00 2001 From: jdevcs Date: Thu, 18 Jul 2024 11:35:25 +0200 Subject: [PATCH 01/20] updated config and closeOpenConnection in eth --- packages/web3-eth/package.json | 2 +- .../test/integration/web3_eth/send_transaction.test.ts | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/web3-eth/package.json b/packages/web3-eth/package.json index 2de8e03264f..43a9f49eb2f 100644 --- a/packages/web3-eth/package.json +++ b/packages/web3-eth/package.json @@ -40,7 +40,7 @@ "test:e2e:sepolia": "jest --config=./test/e2e/jest.config.js --forceExit", "test:watch": "npm test -- --watch", "test:unit": "jest --config=./test/unit/jest.config.js", - "test:integration": "jest --config=./test/integration/jest.config.js --runInBand --forceExit", + "test:integration": "jest --config=./test/integration/jest.config.js --runInBand", "test:coverage:integration": "jest --config=./test/integration/jest.config.js --runInBand --forceExit --coverage=true --coverage-reporters=text", "test:e2e:electron": "npx cypress run --headless --browser electron", "test:e2e:chrome": "npx cypress run --headless --browser chrome", diff --git a/packages/web3-eth/test/integration/web3_eth/send_transaction.test.ts b/packages/web3-eth/test/integration/web3_eth/send_transaction.test.ts index 9b0eda360dc..b16479d886b 100644 --- a/packages/web3-eth/test/integration/web3_eth/send_transaction.test.ts +++ b/packages/web3-eth/test/integration/web3_eth/send_transaction.test.ts @@ -99,6 +99,8 @@ describe('Web3Eth.sendTransaction', () => { to: '0x0000000000000000000000000000000000000000', value: BigInt(1), }); + + await closeOpenConnection(web3EthWithWallet); }); it('should make a simple value transfer - with local wallet indexed receiver', async () => { @@ -130,6 +132,8 @@ describe('Web3Eth.sendTransaction', () => { to: wallet.get(0)?.address.toLowerCase(), value: BigInt(1), }); + + await closeOpenConnection(web3EthWithWallet); }); it('should make a simple value transfer - with local wallet indexed sender and receiver', async () => { @@ -165,6 +169,8 @@ describe('Web3Eth.sendTransaction', () => { to: wallet.get(1)?.address.toLowerCase(), value: BigInt(1), }); + + await closeOpenConnection(web3EthWithWallet); }); it('should make a transaction with no value transfer', async () => { const transaction: Transaction = { From 08d18181766f2df0a869a10dff0e238e28251ea7 Mon Sep 17 00:00:00 2001 From: jdevcs Date: Thu, 18 Jul 2024 11:35:49 +0200 Subject: [PATCH 02/20] no default to http and close open con --- scripts/system_tests_utils.ts | 59 +++++++++++++++-------------------- 1 file changed, 26 insertions(+), 33 deletions(-) diff --git a/scripts/system_tests_utils.ts b/scripts/system_tests_utils.ts index 48ea2129c06..2305d106e9a 100644 --- a/scripts/system_tests_utils.ts +++ b/scripts/system_tests_utils.ts @@ -54,12 +54,12 @@ import { // eslint-disable-next-line import/no-extraneous-dependencies import { Personal } from 'web3-eth-personal'; // eslint-disable-next-line import/no-extraneous-dependencies -import Web3 from 'web3'; +import {Web3, WebSocketProvider } from 'web3'; // eslint-disable-next-line import/no-extraneous-dependencies import { NonPayableMethodObject } from 'web3-eth-contract'; // eslint-disable-next-line import/no-extraneous-dependencies -import HttpProvider from 'web3-providers-http'; +//import HttpProvider from 'web3-providers-http'; // eslint-disable-next-line import/no-extraneous-dependencies import { IpcProvider } from 'web3-providers-ipc'; import accountsString from './accounts.json'; @@ -73,7 +73,7 @@ export const getEnvVar = (name: string): string | undefined => // eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access global.Cypress ? Cypress.env(name) : process.env[name]; -export const DEFAULT_SYSTEM_PROVIDER = 'http://127.0.0.1:8545'; +export const DEFAULT_SYSTEM_PROVIDER = getEnvVar('WEB3_SYSTEM_TEST_PROVIDER') ?? ""; export const DEFAULT_SYSTEM_ENGINE = 'node'; export const BACKEND = { GETH: 'geth', @@ -83,13 +83,17 @@ export const BACKEND = { MAINNET: 'mainnet', }; -export const getSystemTestProviderUrl = (): string => - getEnvVar('WEB3_SYSTEM_TEST_PROVIDER') ?? DEFAULT_SYSTEM_PROVIDER; +export const getSystemTestProviderUrl = (): string => + getEnvVar('WEB3_SYSTEM_TEST_PROVIDER') ?? ""; export const getSystemTestProvider = (): | string | SupportedProviders => { const url = getSystemTestProviderUrl(); + + if(url === "") + throw new Error("undefined URL in env var WEB3_SYSTEM_TEST_PROVIDER"); + if (url.includes('ipc')) { return new IpcProvider(url); } @@ -155,36 +159,16 @@ export const waitForOpenConnection = async ( export const closeOpenConnection = async (web3Context: Web3Context) => { if ( - !isSocket || - web3Context?.provider instanceof HttpProvider || - (web3Context?.provider?.supportsSubscriptions && - !web3Context.provider?.supportsSubscriptions()) - ) { - return; - } - // make sure we try to close the connection after it is established - if ( - web3Context?.provider && - (web3Context.provider as unknown as Web3BaseProvider).getStatus() === 'connecting' - ) { - await waitForOpenConnection(web3Context); - } - // If an error happened during closing, that is acceptable at tests, just print a 'warn'. - if (web3Context?.provider) { - (web3Context.provider as unknown as Web3BaseProvider).on('error', (err: any) => { - console.warn('error while trying to close the connection', err); - }); - } - // Wait a bit to ensure the connection does not have a pending data that - // could cause an error if written after closing the connection. - await new Promise(resolve => { - setTimeout(resolve, 500); - }); - if ( - web3Context?.provider && + web3Context?.provider && ( + web3Context?.provider instanceof WebSocketProvider || + web3Context?.provider instanceof IpcProvider + + ) && 'disconnect' in (web3Context.provider as unknown as Web3BaseProvider) ) { - (web3Context.provider as unknown as Web3BaseProvider).disconnect(1000, ''); + (web3Context.provider as unknown as Web3BaseProvider).reset(); + (web3Context.provider as unknown as Web3BaseProvider).disconnect();//ß1000, ''); + } }; @@ -246,6 +230,8 @@ export const refillAccount = async (from: string, to: string, value: string | nu to, value, }); + + await closeOpenConnection(web3Eth); }; let mainAcc: string; @@ -256,6 +242,7 @@ export const createNewAccount = async (config?: { password?: string; doNotImport?: boolean; }): Promise<{ address: string; privateKey: string }> => { + const acc = config?.privateKey ? privateKeyToAccount(config?.privateKey) : _createAccount(); const clientUrl = DEFAULT_SYSTEM_PROVIDER; @@ -267,6 +254,7 @@ export const createNewAccount = async (config?: { await web3.hardhat.impersonateAccount(acc.address); // await impersonateAccount(acc.address); await web3.hardhat.setBalance(acc.address, web3.utils.toHex('100000000')); + await closeOpenConnection(web3); } else { const web3Personal = new Personal(clientUrl); if (!config?.doNotImport) { @@ -279,6 +267,7 @@ export const createNewAccount = async (config?: { } await web3Personal.unlockAccount(acc.address, config.password ?? '123456', 100000000); + await closeOpenConnection(web3Personal); } } @@ -288,17 +277,21 @@ export const createNewAccount = async (config?: { const web3 = new Web3(url); web3.registerPlugin(new HardhatPlugin()); await web3.hardhat.setBalance(acc.address, web3.utils.toHex('100000000')); + await closeOpenConnection(web3); } else { const web3Personal = new Personal(clientUrl); if (!mainAcc) { [mainAcc] = await web3Personal.getAccounts(); } await refillAccount(mainAcc, acc.address, '100000000000000000'); + await closeOpenConnection(web3Personal); } } return { address: acc.address.toLowerCase(), privateKey: acc.privateKey }; }; + + let tempAccountList: { address: string; privateKey: string }[] = []; const walletsOnWorker = 20; From 2f0f9bbdfa8d7894e491af51fe68f0641420e582 Mon Sep 17 00:00:00 2001 From: jdevcs Date: Thu, 18 Jul 2024 11:50:53 +0200 Subject: [PATCH 03/20] unit test using local provider instead of default in utils --- packages/web3-eth-contract/test/unit/contract.test.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/web3-eth-contract/test/unit/contract.test.ts b/packages/web3-eth-contract/test/unit/contract.test.ts index 449280c41a7..c032f08e367 100644 --- a/packages/web3-eth-contract/test/unit/contract.test.ts +++ b/packages/web3-eth-contract/test/unit/contract.test.ts @@ -38,7 +38,6 @@ import { GreeterWithOverloadingBytecode, } from '../shared_fixtures/build/GreeterWithOverloading'; import { AllGetPastEventsData, getLogsData, getPastEventsData } from '../fixtures/unitTestFixtures'; -import { getSystemTestProvider } from '../fixtures/system_test_utils'; import { erc721Abi } from '../fixtures/erc721'; import { ERC20TokenAbi } from '../shared_fixtures/build/ERC20Token'; import { processAsync } from '../shared_fixtures/utils'; @@ -150,7 +149,7 @@ describe('Contract', () => { }); it('should set the provider, from options, upon instantiation', () => { - const provider = getSystemTestProvider(); + const provider = "http://127.0.0.1:4545"; const contract = new Contract([], '', { provider, }); @@ -162,7 +161,7 @@ describe('Contract', () => { }); it('should set the provider, from context, upon instantiation', () => { - const provider = getSystemTestProvider(); + const provider = "http://127.0.0.1:4545"; const contract = new Contract( [], '', From 13a812673e9cc1de2e2f8de5931bd2ce0dba3f85 Mon Sep 17 00:00:00 2001 From: jdevcs Date: Thu, 18 Jul 2024 11:52:54 +0200 Subject: [PATCH 04/20] lint fix --- scripts/system_tests_utils.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/scripts/system_tests_utils.ts b/scripts/system_tests_utils.ts index 2305d106e9a..78a8399e78a 100644 --- a/scripts/system_tests_utils.ts +++ b/scripts/system_tests_utils.ts @@ -58,8 +58,7 @@ import {Web3, WebSocketProvider } from 'web3'; // eslint-disable-next-line import/no-extraneous-dependencies import { NonPayableMethodObject } from 'web3-eth-contract'; -// eslint-disable-next-line import/no-extraneous-dependencies -//import HttpProvider from 'web3-providers-http'; + // eslint-disable-next-line import/no-extraneous-dependencies import { IpcProvider } from 'web3-providers-ipc'; import accountsString from './accounts.json'; @@ -167,7 +166,7 @@ export const closeOpenConnection = async (web3Context: Web3Context) => { 'disconnect' in (web3Context.provider as unknown as Web3BaseProvider) ) { (web3Context.provider as unknown as Web3BaseProvider).reset(); - (web3Context.provider as unknown as Web3BaseProvider).disconnect();//ß1000, ''); + (web3Context.provider as unknown as Web3BaseProvider).disconnect(); } }; From c082a87b654038ae2346cc60b8b92d3eed3cad18 Mon Sep 17 00:00:00 2001 From: jdevcs Date: Mon, 22 Jul 2024 14:46:55 +0200 Subject: [PATCH 05/20] bug fix of maxListenersWarningThreshold --- packages/web3-core/src/web3_config.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/web3-core/src/web3_config.ts b/packages/web3-core/src/web3_config.ts index 3c335292c4a..7e96c3de3d7 100644 --- a/packages/web3-core/src/web3_config.ts +++ b/packages/web3-core/src/web3_config.ts @@ -114,6 +114,14 @@ export abstract class Web3Config const keys = Object.keys(options) as (keyof Web3ConfigOptions)[]; for (const key of keys) { this._triggerConfigChange(key, options[key]); + + if(!isNullish(options[key] && + typeof options[key] === 'number' && + key === 'maxListenersWarningThreshold' as keyof Web3ConfigOptions) ) + { + // additionally set in event emitter + this.setMaxListenerWarningThreshold(Number(options[key])); + } } Object.assign(this.config, options); } From b419b781790f6fd31ee91cd5e51a3970c024281d Mon Sep 17 00:00:00 2001 From: jdevcs Date: Tue, 23 Jul 2024 13:17:19 +0200 Subject: [PATCH 06/20] fixes in defaults and sys test utils --- .../test/integration/defaults.test.ts | 185 ++++++++++-------- scripts/system_tests_utils.ts | 33 ++-- 2 files changed, 127 insertions(+), 91 deletions(-) diff --git a/packages/web3-eth/test/integration/defaults.test.ts b/packages/web3-eth/test/integration/defaults.test.ts index a9651fa20e3..386ae618cca 100644 --- a/packages/web3-eth/test/integration/defaults.test.ts +++ b/packages/web3-eth/test/integration/defaults.test.ts @@ -51,11 +51,10 @@ import { import { BasicAbi, BasicBytecode } from '../shared_fixtures/build/Basic'; import { MsgSenderAbi, MsgSenderBytecode } from '../shared_fixtures/build/MsgSender'; import { getTransactionGasPricing } from '../../src/utils/get_transaction_gas_pricing'; -import { Resolve, sendFewTxes } from './helper'; +import { sendFewTxes } from './helper'; describe('defaults', () => { let web3Eth: Web3Eth; - let eth2: Web3Eth; let clientUrl: string | SupportedProviders; let contract: Contract; let deployOptions: Record; @@ -76,10 +75,8 @@ describe('defaults', () => { afterEach(async () => { await closeOpenConnection(web3Eth); - await closeOpenConnection(eth2); + await closeOpenConnection(contract); }); - - describe('defaults', () => { it('defaultAccount', async () => { const tempAcc2 = await createTempAccount(); const tempAcc3 = await createTempAccount(); @@ -102,7 +99,7 @@ describe('defaults', () => { expect(web3Eth.defaultAccount).toBe(tempAcc.address); // set by create new instance - eth2 = new Web3Eth({ + const eth2 = new Web3Eth({ config: { defaultAccount: tempAcc3.address, }, @@ -152,6 +149,8 @@ describe('defaults', () => { expect((fromPass2 as unknown as string).toLowerCase()).toBe( tempAcc2.address.toLowerCase(), ); + await closeOpenConnection(eth2); + await closeOpenConnection(contractMsgFrom); }); it('handleRevert', () => { /* @@ -174,7 +173,7 @@ describe('defaults', () => { expect(web3Eth.handleRevert).toBe(true); // set by create new instance - eth2 = new Web3Eth({ + const eth2 = new Web3Eth({ config: { handleRevert: true, }, @@ -203,7 +202,7 @@ describe('defaults', () => { expect(web3Eth.defaultBlock).toBe('earliest'); // set by create new instance - eth2 = new Web3Eth({ + const eth2 = new Web3Eth({ provider: web3Eth.provider, config: { defaultBlock: 'earliest', @@ -266,8 +265,10 @@ describe('defaults', () => { expect(Number(hexToNumber(storageLatest))).toBe(10); expect(transactionCountLatest).toBe(BigInt(1)); expect(Number(balanceLatest)).toBeGreaterThan(0); + await closeOpenConnection(eth2); + await closeOpenConnection(contractDeployed); }); - it('transactionSendTimeout', () => { + it('transactionSendTimeout', async () => { // default expect(web3Eth.transactionSendTimeout).toBe(750 * 1000); @@ -278,15 +279,16 @@ describe('defaults', () => { expect(web3Eth.transactionSendTimeout).toBe(1); // set by create new instance - eth2 = new Web3Eth({ + const eth2 = new Web3Eth({ provider: web3Eth.provider, config: { transactionSendTimeout: 120, }, }); expect(eth2.transactionSendTimeout).toBe(120); + await closeOpenConnection(eth2); }); - it('transactionBlockTimeout', () => { + it('transactionBlockTimeout', async () => { // default expect(web3Eth.transactionBlockTimeout).toBe(50); @@ -297,14 +299,14 @@ describe('defaults', () => { expect(web3Eth.transactionBlockTimeout).toBe(1); // set by create new instance - eth2 = new Web3Eth({ + const eth2 = new Web3Eth({ config: { transactionBlockTimeout: 120, }, }); expect(eth2.transactionBlockTimeout).toBe(120); }); - it('transactionConfirmationBlocks', () => { + it('transactionConfirmationBlocks default change should work', async () => { // default // eslint-disable-next-line jest/no-standalone-expect expect(web3Eth.transactionConfirmationBlocks).toBe(24); @@ -317,23 +319,28 @@ describe('defaults', () => { expect(web3Eth.transactionConfirmationBlocks).toBe(3); // set by create new instance - eth2 = new Web3Eth({ + const eth2 = new Web3Eth({ config: { transactionConfirmationBlocks: 4, }, }); // eslint-disable-next-line jest/no-standalone-expect expect(eth2.transactionConfirmationBlocks).toBe(4); + }); + it('transactionConfirmationBlocks implementation', async () => { const tempAcc2 = await createTempAccount(); - const waitConfirmations = 1; - const eth = new Web3Eth(web3Eth.provider); + const waitConfirmations = 2; + const eth = new Web3Eth(getSystemTestProvider()); eth.setConfig({ transactionConfirmationBlocks: waitConfirmations }); - + const from = tempAcc.address; const to = tempAcc2.address; const value = `0x1`; + + let confirmationCount = 0; + const sentTx: Web3PromiEvent< TransactionReceipt, SendTransactionEvents @@ -341,35 +348,33 @@ describe('defaults', () => { to, value, from, + }).on('confirmation', (_data) => { + confirmationCount += 1; + if (confirmationCount >= waitConfirmations) { + sentTx.removeAllListeners(); // Clean up listeners + } }); - const receiptPromise = new Promise((resolve: Resolve) => { - // Tx promise is handled separately - // eslint-disable-next-line no-void - void sentTx.on('receipt', (params: TransactionReceipt) => { - expect(Number(params.status)).toBe(1); - resolve(); - }); - }); - let shouldBe = 1; - const confirmationPromise = new Promise((resolve: Resolve) => { - // Tx promise is handled separately - // eslint-disable-next-line no-void - void sentTx.on('confirmation', ({ confirmations }) => { - expect(Number(confirmations)).toBeGreaterThanOrEqual(shouldBe); - shouldBe += 1; - if (shouldBe > waitConfirmations) { + const receipt = await sentTx; + expect(Number(receipt.status)).toBe(1); + + // Optionally send a few sample transactions + await sendFewSampleTxs(isIpc ? 5 * waitConfirmations : 2 * waitConfirmations); + + expect(confirmationCount).toBe(waitConfirmations); + + await new Promise((resolve) => { + const timeout = setTimeout(() => { + if (confirmationCount >= waitConfirmations) { + clearTimeout(timeout); resolve(); } - }); + }, 5000); }); - await sentTx; - await receiptPromise; - await sendFewSampleTxs(isIpc ? 2 * waitConfirmations : waitConfirmations); - await confirmationPromise; await closeOpenConnection(eth); }); - it('transactionPollingInterval and transactionPollingTimeout', () => { + + it('transactionPollingInterval and transactionPollingTimeout', async () => { // default expect(web3Eth.transactionPollingInterval).toBe(1000); expect(web3Eth.transactionPollingTimeout).toBe(750 * 1000); @@ -383,7 +388,7 @@ describe('defaults', () => { expect(web3Eth.transactionPollingTimeout).toBe(10); // set by create new instance - eth2 = new Web3Eth({ + const eth2 = new Web3Eth({ config: { transactionPollingInterval: 400, transactionPollingTimeout: 10, @@ -451,7 +456,7 @@ describe('defaults', () => { expect(web3Eth.transactionConfirmationPollingInterval).toBe(10); // set by create new instance - eth2 = new Web3Eth({ + const eth2 = new Web3Eth({ config: { transactionReceiptPollingInterval: 400, transactionConfirmationPollingInterval: 10, @@ -471,7 +476,7 @@ describe('defaults', () => { expect(web3Eth.blockHeaderTimeout).toBe(3); // set by create new instance - eth2 = new Web3Eth({ + const eth2 = new Web3Eth({ config: { blockHeaderTimeout: 4, }, @@ -497,7 +502,7 @@ describe('defaults', () => { ); // set by create new instance - eth2 = new Web3Eth({ + const eth2 = new Web3Eth({ config: { enableExperimentalFeatures: { useSubscriptionWhenCheckingBlockTimeout: true, @@ -524,7 +529,7 @@ describe('defaults', () => { expect(web3Eth.enableExperimentalFeatures.useRpcCallSpecification).toBe(true); // set by create new instance - eth2 = new Web3Eth({ + const eth2 = new Web3Eth({ config: { enableExperimentalFeatures: { useSubscriptionWhenCheckingBlockTimeout: false, @@ -533,6 +538,7 @@ describe('defaults', () => { }, }); expect(eth2.enableExperimentalFeatures.useRpcCallSpecification).toBe(true); + }); it('should fallback to polling if provider support `on` but `newBlockHeaders` does not arrive in `blockHeaderTimeout` seconds', async () => { @@ -572,6 +578,7 @@ describe('defaults', () => { value, }); + let confirmationCount = 0; const confirmationPromise = new Promise((resolve: (status: bigint) => void) => { // Tx promise is handled separately // eslint-disable-next-line no-void @@ -584,45 +591,54 @@ describe('defaults', () => { confirmations: bigint; receipt: { status: bigint }; }) => { + confirmationCount = Number(confirmations); // Being able to get 2 confirmations means the polling for new blocks works if (confirmations >= 2) { sentTx.removeAllListeners(); resolve(status); } else { - // Send a transaction to cause dev providers creating new blocks to fire the 'confirmation' event again. - await tempEth.sendTransaction({ - from, - to, - value, - }); + // Send few transaction to cause dev providers creating new blocks to fire the 'confirmation' event again. + await sendFewSampleTxs(5); } }, ); }); - await sentTx; + const receipt = await sentTx; + expect(Number(receipt.status)).toBe(1); // Ensure the promise the get the confirmations resolves with no error const status = await confirmationPromise; expect(status).toBe(BigInt(1)); + + await new Promise((resolve) => { + const timeout = setTimeout(async () => { + if (confirmationCount >= 2) { + clearTimeout(timeout); + resolve(); + } + }, 8000) + }); + await closeOpenConnection(tempEth); }); + it('maxListenersWarningThreshold test default config', () => { // default expect(web3Eth.maxListenersWarningThreshold).toBe(100); }); it('maxListenersWarningThreshold set maxListeners through variable', () => { - eth2 = new Web3Eth({}); + const eth2 = new Web3Eth({}); eth2.maxListenersWarningThreshold = 3; expect(eth2.maxListenersWarningThreshold).toBe(3); expect(eth2.getMaxListeners()).toBe(3); }); it('maxListenersWarningThreshold set config', () => { - const eth = new Web3Eth({}); - eth.setConfig({ + const eth3 = new Web3Eth({}); + eth3.setConfig({ maxListenersWarningThreshold: 3, }); - expect(eth2.maxListenersWarningThreshold).toBe(3); - expect(eth2.getMaxListeners()).toBe(3); + expect(eth3.maxListenersWarningThreshold).toBe(3); + expect(eth3.getMaxListeners()).toBe(3); }); it('defaultNetworkId', async () => { // default @@ -635,13 +651,13 @@ describe('defaults', () => { expect(web3Eth.defaultNetworkId).toBe(3); // set by create new instance - eth2 = new Web3Eth({ + const eth2_2 = new Web3Eth({ provider: web3Eth.provider, config: { defaultNetworkId: 4, }, }); - expect(eth2.defaultNetworkId).toBe(4); + expect(eth2_2.defaultNetworkId).toBe(4); const res = await defaultTransactionBuilder({ transaction: { from: '0xEB014f8c8B418Db6b45774c326A0E64C78914dC0', @@ -649,7 +665,7 @@ describe('defaults', () => { value: '0x174876e800', gas: '0x5208', }, - web3Context: eth2 as Web3Context, + web3Context: eth2_2 as Web3Context, }); expect(res.networkId).toBe(4); @@ -662,10 +678,12 @@ describe('defaults', () => { gas: '0x5208', networkId: 5, }, - web3Context: eth2 as Web3Context, + web3Context: eth2_2 as Web3Context, }); expect(resWithPassNetworkId.networkId).toBe(BigInt(5)); + + await closeOpenConnection(eth2_2); }); it('defaultChain', async () => { // default @@ -678,7 +696,7 @@ describe('defaults', () => { expect(web3Eth.defaultChain).toBe('ropsten'); // set by create new instance - eth2 = new Web3Eth({ + const eth2 = new Web3Eth({ provider: web3Eth.provider, config: { defaultChain: 'rinkeby', @@ -695,6 +713,7 @@ describe('defaults', () => { web3Context: eth2 as Web3Context, }); expect(res.chain).toBe('rinkeby'); + await closeOpenConnection(eth2); }); it('defaultHardfork', async () => { // default @@ -707,7 +726,7 @@ describe('defaults', () => { expect(web3Eth.defaultHardfork).toBe('dao'); // set by create new instance - eth2 = new Web3Eth({ + const eth2 = new Web3Eth({ provider: web3Eth.provider, config: { defaultHardfork: 'istanbul', @@ -732,6 +751,7 @@ describe('defaults', () => { eth2, ); expect(res.common.hardfork()).toBe('istanbul'); + await closeOpenConnection(eth2); }); it('defaultCommon', () => { // default @@ -754,13 +774,14 @@ describe('defaults', () => { expect(web3Eth.defaultCommon).toBe(common); // set by create new instance - eth2 = new Web3Eth({ + const eth2 = new Web3Eth({ config: { defaultCommon: common, }, }); expect(eth2.defaultCommon).toBe(common); }); + it('defaultTransactionType', async () => { // default expect(web3Eth.defaultTransactionType).toBe('0x2'); @@ -770,15 +791,20 @@ describe('defaults', () => { }); expect(web3Eth.defaultTransactionType).toBe('0x3'); + //revert back to default + web3Eth.setConfig({ + defaultTransactionType: '0x2', + }); + // set by create new instance - eth2 = new Web3Eth({ - provider: clientUrl, + const eth_2 = new Web3Eth({ + provider: web3Eth.provider, config: { defaultTransactionType: '0x4444', }, }); - expect(eth2.defaultTransactionType).toBe('0x4444'); + expect(eth_2.defaultTransactionType).toBe('0x4444'); const res = getTransactionType( { @@ -791,7 +817,7 @@ describe('defaults', () => { chainId: '0x1', gasLimit: '0x5208', }, - eth2, + eth_2, ); expect(res).toBe('0x4444'); @@ -812,7 +838,7 @@ describe('defaults', () => { gasLimit: '0x5208', maxFeePerGas: '0x32', }, - eth2, + eth_2, ); expect(maxFeePerGasOverride).toBe('0x2'); const maxPriorityFeePerGasOverride = getTransactionType( @@ -827,7 +853,7 @@ describe('defaults', () => { gasLimit: '0x5208', maxPriorityFeePerGas: '0x32', }, - eth2, + eth_2, ); expect(maxPriorityFeePerGasOverride).toBe('0x2'); const hardforkOverride = getTransactionType( @@ -842,7 +868,7 @@ describe('defaults', () => { gasLimit: '0x5208', hardfork: 'london', }, - eth2, + eth_2, ); expect(hardforkOverride).toBe('0x2'); const commonOverride = getTransactionType( @@ -860,7 +886,7 @@ describe('defaults', () => { hardfork: 'london', }, }, - eth2, + eth_2, ); expect(commonOverride).toBe('0x2'); @@ -884,7 +910,7 @@ describe('defaults', () => { }, ], }, - eth2, + eth_2, ); expect(accessListOverride).toBe('0x1'); @@ -900,7 +926,7 @@ describe('defaults', () => { gasLimit: '0x5208', hardfork: 'berlin', }, - eth2, + eth_2, ); expect(hardforkBerlinOverride).toBe('0x0'); @@ -919,9 +945,10 @@ describe('defaults', () => { hardfork: 'berlin', }, }, - eth2, + eth_2, ); expect(commonBerlinOverride).toBe('0x0'); + await closeOpenConnection(eth_2); }); it('defaultMaxPriorityFeePerGas', async () => { // default @@ -933,7 +960,7 @@ describe('defaults', () => { expect(web3Eth.defaultMaxPriorityFeePerGas).toBe(numberToHex(2100000000)); // set by create new instance - eth2 = new Web3Eth({ + const eth2 = new Web3Eth({ provider: web3Eth.provider, config: { defaultMaxPriorityFeePerGas: numberToHex(1200000000), @@ -976,6 +1003,7 @@ describe('defaults', () => { DEFAULT_RETURN_FORMAT, ); expect(resOverride?.maxPriorityFeePerGas).toBe(BigInt('4883362083')); + await closeOpenConnection(eth2); }); it('transactionBuilder', async () => { // default @@ -992,7 +1020,7 @@ describe('defaults', () => { expect(web3Eth.transactionBuilder).toBe(newBuilderMock); // set by create new instance - eth2 = new Web3Eth({ + const eth2 = new Web3Eth({ config: { transactionBuilder: newBuilderMock, }, @@ -1027,7 +1055,7 @@ describe('defaults', () => { expect(web3Eth.transactionTypeParser).toBe(newParserMock); // set by create new instance - eth2 = new Web3Eth({ + const eth2 = new Web3Eth({ config: { transactionTypeParser: newParserMock, }, @@ -1049,5 +1077,4 @@ describe('defaults', () => { ); expect(newParserMock).toHaveBeenCalled(); }); - }); -}); +}); \ No newline at end of file diff --git a/scripts/system_tests_utils.ts b/scripts/system_tests_utils.ts index 78a8399e78a..1bee803dc5d 100644 --- a/scripts/system_tests_utils.ts +++ b/scripts/system_tests_utils.ts @@ -50,6 +50,7 @@ import { Web3EthExecutionAPI, FMT_NUMBER, FMT_BYTES, + TransactionReceipt, } from 'web3-types'; // eslint-disable-next-line import/no-extraneous-dependencies import { Personal } from 'web3-eth-personal'; @@ -165,10 +166,10 @@ export const closeOpenConnection = async (web3Context: Web3Context) => { ) && 'disconnect' in (web3Context.provider as unknown as Web3BaseProvider) ) { - (web3Context.provider as unknown as Web3BaseProvider).reset(); - (web3Context.provider as unknown as Web3BaseProvider).disconnect(); - } + (web3Context.provider as unknown as Web3BaseProvider).reset(); + (web3Context.provider as unknown as Web3BaseProvider).disconnect(); + } }; export const createAccountProvider = (context: Web3Context) => { @@ -224,12 +225,15 @@ export const createAccountProvider = (context: Web3Context) => export const refillAccount = async (from: string, to: string, value: string | number) => { const web3Eth = new Web3Eth(DEFAULT_SYSTEM_PROVIDER); - await web3Eth.sendTransaction({ + const receipt = await web3Eth.sendTransaction({ from, to, value, }); + if(receipt.status !== BigInt(1)) + throw new Error("refillAccount failed"); + await closeOpenConnection(web3Eth); }; @@ -477,16 +481,21 @@ export const sendFewSampleTxs = async (cnt = 1) => { const web3 = new Web3(DEFAULT_SYSTEM_PROVIDER); const fromAcc = await createLocalAccount(web3); const toAcc = createAccount(); - const res = []; + const res : TransactionReceipt[]= []; for (let i = 0; i < cnt; i += 1) { + + const receipt = await web3.eth.sendTransaction({ + to: toAcc.address, + value: '0x1', + from: fromAcc.address, + gas: '300000', + }); + + if(receipt.status !== BigInt(1)) + throw new Error("sendFewSampleTxs failed "); + res.push( - // eslint-disable-next-line no-await-in-loop - await web3.eth.sendTransaction({ - to: toAcc.address, - value: '0x1', - from: fromAcc.address, - gas: '300000', - }), + receipt ); } await closeOpenConnection(web3); From 9e6de4f281b91a784f61230401e0aaed396aebc8 Mon Sep 17 00:00:00 2001 From: jdevcs Date: Tue, 23 Jul 2024 13:32:13 +0200 Subject: [PATCH 07/20] lint fix --- .../test/integration/defaults.test.ts | 37 ++++++++++--------- scripts/system_tests_utils.ts | 4 +- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/packages/web3-eth/test/integration/defaults.test.ts b/packages/web3-eth/test/integration/defaults.test.ts index 386ae618cca..c8569d08705 100644 --- a/packages/web3-eth/test/integration/defaults.test.ts +++ b/packages/web3-eth/test/integration/defaults.test.ts @@ -288,7 +288,7 @@ describe('defaults', () => { expect(eth2.transactionSendTimeout).toBe(120); await closeOpenConnection(eth2); }); - it('transactionBlockTimeout', async () => { + it('transactionBlockTimeout', () => { // default expect(web3Eth.transactionBlockTimeout).toBe(50); @@ -611,6 +611,7 @@ describe('defaults', () => { expect(status).toBe(BigInt(1)); await new Promise((resolve) => { + // eslint-disable-next-line @typescript-eslint/no-misused-promises const timeout = setTimeout(async () => { if (confirmationCount >= 2) { clearTimeout(timeout); @@ -651,13 +652,13 @@ describe('defaults', () => { expect(web3Eth.defaultNetworkId).toBe(3); // set by create new instance - const eth2_2 = new Web3Eth({ + const eth2 = new Web3Eth({ provider: web3Eth.provider, config: { defaultNetworkId: 4, }, }); - expect(eth2_2.defaultNetworkId).toBe(4); + expect(eth2.defaultNetworkId).toBe(4); const res = await defaultTransactionBuilder({ transaction: { from: '0xEB014f8c8B418Db6b45774c326A0E64C78914dC0', @@ -665,7 +666,7 @@ describe('defaults', () => { value: '0x174876e800', gas: '0x5208', }, - web3Context: eth2_2 as Web3Context, + web3Context: eth2 as Web3Context, }); expect(res.networkId).toBe(4); @@ -678,12 +679,12 @@ describe('defaults', () => { gas: '0x5208', networkId: 5, }, - web3Context: eth2_2 as Web3Context, + web3Context: eth2 as Web3Context, }); expect(resWithPassNetworkId.networkId).toBe(BigInt(5)); - await closeOpenConnection(eth2_2); + await closeOpenConnection(eth2); }); it('defaultChain', async () => { // default @@ -791,20 +792,20 @@ describe('defaults', () => { }); expect(web3Eth.defaultTransactionType).toBe('0x3'); - //revert back to default + // revert back to default web3Eth.setConfig({ defaultTransactionType: '0x2', }); // set by create new instance - const eth_2 = new Web3Eth({ + const eth = new Web3Eth({ provider: web3Eth.provider, config: { defaultTransactionType: '0x4444', }, }); - expect(eth_2.defaultTransactionType).toBe('0x4444'); + expect(eth.defaultTransactionType).toBe('0x4444'); const res = getTransactionType( { @@ -817,7 +818,7 @@ describe('defaults', () => { chainId: '0x1', gasLimit: '0x5208', }, - eth_2, + eth, ); expect(res).toBe('0x4444'); @@ -838,7 +839,7 @@ describe('defaults', () => { gasLimit: '0x5208', maxFeePerGas: '0x32', }, - eth_2, + eth, ); expect(maxFeePerGasOverride).toBe('0x2'); const maxPriorityFeePerGasOverride = getTransactionType( @@ -853,7 +854,7 @@ describe('defaults', () => { gasLimit: '0x5208', maxPriorityFeePerGas: '0x32', }, - eth_2, + eth, ); expect(maxPriorityFeePerGasOverride).toBe('0x2'); const hardforkOverride = getTransactionType( @@ -868,7 +869,7 @@ describe('defaults', () => { gasLimit: '0x5208', hardfork: 'london', }, - eth_2, + eth, ); expect(hardforkOverride).toBe('0x2'); const commonOverride = getTransactionType( @@ -886,7 +887,7 @@ describe('defaults', () => { hardfork: 'london', }, }, - eth_2, + eth, ); expect(commonOverride).toBe('0x2'); @@ -910,7 +911,7 @@ describe('defaults', () => { }, ], }, - eth_2, + eth, ); expect(accessListOverride).toBe('0x1'); @@ -926,7 +927,7 @@ describe('defaults', () => { gasLimit: '0x5208', hardfork: 'berlin', }, - eth_2, + eth, ); expect(hardforkBerlinOverride).toBe('0x0'); @@ -945,10 +946,10 @@ describe('defaults', () => { hardfork: 'berlin', }, }, - eth_2, + eth, ); expect(commonBerlinOverride).toBe('0x0'); - await closeOpenConnection(eth_2); + await closeOpenConnection(eth); }); it('defaultMaxPriorityFeePerGas', async () => { // default diff --git a/scripts/system_tests_utils.ts b/scripts/system_tests_utils.ts index 1bee803dc5d..14dd4279fd3 100644 --- a/scripts/system_tests_utils.ts +++ b/scripts/system_tests_utils.ts @@ -481,9 +481,9 @@ export const sendFewSampleTxs = async (cnt = 1) => { const web3 = new Web3(DEFAULT_SYSTEM_PROVIDER); const fromAcc = await createLocalAccount(web3); const toAcc = createAccount(); - const res : TransactionReceipt[]= []; + const res: TransactionReceipt[]= []; for (let i = 0; i < cnt; i += 1) { - + // eslint-disable-next-line no-await-in-loop const receipt = await web3.eth.sendTransaction({ to: toAcc.address, value: '0x1', From cba43eed9b0f258b0dcd147bcff87ae52207a0cb Mon Sep 17 00:00:00 2001 From: jdevcs Date: Tue, 23 Jul 2024 13:39:49 +0200 Subject: [PATCH 08/20] tests update --- packages/web3-eth/test/integration/defaults.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/web3-eth/test/integration/defaults.test.ts b/packages/web3-eth/test/integration/defaults.test.ts index c8569d08705..c797ed75e46 100644 --- a/packages/web3-eth/test/integration/defaults.test.ts +++ b/packages/web3-eth/test/integration/defaults.test.ts @@ -331,7 +331,7 @@ describe('defaults', () => { it('transactionConfirmationBlocks implementation', async () => { const tempAcc2 = await createTempAccount(); - const waitConfirmations = 2; + const waitConfirmations = 1; const eth = new Web3Eth(getSystemTestProvider()); eth.setConfig({ transactionConfirmationBlocks: waitConfirmations }); From d06aaf8039bb5aa455c1da316aa1dba2d64c3ddf Mon Sep 17 00:00:00 2001 From: jdevcs Date: Tue, 23 Jul 2024 14:11:44 +0200 Subject: [PATCH 09/20] timeout chenged for contract tests --- packages/web3-eth-contract/test/integration/setup.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/web3-eth-contract/test/integration/setup.js b/packages/web3-eth-contract/test/integration/setup.js index 6170e9ca349..26ba888e0cf 100644 --- a/packages/web3-eth-contract/test/integration/setup.js +++ b/packages/web3-eth-contract/test/integration/setup.js @@ -19,6 +19,6 @@ along with web3.js. If not, see . // eslint-disable-next-line @typescript-eslint/no-require-imports require('../config/setup'); -const jestTimeout = String(process.env.WEB3_SYSTEM_TEST_PROVIDER).includes('ipc') ? 35000 : 15000; +const jestTimeout = String(process.env.WEB3_SYSTEM_TEST_PROVIDER).includes('ipc') ? 35000 : 25000; jest.setTimeout(jestTimeout); From 358cc2cc57b67345a74bd8dbb815a9c0cd6ee5e7 Mon Sep 17 00:00:00 2001 From: jdevcs Date: Wed, 24 Jul 2024 12:30:26 +0200 Subject: [PATCH 10/20] DEFAULT_SYSTEM_PROVIDER revert --- scripts/system_tests_utils.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/scripts/system_tests_utils.ts b/scripts/system_tests_utils.ts index 14dd4279fd3..6baf53ee19f 100644 --- a/scripts/system_tests_utils.ts +++ b/scripts/system_tests_utils.ts @@ -73,7 +73,7 @@ export const getEnvVar = (name: string): string | undefined => // eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access global.Cypress ? Cypress.env(name) : process.env[name]; -export const DEFAULT_SYSTEM_PROVIDER = getEnvVar('WEB3_SYSTEM_TEST_PROVIDER') ?? ""; +export const DEFAULT_SYSTEM_PROVIDER = 'http://127.0.0.1:8545'; export const DEFAULT_SYSTEM_ENGINE = 'node'; export const BACKEND = { GETH: 'geth', @@ -84,16 +84,13 @@ export const BACKEND = { }; export const getSystemTestProviderUrl = (): string => - getEnvVar('WEB3_SYSTEM_TEST_PROVIDER') ?? ""; + getEnvVar('WEB3_SYSTEM_TEST_PROVIDER') ?? DEFAULT_SYSTEM_PROVIDER; export const getSystemTestProvider = (): | string | SupportedProviders => { const url = getSystemTestProviderUrl(); - if(url === "") - throw new Error("undefined URL in env var WEB3_SYSTEM_TEST_PROVIDER"); - if (url.includes('ipc')) { return new IpcProvider(url); } From 0b3a8fb5473caf67c60e6d1b52954d4645aaa861 Mon Sep 17 00:00:00 2001 From: jdevcs Date: Wed, 24 Jul 2024 13:26:41 +0200 Subject: [PATCH 11/20] cypress timout change --- templates/cypress.config.js | 1 + 1 file changed, 1 insertion(+) diff --git a/templates/cypress.config.js b/templates/cypress.config.js index 95874902d93..977e51c90db 100644 --- a/templates/cypress.config.js +++ b/templates/cypress.config.js @@ -25,6 +25,7 @@ const config = { }, specPattern: 'test/integration/**/**/*.test.ts', excludeSpecPattern: ['**/contract_defaults_extra.test.ts'], + defaultCommandTimeout: 120000 }, }; From 8f41c29ac723bcaa250756fdaddd907f768ff389 Mon Sep 17 00:00:00 2001 From: jdevcs Date: Wed, 24 Jul 2024 15:37:35 +0200 Subject: [PATCH 12/20] reusing waitForCondition --- .../test/integration/defaults.test.ts | 29 ++++++------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/packages/web3-eth/test/integration/defaults.test.ts b/packages/web3-eth/test/integration/defaults.test.ts index c797ed75e46..8836fb0f16e 100644 --- a/packages/web3-eth/test/integration/defaults.test.ts +++ b/packages/web3-eth/test/integration/defaults.test.ts @@ -41,6 +41,7 @@ import { getSystemTestProvider, isIpc, sendFewSampleTxs, + waitForCondition } from '../fixtures/system_test_utils'; import { @@ -363,15 +364,11 @@ describe('defaults', () => { expect(confirmationCount).toBe(waitConfirmations); - await new Promise((resolve) => { - const timeout = setTimeout(() => { - if (confirmationCount >= waitConfirmations) { - clearTimeout(timeout); - resolve(); - } - }, 5000); - }); - await closeOpenConnection(eth); + await waitForCondition( + () => confirmationCount >= waitConfirmations, + async () => await closeOpenConnection(eth)), + 10, + 5000 }); it('transactionPollingInterval and transactionPollingTimeout', async () => { @@ -610,17 +607,9 @@ describe('defaults', () => { const status = await confirmationPromise; expect(status).toBe(BigInt(1)); - await new Promise((resolve) => { - // eslint-disable-next-line @typescript-eslint/no-misused-promises - const timeout = setTimeout(async () => { - if (confirmationCount >= 2) { - clearTimeout(timeout); - resolve(); - } - }, 8000) - }); - - await closeOpenConnection(tempEth); + await waitForCondition( + () => confirmationCount >= 2, + async () => await closeOpenConnection(tempEth)); }); it('maxListenersWarningThreshold test default config', () => { From e804bedae394388f7aded03ff36527d56cc7ec5f Mon Sep 17 00:00:00 2001 From: jdevcs Date: Wed, 24 Jul 2024 15:38:06 +0200 Subject: [PATCH 13/20] revert tests fix --- .../web3-eth/test/integration/get_revert_reason.test.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/web3-eth/test/integration/get_revert_reason.test.ts b/packages/web3-eth/test/integration/get_revert_reason.test.ts index 5318b06400c..4180742a21b 100644 --- a/packages/web3-eth/test/integration/get_revert_reason.test.ts +++ b/packages/web3-eth/test/integration/get_revert_reason.test.ts @@ -26,7 +26,8 @@ import { createTempAccount, getSystemTestBackend, getSystemTestProvider, - BACKEND + BACKEND, + closeOpenConnection } from '../fixtures/system_test_utils'; describe('Web3Eth.getRevertReason', () => { @@ -49,6 +50,10 @@ describe('Web3Eth.getRevertReason', () => { .contractAddress as Address; }); + afterAll(async () => { + await closeOpenConnection(web3Eth); + }); + it('should return reason for a contract call', async () => { const transaction: TransactionCall = { from: tempAccount.address, From 69cbe9759d55c4ac466d3d4f367545386eafa4de Mon Sep 17 00:00:00 2001 From: jdevcs Date: Wed, 24 Jul 2024 15:39:32 +0200 Subject: [PATCH 14/20] watch subscription transaction fix --- .../test/integration/watch_transaction.test.ts | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/packages/web3-eth/test/integration/watch_transaction.test.ts b/packages/web3-eth/test/integration/watch_transaction.test.ts index 4aa607c8fdd..cf575277221 100644 --- a/packages/web3-eth/test/integration/watch_transaction.test.ts +++ b/packages/web3-eth/test/integration/watch_transaction.test.ts @@ -25,11 +25,12 @@ import { describeIf, closeOpenConnection, isSocket, - waitForOpenConnection, + //waitForOpenConnection, createLocalAccount, isIpc, sendFewSampleTxs, createAccount, + waitForCondition, } from '../fixtures/system_test_utils'; const waitConfirmations = 2; @@ -46,7 +47,7 @@ describeIf(isSocket)('watch subscription transaction', () => { web3 = new Web3(clientUrl); account1 = await createLocalAccount(web3); account2 = createAccount(); - await waitForOpenConnection(web3.eth); + //await waitForOpenConnection(web3.eth); }); describe('wait for confirmation subscription', () => { it('subscription to heads', async () => { @@ -83,10 +84,18 @@ describeIf(isSocket)('watch subscription transaction', () => { } }); }); + await receiptPromise; await sendFewSampleTxs(isIpc ? 2 * waitConfirmations : waitConfirmations); - await confirmationPromise; - await closeOpenConnection(web3.eth); + + const resourcePromise = waitForCondition( + () => shouldBe >= waitConfirmations, + async () => { + sentTx.removeAllListeners(); + await closeOpenConnection(web3);} + ); + + await Promise.all([confirmationPromise,resourcePromise]); }); }); }); From ff179bda8e6d0b2bc3f24cf2dbac7641bdea1b2d Mon Sep 17 00:00:00 2001 From: jdevcs Date: Wed, 24 Jul 2024 15:39:48 +0200 Subject: [PATCH 15/20] waitForCondition --- scripts/system_tests_utils.ts | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/scripts/system_tests_utils.ts b/scripts/system_tests_utils.ts index 6baf53ee19f..69e8d5dff79 100644 --- a/scripts/system_tests_utils.ts +++ b/scripts/system_tests_utils.ts @@ -514,3 +514,28 @@ export const mapFormatToType: { [key: string]: string } = { [FMT_BYTES.HEX]: 'string', [FMT_BYTES.UINT8ARRAY]: 'object', }; + +export const waitForCondition = async ( + conditionFunc : () => boolean, + logicFunc: () => Promise | void, + maxIterations: number = 10, // 10 times + duration: number = 8000, // check after each 8 seconds +): Promise => { + return new Promise((resolve) => { + let iterations = 0; + const interval = setInterval(async () => { + if (iterations>0 && conditionFunc()) { // wait duration before first check + clearInterval(interval); + await logicFunc(); + resolve(); + } else { + iterations++; + if (iterations >= maxIterations) { + clearInterval(interval); + await logicFunc(); + throw new Error('Condition not met after 10 iterations.'); + } + } + }, duration); + }); +} \ No newline at end of file From b80fc947ec976ca638c2917d9ba53af518f69d7d Mon Sep 17 00:00:00 2001 From: jdevcs Date: Thu, 25 Jul 2024 13:48:25 +0200 Subject: [PATCH 16/20] TransactionBlockTimeoutError tests updates --- .../defaults.transactionBlockTimeout.test.ts | 115 ++++++++++-------- 1 file changed, 61 insertions(+), 54 deletions(-) diff --git a/packages/web3-eth/test/integration/defaults.transactionBlockTimeout.test.ts b/packages/web3-eth/test/integration/defaults.transactionBlockTimeout.test.ts index fe679bb0dbe..f36fc07ffe4 100644 --- a/packages/web3-eth/test/integration/defaults.transactionBlockTimeout.test.ts +++ b/packages/web3-eth/test/integration/defaults.transactionBlockTimeout.test.ts @@ -34,6 +34,7 @@ import { getSystemTestBackend, describeIf, BACKEND + } from '../fixtures/system_test_utils'; const MAX_32_SIGNED_INTEGER = 2147483647; @@ -45,28 +46,72 @@ describeIf(getSystemTestBackend() !== BACKEND.HARDHAT)('defaults', () => { let clientUrl: string | SupportedProviders; let account1: Web3Account; let account2: Web3Account; + let transactionBlockTimeout: number, + transactionSendTimeout: number, + transactionPollingTimeout: number, + blockHeaderTimeout: number; beforeEach(() => { clientUrl = getSystemTestProvider(); web3 = new Web3(clientUrl); + transactionBlockTimeout = web3.eth.transactionBlockTimeout; + transactionSendTimeout = web3.eth.transactionSendTimeout; + transactionPollingTimeout = web3.eth.transactionPollingTimeout; + blockHeaderTimeout = web3.eth.blockHeaderTimeout; + }); + + afterEach(async () => { + web3.eth.transactionBlockTimeout = transactionBlockTimeout; + web3.eth.transactionSendTimeout = transactionSendTimeout ; + web3.eth.transactionPollingTimeout = transactionPollingTimeout; + web3.eth.blockHeaderTimeout = blockHeaderTimeout; + await closeOpenConnection(web3); + }); + + it('should fail if transaction was not mined within `transactionBlockTimeout` blocks', async () => { + account1 = await createLocalAccount(web3); + account2 = await createLocalAccount(web3); + + const sentTx: Web3PromiEvent< + TransactionReceipt, + SendTransactionEvents + > = web3.eth.sendTransaction({ + from: account1.address, + to: account2.address, + gas, + value: '0x1', + // Give a high nonce so the transaction stuck forever. + // However, make this random to be able to run the test many times without receiving an error that indicate submitting the same transaction twice. + nonce: Number.MAX_SAFE_INTEGER, + }); + + // Some providers (mostly used for development) will make blocks only when there are new transactions + // So, send 2 transactions, one after another, because in this test `transactionBlockTimeout = 2`. + // eslint-disable-next-line no-void + await sendFewSampleTxs(2); + // Increase other timeouts so only `transactionBlockTimeout` would be reached web3.eth.transactionSendTimeout = MAX_32_SIGNED_INTEGER; web3.eth.transactionPollingTimeout = MAX_32_SIGNED_INTEGER; web3.eth.blockHeaderTimeout = MAX_32_SIGNED_INTEGER / 1000; - }); + web3.eth.transactionBlockTimeout = 2; + + await expect(sentTx).rejects.toThrow(/was not mined within [0-9]+ blocks/); + + await expect(sentTx).rejects.toThrow(TransactionBlockTimeoutError); - afterEach(async () => { - web3.eth.transactionBlockTimeout = 50; - await closeOpenConnection(web3.eth); }); - describe('defaults', () => { - it('should fail if transaction was not mined within `transactionBlockTimeout` blocks', async () => { + // The code of this test case is identical to the pervious one except for `eth.enableExperimentalFeatures = true` + // TODO: And this test case will be removed once https://github.com/web3/web3.js/issues/5521 is implemented. + itIf(isSocket)( + 'should fail if transaction was not mined within `transactionBlockTimeout` blocks - when subscription is used', + async () => { account1 = await createLocalAccount(web3); account2 = await createLocalAccount(web3); - // Setting a high `nonce` when sending a transaction, to cause the RPC call to stuck at the Node + // Setting a high `nonce` when sending a transaction, to cause the RPC call to stuck at the Node const sentTx: Web3PromiEvent< TransactionReceipt, SendTransactionEvents @@ -75,6 +120,7 @@ describeIf(getSystemTestBackend() !== BACKEND.HARDHAT)('defaults', () => { to: account2.address, gas, value: '0x1', + type: '0x1', // Give a high nonce so the transaction stuck forever. // However, make this random to be able to run the test many times without receiving an error that indicate submitting the same transaction twice. nonce: Number.MAX_SAFE_INTEGER, @@ -82,58 +128,19 @@ describeIf(getSystemTestBackend() !== BACKEND.HARDHAT)('defaults', () => { // Some providers (mostly used for development) will make blocks only when there are new transactions // So, send 2 transactions, one after another, because in this test `transactionBlockTimeout = 2`. - // eslint-disable-next-line no-void + // eslint-disable-next-line no-void, @typescript-eslint/no-unsafe-call await sendFewSampleTxs(2); - + web3.eth.transactionBlockTimeout = 2; + // using subscription to get the new blocks and fire `TransactionBlockTimeoutError` is currently supported only + // with `enableExperimentalFeatures.useSubscriptionWhenCheckingBlockTimeout` equal true. + web3.eth.enableExperimentalFeatures.useSubscriptionWhenCheckingBlockTimeout = true; + await expect(sentTx).rejects.toThrow(/was not mined within [0-9]+ blocks/); await expect(sentTx).rejects.toThrow(TransactionBlockTimeoutError); - await closeOpenConnection(web3.eth); - }); - - // The code of this test case is identical to the pervious one except for `eth.enableExperimentalFeatures = true` - // TODO: And this test case will be removed once https://github.com/web3/web3.js/issues/5521 is implemented. - itIf(isSocket)( - 'should fail if transaction was not mined within `transactionBlockTimeout` blocks - when subscription is used', - async () => { - account1 = await createLocalAccount(web3); - account2 = await createLocalAccount(web3); - await waitForOpenConnection(web3.eth); - // using subscription to get the new blocks and fire `TransactionBlockTimeoutError` is currently supported only - // with `enableExperimentalFeatures.useSubscriptionWhenCheckingBlockTimeout` equal true. - web3.eth.enableExperimentalFeatures.useSubscriptionWhenCheckingBlockTimeout = true; - - // Setting a high `nonce` when sending a transaction, to cause the RPC call to stuck at the Node - const sentTx: Web3PromiEvent< - TransactionReceipt, - SendTransactionEvents - > = web3.eth.sendTransaction({ - from: account1.address, - to: account2.address, - gas, - value: '0x1', - type: '0x1', - // Give a high nonce so the transaction stuck forever. - // However, make this random to be able to run the test many times without receiving an error that indicate submitting the same transaction twice. - nonce: Number.MAX_SAFE_INTEGER, - }); - - // Some providers (mostly used for development) will make blocks only when there are new transactions - // So, send 2 transactions, one after another, because in this test `transactionBlockTimeout = 2`. - // eslint-disable-next-line no-void, @typescript-eslint/no-unsafe-call - void sendFewSampleTxs(2); - - web3.eth.transactionBlockTimeout = 2; - - await expect(sentTx).rejects.toThrow(/was not mined within [0-9]+ blocks/); - - await expect(sentTx).rejects.toThrow(TransactionBlockTimeoutError); - - await closeOpenConnection(web3.eth); - }, - ); - }); + }, + ); }); From 9cdd968217d52cd08ff1dd1f1371b595d2c9b7d2 Mon Sep 17 00:00:00 2001 From: jdevcs Date: Thu, 25 Jul 2024 14:34:39 +0200 Subject: [PATCH 17/20] updates --- .../test/integration/defaults.test.ts | 6 ++-- .../defaults.transactionBlockTimeout.test.ts | 11 +++--- .../integration/watch_transaction.test.ts | 2 -- scripts/system_tests_utils.ts | 36 ++++++++++++------- 4 files changed, 31 insertions(+), 24 deletions(-) diff --git a/packages/web3-eth/test/integration/defaults.test.ts b/packages/web3-eth/test/integration/defaults.test.ts index 8836fb0f16e..351152158b4 100644 --- a/packages/web3-eth/test/integration/defaults.test.ts +++ b/packages/web3-eth/test/integration/defaults.test.ts @@ -366,9 +366,9 @@ describe('defaults', () => { await waitForCondition( () => confirmationCount >= waitConfirmations, - async () => await closeOpenConnection(eth)), + async () => { await closeOpenConnection(eth)}, 10, - 5000 + 5000); }); it('transactionPollingInterval and transactionPollingTimeout', async () => { @@ -609,7 +609,7 @@ describe('defaults', () => { await waitForCondition( () => confirmationCount >= 2, - async () => await closeOpenConnection(tempEth)); + async () => { await closeOpenConnection(tempEth)}); }); it('maxListenersWarningThreshold test default config', () => { diff --git a/packages/web3-eth/test/integration/defaults.transactionBlockTimeout.test.ts b/packages/web3-eth/test/integration/defaults.transactionBlockTimeout.test.ts index f36fc07ffe4..ba7fd5f15e0 100644 --- a/packages/web3-eth/test/integration/defaults.transactionBlockTimeout.test.ts +++ b/packages/web3-eth/test/integration/defaults.transactionBlockTimeout.test.ts @@ -28,7 +28,6 @@ import { getSystemTestProvider, isSocket, itIf, - waitForOpenConnection, createLocalAccount, sendFewSampleTxs, getSystemTestBackend, @@ -46,10 +45,10 @@ describeIf(getSystemTestBackend() !== BACKEND.HARDHAT)('defaults', () => { let clientUrl: string | SupportedProviders; let account1: Web3Account; let account2: Web3Account; - let transactionBlockTimeout: number, - transactionSendTimeout: number, - transactionPollingTimeout: number, - blockHeaderTimeout: number; + let transactionBlockTimeout: number; + let transactionSendTimeout: number; + let transactionPollingTimeout: number; + let blockHeaderTimeout: number; beforeEach(() => { clientUrl = getSystemTestProvider(); @@ -69,7 +68,7 @@ describeIf(getSystemTestBackend() !== BACKEND.HARDHAT)('defaults', () => { await closeOpenConnection(web3); }); - it('should fail if transaction was not mined within `transactionBlockTimeout` blocks', async () => { + test('should fail if transaction was not mined within `transactionBlockTimeout` blocks', async () => { account1 = await createLocalAccount(web3); account2 = await createLocalAccount(web3); diff --git a/packages/web3-eth/test/integration/watch_transaction.test.ts b/packages/web3-eth/test/integration/watch_transaction.test.ts index cf575277221..733b64a089b 100644 --- a/packages/web3-eth/test/integration/watch_transaction.test.ts +++ b/packages/web3-eth/test/integration/watch_transaction.test.ts @@ -25,7 +25,6 @@ import { describeIf, closeOpenConnection, isSocket, - //waitForOpenConnection, createLocalAccount, isIpc, sendFewSampleTxs, @@ -47,7 +46,6 @@ describeIf(isSocket)('watch subscription transaction', () => { web3 = new Web3(clientUrl); account1 = await createLocalAccount(web3); account2 = createAccount(); - //await waitForOpenConnection(web3.eth); }); describe('wait for confirmation subscription', () => { it('subscription to heads', async () => { diff --git a/scripts/system_tests_utils.ts b/scripts/system_tests_utils.ts index 69e8d5dff79..11f9bcef717 100644 --- a/scripts/system_tests_utils.ts +++ b/scripts/system_tests_utils.ts @@ -166,6 +166,10 @@ export const closeOpenConnection = async (web3Context: Web3Context) => { (web3Context.provider as unknown as Web3BaseProvider).reset(); (web3Context.provider as unknown as Web3BaseProvider).disconnect(); + + await new Promise(resolve => { + setTimeout(resolve, 1000); + }); } }; @@ -516,26 +520,32 @@ export const mapFormatToType: { [key: string]: string } = { }; export const waitForCondition = async ( - conditionFunc : () => boolean, + conditionFunc: () => boolean, logicFunc: () => Promise | void, - maxIterations: number = 10, // 10 times - duration: number = 8000, // check after each 8 seconds + maxIterations = 10, // 10 times + duration = 8000, // check after each 8 seconds ): Promise => { - return new Promise((resolve) => { + return new Promise((resolve, reject) => { let iterations = 0; + // eslint-disable-next-line @typescript-eslint/no-misused-promises const interval = setInterval(async () => { - if (iterations>0 && conditionFunc()) { // wait duration before first check - clearInterval(interval); - await logicFunc(); - resolve(); - } else { - iterations++; - if (iterations >= maxIterations) { + try { + if (iterations > 0 && conditionFunc()) { // wait duration before first check clearInterval(interval); await logicFunc(); - throw new Error('Condition not met after 10 iterations.'); + resolve(); + } else { + iterations += 1; + if (iterations >= maxIterations) { + clearInterval(interval); + await logicFunc(); + reject(new Error('Condition not met after 10 iterations.')); + } } + } catch (error) { + clearInterval(interval); + reject(error); } }, duration); }); -} \ No newline at end of file +}; \ No newline at end of file From 09c74e2fe04fed6ef2e197b77502d2f623f94843 Mon Sep 17 00:00:00 2001 From: jdevcs Date: Thu, 25 Jul 2024 18:39:48 +0200 Subject: [PATCH 18/20] update --- packages/web3-core/src/web3_config.ts | 4 ++-- packages/web3-eth-contract/test/unit/contract.test.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/web3-core/src/web3_config.ts b/packages/web3-core/src/web3_config.ts index 7e96c3de3d7..03e478e9ecd 100644 --- a/packages/web3-core/src/web3_config.ts +++ b/packages/web3-core/src/web3_config.ts @@ -115,9 +115,9 @@ export abstract class Web3Config for (const key of keys) { this._triggerConfigChange(key, options[key]); - if(!isNullish(options[key] && + if(!isNullish(options[key]) && typeof options[key] === 'number' && - key === 'maxListenersWarningThreshold' as keyof Web3ConfigOptions) ) + key === 'maxListenersWarningThreshold' ) { // additionally set in event emitter this.setMaxListenerWarningThreshold(Number(options[key])); diff --git a/packages/web3-eth-contract/test/unit/contract.test.ts b/packages/web3-eth-contract/test/unit/contract.test.ts index c032f08e367..44d6b0c4e13 100644 --- a/packages/web3-eth-contract/test/unit/contract.test.ts +++ b/packages/web3-eth-contract/test/unit/contract.test.ts @@ -815,9 +815,9 @@ describe('Contract', () => { '0x00000000219ab540356cBB839Cbe05303d7705Fa', { gas: '0x97254' }, ); - + contract.maxListenersWarningThreshold = 1000; + const clonnedContract = contract.clone(); - expect(stringify(contract)).toStrictEqual(stringify(clonnedContract)); contract.options.jsonInterface = GreeterAbi; From 52e6850e14af9aa7e2c0f90b5f08cab71970fe22 Mon Sep 17 00:00:00 2001 From: jdevcs Date: Thu, 25 Jul 2024 18:47:57 +0200 Subject: [PATCH 19/20] unit test fix --- packages/web3-eth-contract/test/unit/contract.test.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/web3-eth-contract/test/unit/contract.test.ts b/packages/web3-eth-contract/test/unit/contract.test.ts index 44d6b0c4e13..6ae5f055904 100644 --- a/packages/web3-eth-contract/test/unit/contract.test.ts +++ b/packages/web3-eth-contract/test/unit/contract.test.ts @@ -825,7 +825,8 @@ describe('Contract', () => { it('should clone new contract', () => { const contract = new Contract(sampleStorageContractABI); - + contract.maxListenersWarningThreshold = 1000; + const clonnedContract = contract.clone(); expect(stringify(contract)).toStrictEqual(stringify(clonnedContract)); }); From 1edaf85042b36e106c9900261633b0711b03ef09 Mon Sep 17 00:00:00 2001 From: jdevcs Date: Fri, 2 Aug 2024 16:24:43 +0200 Subject: [PATCH 20/20] changelog update --- packages/web3-core/CHANGELOG.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/web3-core/CHANGELOG.md b/packages/web3-core/CHANGELOG.md index 67ef64ece5b..8f61b018479 100644 --- a/packages/web3-core/CHANGELOG.md +++ b/packages/web3-core/CHANGELOG.md @@ -227,4 +227,8 @@ Documentation: - Now when existing packages are added in web3, will be avalible for plugins via context. (#7088) -## [Unreleased] \ No newline at end of file +## [Unreleased] + +### Fixed + +- `setConfig()` fix for `setMaxListenerWarningThreshold` fix (#5079)