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

Configurable testutil's BeaconState #8407

Merged
merged 7 commits into from
Feb 8, 2021

Conversation

rkapka
Copy link
Contributor

@rkapka rkapka commented Feb 8, 2021

What type of PR is this?

Testing

What does this PR do? Why is it needed?

Using NewBeaconState from shared/testutil is very unwelcoming when one wants to work with a state with non-default values. This is because each call to NewBeaconState initializes a new state from package-level "seed" data. Changing the seed data inside one test breaks other tests. This is bad as tests are not isolated from each other.

This PR decouples tests by creating new seed data every time NewBeaconState is called. The function allows passing one or more options which modify the desired state.

Which issues(s) does this PR fix?

Part of #7510. This PR is a prerequisite for some API unit tests.

Other notes for review

Main file to look at: shared/testutil/state.go. The ...options argument idea comes from https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis. The main benefit is not having to declare a big config object and pass nil values everywhere where the default is sufficient (meaning everywhere at this point).

Example usage:

st, err := NewBeaconState(func(state *pb.BeaconState) {
    state.Slot = 1234
})

Comment on lines +14 to +17
BlockRoots: filledByteSlice2D(params.MainnetConfig().SlotsPerHistoricalRoot, 32),
StateRoots: filledByteSlice2D(params.MainnetConfig().SlotsPerHistoricalRoot, 32),
Slashings: make([]uint64, params.MainnetConfig().EpochsPerSlashingsVector),
RandaoMixes: filledByteSlice2D(params.MainnetConfig().EpochsPerHistoricalVector, 32),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This changed from BeaconConfig to MainnetConfig. This is because minimal values cannot be encoded properly when saving to the database (I guess it's an ssz thing). If other values are really required, they can be passed in through ...options.

@rkapka rkapka marked this pull request as ready for review February 8, 2021 12:55
@rkapka rkapka requested a review from a team as a code owner February 8, 2021 12:55
@prylabs-bulldozer prylabs-bulldozer bot merged commit 86a9d4c into develop Feb 8, 2021
@delete-merged-branch delete-merged-branch bot deleted the configurable-test-beacon-state branch February 8, 2021 20:00
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.

2 participants