-
Notifications
You must be signed in to change notification settings - Fork 5.1k
hexToNumber: return BigInt if result is bigger than max integer #5157
Conversation
|
Your Render PR Server URL is https://web3-js-pr-5157.onrender.com. Follow its progress at https://dashboard.render.com/static/srv-capgilirrk0fte5ckf4g. |
c5925f2 to
2970891
Compare
2970891 to
45534f7
Compare
Pull Request Test Coverage Report for Build 2609049428
💛 - Coveralls |
b78aa2e to
7bc0db2
Compare
|
I changed |
test/utils.toNumber.js
Outdated
| // { 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'}, |
There was a problem hiding this comment.
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?
nazarhussain
left a comment
There was a problem hiding this 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.
7bc0db2 to
7910356
Compare
packages/web3-utils/src/utils.js
Outdated
| 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(); |
There was a problem hiding this comment.
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.
packages/web3-utils/src/utils.js
Outdated
| const num = toBN(hexValue); | ||
|
|
||
| if (num > Number.MAX_SAFE_INTEGER) { | ||
| return negative ? -num : num; |
There was a problem hiding this comment.
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.
nazarhussain
left a comment
There was a problem hiding this 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.
|
@nazarhussain |
jdevcs
left a comment
There was a problem hiding this 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.
|
Todo: I have to update docs if change is ok |
1 similar comment
|
Todo: I have to update docs if change is ok |
jdevcs
left a comment
There was a problem hiding this 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
b8b6ef6 to
5c0a7ce
Compare
Description
#5153
Type of change
Breaking change (fix or feature that would cause existing functionality to not work as expected)
Checklist:
npm run dtslintwith success and extended the tests and types if necessary.npm run test:covand my test cases cover all the lines and branches of the added code.npm run buildwith success.dist/web3.min.jsin a browser.CHANGELOG.mdfile in the root folder.