Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make providers WS and IPC reliable #5763

Merged
merged 32 commits into from
Jan 25, 2023

Conversation

avkos
Copy link
Contributor

@avkos avkos commented Jan 17, 2023

Description

fixes #5632

Please include a summary of the changes and be sure to follow our Contribution Guidelines.

Checklist for 4.x:

  • I have selected the correct 4.x base branch.
  • Within the description, the feature or issue is discussed in detail or an issue is linked.
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added any required tests for the changes I made
  • I have made corresponding changes to the documentation
  • Any dependent changes have been merged and published in downstream modules.
  • I ran yarn successfully
  • I ran yarn lint successfully
  • I ran yarn build:web successfully
  • I ran yarn test:unit successfully
  • I ran yarn test:integration successfully
  • I ran compile:contracts successfully
  • I have tested my code.
  • I have updated the corresponding CHANGELOG.md file in the packages I have edited.

@github-actions github-actions bot temporarily deployed to Preview: (ok/5632-Make-providers-WS-and-IPC-reliable) January 17, 2023 02:43 Inactive
@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Jan 17, 2023

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: b44fc31
Status: ✅  Deploy successful!
Preview URL: https://ac1be8b6.web3-js-docs.pages.dev
Branch Preview URL: https://ok-5632-make-providers-ws-an.web3-js-docs.pages.dev

View logs

@github-actions github-actions bot temporarily deployed to Preview: (ok/5632-Make-providers-WS-and-IPC-reliable) January 17, 2023 02:52 Inactive
@github-actions github-actions bot temporarily deployed to Preview: (ok/5632-Make-providers-WS-and-IPC-reliable) January 17, 2023 03:06 Inactive
@github-actions github-actions bot temporarily deployed to Preview: (ok/5632-Make-providers-WS-and-IPC-reliable) January 17, 2023 03:25 Inactive
@@ -22,7 +22,7 @@
"test:ci": "jest --coverage=true --coverage-reporters=json --verbose",
"test:watch": "npm test -- --watch",
"test:unit": "jest --config=./test/unit/jest.config.js",
"test:integration": "jest --config=./test/integration/jest.config.js --passWithNoTests"
"test:integration": "jest --config=./test/integration/jest.config.js --passWithNoTests --forceExit"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is --forceExit on for debugging?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Locally it is not stuck, but in GitHub Action, sometimes yes. I need to investigate it deeply

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If its not fixed in current PR pls create issue pointing to this discussion.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've fixed the test. So it works without --forceExit

@luu-alex
Copy link
Contributor

Looks good! 👍

