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

Cleaner genesis state population #11809

Merged
merged 23 commits into from
Dec 22, 2022
Merged

Conversation

kasey
Copy link
Contributor

@kasey kasey commented Dec 22, 2022

What type of PR is this?

Other

What does this PR do? Why is it needed?

#11746 introduced some super nasty copypasta for populating BeaconState values in order to generate the Bellatrix genesis state. This PR cleans that up, adding a new beacon state generator that attempts to reduce boilerplate across forks by using the state.BeaconState setters.

Other notes for review
Shout out to @rkapka for all the hard work on native beacon state and moving everything to using the interface - this wouldn't be possible without all that effort.

@kasey kasey requested a review from a team as a code owner December 22, 2022 01:53
FinalizedCheckpoint: &ethpb.Checkpoint{
Epoch: 0,
Root: params.BeaconConfig().ZeroHash[:],
},
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I stopped using this function in favor of the .empty method on the premineGenesisConfig type, so I can revert this change, but it is probably a good idea to initialize these empty values.

@@ -227,6 +227,8 @@ type WriteOnlyCheckpoint interface {
type WriteOnlyAttestations interface {
AppendCurrentEpochAttestations(val *ethpb.PendingAttestation) error
AppendPreviousEpochAttestations(val *ethpb.PendingAttestation) error
SetPreviousEpochAttestations([]*ethpb.PendingAttestation) error
SetCurrentEpochAttestations([]*ethpb.PendingAttestation) error
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added these setters so that the .empty method of premineGenesisConfig could purely use the state.BeaconState interface to prepare the BeaconState.

}

b.previousEpochAttestations = al
b.markFieldAsDirty(nativetypes.PreviousEpochAttestations)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wasn't sure if I also need to call addDirtyIndices like the Append methods do?

t := e2e.TestParams.CLGenesisTime
nvals := params.BeaconConfig().MinGenesisActiveValidatorCount
version := e2etypes.GenesisFork()
return spectypes.NewPreminedGenesis(ctx, t, nvals, version, gb)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

NewPreminedGenesis does most of the heavy lifting now so this component gets much simpler.

@@ -0,0 +1,28 @@
load("@prysm//tools/go:def.bzl", "go_library")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I felt a little meh about the spectypes package name, open to suggestions.

Copy link
Member

Choose a reason for hiding this comment

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

Curious, is there any reason the core package couldn't be used ? A lot of the previous state related functions were added in there in your previous PRs

Copy link
Contributor Author

Choose a reason for hiding this comment

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

An out-of-band discussion led us to moving this file to testing/utils for now, because a number of similar "populate a struct for testing" functions live there.

var body rooter
switch s.Version {
case version.Phase0:
body = &ethpb.BeaconBlockBody{
Copy link
Contributor Author

Choose a reason for hiding this comment

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

maybe it would be nice to factor this into a method that NewGenesisBlockForState could share. Note the only difference is that NewGenesisBlockForState wants to actually set the StateRoot attribute in the body, but it could do that after calling the shared method. I didn't look to see if the beacon block interface has comprehensive setters like BeaconState does. If so I could rewrite this to use the interface setters as well.

if err != nil {
return err
}
wh, err := blocks.WrappedExecutionPayloadHeader(eph)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The back and forth between wrapped and concrete types is rather awkward. In particular I wondered why PayloadToHeader gives the concrete type rather than interfaces.ExecutionData? Maybe obvious if I look at how it's used elsewhere but I haven't done that yet.

}

// NewPreminedGenesis creates a genesis BeaconState at the given fork version, suitable for using as an e2e genesis.
func NewPreminedGenesis(ctx context.Context, t, nvals uint64, version int, gb *types.Block) (state.BeaconState, error) {
Copy link
Member

Choose a reason for hiding this comment

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

I am a bit surprised by the location of this file. Is there any reason it is in our spectests folder ? Just realised its a new package

@@ -0,0 +1,28 @@
load("@prysm//tools/go:def.bzl", "go_library")
Copy link
Member

Choose a reason for hiding this comment

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

Curious, is there any reason the core package couldn't be used ? A lot of the previous state related functions were added in there in your previous PRs

@prylabs-bulldozer prylabs-bulldozer bot merged commit 77a9dca into develop Dec 22, 2022
@delete-merged-branch delete-merged-branch bot deleted the cleaner-genesis-premine branch December 22, 2022 05:38
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