Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix isHex and isHexStrict for some edge cases and enrich their tests #5655

Merged

Conversation

Muhammad-Altabba
Copy link
Contributor

@Muhammad-Altabba Muhammad-Altabba commented Nov 25, 2022

Description

Fixes #5616 (in addition to the added comment #5616 (comment))

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist for 1.x:

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

Checklist for 4.x:

  • I have selected the correct 4.x base branch.
  • Within the description, the feature or issue is discussed in detail or an issue is linked.
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added any required tests for the changes I made
  • I have made corresponding changes to the documentation
  • Any dependent changes have been merged and published in downstream modules.
  • I ran yarn successfully
  • I ran yarn lint successfully
  • I ran yarn build:web successfully
  • I ran yarn test:unit successfully
  • I ran yarn test:integration successfully
  • I ran compile:contracts successfully
  • I have tested my code.
  • I have updated the corresponding CHANGELOG.md file in the packages I have edited.

@render
Copy link

render bot commented Nov 25, 2022

@Muhammad-Altabba Muhammad-Altabba changed the title [DRAFT] fix isHex and isHexStrict and modify test cases Fix isHex and isHexStrict and modify test cases Nov 28, 2022
@Muhammad-Altabba Muhammad-Altabba changed the title Fix isHex and isHexStrict and modify test cases Fix isHex and isHexStrict for some edge cases and enrich their tests Nov 28, 2022
@Muhammad-Altabba Muhammad-Altabba marked this pull request as ready for review November 28, 2022 18:09
Copy link
Contributor

@spacesailor24 spacesailor24 left a comment

Choose a reason for hiding this comment

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

In 1.x, 0x and -0x return true for both isHex and isHexStrict

image

image


'' also returns true for isHex in 1.x, I would consider making this breaking change (to return false instead), however I'm not sure how many of our users are relying on this behaviour

image

@spacesailor24
Copy link
Contributor

1.x also returns false for -123 as well...

image

but true for 123

image

@luu-alex luu-alex self-requested a review November 29, 2022 05:20
@luu-alex
Copy link
Contributor

I think it'll be fine to stray away from these 1.x changes, as these results are a bit misleading and include these changes in the breaking changes

@spacesailor24
Copy link
Contributor

@Muhammad-Altabba @luu-alex

These are the regex tests from 1.x:

isHexStrict: /^(-)?0x[0-9a-f]*$/i
isHex: ^(-0x|0x)?[0-9a-f]*$/i

Because of (-)?0x in isHexStrict check, I would assume that -123 being false and 123 being true for isHex is erroneous, and a breaking change would make sense (both should return true).


I'm not so sure we should make the breaking change for 0x and -0x, I think these should return true as this has the highest probability of breaking user code out of all the mentioned edge cases. As an example, even in our own code do we allow certain properties such as value, to, and input/data to be 0x:


Not that it matters too much, but this is what Ethers is returning

image

@luu-alex
Copy link
Contributor

@spacesailor24 agreed, I think 0x should be okay to include but -0x probably not. If we include these breaking changes apart of the 4.x utils migration doc so that theres awareness around this for the users.

@spacesailor24
Copy link
Contributor

@luu-alex Yea -0x doesn't make sense. Even if we include changes in the migration docs, it would still serve our community the best to make as little changes as possible, especially changes that might require users to make many changes across their projects (and imo, any breaking changes that involve transactions shouldn't be taken lightly)

@Muhammad-Altabba
Copy link
Contributor Author

Muhammad-Altabba commented Dec 2, 2022

@spacesailor24
Actually the returned value when passing 0x to isHex and isHexStrict was never mentioned to be changed in this MR. As there is no behavior change for this case. Additionally, there is a test case added to ensure this: https://github.com/web3/web3.js/pull/5655/files#diff-d8d5a378a9499635d2f6cca11a00d76024d6a3e6afde5a6db676f824563ed75dR135 And so there is no code that would be broken for this case 😄

The only additional changes in this MR (in addition to allow accepting -123) are to fix the issues mentioned in #5616 (comment)

  • both isHex and isHexStrict returns true for -0x, which does not make sense.
  • isHexreturns true for empty strings ''.

