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: toHex not supporting Uint8Array #6958

Merged
merged 2 commits into from
Apr 15, 2024
Merged

fix: toHex not supporting Uint8Array #6958

merged 2 commits into from
Apr 15, 2024

Conversation

mpetrunic
Copy link
Contributor

@mpetrunic mpetrunic commented Apr 5, 2024

Description

Please include a summary of the changes and be sure to follow our Contribution Guidelines.

Fixes #6957

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:

  • 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 lint with success and extended the tests and types if necessary.
  • I ran npm run test:unit with success.
  • I ran npm run test:coverage and my test cases cover all the lines and branches of the added code.
  • I ran npm run build and tested 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.
  • I have linked Issue(s) with this PR in "Linked Issues" menu.

Signed-off-by: Marin Petrunic <marin.petrunic@gmail.com>
Copy link

github-actions bot commented Apr 5, 2024

Bundle Stats

Hey there, this message comes from a GitHub action that helps you and reviewers to understand how these changes affect the size of this project's bundle.

As this PR is updated, I'll keep you updated on how the bundle size is impacted.

Total

Files count Total bundle size % Changed
10 619.77 kB → 620.74 kB (+994 B) +0.16%
Changeset
File Δ Size
../web3-eth-contract/lib/commonjs/contract.js 📈 +3.52 kB (+8.87%) 39.7 kB → 43.22 kB
../web3-eth-contract/lib/commonjs/encoding.js 📈 +504 B (+8.38%) 5.87 kB → 6.36 kB
../web3-utils/lib/commonjs/uint8array.js 📈 +143 B (+7.45%) 1.87 kB → 2.01 kB
../web3-validator/lib/commonjs/validation/bytes.js 📈 +135 B (+5.07%) 2.6 kB → 2.73 kB
../web3-utils/lib/commonjs/converters.js 📈 +128 B (+0.63%) 19.82 kB → 19.94 kB
View detailed bundle breakdown

Added

No assets were added

Removed

No assets were removed

Bigger

Asset File Size % Changed
web3.min.js 600.7 kB → 601.67 kB (+994 B) +0.16%

Smaller

No assets were smaller

Unchanged

Asset File Size % Changed
../lib/commonjs/index.d.ts 8.69 kB 0%
../lib/commonjs/accounts.d.ts 3.89 kB 0%
../lib/commonjs/types.d.ts 2.45 kB 0%
../lib/commonjs/web3.d.ts 1.35 kB 0%
../lib/commonjs/web3_eip6963.d.ts 1.2 kB 0%
../lib/commonjs/abi.d.ts 999 B 0%
../lib/commonjs/eth.exports.d.ts 280 B 0%
../lib/commonjs/providers.exports.d.ts 183 B 0%
../lib/commonjs/version.d.ts 60 B 0%

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Benchmark

Benchmark suite Current: d781ddc Previous: 6c075db Ratio
processingTx 9360 ops/sec (±3.90%) 9301 ops/sec (±4.81%) 0.99
processingContractDeploy 40647 ops/sec (±6.12%) 39129 ops/sec (±7.62%) 0.96
processingContractMethodSend 19181 ops/sec (±8.72%) 19443 ops/sec (±5.19%) 1.01
processingContractMethodCall 40264 ops/sec (±6.36%) 38971 ops/sec (±6.34%) 0.97
abiEncode 42691 ops/sec (±8.40%) 44252 ops/sec (±6.92%) 1.04
abiDecode 30239 ops/sec (±6.41%) 30419 ops/sec (±8.89%) 1.01
sign 1557 ops/sec (±3.87%) 1656 ops/sec (±4.08%) 1.06
verify 382 ops/sec (±0.61%) 373 ops/sec (±0.78%) 0.98

This comment was automatically generated by workflow using github-action-benchmark.

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.

need changelog update, Thanks

