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

Incorrect usage of bytes constructor #15

Open
zilm13 opened this issue Nov 21, 2022 · 3 comments · May be fixed by #16
Open

Incorrect usage of bytes constructor #15

zilm13 opened this issue Nov 21, 2022 · 3 comments · May be fixed by #16

Comments

@zilm13
Copy link

zilm13 commented Nov 21, 2022

From the python spec:
A zero-filled bytes object of a specified length: bytes(10)
https://docs.python.org/3.10/library/stdtypes.html#binaryseq
So when initialized with int or something like 0x01 in https://github.com/protolambda/remerkleable/blob/master/remerkleable/byte_arrays.py#L28 you get 00-filled bytes of input length.
Broken usage is, for example, here: https://github.com/ethereum/consensus-specs/blame/dev/specs/eip4844/beacon-chain.md#L58

@zilm13
Copy link
Author

zilm13 commented Nov 21, 2022

I'm not sure, maybe consensus-specs input should be changed but it's easy repeatable error

@protolambda
Copy link
Owner

Nice catch! I think consensus-specs should specify it as string input for clarity, based on previous similar usage in the withdrawal prefix constants here: https://github.com/ethereum/consensus-specs/blob/dev/specs/phase0/beacon-chain.md#withdrawal-prefixes

That said, it's indeed an easy thing to get wrong, at least for the case when the value is equal to the bytevector length. (since it's checked here:

if len(out) != byte_len:

And for a ByteList it makes sense to just follow the bytes behavior. For the fixed-size ByteVector it doesn't make sense, as one should just call the constructor without arguments to get the default value. So to make it less error-prone, I think we should add a check to the ByteVector constructor, before calling the super constructor, like if len(args) == 1 and isinstance(args[0], int): raise Exception(f"cannot init fixed-length ByteVector[{byte_len}] with dynamic-length {args[0]}")

What do you think?

@zilm13
Copy link
Author

zilm13 commented Nov 21, 2022

I agree with you as it's better to throw an exception as it could be not one byte integer but any size and even if we do conversion back it will be useless two-way conversion with new potential for error by filling leading zeros for example. I think we should go exception way

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 a pull request may close this issue.

2 participants