From 5afcb5411d35b9c3efb1f323336b7c2661328d28 Mon Sep 17 00:00:00 2001 From: Oleksii Kosynskyi Date: Mon, 4 Dec 2023 22:13:33 -0500 Subject: [PATCH] cover chunker and converters (#6638) --- .../web3-utils/src/chunk_response_parser.ts | 4 +- .../web3-utils/test/fixtures/converters.ts | 25 +++ packages/web3-utils/test/fixtures/hugeData.ts | 29 +++ .../test/unit/chunk_response_parser.test.ts | 74 +++++++ .../web3-utils/test/unit/converters.test.ts | 17 ++ packages/web3/test/e2e_manual/data.test.ts | 70 ------- packages/web3/test/e2e_manual/jest.config.js | 32 --- .../web3/test/e2e_manual/requests.test.ts | 147 -------------- packages/web3/test/e2e_manual/setup.js | 24 --- .../web3/test/e2e_manual/validation.test.ts | 183 ------------------ 10 files changed, 148 insertions(+), 457 deletions(-) create mode 100644 packages/web3-utils/test/fixtures/hugeData.ts create mode 100644 packages/web3-utils/test/unit/chunk_response_parser.test.ts delete mode 100644 packages/web3/test/e2e_manual/data.test.ts delete mode 100644 packages/web3/test/e2e_manual/jest.config.js delete mode 100644 packages/web3/test/e2e_manual/requests.test.ts delete mode 100644 packages/web3/test/e2e_manual/setup.js delete mode 100644 packages/web3/test/e2e_manual/validation.test.ts diff --git a/packages/web3-utils/src/chunk_response_parser.ts b/packages/web3-utils/src/chunk_response_parser.ts index d9b37c7b6ee..629323415cb 100644 --- a/packages/web3-utils/src/chunk_response_parser.ts +++ b/packages/web3-utils/src/chunk_response_parser.ts @@ -25,10 +25,12 @@ export class ChunkResponseParser { private _clearQueues: (() => void) | undefined; private readonly eventEmitter: EventEmitter; private readonly autoReconnect: boolean; + private readonly chunkTimout: number; public constructor(eventEmitter: EventEmitter, autoReconnect: boolean) { this.eventEmitter = eventEmitter; this.autoReconnect = autoReconnect; + this.chunkTimout = 1000 * 15; } private clearQueues(): void { if (typeof this._clearQueues === 'function') { @@ -81,7 +83,7 @@ export class ChunkResponseParser { error: { code: 2, message: 'Chunk timeout' }, }), ); - }, 1000 * 15); + }, this.chunkTimout); return; } diff --git a/packages/web3-utils/test/fixtures/converters.ts b/packages/web3-utils/test/fixtures/converters.ts index 42d992ec34a..464c196b32d 100644 --- a/packages/web3-utils/test/fixtures/converters.ts +++ b/packages/web3-utils/test/fixtures/converters.ts @@ -326,6 +326,8 @@ export const bytesToUint8ArrayValidData: [Bytes, Uint8Array][] = [ ['0X12c6', new Uint8Array([18, 198])], ['0X01', new Uint8Array([1])], ['0X00', new Uint8Array([0])], + ['0X00', new Uint8Array([0])], + ['0x1234', new Uint8Array([18, 52])], ['0x1234', new Uint8Array([18, 52])], [new Uint8Array(hexToBytes('0c12')), new Uint8Array(hexToBytes('0c12'))], ]; @@ -335,6 +337,7 @@ export const toBigIntValidData: [any, bigint][] = [ [24, BigInt(24)], ['123', BigInt(123)], ['0x04', BigInt(4)], + ['-0x1', -BigInt(1)], ]; export const toBigIntInvalidData: [any, string][] = [ @@ -342,3 +345,25 @@ export const toBigIntInvalidData: [any, string][] = [ ['wwwww', ' Error: can not parse as number data'], ['zzzzee0xiiuu', ' Error: can not parse as number data'], ]; + +export const toBoolValidData: [boolean | string | number | unknown, boolean][] = [ + [true, true], + [false, false], + [0, false], + [1, true], + [BigInt(1), true], + [BigInt(0), false], + ['true', true], + ['false', false], + ['1', true], + ['0', false], + ['0x0', false], + ['0x1', true], +]; + +export const toBoolInvalidData: [boolean | string | number | unknown, string][] = [ + [100, 'not a valid boolean'], + [BigInt(100), 'not a valid boolean'], + ['100', 'not a valid boolean'], + [{}, 'not a valid boolean'], +]; diff --git a/packages/web3-utils/test/fixtures/hugeData.ts b/packages/web3-utils/test/fixtures/hugeData.ts new file mode 100644 index 00000000000..0470ae6c807 --- /dev/null +++ b/packages/web3-utils/test/fixtures/hugeData.ts @@ -0,0 +1,29 @@ +/* +This file is part of web3.js. + +web3.js is free software: you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +web3.js is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with web3.js. If not, see . +*/ +export const hugeData = { + data: '{"jsonrpc":"2.0","id":"96aa3f13-077c-4c82-a64a-64b8626f8192","result":"0x1414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141"}\n{"jsonrpc":"2.0","id":"1b10c675-5eac-4c80-b300-36085d63d5f4","result":"0xd5"}\n{"jsonrpc":"2.0","id":"96aa3f13-077c-4c82-a64a-64b8626f819c","result":"0xd5"}\n{"jsonrpc":"2.0","id":"bba428bf-6726-425a-8cbc-2ed7c9448ee7","result":{"blockHash":"0xccfea468ce813e63d59be075dd71de76d4236fa3292dc3b4cc5e6f15c27c3ad1","blockNumber":"0xd4","contractAddress":null,"cumulativeGasUsed":"0xad8c516","effectiveGasPrice":"0x9502f907","from":"0x12b1d9d74d73b1c3a245b19c1c5501c653af1af9","gasUsed":"0x22b5a9e","logs":[],"logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","status":"0x1","to":"0xf362b2285d41dc806fd61b92f7c192006b707f42","transactionHash":"0x01ab900543af6d9dbb27b21cf62339c07bfd6d1acdc01f66c932ed8f3c731412","transactionIndex":"0x4","type":"0x2"}}\n', + result: [ + JSON.parse( + '{"jsonrpc":"2.0","id":"96aa3f13-077c-4c82-a64a-64b8626f8192","result":"0x1414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141"}', + ), + JSON.parse('{"jsonrpc":"2.0","id":"1b10c675-5eac-4c80-b300-36085d63d5f4","result":"0xd5"}'), + JSON.parse('{"jsonrpc":"2.0","id":"96aa3f13-077c-4c82-a64a-64b8626f819c","result":"0xd5"}'), + JSON.parse( + '{"jsonrpc":"2.0","id":"bba428bf-6726-425a-8cbc-2ed7c9448ee7","result":{"blockHash":"0xccfea468ce813e63d59be075dd71de76d4236fa3292dc3b4cc5e6f15c27c3ad1","blockNumber":"0xd4","contractAddress":null,"cumulativeGasUsed":"0xad8c516","effectiveGasPrice":"0x9502f907","from":"0x12b1d9d74d73b1c3a245b19c1c5501c653af1af9","gasUsed":"0x22b5a9e","logs":[],"logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","status":"0x1","to":"0xf362b2285d41dc806fd61b92f7c192006b707f42","transactionHash":"0x01ab900543af6d9dbb27b21cf62339c07bfd6d1acdc01f66c932ed8f3c731412","transactionIndex":"0x4","type":"0x2"}}\n', + ), + ], +}; diff --git a/packages/web3-utils/test/unit/chunk_response_parser.test.ts b/packages/web3-utils/test/unit/chunk_response_parser.test.ts new file mode 100644 index 00000000000..1fc08c2fceb --- /dev/null +++ b/packages/web3-utils/test/unit/chunk_response_parser.test.ts @@ -0,0 +1,74 @@ +/* +This file is part of web3.js. + +web3.js is free software: you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +web3.js is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with web3.js. If not, see . +*/ + +import { EventEmitter } from 'events'; +import { InvalidResponseError } from 'web3-errors'; +import { ChunkResponseParser } from '../../src/chunk_response_parser'; +import { hugeData } from '../fixtures/hugeData'; + +jest.setTimeout(20000); +describe('chunk_response_parser', () => { + let parser: ChunkResponseParser; + let eventEmiter: EventEmitter; + beforeEach(() => { + eventEmiter = new EventEmitter(); + parser = new ChunkResponseParser(eventEmiter, false); + }); + it('clearQueue', () => { + const clearQueue = jest.fn(); + parser.onError(clearQueue); + // @ts-expect-error call private method + parser.clearQueues(); + expect(clearQueue).toHaveBeenCalled(); + }); + it('parse response', () => { + const res = parser.parseResponse(hugeData.data); + expect(res).toEqual(hugeData.result); + }); + + it('parse response with last chunk mechanism', () => { + parser.parseResponse( + '{"jsonrpc":"2.0","id":"96aa3f13-077c-4c82-a64a-64b8626f8192","result":"0x141414141', + ); + parser.parseResponse('123'); + const res = parser.parseResponse('14141"}\n'); + expect(res[0]).toEqual({ + jsonrpc: '2.0', + id: '96aa3f13-077c-4c82-a64a-64b8626f8192', + result: '0x14141414112314141', + }); + }); + + it('lastChunkTimeout error', async () => { + // @ts-expect-error set private property + parser.chunkTimout = 10; + parser.parseResponse( + '{"jsonrpc":"2.0","id":"96aa3f13-077c-4c82-a64a-64b8626f8192","result":"0x141414141', + ); + const onError = jest.fn(); + eventEmiter.on('error', onError); + // eslint-disable-next-line no-promise-executor-return + await new Promise(resolve => setTimeout(resolve, 1000)); + expect(onError).toHaveBeenCalledWith( + new InvalidResponseError({ + id: 1, + jsonrpc: '2.0', + error: { code: 2, message: 'Chunk timeout' }, + }), + ); + }); +}); diff --git a/packages/web3-utils/test/unit/converters.test.ts b/packages/web3-utils/test/unit/converters.test.ts index 88e2f4c2385..cb6fa58caa0 100644 --- a/packages/web3-utils/test/unit/converters.test.ts +++ b/packages/web3-utils/test/unit/converters.test.ts @@ -40,6 +40,7 @@ import { toChecksumAddress, bytesToUint8Array, toBigInt, + toBool, } from '../../src/converters'; import { @@ -71,6 +72,8 @@ import { toBigIntInvalidData, toCheckSumInvalidData, numberToHexstrictValidData, + toBoolValidData, + toBoolInvalidData, } from '../fixtures/converters'; describe('converters', () => { @@ -403,4 +406,18 @@ describe('converters', () => { }); }); }); + + describe('toBool', () => { + describe('valid cases', () => { + it.each(toBoolValidData)('%s', (input, output) => { + expect(toBool(input)).toEqual(output); + }); + }); + + describe('invalid cases', () => { + it.each(toBoolInvalidData)('%s', (input, output) => { + expect(() => toBool(input)).toThrow(output); + }); + }); + }); }); diff --git a/packages/web3/test/e2e_manual/data.test.ts b/packages/web3/test/e2e_manual/data.test.ts deleted file mode 100644 index eb57a6ebab8..00000000000 --- a/packages/web3/test/e2e_manual/data.test.ts +++ /dev/null @@ -1,70 +0,0 @@ -/* -This file is part of web3.js. - -web3.js is free software: you can redistribute it and/or modify -it under the terms of the GNU Lesser General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -web3.js is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU Lesser General Public License for more details. - -You should have received a copy of the GNU Lesser General Public License -along with web3.js. If not, see . -*/ - -/* eslint-disable */ -import { Web3 } from 'web3'; -import WebSocketProvider from 'web3-providers-ws'; -import accounts from '../../../../scripts/accounts.json'; -import { isWs, isIpc, getSystemTestProvider } from '../../../../scripts/system_tests_utils'; -import { BasicAbi, BasicBytecode } from '../../../../fixtures/build/Basic'; - -const DATA_AMOUNT = 50 * 1024; // 50 kB - -const sendAndGetData = async (web3: Web3, i: number) => { - const sendOptions = { from: accounts[i].address }; - const deployOptions = { - data: BasicBytecode, - arguments: [0, ''] as [number, string], - gasPrice: await web3.eth.getGasPrice(), - gas: BigInt(9000000000000), - gasLimit: BigInt(9000000000000), - type: BigInt(0), - }; - const c = new web3.eth.Contract(BasicAbi); - const contract = await c.deploy(deployOptions).send(sendOptions); - - await contract.methods - .setValues(1, 'A'.repeat(DATA_AMOUNT), true) - .send({ from: accounts[i].address }); - - await contract.methods.getStringValue().call(); -}; - -describe('huge data', () => { - let web3: Web3; - beforeAll(() => { - web3 = new Web3(getSystemTestProvider()); - }); - afterAll(() => { - if (isWs || isIpc) { - (web3.provider as unknown as WebSocketProvider).disconnect(); - } - }); - - it('send and get', async () => { - for (const a of accounts) { - const acc = web3.eth.accounts.privateKeyToAccount(a.privateKey); - web3.eth.accounts.wallet.add(acc); - } - - const prs = []; - for (let i = 0; i < 15; i++) { - prs.push(sendAndGetData(web3, i)); - } - await Promise.all(prs); - }); -}); diff --git a/packages/web3/test/e2e_manual/jest.config.js b/packages/web3/test/e2e_manual/jest.config.js deleted file mode 100644 index 7ce09e43a4b..00000000000 --- a/packages/web3/test/e2e_manual/jest.config.js +++ /dev/null @@ -1,32 +0,0 @@ -'use strict'; - -const base = require('../config/jest.config'); - -module.exports = { - ...base, - setupFilesAfterEnv: ['/test/e2e/setup.js'], - testMatch: [`/test/e2e_manual/**/*.(spec|test).(js|ts)`], - /** - * restoreMocks [boolean] - * - * Default: false - * - * Automatically restore mock state between every test. - * Equivalent to calling jest.restoreAllMocks() between each test. - * This will lead to any mocks having their fake implementations removed - * and restores their initial implementation. - */ - restoreMocks: true, - - /** - * resetModules [boolean] - * - * Default: false - * - * By default, each test file gets its own independent module registry. - * Enabling resetModules goes a step further and resets the module registry before running each individual test. - * This is useful to isolate modules for every test so that local module state doesn't conflict between tests. - * This can be done programmatically using jest.resetModules(). - */ - resetModules: true, -}; diff --git a/packages/web3/test/e2e_manual/requests.test.ts b/packages/web3/test/e2e_manual/requests.test.ts deleted file mode 100644 index b79e09fd573..00000000000 --- a/packages/web3/test/e2e_manual/requests.test.ts +++ /dev/null @@ -1,147 +0,0 @@ -/* -This file is part of web3.js. - -web3.js is free software: you can redistribute it and/or modify -it under the terms of the GNU Lesser General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -web3.js is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU Lesser General Public License for more details. - -You should have received a copy of the GNU Lesser General Public License -along with web3.js. If not, see . -*/ - -/* eslint-disable */ -import WebSocketProvider from 'web3-providers-ws'; -import { Web3Account } from 'web3-eth-accounts'; -import { Web3, Contract, Numbers, EventLog } from 'web3'; -import { BasicBytecode, BasicAbi } from '../../../../fixtures/build/Basic'; -import { isWs, isIpc, getSystemTestProvider } from '../../../../scripts/system_tests_utils'; - -const contracts: { [key: string]: Contract } = {}; - -const deployContracts = async (web3: Web3, accounts: Web3Account[]) => { - const prs = []; - for (let i = 0; i < accounts.length; i++) { - const account = accounts[i]; - const sendOptions = { from: account.address }; - const deployOptions = { - data: BasicBytecode, - arguments: [123, ''] as [number, string], - gas: BigInt(9000000000000), - gasLimit: BigInt(9000000000000), - type: BigInt(0), - }; - const c = new web3.eth.Contract(BasicAbi); - prs.push( - c - .deploy(deployOptions) - .send(sendOptions) - .then((contract: typeof c) => { - contracts[account.address] = contract; - }), - ); - } - await Promise.all(prs); -}; - -const addAccount = async ( - web3: Web3, - mainAcc: string, - address: string, - privateKey: string, - nonce: Numbers, -) => { - web3.eth.accounts.wallet.add(privateKey); - return web3.eth.sendTransaction({ - from: mainAcc, - to: address, - nonce, - gas: 1500000, - value: '1000000000000000000', - }); -}; - -const prepareAccounts = async (web3: Web3, n = 1000) => { - const prs = []; - const list = await web3.eth.personal.getAccounts(); - const mainAcc = list[0]; - const accountList: Web3Account[] = []; - const nonce = await web3.eth.getTransactionCount(mainAcc); - for (let i = 0; i < n; i++) { - const acc = web3.eth.accounts.create(); - prs.push(addAccount(web3, mainAcc, acc.address, acc.privateKey, Number(nonce) + i)); - accountList.push(acc); - } - await Promise.all(prs); - return accountList; -}; - -const sendData = async (account: Web3Account) => { - const contract = contracts[account.address]; - return contract.methods - .firesStringEvent(`String event: ${account.address}`) - .send({ from: account.address }); -}; - -const getData = async (account: Web3Account) => { - const contract = contracts[account.address]; - await contract.methods.getStringValue().call(); -}; - -const receivedEvents: { [key: string]: EventLog } = {}; -const subscribeContract = (acc: Web3Account) => { - const contract = contracts[acc.address]; - const event = contract.events.StringEvent(); - - event.on('data', res => { - if (res.returnValues.str !== `String event: ${acc.address}`) { - throw new Error('Event is not correct'); - } - receivedEvents[acc.address] = res; - }); -}; -const contractSubscriptions = (accounts: Web3Account[]) => { - for (const acc of accounts) { - subscribeContract(acc); - } -}; - -describe('huge data', () => { - let web3: Web3; - let parallelCount = 100; - let accounts: Web3Account[] = []; - beforeAll(async () => { - parallelCount = isIpc ? 5 : parallelCount; - web3 = new Web3(getSystemTestProvider()); - accounts = await prepareAccounts(web3, parallelCount); - await deployContracts(web3, accounts); - }); - afterAll(() => { - if (isWs || isIpc) { - (web3.provider as unknown as WebSocketProvider).disconnect(); - } - }); - it('send requests', async () => { - const sendPrs = []; - for (let i = 0; i < parallelCount; i++) { - sendPrs.push(sendData(accounts[i])); - } - await Promise.all(sendPrs); - // if socket subscribe to events - if (isIpc || isWs) { - contractSubscriptions(accounts); - } - }); - it('get requests', async () => { - const getPrs = []; - for (let i = 0; i < parallelCount; i++) { - getPrs.push(getData(accounts[i])); - } - await Promise.all(getPrs); - }); -}); diff --git a/packages/web3/test/e2e_manual/setup.js b/packages/web3/test/e2e_manual/setup.js deleted file mode 100644 index e9757a61e1c..00000000000 --- a/packages/web3/test/e2e_manual/setup.js +++ /dev/null @@ -1,24 +0,0 @@ -/* -This file is part of web3.js. - -web3.js is free software: you can redistribute it and/or modify -it under the terms of the GNU Lesser General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -web3.js is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU Lesser General Public License for more details. - -You should have received a copy of the GNU Lesser General Public License -along with web3.js. If not, see . -*/ - -// Have to use `require` because of Jest issue https://jestjs.io/docs/ecmascript-modules -// eslint-disable-next-line @typescript-eslint/no-require-imports -require('../config/setup'); - -const jestTimeout = 300000; - -jest.setTimeout(jestTimeout); diff --git a/packages/web3/test/e2e_manual/validation.test.ts b/packages/web3/test/e2e_manual/validation.test.ts deleted file mode 100644 index 7ef7e44ef4c..00000000000 --- a/packages/web3/test/e2e_manual/validation.test.ts +++ /dev/null @@ -1,183 +0,0 @@ -/* -This file is part of web3.js. - -web3.js is free software: you can redistribute it and/or modify -it under the terms of the GNU Lesser General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -web3.js is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU Lesser General Public License for more details. - -You should have received a copy of the GNU Lesser General Public License -along with web3.js. If not, see . -*/ - -import { Web3Validator, Json, JsonSchema, ValidationSchemaInput } from 'web3-validator'; - -const abi = [ - { indexed: true, internalType: 'address', name: 'from', type: 'address' }, - { indexed: true, internalType: 'address', name: 'to', type: 'address' }, - { indexed: false, internalType: 'uint256', name: 'value', type: 'uint256' }, -]; - -const abiJsonSchema = { - type: 'array', - items: [ - { name: 'from', format: 'address' }, - { name: 'to', format: 'address' }, - { name: 'value', format: 'uint256' }, - ], -}; - -const abiData = [ - '0xCB00CDE33a7a0Fba30C63745534F1f7Ae607076b', - '0xCB00CDE33a7a0Fba30C63745534F1f7Ae607076b', - '0xCB00CDE33a7a0Fba30C63745534F1f7Ae607076b', -]; - -const simpleSchema = { - type: 'object', - required: ['blockHash', 'blockNumber', 'from', 'to', 'data'], - properties: { - blockHash: { - format: 'bytes32', - }, - blockNumber: { - format: 'uint', - }, - from: { - format: 'address', - }, - to: { - oneOf: [{ format: 'address' }, { type: 'null' }], - }, - data: { - format: 'bytes', - }, - }, -}; - -const simpleData = { - blockHash: '0x0dec0518fa672a70027b04c286582e543ab17319fbdd384fa7bc8f3d5a542c0b', - blockNumber: BigInt(2), - from: '0xCB00CDE33a7a0Fba30C63745534F1f7Ae607076b', - to: '0xCB00CDE33a7a0Fba30C63745534F1f7Ae607076b', - data: '0xafea', -} as unknown as ValidationSchemaInput; - -const createHugeSchema = ( - schema: JsonSchema, - data: Json, - n = 3, -): { schema: JsonSchema; data: Json } => { - if (n > 0) { - const { data: resultData, schema: resultSchema } = createHugeSchema( - { ...simpleSchema } as JsonSchema, - { ...simpleData } as Json, - n - 1, - ); - return { - data: { ...(data as unknown as object), simple: resultData }, - schema: { ...schema, properties: { ...schema.properties, simple: resultSchema } }, - }; - } - return { - schema, - data, - }; -}; - -const { schema: hugeSchema, data: hugeData } = createHugeSchema( - { ...simpleSchema } as JsonSchema, - { ...simpleData } as Json, - 500, -); - -const { schema: hugeSchema1000, data: hugeData1000 } = createHugeSchema( - { ...simpleSchema } as JsonSchema, - { ...simpleData } as Json, - 1000, -); -describe('validator', () => { - let validator: Web3Validator; - beforeAll(() => { - validator = new Web3Validator(); - }); - - it('huge schema', () => { - let t = 0; - expect(() => { - const t1 = Number(new Date()); - validator.validateJSONSchema(hugeSchema, hugeData as object); - t = Number(new Date()) - t1; - }).not.toThrow(); - expect(t).toBeLessThan(6000); - expect(t).toBeGreaterThan(0); - }); - - it('huge schema 1000', () => { - let t = 0; - expect(() => { - const t1 = Number(new Date()); - validator.validateJSONSchema(hugeSchema1000, hugeData1000 as object); - t = Number(new Date()) - t1; - }).not.toThrow(); - expect(t).toBeLessThan(6000); - expect(t).toBeGreaterThan(0); - }); - - it('simple schema multiple times', () => { - let t = 0; - expect(() => { - const t1 = Number(new Date()); - for (let i = 0; i < 500; i += 1) { - validator.validateJSONSchema(simpleSchema, simpleData as object); - } - t = Number(new Date()) - t1; - }).not.toThrow(); - expect(t).toBeLessThan(3000); - expect(t).toBeGreaterThan(0); - }); - - it('simple schema 1000 times', () => { - let t = 0; - expect(() => { - const t1 = Number(new Date()); - for (let i = 0; i < 1000; i += 1) { - validator.validateJSONSchema(simpleSchema, simpleData as object); - } - t = Number(new Date()) - t1; - }).not.toThrow(); - expect(t).toBeLessThan(4000); - expect(t).toBeGreaterThan(0); - }); - - it('simple JSON schema 1000 times', () => { - let t = 0; - expect(() => { - const t1 = Number(new Date()); - for (let i = 0; i < 1000; i += 1) { - validator.validateJSONSchema(abiJsonSchema, abiData as object); - } - t = Number(new Date()) - t1; - }).not.toThrow(); - expect(t).toBeLessThan(4000); - expect(t).toBeGreaterThan(0); - }); - - it('simple ABI 1000 times', () => { - let t = 0; - expect(() => { - const t1 = Number(new Date()); - for (let i = 0; i < 1000; i += 1) { - validator.validate(abi, abiData); - } - t = Number(new Date()) - t1; - }).not.toThrow(); - expect(t).toBeLessThan(4000); - expect(t).toBeGreaterThan(0); - }); -});