However, it seems that changing the scope of this issue created some confusion, sorry for that. And also seems to better be discussed separately. So, I will revert back to the original scope (allowing -123) and we could open a different issue to discuss the other points.


I would like also to comment on the outputs of ether.js.
It possibly returns false for -123 by mistake because it copied that behavior from this repository (v0.x and v1.x) as it is far older 😄 . And possibly it was kept like that as this function is mostly an internal function. And for that, there were no complains also on their or this repo from any user.

And for returning false for both 0x and 0x123. From some point of view, the hex number is an abstract form of number representation that could represent any data. And so even negative numbers could be represented by something like 0xf123 for example. But also there could be someone who could write -0x456 and expect it to be a valid negative hex.
And if out try to convert -123 to Hex at: https://www.rapidtables.com/convert/number/decimal-to-hex.html it would give different values for what they called Hex number and Hex signed 2'd complement`:

image

For this, I did not change the behavior of returning true for -0x123. But I probably understand why ether.js returns false for that.
However, I found returning true for -0x as non-sense. But I could be wrong. And I will leave this to a possible future discussion 😄

@Muhammad-Altabba Muhammad-Altabba marked this pull request as draft December 2, 2022 15:48
@spacesailor24
Copy link
Contributor

Okay, so I think the consensus is:

  • -0x returning false
  • '' returning false
  • 123 and -123 returning true

@luu-alex
Copy link
Contributor

luu-alex commented Dec 2, 2022

im good with that,

@Muhammad-Altabba
Copy link
Contributor Author

Okay, so I think the consensus is:

  • -0x returning false
  • '' returning false
  • 123 and -123 returning true

Thanks @spacesailor24 . This is great if we all agree on that. As this is what is included in this MR 👍 . And so, could you, please, approve this MR, if you do not have other concerns?
If you still have some douts. It would be OK and we can discuss them here or in another issue 😄

@Muhammad-Altabba Muhammad-Altabba marked this pull request as ready for review December 3, 2022 00:24
@Muhammad-Altabba
Copy link
Contributor Author

I think it'll be fine to stray away from these 1.x changes, as these results are a bit misleading and include these changes in the breaking changes

Thanks for the important suggestion.
Done.

@Muhammad-Altabba Muhammad-Altabba merged commit a37124b into 4.x Dec 5, 2022
@Muhammad-Altabba Muhammad-Altabba deleted the fix/5616/fix-isHex-and-isHexStrict-and-modify-test-cases branch December 5, 2022 15:52
jdevcs added a commit that referenced this pull request Dec 8, 2022
* Update CHANGELOG version headers

