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

Proposal: generic uint_to_bytes #1935

Merged
merged 4 commits into from
Jul 21, 2020
Merged

Proposal: generic uint_to_bytes #1935

merged 4 commits into from
Jul 21, 2020

Conversation

hwwhww
Copy link
Contributor

@hwwhww hwwhww commented Jun 25, 2020

Issue

An alternative to #1746 int_to_bytes and compute_shuffled_index part.

  1. [Typing presentation bug] Status-quo int_to_bytes expects uint64 as the input. But actually we were passing Python int to it in some cases.
  2. Part of Apply strict uint64 casting #1746. Open a new PR to discuss this phase 0 change proposal for decreasing ambiguity. IMHO this PR is neater than Apply strict uint64 casting #1746's casting.

Proposed solution

  1. To make it more explicit, rename int_to_bytes to uint_to_bytes and enforce the input of uint_to_bytes to be SSZ uint type.
  2. Encode the uint object by its length in bytes. i.e., encode uint8 to 1-byte output and uint64 to 8-byte output.
  3. To avoid exposing SSZ APIs, describe uint_to_bytes in prose instead of in code. Similar to hash_tree_root.
  4. To make it more explicit, rename bytes_to_int to bytes_to_uint64.

Note: It's a refactoring change of the current spec and it should not change the consensus logic.

Rename `bytes_to_int` to `bytes_to_uint64`

Use `encode_bytes` to implement `int_to_bytes`

Rename `int_to_bytes` to `uint_to_bytes` and move it to `ssz_impl.py`
@hwwhww hwwhww added general:RFC Request for Comments phase0 labels Jun 25, 2020
@hwwhww hwwhww marked this pull request as ready for review June 25, 2020 12:38
Copy link
Contributor

@djrtwo djrtwo left a comment

Choose a reason for hiding this comment

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

I like this approach

source = hash(seed + int_to_bytes(current_round, length=1) + int_to_bytes(position // 256, length=4))
source = hash(
seed
+ uint_to_bytes(current_round)
Copy link
Contributor

Choose a reason for hiding this comment

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

Doing an explicit cast to uint8 right here might result in better readability and fewer client errors

Copy link
Contributor

Choose a reason for hiding this comment

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

mainly because current_round being 8-bit is an exception to our general 64-bit rule and easy to overlook

Copy link
Contributor

Choose a reason for hiding this comment

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

open to other's thoughts on this

@protolambda
Copy link
Contributor

Please review the type-infer branch (based on #1746), it handles many of the more "obvious" type inferences, to keep the spec clean. For compute_shuffled_index it doesn't involve as many casts anymore, by following a simple set of rules to keep the output size the same as the input size. And it fixes + catches new things.

hwwhww and others added 2 commits June 29, 2020 12:33
@protolambda protolambda added the general:typing Spec typing, no breaking changes label Jul 8, 2020
@hwwhww hwwhww merged commit f7c7c63 into dev Jul 21, 2020
@hwwhww hwwhww deleted the hwwhww/new_int_to_bytes_dev branch July 21, 2020 15:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
general:RFC Request for Comments general:typing Spec typing, no breaking changes phase0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants