Web3.js: Not Compatible v1.3.0-rc.0 with 1.2.11 on web3-utils

Created on 21 Oct 2020  路  8Comments  路  Source: ChainSafe/web3.js

We are developing a smart contract as called tubu.io for private and ethereum-based blockchain networks. Mostly, our focus is on ConsenSys Quorum. The main difference ConsenSys Quorum Blockchain is the block timestamp type.

So, the util method which is called 'hexToNumber' on 'web3-utils' is not suitable for Quorum Blockchain block timestamp type. There are many issues with these 'Number can only safely store up to 53 bits' situations such as #1905 and #1960.

To fix this bug for Quorum, we update the 'hexToNumber' with formatters

const newWeb3 = new Web3(
        new Web3.providers.WebsocketProvider(connectionString)
      );

web3 = newWeb3;
      await this.checkConnection();
      web3.extend({
        property: "eth",
        methods: [
          new web3.extend.Method({
            name: "getBlockByNumber",
            call: "eth_getBlockByNumber",
            params: 2,
            inputFormatter: [
              web3.extend.formatters.inputBlockNumberFormatter,
              (v) => !!v,
            ],
            outputFormatter: web3.extend.formatters.outputBlockFormatter,
          }),
        ],
      });

web3.utils.hexToNumber = (v) => {
        if (!v) return v;
        try {
          return numberToBN(v).toNumber();
        } catch (e) {
          return numberToBN(v).toString();
        }
      };

This fixation way is working on v1.2.11, but on v1.3.0-rc.0 web3-utils are working differently so the fixation method is not working as expected. In addition, we can not find how can we fix this bug on v1.3.0-rc.0

Expected behavior

We expect that web3-utils would not regenerate after initializing on v1.3.0.-rc.0 like on v1.2.11. Another way, because of that package.json includes ^1.2.11 as default it will install the latest version as v1.3.0-rc.0. So, v1.2.11 must be compatible with this situation.

Actual behavior

After initializing web3, while calling contract.deploy or eth.sendTransaction web3-utils are initialized again so override functions do not work.

Possible Solution

hexToNumber method on web3-utils can be updated with :

function hexToNumber(v) {
        if (!v) return v;
        try {
          return numberToBN(v).toNumber();
        } catch (e) {
          return numberToBN(v).toString();
        }
      };

But this updating can affect other parts of web3

Environment

web3 : v1.2.11 vs v1.3.0-rc.0
node : v12.18.2
OS : Linux Mint 20

Investigate Stale bug

Most helpful comment

@spacesailor24 I uses on('receipt') event during sending tx. I think it uses hexToNumber. But block.timestamp type is the main problem. Because the type of Quorum's block timestamp is different, the original hexToNumber cannot convert as I understand. I do not use the method independently.

Overriding hexToNumber was the solution before update web3 version from 1.2.11 to v1.3.0.rc.0

All 8 comments

Sorry for the late reply. I had taken some time off, im flagging this since you should not have been able to download the latest since its an RC

Do you mind explaining what you mean by regenerating?

No problem with the late reply.

When I override the hexToNumber functions on web3-utils, it still uses the classic hexToNumber function. In other words, I can not override any functions on web3-utils after initializing a new web3 instance like that :

const web3 = new Web3();

web3.utils.hexToNumber = (v) => {
        if (!v) return v;
        try {
          return numberToBN(v).toNumber();
        } catch (e) {
          return numberToBN(v).toString();
        }
      };

I think it does not use utils of web3 instance. when I set the web3.utils as undefined, it still uses its own utils.

let me investigate this cc: @frankiebee @koraykoska any initial thoughts?

Still looking into this

@safakoks Are you manually calling hexToNumber, or is Quorum calling it somewhere and you're just getting the error? Because hexToString exists and I think it would make sense to update your code to use it rather than updating hexToNumber to possibly return a string

Also maybe you could use hexToNumberString?

Doc link for hexToNumberString
Doc link for hexToString

@spacesailor24 I uses on('receipt') event during sending tx. I think it uses hexToNumber. But block.timestamp type is the main problem. Because the type of Quorum's block timestamp is different, the original hexToNumber cannot convert as I understand. I do not use the method independently.

Overriding hexToNumber was the solution before update web3 version from 1.2.11 to v1.3.0.rc.0

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions. If you believe this was a mistake, please comment.

Was this page helpful?
0 / 5 - 0 ratings