* Contract call with tuple is missing param names (#5613)

* call special output type

* fix

* fix: enable outputs to have param names (#5624)

* hot fix

* add type if only one param

* overloaded inputs types

* fix resolver tests

* add type tests

* simplify types

* revert registry unit test

* test firefox

* revert firefox test

* update changelogs

Co-authored-by: Marin Petrunić <mpetrunic@users.noreply.github.com>

* Fix `isHex` and `isHexStrict` for some edge cases and enrich their tests (#5655)

* Change `isHex` to return true for negative numbers (for example `-123`)
* Change both `isHex` and `isHexStrict` to return `false` for `-0x`
* Change `isHex` to return `false` for empty strings ''.

* Remove erroneous set provider code for Contract constructor (#5669)

* Remove erroneous set provider code for Contract constructor. Add Contract constructor provider set test

* Update CHANGELOGs

* Debugging failing integration tests

* Apply suggestions from code review

* Use getSystemTestProvider instead of hardcoded string

* Refactor ternaries in Contract constructor to if statements

* Correct CHANGELOG entries

* Remove unneeded checks in if statements

* Test with injected external providers (#5652)

* fix: sending tx with injected provider (#5651)

Co-authored-by: Marin Petrunic <marin.petrunic@gmail.com>

* adding a test for using `ganache` provider

* enable the jsonrpc `id` to optionally be incremented starting from a number
 (Inspired by: #5373 (comment) and needed as a work around for blockchainsllc/in3#46)

* test with `in3` as a provider & skip `in3` test if it takes too long

* increase integration test timeout at web3 package

* add a test for using `hardhat` provider

* improve how legacy providers, such as hardhat, is used

* implement `isPromise` that works in the browsers

* refactor external providers tests

* update CHANGELOG.md

* Use Error ABI to parse errors when sending a transaction (#5662)

* use Error ABI when sending a transaction

* update CHANGELOG.md for #5587

* Remove merge conflict marker

Co-authored-by: Oleksii Kosynskyi <oleksii.kosynskyi@gmail.com>
Co-authored-by: Marin Petrunić <mpetrunic@users.noreply.github.com>
Co-authored-by: Muhammad Altabba <24407834+Muhammad-Altabba@users.noreply.github.com>
Co-authored-by: jdevcs <junaid@chainsafe.io>
spacesailor24 added a commit that referenced this pull request Dec 8, 2022
* Version bump and build for v4.0.1-alpha.2

* Release/4.0.1 alpha.2 changelog update 3 (#5689)

* Update CHANGELOG version headers

* Update CHANGELOGs

* Revert package version bump for web3-packagetemplate

* Release/4.0.1 alpha.2 merge conflicts (#5692)

* Update CHANGELOG version headers

* Contract call with tuple is missing param names (#5613)

* call special output type

* fix

* fix: enable outputs to have param names (#5624)

* hot fix

* add type if only one param

* overloaded inputs types

* fix resolver tests

* add type tests

* simplify types

* revert registry unit test

* test firefox

* revert firefox test

* update changelogs

Co-authored-by: Marin Petrunić <mpetrunic@users.noreply.github.com>

* Fix `isHex` and `isHexStrict` for some edge cases and enrich their tests (#5655)

* Change `isHex` to return true for negative numbers (for example `-123`)
* Change both `isHex` and `isHexStrict` to return `false` for `-0x`
* Change `isHex` to return `false` for empty strings ''.

* Remove erroneous set provider code for Contract constructor (#5669)

* Remove erroneous set provider code for Contract constructor. Add Contract constructor provider set test

* Update CHANGELOGs

* Debugging failing integration tests

* Apply suggestions from code review

* Use getSystemTestProvider instead of hardcoded string

* Refactor ternaries in Contract constructor to if statements

* Correct CHANGELOG entries

* Remove unneeded checks in if statements

* Test with injected external providers (#5652)

* fix: sending tx with injected provider (#5651)

Co-authored-by: Marin Petrunic <marin.petrunic@gmail.com>

* adding a test for using `ganache` provider

* enable the jsonrpc `id` to optionally be incremented starting from a number
 (Inspired by: #5373 (comment) and needed as a work around for blockchainsllc/in3#46)

* test with `in3` as a provider & skip `in3` test if it takes too long

* increase integration test timeout at web3 package

* add a test for using `hardhat` provider

* improve how legacy providers, such as hardhat, is used

* implement `isPromise` that works in the browsers

* refactor external providers tests

* update CHANGELOG.md

* Use Error ABI to parse errors when sending a transaction (#5662)

* use Error ABI when sending a transaction

* update CHANGELOG.md for #5587

* Remove merge conflict marker

Co-authored-by: Oleksii Kosynskyi <oleksii.kosynskyi@gmail.com>
Co-authored-by: Marin Petrunić <mpetrunic@users.noreply.github.com>
Co-authored-by: Muhammad Altabba <24407834+Muhammad-Altabba@users.noreply.github.com>
Co-authored-by: jdevcs <junaid@chainsafe.io>

* Release patch/4.0.1 alpha.2/solve merge conflicts (#5697)

* Update CHANGELOG version headers

* Remove merge conflict markers

* Remove double CHANGELOG entry

Co-authored-by: Oleksii Kosynskyi <oleksii.kosynskyi@gmail.com>
Co-authored-by: Marin Petrunić <mpetrunic@users.noreply.github.com>
Co-authored-by: Muhammad Altabba <24407834+Muhammad-Altabba@users.noreply.github.com>
Co-authored-by: jdevcs <junaid@chainsafe.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants