Skip to content

Commit

Permalink
Start incrementing jsonrpc message id from random number (#5371)
Browse files Browse the repository at this point in the history
* start incrementing jsonrpc.id from random number
  • Loading branch information
Muhammad-Altabba committed Aug 25, 2022
1 parent fce653d commit 0d38050
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -592,3 +592,4 @@ Released with 1.0.0-beta.37 code base.

### Fixed
- Browser builds support polyfills (#5031) (#5053) (#4659) (#4767)
- Start incrementing jsonrpc.id from random number (#5327)
14 changes: 11 additions & 3 deletions packages/web3-core-requestmanager/src/jsonrpc.js
Expand Up @@ -26,7 +26,9 @@

// Initialize Jsonrpc as a simple object with utility functions.
var Jsonrpc = {
messageId: 0
// This is the starting counter for the Jsonrpc.id.
// Pick a random number between 0 and the maximum safe integer
messageId: Math.floor(Math.random() * Number.MAX_SAFE_INTEGER)
};

/**
Expand All @@ -42,8 +44,14 @@ Jsonrpc.toPayload = function (method, params) {
throw new Error('JSONRPC method should be specified for params: "'+ JSON.stringify(params) +'"!');
}

// advance message ID
Jsonrpc.messageId++;
if(Jsonrpc.messageId === Number.MAX_SAFE_INTEGER) {
// if the maximum safe integer has been reached, restart from a random number
Jsonrpc.messageId = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER)
}
else {
// advance message ID
Jsonrpc.messageId++;
}

return {
jsonrpc: '2.0',
Expand Down

0 comments on commit 0d38050

Please sign in to comment.