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

feat(util): implement assertIsUint8Array #2271

Closed
wants to merge 1 commit into from
Closed

Conversation

faustbrian
Copy link
Contributor

No description provided.

@codecov
Copy link

codecov bot commented Sep 7, 2022

Codecov Report

Merging #2271 (93cbb51) into master (1e0de28) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

Flag Coverage Δ
block 92.89% <ø> (ø)
blockchain 88.47% <ø> (ø)
client 86.94% <ø> (ø)
common 98.09% <ø> (ø)
devp2p 92.56% <ø> (+0.10%) ⬆️
ethash ∅ <ø> (∅)
evm 79.25% <ø> (ø)
rlp ∅ <ø> (?)
statemanager 88.47% <ø> (ø)
trie 90.32% <ø> (-0.05%) ⬇️
tx 97.98% <ø> (ø)
util 92.11% <100.00%> (-0.21%) ⬇️
vm 85.31% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

@holgerd77
Copy link
Member

I don't think this is a safe thing to do since one can't anticipate all the use cases here. If this is used for specifically checking for a Buffer on a value and after that Buffer-specific functionality is called things would break.

So my current tendency is to not merge this in.

We can work our way through the libraries and see where we can take in Uint8Array instead of Buffer in a first round for feature functionality methods though without side effects, at least that would be my first-thought suggestion where it would be possible to start on this a bit.

@faustbrian
Copy link
Contributor Author

faustbrian commented Sep 7, 2022

There's virtually no Buffer-specific functionality used in this repository (or it's dependencies that rely on Buffer) and you would have to really go out of your way to even find functionality that Buffer offers but Uint8Array doesn't.

I ran into this while trying to use the util package with a Uint8Array instead of Buffer which then threw an exception while the functionality works just fine with Uint8Array (like everything else in this repository)

@holgerd77
Copy link
Member

Can we then instead just add another method assertIsUint8Array() and switch to that where it is possible? I guess I just don't like the fact that the function after this change just doesn't do any more what it claims to do "by name".

Just searched, this method is only used within Util it seems anyhow (doesn't mean we can delete though).

@faustbrian faustbrian changed the title refactor(util): treat Uint8Array as Buffer feat(util): implement assertIsUint8Array Sep 7, 2022
@faustbrian faustbrian marked this pull request as draft September 7, 2022 10:58
export const generateAddress = function (from: Buffer, nonce: Buffer): Buffer {
assertIsBuffer(from)
assertIsBuffer(nonce)
export const generateAddress = function (from: Buffer, nonce: Uint8Array): Buffer {
Copy link
Member

Choose a reason for hiding this comment

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

from forgotten

@@ -77,7 +77,7 @@ export class Address {
* @param salt A salt
* @param initCode The init code of the contract being created
*/
static generate2(from: Address, salt: Buffer, initCode: Buffer): Address {
static generate2(from: Address, salt: Buffer, initCode: Uint8Array): Address {
Copy link
Member

Choose a reason for hiding this comment

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

salt

@@ -107,8 +106,8 @@ const stripZeros = function (a: any): Buffer | number[] | string {
* @param a (Buffer)
* @return (Buffer)
*/
export const unpadBuffer = function (a: Buffer): Buffer {
assertIsBuffer(a)
export const unpadBuffer = function (a: Uint8Array): Buffer {
Copy link
Member

Choose a reason for hiding this comment

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

I have again this naming problem here. Additionally I don't think it's a good thing for conversion functions to change the type in between, so here, to then have Uint8Array as input and return a Buffer. This is also hard to deprecate at some point.

I wouldn't want to do too quick decisions on these kind of things, since implications on how we can deal with this later on can be significant.

So I think if we do we should likely go the a bit harder way and also add here parallel unpadUint8Array() and the like functionality and then switch over one-by-one at some point deprecate (in the sense of: write a deprecation note, so not directly remove) the Buffer functions (if we decide that we want that. We can also have both in parallel).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can also have both in parallel.

There's no reason to support both because a Buffer is literally Uint8Array, see https://nodejs.org/api/buffer.html. It is also less buggy and more consistent in its behaviour. Also see nodejs/node#41588 (comment) by @paulmillr

The Buffer class is a subclass of JavaScript's Uint8Array class and extends it with methods that cover additional use cases. Node.js APIs accept plain Uint8Arrays wherever Buffers are supported as well.

@holgerd77
Copy link
Member

Generally, do not give too much expectations on having this merged in soon. We have spent months and months on these Buffer vs Uint8Array discussions.

If we want to pick this up again I want to make sure that we have a gentle, consistent and non-breaking strategy (optimally written down as an issue or something) throughout the whole monorepo before we act upon this, especially so close after the breaking releases.

@faustbrian
Copy link
Contributor Author

This would only be a breaking change if you change return types from Buffer to Uint8Array but if only inputs are changed then TypeScript won't even throw any errors because a Buffer is Uint8Array.

@paulmillr
Copy link
Member

paulmillr commented Sep 7, 2022

Thoughts on renaming to assertIsUi8a?

Copy link
Member

@jochem-brouwer jochem-brouwer left a comment

Choose a reason for hiding this comment

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

General Q just to confirm: all Buffers are Uint8Arrays but not all Uint8Arrays are Buffers?

@faustbrian
Copy link
Contributor Author

faustbrian commented Sep 8, 2022

Buffer is basically Buffer extends Uint8Array with some features and buggy behaviours you probably have never heard of or used and never will. Wherever you hint Uint8Array you can pass in Buffer and in 99.9% of cases it will just work unless you some obtuse functionality of Buffer.

CleanShot 2022-09-08 at 16 27 25@2x

See https://nodejs.org/api/buffer.html

@paulmillr
Copy link
Member

Note: Buffer.slice is not compatible with Uint8array.slice — first returns original memory, second copies it.

@faustbrian faustbrian closed this Oct 15, 2022
@holgerd77
Copy link
Member

Find it a bit of a pity that this has been closed. Maybe we can revive parts of it at some point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants