|
| 1 | +/* |
| 2 | +This file is part of web3.js. |
| 3 | +
|
| 4 | +web3.js is free software: you can redistribute it and/or modify |
| 5 | +it under the terms of the GNU Lesser General Public License as published by |
| 6 | +the Free Software Foundation, either version 3 of the License, or |
| 7 | +(at your option) any later version. |
| 8 | +
|
| 9 | +web3.js is distributed in the hope that it will be useful, |
| 10 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +GNU Lesser General Public License for more details. |
| 13 | +
|
| 14 | +You should have received a copy of the GNU Lesser General Public License |
| 15 | +along with web3.js. If not, see <http://www.gnu.org/licenses/>. |
| 16 | +*/ |
| 17 | + |
| 18 | +/* eslint-disable */ |
| 19 | +import WebSocketProvider from 'web3-providers-ws'; |
| 20 | +import { Web3Account } from 'web3-eth-accounts'; |
| 21 | +import { Web3, Contract, Numbers, EventLog } from 'web3'; |
| 22 | +import { BasicBytecode, BasicAbi } from '../../../../fixtures/build/Basic'; |
| 23 | +import { isWs, isIpc, getSystemTestProvider } from '../../../../scripts/system_tests_utils'; |
| 24 | + |
| 25 | +const contracts: { [key: string]: Contract<typeof BasicAbi> } = {}; |
| 26 | + |
| 27 | +const deployContracts = async (web3: Web3, accounts: Web3Account[]) => { |
| 28 | + const prs = []; |
| 29 | + for (let i = 0; i < accounts.length; i++) { |
| 30 | + const account = accounts[i]; |
| 31 | + const sendOptions = { from: account.address }; |
| 32 | + const deployOptions = { |
| 33 | + data: BasicBytecode, |
| 34 | + arguments: [123, ''] as [number, string], |
| 35 | + gas: BigInt(9000000000000), |
| 36 | + gasLimit: BigInt(9000000000000), |
| 37 | + type: BigInt(0), |
| 38 | + }; |
| 39 | + const c = new web3.eth.Contract<typeof BasicAbi>(BasicAbi); |
| 40 | + prs.push( |
| 41 | + c |
| 42 | + .deploy(deployOptions) |
| 43 | + .send(sendOptions) |
| 44 | + .then((contract: typeof c) => { |
| 45 | + contracts[account.address] = contract; |
| 46 | + }), |
| 47 | + ); |
| 48 | + } |
| 49 | + await Promise.all(prs); |
| 50 | +}; |
| 51 | + |
| 52 | +const addAccount = async ( |
| 53 | + web3: Web3, |
| 54 | + mainAcc: string, |
| 55 | + address: string, |
| 56 | + privateKey: string, |
| 57 | + nonce: Numbers, |
| 58 | +) => { |
| 59 | + web3.eth.accounts.wallet.add(privateKey); |
| 60 | + return web3.eth.sendTransaction({ |
| 61 | + from: mainAcc, |
| 62 | + to: address, |
| 63 | + nonce, |
| 64 | + gas: 1500000, |
| 65 | + value: '1000000000000000000', |
| 66 | + }); |
| 67 | +}; |
| 68 | + |
| 69 | +const prepareAccounts = async (web3: Web3, n = 1000) => { |
| 70 | + const prs = []; |
| 71 | + const list = await web3.eth.personal.getAccounts(); |
| 72 | + const mainAcc = list[0]; |
| 73 | + const accountList: Web3Account[] = []; |
| 74 | + const nonce = await web3.eth.getTransactionCount(mainAcc); |
| 75 | + for (let i = 0; i < n; i++) { |
| 76 | + const acc = web3.eth.accounts.create(); |
| 77 | + prs.push(addAccount(web3, mainAcc, acc.address, acc.privateKey, Number(nonce) + i)); |
| 78 | + accountList.push(acc); |
| 79 | + } |
| 80 | + await Promise.all(prs); |
| 81 | + return accountList; |
| 82 | +}; |
| 83 | + |
| 84 | +const sendData = async (account: Web3Account) => { |
| 85 | + const contract = contracts[account.address]; |
| 86 | + return contract.methods |
| 87 | + .firesStringEvent(`String event: ${account.address}`) |
| 88 | + .send({ from: account.address }); |
| 89 | +}; |
| 90 | + |
| 91 | +const getData = async (account: Web3Account) => { |
| 92 | + const contract = contracts[account.address]; |
| 93 | + await contract.methods.getStringValue().call(); |
| 94 | +}; |
| 95 | + |
| 96 | +const receivedEvents: { [key: string]: EventLog } = {}; |
| 97 | +const subscribeContract = (acc: Web3Account) => { |
| 98 | + const contract = contracts[acc.address]; |
| 99 | + const event = contract.events.StringEvent(); |
| 100 | + |
| 101 | + event.on('data', res => { |
| 102 | + if (res.returnValues.str !== `String event: ${acc.address}`) { |
| 103 | + throw new Error('Event is not correct'); |
| 104 | + } |
| 105 | + receivedEvents[acc.address] = res; |
| 106 | + }); |
| 107 | +}; |
| 108 | +const contractSubscriptions = (accounts: Web3Account[]) => { |
| 109 | + for (const acc of accounts) { |
| 110 | + subscribeContract(acc); |
| 111 | + } |
| 112 | +}; |
| 113 | + |
| 114 | +describe('huge data', () => { |
| 115 | + let web3: Web3; |
| 116 | + let parallelCount = 100; |
| 117 | + let accounts: Web3Account[] = []; |
| 118 | + beforeAll(async () => { |
| 119 | + parallelCount = isIpc ? 5 : parallelCount; |
| 120 | + web3 = new Web3(getSystemTestProvider()); |
| 121 | + accounts = await prepareAccounts(web3, parallelCount); |
| 122 | + await deployContracts(web3, accounts); |
| 123 | + }); |
| 124 | + afterAll(() => { |
| 125 | + if (isWs || isIpc) { |
| 126 | + (web3.provider as unknown as WebSocketProvider).disconnect(); |
| 127 | + } |
| 128 | + }); |
| 129 | + it('send requests', async () => { |
| 130 | + const sendPrs = []; |
| 131 | + for (let i = 0; i < parallelCount; i++) { |
| 132 | + sendPrs.push(sendData(accounts[i])); |
| 133 | + } |
| 134 | + await Promise.all(sendPrs); |
| 135 | + // if socket subscribe to events |
| 136 | + if (isIpc || isWs) { |
| 137 | + contractSubscriptions(accounts); |
| 138 | + } |
| 139 | + }); |
| 140 | + it('get requests', async () => { |
| 141 | + const getPrs = []; |
| 142 | + for (let i = 0; i < parallelCount; i++) { |
| 143 | + getPrs.push(getData(accounts[i])); |
| 144 | + } |
| 145 | + await Promise.all(getPrs); |
| 146 | + }); |
| 147 | +}); |
0 commit comments