public constructor(socketPath: string) {
super(socketPath);

public constructor(socketPath: string, options?: object, reconnectOptions?: object) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is doc updated for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ipc provider doesn't have doc

});
} else {
this._connectionStatus = 'disconnected';
throw new InvalidConnectionError(this._socketPath);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should also include more error details coming via e in } catch (e) { to throw new InvalidConnectionError(this._socketPath)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

this._reconnectOptions.autoReconnect &&
(![1000, 1001].includes(event.code) || !event.wasClean)
) {
if (!event && this._reconnectOptions.autoReconnect) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There can be normal close events are those considered as well ?
so this condition should only be true if there is no clean close or event codes are not 1000 and 1001,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IPC providers use import { Socket } from 'net'; to connect.
in the close event of Socket we do not have any code or wasClean information.
That is why for IPC we have a different condition.

but when we call the disconnect method we create an event on our own. and in this case, we will have an event object here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, so is this tested for normal close, its existing without reconnect?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, this is a test "exit without reconnection":

                 it('should emit connect and disconnected events', async () => {
			const server = await startGethServer(8547);
			const web3Provider = new IpcProvider(server.path);
			expect(!!(await waitForEvent(web3Provider, 'connect'))).toBe(true);
			const disconnectPromise = waitForEvent(web3Provider, 'disconnect');
			web3Provider.disconnect();
			expect(!!(await disconnectPromise)).toBe(true);
			await server.close();
		});

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

@@ -76,13 +72,16 @@ export default class WebSocketProvider<

this._addSocketListeners();
} catch (e) {
throw new InvalidConnectionError(this._socketPath);
if (!this.isReconnecting) {
throw new InvalidConnectionError(this._socketPath);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls also include more error details from e obj in rethrow

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@github-actions github-actions bot temporarily deployed to Preview: (ok/5632-Make-providers-WS-and-IPC-reliable) January 19, 2023 22:21 Inactive
@github-actions github-actions bot temporarily deployed to Preview: (ok/5632-Make-providers-WS-and-IPC-reliable) January 19, 2023 22:48 Inactive
@github-actions github-actions bot temporarily deployed to Preview: (ok/5632-Make-providers-WS-and-IPC-reliable) January 19, 2023 23:51 Inactive
@github-actions github-actions bot temporarily deployed to Preview: (ok/5632-Make-providers-WS-and-IPC-reliable) January 20, 2023 02:06 Inactive
@github-actions github-actions bot temporarily deployed to Preview: (ok/5632-Make-providers-WS-and-IPC-reliable) January 20, 2023 10:41 Inactive
@github-actions github-actions bot temporarily deployed to Preview: (ok/5632-Make-providers-WS-and-IPC-reliable) January 20, 2023 22:35 Inactive
@github-actions github-actions bot temporarily deployed to Preview: (ok/5632-Make-providers-WS-and-IPC-reliable) January 24, 2023 00:09 Inactive
@github-actions github-actions bot temporarily deployed to Preview: (ok/5632-Make-providers-WS-and-IPC-reliable) January 24, 2023 00:33 Inactive
@jdevcs
Copy link
Contributor

jdevcs commented Jan 24, 2023

LGTM, Some of WS tests are not invoked in CI, pls check before merging if there is any thing with current PR causing it. @avkos

@github-actions github-actions bot temporarily deployed to Preview: (ok/5632-Make-providers-WS-and-IPC-reliable) January 24, 2023 14:56 Inactive
@github-actions github-actions bot temporarily deployed to Preview: (ok/5632-Make-providers-WS-and-IPC-reliable) January 24, 2023 15:23 Inactive
@github-actions github-actions bot temporarily deployed to Preview: (ok/5632-Make-providers-WS-and-IPC-reliable) January 24, 2023 15:33 Inactive
@github-actions github-actions bot temporarily deployed to Preview: (ok/5632-Make-providers-WS-and-IPC-reliable) January 24, 2023 15:46 Inactive
@github-actions github-actions bot temporarily deployed to Preview: (ok/5632-Make-providers-WS-and-IPC-reliable) January 24, 2023 16:03 Inactive
@github-actions github-actions bot temporarily deployed to Preview: (ok/5632-Make-providers-WS-and-IPC-reliable) January 24, 2023 16:47 Inactive
@github-actions github-actions bot temporarily deployed to Preview: (ok/5632-Make-providers-WS-and-IPC-reliable) January 24, 2023 18:08 Inactive
Copy link
Contributor

@Muhammad-Altabba Muhammad-Altabba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!
However, I have a few comments.

Comment on lines 39 to 62
it('check defaults', async () => {
const web3Provider = new WebSocketProvider(getSystemTestProvider());
// @ts-expect-error-next-line
expect(web3Provider._reconnectOptions).toEqual({
autoReconnect: true,
delay: 5000,
maxAttempts: 5,
});
await waitForOpenConnection(web3Provider);
web3Provider.disconnect(1000, 'test');
await waitForCloseConnection(web3Provider);
});
it('set custom reconnectOptions', async () => {
const web3Provider = new WebSocketProvider(
getSystemTestProvider(),
{},
reconnectionOptions,
);
// @ts-expect-error-next-line
expect(web3Provider._reconnectOptions).toEqual(reconnectionOptions);
await waitForOpenConnection(web3Provider);
web3Provider.disconnect(1000, 'test');
await waitForCloseConnection(web3Provider);
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe the logic of those 2 tests could be moved to web3-utils. We just need to keep the Provider initialization. And the rest is douplicate with IpcProvider. Or what do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can have tests here and in web3-utils just to be sure it works not only on base class but in WebsocketProvider and in IPCProvider

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I meant to have only the provider initialization at this package:

const web3Provider = new WebSocketProvider(
				getSystemTestProvider(),
				{},
				reconnectionOptions,
			);

And then the object web3Provider could be passed to a function that contains the logic (that is exactly the same for both WS and IPC):

			// @ts-expect-error-next-line
			expect(web3Provider._reconnectOptions).toEqual(reconnectionOptions);
			await waitForOpenConnection(web3Provider);
			web3Provider.disconnect(1000, 'test');
			await waitForCloseConnection(web3Provider);

if [ -z "${ORIGARGS[1]}" ]
then
echo "Starting geth..."
echo "docker run -p $WEB3_SYSTEM_TEST_PORT:$WEB3_SYSTEM_TEST_PORT --name web3-geth-client ethereum/client-go --nodiscover --nousb --ws --ws.addr 0.0.0.0 --ws.port $WEB3_SYSTEM_TEST_PORT --http --http.addr 0.0.0.0 --http.port $WEB3_SYSTEM_TEST_PORT --allow-insecure-unlock --http.api personal,web3,eth,admin,debug,txpool,net --ws.api personal,web3,eth,admin,debug,miner,txpool,net --dev"
docker run -p $WEB3_SYSTEM_TEST_PORT:$WEB3_SYSTEM_TEST_PORT ethereum/client-go --nodiscover --nousb --ws --ws.addr 0.0.0.0 --ws.port $WEB3_SYSTEM_TEST_PORT --http --http.addr 0.0.0.0 --http.port $WEB3_SYSTEM_TEST_PORT --allow-insecure-unlock --http.api personal,web3,eth,admin,debug,txpool,net --ws.api personal,web3,eth,admin,debug,miner,txpool,net --dev
echo "docker run -p $WEB3_SYSTEM_TEST_PORT:$WEB3_SYSTEM_TEST_PORT ethereum/client-go:stable --nodiscover --nousb --ws --ws.addr 0.0.0.0 --ws.port $WEB3_SYSTEM_TEST_PORT --http --http.addr 0.0.0.0 --http.port $WEB3_SYSTEM_TEST_PORT --allow-insecure-unlock --http.api personal,web3,eth,admin,debug,txpool,net --ws.api personal,web3,eth,admin,debug,miner,txpool,net --dev"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think having a name for the docker container is something good. I mean to keep --name web3-geth-client

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's make changes in docker in another PR

Copy link
Contributor

@Muhammad-Altabba Muhammad-Altabba Jan 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, for this I recommend not changing the docker configuration related to the docker image name (--name web3-geth-client) in this MR 😄

Copy link
Contributor

@Muhammad-Altabba Muhammad-Altabba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work Oleksii,
I have just 2 minor comments that you may consider. And you may merge without considering them.

@avkos avkos merged commit 8e44e88 into 4.x Jan 25, 2023
@avkos avkos deleted the ok/5632-Make-providers-WS-and-IPC-reliable branch January 25, 2023 17:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants