From eae2c1a6a979dd227d98710ca8e029c0d1f210da Mon Sep 17 00:00:00 2001 From: Muhammad-Altabba <24407834+Muhammad-Altabba@users.noreply.github.com> Date: Mon, 22 Aug 2022 17:52:16 +0200 Subject: [PATCH 1/4] start incrementing jsonrpc.id from random number --- packages/web3-core-requestmanager/src/jsonrpc.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/web3-core-requestmanager/src/jsonrpc.js b/packages/web3-core-requestmanager/src/jsonrpc.js index dfb90184203..e687c32f6d3 100644 --- a/packages/web3-core-requestmanager/src/jsonrpc.js +++ b/packages/web3-core-requestmanager/src/jsonrpc.js @@ -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 half of the max safe integer to stay below the max when incrementing + messageId: Math.floor(Math.random() * Number.MAX_SAFE_INTEGER / 2) }; /** From e766254803d7cf559f4b55ab446559e495109ba6 Mon Sep 17 00:00:00 2001 From: Muhammad-Altabba <24407834+Muhammad-Altabba@users.noreply.github.com> Date: Mon, 22 Aug 2022 22:56:46 +0200 Subject: [PATCH 2/4] modify the starging value of Jsonrpc message id + update CHANGELOG.md --- CHANGELOG.md | 1 + packages/web3-core-requestmanager/src/jsonrpc.js | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 28b0a2fc4c2..017b0d7c6e8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/packages/web3-core-requestmanager/src/jsonrpc.js b/packages/web3-core-requestmanager/src/jsonrpc.js index e687c32f6d3..dfe3bcb8ab0 100644 --- a/packages/web3-core-requestmanager/src/jsonrpc.js +++ b/packages/web3-core-requestmanager/src/jsonrpc.js @@ -27,8 +27,9 @@ // Initialize Jsonrpc as a simple object with utility functions. var Jsonrpc = { // This is the starting counter for the Jsonrpc.id - // Pick a random number between 0 and half of the max safe integer to stay below the max when incrementing - messageId: Math.floor(Math.random() * Number.MAX_SAFE_INTEGER / 2) + // Pick a random number between 0 and (the maximum safe integer minus the max unsigned 32 bit integer) + // Actually, to insure staying in the safe range while incrementing, we insure making the range less than the max unsigned 32 bit integer + messageId: Math.floor(Math.random() * (Number.MAX_SAFE_INTEGER - Math.pow( 2, 32 ))) }; /** From 4a8ae6db5f6ea99d50c7df59283eb75512bae300 Mon Sep 17 00:00:00 2001 From: Muhammad-Altabba <24407834+Muhammad-Altabba@users.noreply.github.com> Date: Mon, 22 Aug 2022 23:05:15 +0200 Subject: [PATCH 3/4] modify a comment at jsonrpc.js --- packages/web3-core-requestmanager/src/jsonrpc.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/web3-core-requestmanager/src/jsonrpc.js b/packages/web3-core-requestmanager/src/jsonrpc.js index dfe3bcb8ab0..5faf62f1bdb 100644 --- a/packages/web3-core-requestmanager/src/jsonrpc.js +++ b/packages/web3-core-requestmanager/src/jsonrpc.js @@ -26,9 +26,8 @@ // Initialize Jsonrpc as a simple object with utility functions. var Jsonrpc = { - // This is the starting counter for the Jsonrpc.id - // Pick a random number between 0 and (the maximum safe integer minus the max unsigned 32 bit integer) - // Actually, to insure staying in the safe range while incrementing, we insure making the range less than the max unsigned 32 bit integer + // 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 ))) }; From 7d83e6b684dc7ab75d85d793bea279dc64fc7320 Mon Sep 17 00:00:00 2001 From: Muhammad-Altabba <24407834+Muhammad-Altabba@users.noreply.github.com> Date: Tue, 23 Aug 2022 16:03:57 +0200 Subject: [PATCH 4/4] enhance calculating Jsonrpc message id --- packages/web3-core-requestmanager/src/jsonrpc.js | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/packages/web3-core-requestmanager/src/jsonrpc.js b/packages/web3-core-requestmanager/src/jsonrpc.js index 5faf62f1bdb..bf1e3aec7ff 100644 --- a/packages/web3-core-requestmanager/src/jsonrpc.js +++ b/packages/web3-core-requestmanager/src/jsonrpc.js @@ -26,9 +26,9 @@ // Initialize Jsonrpc as a simple object with utility functions. var Jsonrpc = { - // 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 ))) + // 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) }; /** @@ -44,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',