Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit 69e9b7b

Browse files
committed
move to TS
1 parent 2431a16 commit 69e9b7b

File tree

5 files changed

+42
-55
lines changed

5 files changed

+42
-55
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
"test:blackbox:geth:ws": "yarn pre-blackbox && yarn geth:start:background && ./scripts/verdaccio.sh startBackgroundAndPublish && lerna run test:blackbox:geth:ws --stream && yarn post-blackbox:geth",
8686
"test:blackbox:infura:http": "yarn pre-blackbox && ./scripts/verdaccio.sh startBackgroundAndPublish && lerna run test:blackbox:infura:http --stream && yarn post-blackbox",
8787
"test:blackbox:infura:ws": "yarn pre-blackbox && ./scripts/verdaccio.sh startBackgroundAndPublish && lerna run test:blackbox:infura:ws --stream && yarn post-blackbox",
88-
"test:manual:stress:data": "packages/web3/stress/large_request_data/start.sh",
88+
"test:manual:stress:data": "packages/web3/test/stress/large_request_data/start.sh",
8989
"test:manual:stress:requests": "packages/web3/test/stress/many_requests/start.sh",
9090
"test:manual:stress:validation": "npx ts-node packages/web3/test/stress/large_validator_data/index.ts",
9191
"test:manual:stress": "yarn test:manual:stress:data && yarn test:manual:stress:validation && yarn test:stress:requests",

packages/web3/test/stress/large_request_data/index.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,12 @@ along with web3.js. If not, see <http://www.gnu.org/licenses/>.
1616
*/
1717

1818
/* eslint-disable */
19-
<<<<<<<< HEAD:packages/web3/test/stress/large_request_data/index.js
20-
const { Web3 } = require('../../../lib/commonjs');
21-
const { IpcProvider } = require('../../../../web3-providers-ipc/lib/commonjs');
22-
const accounts = require('../../../../../scripts/accounts.json');
23-
const contractData = require('../../../../../fixtures/build/Basic.json');
24-
25-
========
2619
import { Web3 } from 'web3';
2720
import { IpcProvider } from 'web3-providers-ipc';
28-
import accounts from '../shared_fixtures/accounts.json';
29-
import { BasicAbi, BasicBytecode } from '../shared_fixtures/build/Basic';
21+
import accounts from '../../shared_fixtures/accounts.json';
22+
import { BasicAbi, BasicBytecode } from '../../shared_fixtures/build/Basic';
3023
import WebSocketProvider from 'web3-providers-ws';
31-
>>>>>>>> ok/5563-Stress-Tests-2---QA-Tests:packages/web3/test/stress/index.ts
24+
3225
const DATA_AMOUNT = 50 * 1024; // 50 kB
3326

3427
const sendAndGetData = async (web3: Web3, i: number) => {

packages/web3/test/stress/large_validator_data/index.ts

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,7 @@ along with web3.js. If not, see <http://www.gnu.org/licenses/>.
1616
*/
1717

1818
/* eslint-disable */
19-
<<<<<<<< HEAD:packages/web3/test/stress/large_validator_data/index.js
20-
const { Web3Validator } = require('../../../../web3-validator/lib/commonjs');
21-
========
2219
import { Web3Validator, JsonSchema, Json } from 'web3-validator';
23-
>>>>>>>> ok/5563-Stress-Tests-2---QA-Tests:packages/web3/test/stress/validator.ts
2420

2521
const abi = [
2622
{ indexed: true, internalType: 'address', name: 'from', type: 'address' },
@@ -107,34 +103,24 @@ const { schema: hugeSchema1000, data: hugeData1000 } = createHugeSchema(
107103
1000,
108104
);
109105

110-
const index = new Web3Validator();
106+
const validator = new Web3Validator();
111107

112-
<<<<<<<< HEAD:packages/web3/test/stress/large_validator_data/index.js
113-
console.time('huge schema');
114-
index.validateJSONSchema(hugeSchema, hugeData);
115-
console.timeLog('huge schema');
116-
117-
console.time('huge schema 1000');
118-
index.validateJSONSchema(hugeSchema1000, hugeData1000);
119-
console.timeLog('huge schema 1000');
120-
========
121108
validator.validateJSONSchema(hugeSchema, hugeData as object);
122109

123110
validator.validateJSONSchema(hugeSchema1000, hugeData1000 as object);
124-
>>>>>>>> ok/5563-Stress-Tests-2---QA-Tests:packages/web3/test/stress/validator.ts
125111

126112
for (let i = 0; i < 500; i += 1) {
127-
index.validateJSONSchema(simpleSchema, simpleData);
113+
validator.validateJSONSchema(simpleSchema, simpleData);
128114
}
129115

130116
for (let i = 0; i < 1000; i += 1) {
131-
index.validateJSONSchema(simpleSchema, simpleData);
117+
validator.validateJSONSchema(simpleSchema, simpleData);
132118
}
133119

134120
for (let i = 0; i < 1000; i += 1) {
135-
index.validateJSONSchema(abiJsonSchema, abiData);
121+
validator.validateJSONSchema(abiJsonSchema, abiData);
136122
}
137123

138124
for (let i = 0; i < 1000; i += 1) {
139-
index.validate(abi, abiData);
125+
validator.validate(abi, abiData);
140126
}

packages/web3/test/stress/many_requests/index.js renamed to packages/web3/test/stress/many_requests/index.ts

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,41 +16,48 @@ along with web3.js. If not, see <http://www.gnu.org/licenses/>.
1616
*/
1717

1818
/* eslint-disable */
19-
const { Web3 } = require('../../../lib/commonjs');
20-
const { IpcProvider } = require('../../../../web3-providers-ipc/lib/commonjs');
21-
const contractData = require('../../../../../fixtures/build/Basic.json');
19+
import { Web3, Contract, Numbers, EventLog } from 'web3';
20+
import { IpcProvider } from 'web3-providers-ipc';
21+
import { BasicBytecode, BasicAbi } from '../../shared_fixtures/build/Basic';
22+
import { Web3Account } from 'web3-eth-accounts';
2223

23-
const providerString = process.env.WEB3_SYSTEM_TEST_PROVIDER;
24+
const providerString = String(process.env.WEB3_SYSTEM_TEST_PROVIDER);
2425
const isWs = providerString.startsWith('ws');
2526
const isIpc = providerString.includes('ipc');
26-
const contracts = {};
27+
const contracts: { [key: string]: Contract<typeof BasicAbi> } = {};
2728

28-
const deployContracts = async (web3, accounts) => {
29+
const deployContracts = async (web3: Web3, accounts: Web3Account[]) => {
2930
const prs = [];
3031
for (let i = 0; i < accounts.length; i++) {
3132
const account = accounts[i];
3233
const sendOptions = { from: account.address };
3334
const deployOptions = {
34-
data: contractData.evm.bytecode.object,
35-
arguments: [123, ''],
35+
data: BasicBytecode,
36+
arguments: [123, ''] as [number, string],
3637
gas: BigInt(9000000000000),
3738
gasLimit: BigInt(9000000000000),
3839
type: BigInt(0),
3940
};
40-
const c = new web3.eth.Contract(contractData.abi);
41+
const c = new web3.eth.Contract<typeof BasicAbi>(BasicAbi);
4142
prs.push(
4243
c
4344
.deploy(deployOptions)
4445
.send(sendOptions)
45-
.then(contract => {
46+
.then((contract: typeof c) => {
4647
contracts[account.address] = contract;
4748
}),
4849
);
4950
}
5051
await Promise.all(prs);
5152
};
5253

53-
const addAccount = async (web3, mainAcc, address, privateKey, nonce) => {
54+
const addAccount = async (
55+
web3: Web3,
56+
mainAcc: string,
57+
address: string,
58+
privateKey: string,
59+
nonce: Numbers,
60+
) => {
5461
web3.eth.accounts.wallet.add(privateKey);
5562
return web3.eth.sendTransaction({
5663
from: mainAcc,
@@ -61,11 +68,11 @@ const addAccount = async (web3, mainAcc, address, privateKey, nonce) => {
6168
});
6269
};
6370

64-
const prepareAccounts = async (web3, n = 1000) => {
71+
const prepareAccounts = async (web3: Web3, n = 1000) => {
6572
const prs = [];
6673
const list = await web3.eth.personal.getAccounts();
6774
const mainAcc = list[0];
68-
const accountList = [];
75+
const accountList: Web3Account[] = [];
6976
const nonce = await web3.eth.getTransactionCount(mainAcc);
7077
for (let i = 0; i < n; i++) {
7178
const acc = web3.eth.accounts.create();
@@ -76,20 +83,20 @@ const prepareAccounts = async (web3, n = 1000) => {
7683
return accountList;
7784
};
7885

79-
const sendData = async (web3, account) => {
86+
const sendData = async (account: Web3Account) => {
8087
const contract = contracts[account.address];
8188
return contract.methods
8289
.firesStringEvent(`String event: ${account.address}`)
8390
.send({ from: account.address });
8491
};
8592

86-
const getData = async (web3, account) => {
93+
const getData = async (account: Web3Account) => {
8794
const contract = contracts[account.address];
8895
await contract.methods.getStringValue().call();
8996
};
9097

91-
const receivedEvents = {};
92-
const subscribeContract = acc => {
98+
const receivedEvents: { [key: string]: EventLog } = {};
99+
const subscribeContract = (acc: Web3Account) => {
93100
const contract = contracts[acc.address];
94101
const event = contract.events.StringEvent();
95102

@@ -100,7 +107,7 @@ const subscribeContract = acc => {
100107
receivedEvents[acc.address] = res;
101108
});
102109
};
103-
const contractSubscriptions = (web3, accounts) => {
110+
const contractSubscriptions = (accounts: Web3Account[]) => {
104111
console.log(`Subscribe to ${accounts.length} contracts events`);
105112
for (const acc of accounts) {
106113
subscribeContract(acc);
@@ -123,13 +130,13 @@ const test = async () => {
123130
await deployContracts(web3, accounts);
124131
// if socket subscribe to events
125132
if (isIpc || isWs) {
126-
contractSubscriptions(web3, accounts);
133+
contractSubscriptions(accounts);
127134
}
128135

129136
console.log(`Send data from ${n} accounts in parallel`);
130137
const sendPrs = [];
131138
for (let i = 0; i < n; i++) {
132-
sendPrs.push(sendData(web3, accounts[i]));
139+
sendPrs.push(sendData(accounts[i]));
133140
}
134141
await Promise.all(sendPrs);
135142

@@ -142,11 +149,11 @@ const test = async () => {
142149
console.log(`Get data from ${n} accounts in parallel`);
143150
const getPrs = [];
144151
for (let i = 0; i < n; i++) {
145-
getPrs.push(getData(web3, accounts[i]));
152+
getPrs.push(getData(accounts[i]));
146153
}
147154
await Promise.all(getPrs);
148155
if (isIpc || isWs) {
149-
web3.provider.disconnect();
156+
(web3.provider as IpcProvider).disconnect();
150157
}
151158
};
152159

packages/web3/test/stress/many_requests/start.sh

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,24 @@
22
. scripts/env.sh
33

44
export WEB3_SYSTEM_TEST_BACKEND="geth"
5+
export TS_NODE_PREFER_TS_EXTS=true
56

67
./scripts/geth_binary.sh stressStart
78
export PARALLEL_COUNT=10
89
export WEB3_SYSTEM_TEST_PROVIDER=$IPC_PATH
9-
node ./packages/web3/test/stress/many_requests/index.js
10+
ts-node ./packages/web3/test/stress/many_requests/index.ts
1011
./scripts/geth_binary.sh stop
1112

1213
./scripts/geth_binary.sh stressStart
1314
export PARALLEL_COUNT=1000
1415
export WEB3_SYSTEM_TEST_PROVIDER=ws://127.0.0.1:8545
15-
node ./packages/web3/test/stress/many_requests/index.js
16+
ts-node ./packages/web3/test/stress/many_requests/index.ts
1617

1718

1819
./scripts/geth_binary.sh stressStart
1920
export PARALLEL_COUNT=100
2021
export WEB3_SYSTEM_TEST_PROVIDER=http://127.0.0.1:8545
21-
node ./packages/web3/test/stress/many_requests/index.js
22+
ts-node ./packages/web3/test/stress/many_requests/index.ts
2223
./scripts/geth_binary.sh stop
2324

2425

0 commit comments

Comments
 (0)