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

Start incrementing jsonrpc message id from random number #5371

Merged
merged 4 commits into from
Aug 25, 2022

Conversation

Muhammad-Altabba
Copy link
Contributor

@Muhammad-Altabba Muhammad-Altabba commented Aug 22, 2022

Description

Fixes #(issue)

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist for 1.x:

  • I have selected the correct base branch.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have made corresponding changes to the documentation.
  • My changes generate no new warnings.
  • Any dependent changes have been merged and published in downstream modules.
  • I ran npm run dtslint with success and extended the tests and types if necessary.
  • I ran npm run test:cov and my test cases cover all the lines and branches of the added code.
  • I ran npm run build with success.
  • I have tested the built dist/web3.min.js in a browser.
  • I have tested my code on the live network.
  • I have checked the Deploy Preview and it looks correct.
  • I have updated the CHANGELOG.md file in the root folder.

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.

@render
Copy link

render bot commented Aug 22, 2022

@Muhammad-Altabba Muhammad-Altabba added Bug Addressing a bug 1.x 1.0 related issues labels Aug 22, 2022
@Muhammad-Altabba Muhammad-Altabba changed the title start incrementing jsonrpc.id from random number Start incrementing jsonrpc message id from random number Aug 22, 2022
@coveralls
Copy link

coveralls commented Aug 22, 2022

Pull Request Test Coverage Report for Build 2912165598

  • 2 of 4 (50.0%) changed or added relevant lines in 1 file are covered.
  • 2 unchanged lines in 1 file lost coverage.
  • Overall coverage decreased (-0.03%) to 74.571%

Changes Missing Coverage Covered Lines Changed/Added Lines %
packages/web3-core-requestmanager/src/jsonrpc.js 2 4 50.0%
Files with Coverage Reduction New Missed Lines %
packages/web3-core-requestmanager/lib/jsonrpc.js 2 75.86%
Totals Coverage Status
Change from base Build 2891786605: -0.03%
Covered Lines: 3271
Relevant Lines: 4135

💛 - Coveralls

@luu-alex
Copy link
Contributor

Looks good 👍 is this something we want to consider for 4.x as well?

messageId: 0
// This is the starting counter for the Jsonrpc.id. And, to insure staying in the safe range while incrementing later,
// pick a random number between 0 and (the maximum safe integer minus the max unsigned 32 bit integer)
messageId: Math.floor(Math.random() * (Number.MAX_SAFE_INTEGER - Math.pow( 2, 32 )))
Copy link
Contributor

Choose a reason for hiding this comment

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

I had a hunch that this will not fix the actual issue.

Responses get mixed up due to conflicting payload IDs

The response will still be mixed up if the payload Ids are conflicting, starting point does not matter and randomizing starting point would minimize but not fixed the problem.

For once instance of the web3 there will be no conflicting payload IDs in any case.

Copy link
Contributor

Choose a reason for hiding this comment

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

As per here: https://www.jsonrpc.org/specification

An identifier established by the Client that MUST contain a String, Number, or NULL value if included. 

uuid is already used in 1.x, maybe we could use it, also, for request id`s. We won't need a starting point and other libraries use numbers for ids.

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, there will be no issue if one instance is running and one library is used. And, yes, this solution will minimize the possibility of a conflict to some grade that is neglected but the possibility is still there.
But using a uuid (that would be handled as a string) instead of a number would be a breaking change. Even the tests for 1.x would need to be modified accordingly.

However, I proposed to handle this in a different way in v4.x as there we can do a breaking change by making the message id as a string that contains a UUID or another format. The new issue is here: #5373

And if you have a better suggestion to solve this in v1.x, please advise.

Thanks,

Copy link
Contributor

Choose a reason for hiding this comment

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

We can proceed with this approach but considering it's not a permanent fix.

Also it does not matter if there is more than one library using the same id for the same endpoint. For the HTTP, the RPC server will be responding synchronously. For WS based RPC server it will be responding per connection. So if there are two libraries, both would be having different connections and that will not conflict the ids.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks @nazarhussain ,
As you see this as "not a permanent fix", do you have a better suggestion for 1.x that does not cause a breaking change?

Copy link

@abrindam abrindam Aug 25, 2022

Choose a reason for hiding this comment

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

So if there are two libraries, both would be having different connections and that will not conflict the ids.

@nazarhussain I just wanted to note in my original report #5327, I saw a situation where two libraries were using the same connection, which resulted in an ID conflict. I'm not certain how this happened because I'm completely unfamilar with your codebase, but it's somehow possible.

In my case, it was eth-block-tracker that was sending a message that somehow bypassed jsonrpc.js but still was being processed by the same websocket connection that jsonrpc.js was using, which resulted in a conflict.

Maybe someone more experienced than me could figure out how that happened? There might be another lurking bug there.

Copy link
Contributor

@nikoulai nikoulai left a comment

Choose a reason for hiding this comment

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

PR looks good, just added a comment in the conversation.

This was referenced Feb 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1.x 1.0 related issues Bug Addressing a bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants