Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Conversation

@nikoulai
Copy link
Contributor

Description

#5153

Type of change

Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist:

  • 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.

@render
Copy link

render bot commented Jun 22, 2022

@nikoulai nikoulai force-pushed the 5153/Number-can-only-safely-store-53-bits branch from c5925f2 to 2970891 Compare June 22, 2022 12:28
@nikoulai nikoulai changed the title In hexToNumber return BigInt if result is bigger than max integer hexToNumber: return BigInt if result is bigger than max integer Jun 22, 2022
@nikoulai nikoulai force-pushed the 5153/Number-can-only-safely-store-53-bits branch from 2970891 to 45534f7 Compare June 22, 2022 12:37
@coveralls
Copy link

coveralls commented Jun 22, 2022

Pull Request Test Coverage Report for Build 2609049428

  • 11 of 11 (100.0%) changed or added relevant lines in 1 file are covered.
  • 282 unchanged lines in 9 files lost coverage.
  • Overall coverage increased (+2.3%) to 74.331%

Files with Coverage Reduction New Missed Lines %
packages/web3-core-requestmanager/src/jsonrpc.js 1 70.0%
packages/web3-core-method/lib/index.js 5 93.79%
packages/web3-core-helpers/src/formatters.js 8 84.3%
packages/web3-core-helpers/lib/formatters.js 10 81.58%
packages/web3-core-helpers/src/errors.js 29 1.56%
packages/web3-utils/src/soliditySha3.js 34 3.43%
packages/web3-utils/src/index.js 43 31.38%
packages/web3-utils/src/utils.js 45 10.67%
packages/web3-eth-accounts/src/index.js 107 23.67%
Totals Coverage Status
Change from base Build 2596319045: 2.3%
Covered Lines: 3261
Relevant Lines: 4137

💛 - Coveralls

@nikoulai nikoulai force-pushed the 5153/Number-can-only-safely-store-53-bits branch from b78aa2e to 7bc0db2 Compare June 22, 2022 15:56
@nikoulai
Copy link
Contributor Author

I changed hexToNumber to return BigNumber if the value is greater than max_sage_in. The reason is not to introduce a breaking change to the application already using the functionality.

// { value: 'myString 34534!', expected: '0x6d79537472696e6720333435333421', error: true, errorMessage: 'Number can only safely store up to 53 bits'},
{ value: new BN(15), expected: 15 },
{ value: new BigNumber(15), expected: 15 },
// { value: 'Heeäööä👅D34ɝɣ24Єͽ-.,äü+#/', expected: '0x486565c3a4c3b6c3b6c3a4f09f9185443334c99dc9a33234d084cdbd2d2e2cc3a4c3bc2b232f', error: true, errorMessage: 'Number can only safely store up to 53 bits'},
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These tests are no longer errors because toHex is called firstly and a valid input is formed. They were failing due to being too big. Are they valid inputs, though?

@nikoulai nikoulai self-assigned this Jun 22, 2022
@nikoulai nikoulai added the 1.x 1.0 related issues label Jun 22, 2022
@nikoulai nikoulai linked an issue Jun 22, 2022 that may be closed by this pull request
1 task
Copy link
Contributor

@nazarhussain nazarhussain left a comment

Choose a reason for hiding this comment

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

Please remove the unnecessary and white space changes so we can focus on what the issue was.

@nikoulai nikoulai force-pushed the 5153/Number-can-only-safely-store-53-bits branch from 7bc0db2 to 7910356 Compare June 23, 2022 09:22
@nikoulai nikoulai requested review from nazarhussain June 23, 2022 15:31
Comment on lines 237 to 246
const [negative, hexValue] = String(value).startsWith("-")
? [true, value.slice(1)]
: [false, value];
const num = toBN(hexValue);

if (num > Number.MAX_SAFE_INTEGER) {
return negative ? -num : num;
}

return negative ? -1 * num.toNumber() : num.toNumber();
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems to be a problem with the .toNumber(), so we need to focus on it.

const num = toBN(hexValue);

if (num > Number.MAX_SAFE_INTEGER) {
return negative ? -num : num;
Copy link
Contributor

Choose a reason for hiding this comment

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

It will return an integer if negative and a BN object if positive.

Copy link
Contributor

@nazarhussain nazarhussain left a comment

Choose a reason for hiding this comment

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

@nikoulai Did you get a chance to look at the feedback.

@nikoulai
Copy link
Contributor Author

@nazarhussain
I tried the suggested solution, but, in big value tests ("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" etc) the result is truncated (1.157920892373162e+77) due to the multiplication.

Copy link
Contributor

@jdevcs jdevcs left a comment

Choose a reason for hiding this comment

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

It will be breaking change in 1.x as discussed in team, so we should try to avoid that where possible for semver.
I think more good approach can be to use an optional output formatting param ( hex formatting ) in function and in case user explicitly wants to have hex it should not do formatting of that field to number like https://github.com/ChainSafe/web3.js/blob/aae9d4a9a4ab6d68296f0cc1a2a29540eb3ff433/packages/web3-core-helpers/src/formatters.js#L292 . If user wants to continue output formatted to number we can show a warning. It will be backward compatible in this way, and we will be in compliance with semver.

@nikoulai
Copy link
Contributor Author

nikoulai commented Jul 1, 2022

Todo: I have to update docs if change is ok

1 similar comment
@nikoulai
Copy link
Contributor Author

nikoulai commented Jul 1, 2022

Todo: I have to update docs if change is ok

Copy link
Contributor

@jdevcs jdevcs left a comment

Choose a reason for hiding this comment

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

LGTM, @nikoulai

  • yes we need to mention this in doc and
  • also could you remove new changes (space etc ) from changelog

@nikoulai nikoulai force-pushed the 5153/Number-can-only-safely-store-53-bits branch from b8b6ef6 to 5c0a7ce Compare July 4, 2022 09:15
@nikoulai nikoulai requested a review from jdevcs July 4, 2022 10:37
@nikoulai nikoulai merged commit 9e0d9d1 into 1.x Jul 4, 2022
@nikoulai nikoulai deleted the 5153/Number-can-only-safely-store-53-bits branch July 4, 2022 10:49
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

1.x 1.0 related issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

getTransactionReceipt: Number can only safely store up to 53 bits

6 participants