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 shuffle() function errors #65

Merged
merged 2 commits into from Oct 16, 2018

Conversation

Projects
None yet
4 participants
@paulhauner
Contributor

paulhauner commented Oct 11, 2018

I updated the shuffle() function as discussed in #55.

The following changes were implemented:

  1. sample_max was made distinct from rand_max.
  2. rand_max now represents the greatest value returned by the RNG
    instead of the length of the range. This is consistent with the cstdlib RAND_MAX constant.
  3. while condition fixed to stop infinite loop.
  4. Comments updated.

After fixing (2) I noticed that there was an error in generating sample_max caused by rand_max being too high. This affects the output of the shuffle. All clients should update their shuffler if this PR is accepted.

I have used the shuffling_sandbox to fuzz this code against my reference implementation.

Fix shuffle() function errors
 - `rand_max` now represents the greatest value returned by the RNG,
instead of the length of the range.
 - Loop condition fixed to stop infinite loop.
 - Comments updated
Show resolved Hide resolved specs/beacon-chain.md
Show resolved Hide resolved specs/beacon-chain.md
Show resolved Hide resolved specs/beacon-chain.md
Show resolved Hide resolved specs/beacon-chain.md
Show resolved Hide resolved specs/beacon-chain.md
@paulhauner

This comment has been minimized.

Contributor

paulhauner commented Oct 11, 2018

Thanks for the comments @JustinDrake!

I've added my responses and will await further direction from the repository maintainers.

@paulhauner

This comment has been minimized.

Contributor

paulhauner commented Oct 14, 2018

Thanks for the feedback. I will implement the following changes:

  1. Introduce a rand_bytes constant set to 3 and use it wherever possible.
  2. Set the range on the byte-sampling for loop to: range(0, 32 - (32 % rand_bytes), rand_bytes)
  3. Ensure all comments are on the line above to the statement (instead of inline).

I could use additional direction regarding assert values_count <= sample_max. I see @hwwhww suggested throwing a ValueError, is this how we should handle it?

@djrtwo

This comment has been minimized.

Contributor

djrtwo commented Oct 14, 2018

I'm cool with an assert in the spec code as long as there is a comment about why it is there and how it is up to a client on how to proceed. Similar to the commented assert in add_validator

If we want to remove asserts entirely (there are many), we can do it in a separate issue.

Update shuffle() as per review
- Add `rand_bytes`
- Change `for` loop condition for readability and generality.
- Ensure consistency of comment spacing
- Update comments
@paulhauner

This comment has been minimized.

Contributor

paulhauner commented Oct 16, 2018

All updates have been pushed :)

@djrtwo

This comment has been minimized.

Contributor

djrtwo commented Oct 16, 2018

lgtm!

@djrtwo djrtwo merged commit 1101244 into ethereum:master Oct 16, 2018

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