@@ -362,6 +362,10 @@ export const toHex = (
return returnType ? 'bigint' : numberToHex(value);
}

if(value instanceof Uint8Array) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you please use isUint8Array from web3-validator because instanceof is not reliable at some rare edge cases.

Suggested change
if(value instanceof Uint8Array) {
if( isUint8Array(value) ) {

Thanks,

Copy link
Contributor Author

Choose a reason for hiding this comment

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

isUint8Array implementation is incorrect as it returns false on buffer even though buffer is uint8array.

In what cases does instanceof not work?

Copy link
Contributor

Choose a reason for hiding this comment

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

isUint8Array checks for data.constructor.name === 'Uint8Array' in addition to checking the instanceof. So it is more inclusive than instanceof.

The edge case were instanceof fails was reported at our library at: #6393. And it was also reported to be unreliable at one of our dependencies, where they applied our suggestion (paulmillr/noble-hashes#25 (comment)) to fix it.

Copy link
Contributor Author

@mpetrunic mpetrunic Apr 8, 2024

Choose a reason for hiding this comment

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

🤦🏻‍♂️
image

--rant
Usually when people complain that `x instanceOf Uint8Array`, you are supposed to close the issue and tell them not to use a shitty engine that modifies globals (that also means not using jest+jsdom when you are developing a cross-platform library). 😅 

I'm really worried how everyone is ok with libraries and engines like jest/jsdome modifying globals and everybody else is applying hacks to make it work instead of urging people to not use projects like that
-- end rant

We cannot pretend Node.js doesn't exist and ignore Buffers so added commit which add support for Buffer in isUint8Array function and web3-validator.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for the modifications. It looks good now that is not solely depending on instanceof.
Thanks also for using the same data?.constructor?.name for Buffers.

A note and a question on the side: It would never be the intention to fix for jest/jsdom because nobody cares about it. And in the issue that was open at our library (#6393) the guy stated "I have a Hyperledger Besu blockchain working on my VM with 4 nodes, built with Qorum Quickstart". And the guy stated that he did not face the issue when he used ethers.js. Do you think that the issue that the guy reported was with jest/jsdom but falsely reported and we should had closed the issue stating "not to use a shitty engine that modifies globals"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hard to say. My assumption is that Besu is using some shitty js engine to execute his javascript script so he should've open issue on besu repo saying that their js execution illegally modifies globals.

I would call bullshit that it works with ethers because: https://github.com/ethers-io/ethers.js/blob/ad5f1c5fc7b73cfb20d9012b669e9dcb9122d244/src.ts/utils/data.ts#L29 😂 😂

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks @mpetrunic , And thanks for looking ethers code.

However, just for clarification, using instanceof is not reliable does not seem to be because of someone modifies globals. But because Uint8Array is "serializable, but not transferable" (#6393 (comment)).

And following the provided links in the issues leads to this article that mentions one of the cases for the failure of using instanceof when the guy is working on a browser extension. This is another example of the edge case that comes because of the nature of Uint8Array being "not transferable" as mozilla documentation stated.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But doesn't that just mean that somebody illegally transferred "non-transferable" Uint8Array and then it was surprised that intanceof didn't work. Solution would then be to transfer underlying ArrayBuffer and on destination create new Uint8Array(arrayBuffer...) after which instanceof would work?

Copy link
Contributor Author

@mpetrunic mpetrunic Apr 24, 2024

Choose a reason for hiding this comment

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

But for jsdom: jsdom/jsdom#1769

(for what it's worth happy-dom doesn't have those issues so if you are using vitest it's better to switch)

Signed-off-by: Marin Petrunic <marin.petrunic@gmail.com>
@luu-alex luu-alex merged commit 1c03666 into 4.x Apr 15, 2024
15 of 18 checks passed
@luu-alex luu-alex deleted the fix/toHexBugUtin8Array branch April 15, 2024 13:22
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.

toHex is not supporting passing Uint8Array
5 participants