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

Commit a9c5427

Browse files
authored
Main web3 package - 4.0.0 Alpha.1 System Tests (#4910)
* Config docker, Ganache and Geth * Add some initial tests * Add bash script for permissions * Use local geth for integration tests -Add commands to install and run geth for ubuntu -Change github workflow to use local geth instead of docker The Reason: ipc socket is not accessible outside docker * Filter out ipc when testing ganache * Fix ipc test * Test workflow * Remove verbose from jest * Change when to init geth * Add nohup again * move stdout and stderr on geth-local command * Add nohup again * Run geth in background with screen * Merge * Add test using external wallet provider * Temp solution to stop jest from hanging * Test browser tests * Karma config for integration tests * Karma config for integration tests * More (unsuccessfull) tests for karma * Batch request tests - Expose BatchRequest class in web3 as part of Web3Context - Add integration tests for BatchRequest * Corrent ipc path argument in geth-local * Add wallet provider test * Change test file filtering function so it can work on array * Filter out ws * Rename batch request test * Add more tests * Geth config changes - Use same port for ws and http - Remove unecessary exposes port exposes - Remove permission in docker folder * Change path * Add screen session name/fix command * Test karma conf * Change tests to cover both jest and jasmine/workaround * Clean package.json * Expose Web3BatchRequest directly * Re add ipc test * Add some headers * WIP: Refactoring to new test infrastructure * Refactoring to new test infrastructure * Refactor deferred promises * Revert deferred promise to it's initial implementation (not before previous commit) * Apply changes * fix Greeter and yarn.lock * Add infura secrets and fix class * Possible fix build workflow * Fix BatchRequest function type * Add abi encodeParameters test * Add more provider tests * change module tests * Changes from feedback -remove lint disable for floating promises -remove unecessary setup.sh file -stop exporting accounts from system_utils -use createNewAccount function for tests * Fix batch request test
1 parent f968381 commit a9c5427

File tree

13 files changed

+1401
-64
lines changed

13 files changed

+1401
-64
lines changed

.github/workflows/build.yml

+3
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ jobs:
5555
name: Integration
5656
needs: unit
5757
runs-on: ubuntu-latest
58+
env:
59+
INFURA_GOERLI_HTTP: ${{ secrets.INFURA_GOERLI_HTTP }}
60+
INFURA_GOERLI_WS: ${{ secrets.INFURA_GOERLI_WS }}
5861
strategy:
5962
fail-fast: false
6063
matrix:

jest.config.js

+31-15
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,37 @@
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+
118
const path = require('path');
219
const { lstatSync, readdirSync } = require('fs');
320
// get listing of packages in the mono repo
421
const basePath = path.resolve(__dirname, 'packages');
5-
const packages = readdirSync(basePath).filter(name => {
6-
return lstatSync(path.join(basePath, name)).isDirectory();
7-
});
22+
const packages = readdirSync(basePath).filter(name =>
23+
lstatSync(path.join(basePath, name)).isDirectory(),
24+
);
825
module.exports = {
9-
preset: 'ts-jest',
10-
testEnvironment: 'node',
11-
moduleNameMapper: {
12-
...packages.reduce(
13-
(acc, name) => ({
14-
...acc,
15-
[`${name}(.*)$`]:
16-
`<rootDir>/packages/./${name}/$1`,
17-
}),
18-
{},
19-
),
20-
},
26+
preset: 'ts-jest',
27+
testEnvironment: 'node',
28+
moduleNameMapper: {
29+
...packages.reduce(
30+
(acc, name) => ({
31+
...acc,
32+
[`${name}(.*)$`]: `<rootDir>/packages/./${name}/$1`,
33+
}),
34+
{},
35+
),
36+
},
2137
};

packages/web3-common/src/deferred_promise.ts

+34-22
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,6 @@ along with web3.js. If not, see <http://www.gnu.org/licenses/>.
1717

1818
import { OperationTimeoutError } from 'web3-errors';
1919

20-
export const promiseTimeout = async <T = void>(ms: number, message: string): Promise<T> =>
21-
new Promise((_, reject) => {
22-
const id = setTimeout(() => {
23-
clearTimeout(id);
24-
reject(new OperationTimeoutError(message ?? `Timed out in ${ms}ms.`));
25-
}, ms);
26-
});
27-
2820
export class DeferredPromise<T> implements Promise<T> {
2921
// public tag to treat object as promise by different libs
3022
public [Symbol.toStringTag]: 'Promise';
@@ -33,6 +25,9 @@ export class DeferredPromise<T> implements Promise<T> {
3325
private _resolve!: (value: T | PromiseLike<T>) => void;
3426
private _reject!: (reason?: unknown) => void;
3527
private _state: 'pending' | 'fulfilled' | 'rejected' = 'pending';
28+
private _timeoutId?: NodeJS.Timeout;
29+
private readonly _timeoutInterval?: number;
30+
private readonly _timeoutMessage: string;
3631

3732
public constructor(
3833
{
@@ -45,19 +40,16 @@ export class DeferredPromise<T> implements Promise<T> {
4540
timeoutMessage: 'DeferredPromise timed out',
4641
},
4742
) {
43+
this._promise = new Promise<T>((resolve, reject) => {
44+
this._resolve = resolve;
45+
this._reject = reject;
46+
});
47+
48+
this._timeoutMessage = timeoutMessage;
49+
this._timeoutInterval = timeout;
50+
4851
if (eagerStart) {
49-
this._promise = Promise.race([
50-
new Promise<T>((resolve, reject) => {
51-
this._resolve = resolve;
52-
this._reject = reject;
53-
}),
54-
promiseTimeout(timeout, timeoutMessage),
55-
]) as Promise<T>;
56-
} else {
57-
this._promise = new Promise<T>((resolve, reject) => {
58-
this._resolve = resolve;
59-
this._reject = reject;
60-
});
52+
this.startTimer();
6153
}
6254
}
6355

@@ -84,12 +76,32 @@ export class DeferredPromise<T> implements Promise<T> {
8476
}
8577

8678
public resolve(value: T | PromiseLike<T>): void {
87-
this._state = 'fulfilled';
8879
this._resolve(value);
80+
this._state = 'fulfilled';
81+
this._clearTimeout();
8982
}
9083

9184
public reject(reason?: unknown): void {
92-
this._state = 'rejected';
9385
this._reject(reason);
86+
this._state = 'rejected';
87+
this._clearTimeout();
88+
}
89+
90+
public startTimer() {
91+
if (this._timeoutInterval && this._timeoutInterval > 0) {
92+
this._timeoutId = setTimeout(this._checkTimeout.bind(this), this._timeoutInterval);
93+
}
94+
}
95+
96+
private _checkTimeout() {
97+
if (this._state === 'pending' && this._timeoutId) {
98+
this.reject(new OperationTimeoutError(this._timeoutMessage));
99+
}
100+
}
101+
102+
private _clearTimeout() {
103+
if (this._timeoutId) {
104+
clearTimeout(this._timeoutId);
105+
}
94106
}
95107
}

packages/web3-common/src/web3_base_provider.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,6 @@ export abstract class Web3BaseProvider<API extends Web3APISpec = EthExecutionAPI
8787
): void;
8888
public abstract removeAllListeners?(type: string): void;
8989
public abstract connect(): void;
90-
public abstract disconnect(code: number, reason: string): void;
90+
public abstract disconnect(code?: number, reason?: string): void;
9191
public abstract reset(): void;
9292
}

packages/web3-core/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ export * from './web3_request_manager';
2020
export * from './web3_subscription_manager';
2121
export * from './web3_subscriptions';
2222
export * from './web3_context';
23+
export * from './web3_batch_request';
2324
export * from './utils';
2425
export * from './types';

packages/web3-core/src/web3_context.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ export class Web3Context<
246246
this.provider = provider;
247247
}
248248

249-
public get BatchRequest() {
249+
public get BatchRequest(): new () => Web3BatchRequest {
250250
return Web3BatchRequest.bind(null, this._requestManager as unknown as Web3RequestManager);
251251
}
252252
}

packages/web3/package.json

+8-4
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@
3030
"test:ci": "jest --coverage=true --coverage-reporters=json --verbose",
3131
"test:watch": "npm test -- --watch",
3232
"test:unit": "jest --config=./test/unit/jest.config.js",
33-
"test:integration": "jest --config=./test/integration/jest.config.js --runInBand --passWithNoTests"
33+
"test:integration": "jest --config=./test/integration/jest.config.js --runInBand --forceExit"
3434
},
3535
"devDependencies": {
36+
"@truffle/hdwallet-provider": "^2.0.4",
3637
"@types/jest": "^27.0.3",
3738
"@typescript-eslint/eslint-plugin": "^5.4.0",
3839
"@typescript-eslint/parser": "^5.4.0",
@@ -44,19 +45,22 @@
4445
"jest-extended": "^1.1.0",
4546
"prettier": "^2.4.1",
4647
"ts-jest": "^27.0.7",
47-
"typescript": "^4.5.2"
48+
"typescript": "^4.5.2",
49+
"web3-providers-ws": "4.0.0-alpha.0",
50+
"web3-providers-http": "4.0.0-alpha.1",
51+
"web3-providers-ipc": "4.0.0-alpha.0"
4852
},
4953
"dependencies": {
5054
"web3-common": "1.0.0-alpha.0",
5155
"web3-core": "4.0.0-alpha.0",
56+
"web3-errors": "1.0.0-alpha.0",
5257
"web3-eth": "4.0.0-alpha.1",
5358
"web3-eth-abi": "4.0.0-alpha.0",
5459
"web3-eth-accounts": "4.0.0-alpha.0",
5560
"web3-eth-contract": "4.0.0-alpha.0",
5661
"web3-eth-ens": "4.0.0-alpha.0",
5762
"web3-eth-iban": "4.0.0-alpha.0",
5863
"web3-net": "^4.0.0-alpha.0",
59-
"web3-utils": "4.0.0-alpha.1",
60-
"web3-errors": "1.0.0-alpha.0"
64+
"web3-utils": "4.0.0-alpha.1"
6165
}
6266
}

0 commit comments

Comments
 (0)