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
Show file tree
Hide file tree
Changes from 22 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions beacon-chain/core/altair/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,7 @@ go_library(
"upgrade.go",
],
importpath = "github.com/prysmaticlabs/prysm/v3/beacon-chain/core/altair",
visibility = [
"//beacon-chain:__subpackages__",
"//cmd/prysmctl/testnet:__pkg__",
"//testing/endtoend/evaluators:__subpackages__",
"//testing/spectest:__subpackages__",
"//testing/util:__pkg__",
"//validator/client:__pkg__",
],
visibility = ["//visibility:public"],
deps = [
"//beacon-chain/core/blocks:go_default_library",
"//beacon-chain/core/epoch:go_default_library",
Expand Down
22 changes: 21 additions & 1 deletion beacon-chain/core/blocks/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func NewGenesisBlockForState(ctx context.Context, st state.BeaconState) (interfa
}
ps := st.ToProto()
switch ps.(type) {
case *ethpb.BeaconState, *ethpb.BeaconStateAltair:
case *ethpb.BeaconState:
return blocks.NewSignedBeaconBlock(&ethpb.SignedBeaconBlock{
Block: &ethpb.BeaconBlock{
ParentRoot: params.BeaconConfig().ZeroHash[:],
Expand All @@ -62,6 +62,26 @@ func NewGenesisBlockForState(ctx context.Context, st state.BeaconState) (interfa
},
Signature: params.BeaconConfig().EmptySignature[:],
})
case *ethpb.BeaconStateAltair:
return blocks.NewSignedBeaconBlock(&ethpb.SignedBeaconBlockAltair{
Block: &ethpb.BeaconBlockAltair{
ParentRoot: params.BeaconConfig().ZeroHash[:],
StateRoot: root[:],
Body: &ethpb.BeaconBlockBodyAltair{
RandaoReveal: make([]byte, fieldparams.BLSSignatureLength),
Eth1Data: &ethpb.Eth1Data{
DepositRoot: make([]byte, 32),
BlockHash: make([]byte, 32),
},
Graffiti: make([]byte, 32),
SyncAggregate: &ethpb.SyncAggregate{
SyncCommitteeBits: make([]byte, fieldparams.SyncCommitteeLength/8),
SyncCommitteeSignature: make([]byte, fieldparams.BLSSignatureLength),
},
},
},
Signature: params.BeaconConfig().EmptySignature[:],
})
case *ethpb.BeaconStateBellatrix:
return blocks.NewSignedBeaconBlock(&ethpb.SignedBeaconBlockBellatrix{
Block: &ethpb.BeaconBlockBellatrix{
Expand Down
11 changes: 1 addition & 10 deletions beacon-chain/core/transition/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,7 @@ go_library(
"transition_no_verify_sig.go",
],
importpath = "github.com/prysmaticlabs/prysm/v3/beacon-chain/core/transition",
visibility = [
"//beacon-chain:__subpackages__",
"//runtime/interop:__pkg__",
"//testing/endtoend:__pkg__",
"//testing/spectest:__subpackages__",
"//testing/util:__pkg__",
"//tools/benchmark-files-gen:__pkg__",
"//tools/genesis-state-gen:__pkg__",
"//tools/pcli:__pkg__",
],
visibility = ["//visibility:public"],
deps = [
"//beacon-chain/cache:go_default_library",
"//beacon-chain/core/altair:go_default_library",
Expand Down
2 changes: 2 additions & 0 deletions beacon-chain/state/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.

RotateAttestations() error
}

Expand Down
22 changes: 22 additions & 0 deletions beacon-chain/state/state-native/setters_attestation.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,25 @@ func (b *BeaconState) AppendPreviousEpochAttestations(val *ethpb.PendingAttestat
b.addDirtyIndices(nativetypes.PreviousEpochAttestations, []uint64{uint64(len(b.previousEpochAttestations) - 1)})
return nil
}

func (b *BeaconState) SetPreviousEpochAttestations(a []*ethpb.PendingAttestation) error {
b.lock.Lock()
defer b.lock.Unlock()

if b.version != version.Phase0 {
return errNotSupported("SetPreviousEpochAttestations", b.version)
}
b.setPreviousEpochAttestations(a)
return nil
}

func (b *BeaconState) SetCurrentEpochAttestations(a []*ethpb.PendingAttestation) error {
b.lock.Lock()
defer b.lock.Unlock()

if b.version != version.Phase0 {
return errNotSupported("SetCurrentEpochAttestations", b.version)
}
b.setCurrentEpochAttestations(a)
return nil
}
9 changes: 1 addition & 8 deletions testing/endtoend/components/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ go_library(
srcs = [
"beacon_node.go",
"boot_node.go",
"genesis.go",
"lighthouse_beacon.go",
"lighthouse_validator.go",
"log.go",
Expand All @@ -22,7 +21,6 @@ go_library(
visibility = ["//testing/endtoend:__subpackages__"],
deps = [
"//beacon-chain/state:go_default_library",
"//beacon-chain/state/state-native:go_default_library",
"//cmd:go_default_library",
"//cmd/beacon-chain/flags:go_default_library",
"//cmd/beacon-chain/sync/genesis:go_default_library",
Expand All @@ -31,21 +29,16 @@ go_library(
"//config/fieldparams:go_default_library",
"//config/params:go_default_library",
"//config/validator/service:go_default_library",
"//container/trie:go_default_library",
"//crypto/bls:go_default_library",
"//encoding/bytesutil:go_default_library",
"//io/file:go_default_library",
"//proto/engine/v1:go_default_library",
"//proto/prysm/v1alpha1:go_default_library",
"//runtime/interop:go_default_library",
"//runtime/version:go_default_library",
"//testing/endtoend/helpers:go_default_library",
"//testing/endtoend/params:go_default_library",
"//testing/endtoend/types:go_default_library",
"//testing/spectypes:go_default_library",
"//validator/keymanager:go_default_library",
"@com_github_ethereum_go_ethereum//common:go_default_library",
"@com_github_ethereum_go_ethereum//common/hexutil:go_default_library",
"@com_github_ethereum_go_ethereum//core/types:go_default_library",
"@com_github_google_uuid//:go_default_library",
"@com_github_pkg_errors//:go_default_library",
"@com_github_sirupsen_logrus//:go_default_library",
Expand Down
13 changes: 13 additions & 0 deletions testing/endtoend/components/beacon_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/bazelbuild/rules_go/go/tools/bazel"
"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/v3/beacon-chain/state"
cmdshared "github.com/prysmaticlabs/prysm/v3/cmd"
"github.com/prysmaticlabs/prysm/v3/cmd/beacon-chain/flags"
"github.com/prysmaticlabs/prysm/v3/cmd/beacon-chain/sync/genesis"
Expand All @@ -23,6 +24,7 @@ import (
"github.com/prysmaticlabs/prysm/v3/testing/endtoend/helpers"
e2e "github.com/prysmaticlabs/prysm/v3/testing/endtoend/params"
e2etypes "github.com/prysmaticlabs/prysm/v3/testing/endtoend/types"
"github.com/prysmaticlabs/prysm/v3/testing/spectypes"
)

var _ e2etypes.ComponentRunner = (*BeaconNode)(nil)
Expand Down Expand Up @@ -349,3 +351,14 @@ func (node *BeaconNode) Stop() error {
func (node *BeaconNode) UnderlyingProcess() *os.Process {
return node.cmd.Process
}

func generateGenesis(ctx context.Context) (state.BeaconState, error) {
if e2e.TestParams.Eth1GenesisBlock == nil {
return nil, errors.New("Cannot construct bellatrix block, e2e.TestParams.Eth1GenesisBlock == nil")
}
gb := e2e.TestParams.Eth1GenesisBlock
t := e2e.TestParams.CLGenesisTime
nvals := params.BeaconConfig().MinGenesisActiveValidatorCount
version := e2etypes.GenesisFork()
return spectypes.NewPreminedGenesis(ctx, t, nvals, version, gb)
}
83 changes: 0 additions & 83 deletions testing/endtoend/components/genesis.go

This file was deleted.

28 changes: 28 additions & 0 deletions testing/spectypes/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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.


go_library(
name = "go_default_library",
srcs = ["premine-state.go"],
importpath = "github.com/prysmaticlabs/prysm/v3/testing/spectypes",
visibility = ["//visibility:public"],
deps = [
"//beacon-chain/core/altair:go_default_library",
"//beacon-chain/core/blocks:go_default_library",
"//beacon-chain/core/helpers:go_default_library",
"//beacon-chain/state:go_default_library",
"//beacon-chain/state/state-native:go_default_library",
"//beacon-chain/state/stateutil:go_default_library",
"//config/fieldparams:go_default_library",
"//config/params:go_default_library",
"//consensus-types/blocks:go_default_library",
"//container/trie:go_default_library",
"//crypto/bls:go_default_library",
"//encoding/bytesutil:go_default_library",
"//proto/engine/v1:go_default_library",
"//proto/prysm/v1alpha1:go_default_library",
"//runtime/interop:go_default_library",
"//runtime/version:go_default_library",
"@com_github_ethereum_go_ethereum//core/types:go_default_library",
"@com_github_pkg_errors//:go_default_library",
],
)
Loading