mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-09 15:37:56 -05:00
* Ran gopls modernize to fix everything go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix -test ./... * Override rules_go provided dependency for golang.org/x/tools to v0.38.0. To update this, checked out rules_go, then ran `bazel run //go/tools/releaser -- upgrade-dep -mirror=false org_golang_x_tools` and copied the patches. * Fix buildtag violations and ignore buildtag violations in external * Introduce modernize analyzer package. * Add modernize "any" analyzer. * Fix violations of any analyzer * Add modernize "appendclipped" analyzer. * Fix violations of appendclipped * Add modernize "bloop" analyzer. * Add modernize "fmtappendf" analyzer. * Add modernize "forvar" analyzer. * Add modernize "mapsloop" analyzer. * Add modernize "minmax" analyzer. * Fix violations of minmax analyzer * Add modernize "omitzero" analyzer. * Add modernize "rangeint" analyzer. * Fix violations of rangeint. * Add modernize "reflecttypefor" analyzer. * Fix violations of reflecttypefor analyzer. * Add modernize "slicescontains" analyzer. * Add modernize "slicessort" analyzer. * Add modernize "slicesdelete" analyzer. This is disabled by default for now. See https://go.dev/issue/73686. * Add modernize "stringscutprefix" analyzer. * Add modernize "stringsbuilder" analyzer. * Fix violations of stringsbuilder analyzer. * Add modernize "stringsseq" analyzer. * Add modernize "testingcontext" analyzer. * Add modernize "waitgroup" analyzer. * Changelog fragment * gofmt * gazelle * Add modernize "newexpr" analyzer. * Disable newexpr until go1.26 * Add more details in WORKSPACE on how to update the override * @nalepae feedback on min() * gofmt * Fix violations of forvar
266 lines
8.2 KiB
Go
266 lines
8.2 KiB
Go
package util
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/OffchainLabs/prysm/v7/beacon-chain/core/helpers"
|
|
"github.com/OffchainLabs/prysm/v7/beacon-chain/state"
|
|
state_native "github.com/OffchainLabs/prysm/v7/beacon-chain/state/state-native"
|
|
"github.com/OffchainLabs/prysm/v7/beacon-chain/state/stateutil"
|
|
fieldparams "github.com/OffchainLabs/prysm/v7/config/fieldparams"
|
|
"github.com/OffchainLabs/prysm/v7/config/params"
|
|
"github.com/OffchainLabs/prysm/v7/crypto/bls"
|
|
"github.com/OffchainLabs/prysm/v7/encoding/bytesutil"
|
|
enginev1 "github.com/OffchainLabs/prysm/v7/proto/engine/v1"
|
|
ethpb "github.com/OffchainLabs/prysm/v7/proto/prysm/v1alpha1"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
// DeterministicGenesisStateBellatrix returns a genesis state in Bellatrix format made using the deterministic deposits.
|
|
func DeterministicGenesisStateBellatrix(t testing.TB, numValidators uint64) (state.BeaconState, []bls.SecretKey) {
|
|
deposits, privKeys, err := DeterministicDepositsAndKeys(numValidators)
|
|
if err != nil {
|
|
t.Fatal(errors.Wrapf(err, "failed to get %d deposits", numValidators))
|
|
}
|
|
eth1Data, err := DeterministicEth1Data(len(deposits))
|
|
if err != nil {
|
|
t.Fatal(errors.Wrapf(err, "failed to get eth1data for %d deposits", numValidators))
|
|
}
|
|
beaconState, err := genesisBeaconStateBellatrix(t.Context(), deposits, time.Unix(0, 0), eth1Data)
|
|
if err != nil {
|
|
t.Fatal(errors.Wrapf(err, "failed to get genesis beacon state of %d validators", numValidators))
|
|
}
|
|
resetCache()
|
|
return beaconState, privKeys
|
|
}
|
|
|
|
// genesisBeaconStateBellatrix returns the genesis beacon state.
|
|
func genesisBeaconStateBellatrix(ctx context.Context, deposits []*ethpb.Deposit, genesisTime time.Time, eth1Data *ethpb.Eth1Data) (state.BeaconState, error) {
|
|
st, err := emptyGenesisStateBellatrix()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Process initial deposits.
|
|
st, err = helpers.UpdateGenesisEth1Data(st, deposits, eth1Data)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
st, err = processPreGenesisDeposits(ctx, st, deposits)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "could not process validator deposits")
|
|
}
|
|
|
|
return buildGenesisBeaconStateBellatrix(genesisTime, st, st.Eth1Data())
|
|
}
|
|
|
|
// emptyGenesisStateBellatrix returns an empty genesis state in Bellatrix format.
|
|
func emptyGenesisStateBellatrix() (state.BeaconState, error) {
|
|
st := ðpb.BeaconStateBellatrix{
|
|
// Misc fields.
|
|
Slot: 0,
|
|
Fork: ðpb.Fork{
|
|
PreviousVersion: params.BeaconConfig().AltairForkVersion,
|
|
CurrentVersion: params.BeaconConfig().BellatrixForkVersion,
|
|
Epoch: 0,
|
|
},
|
|
// Validator registry fields.
|
|
Validators: []*ethpb.Validator{},
|
|
Balances: []uint64{},
|
|
InactivityScores: []uint64{},
|
|
|
|
JustificationBits: []byte{0},
|
|
HistoricalRoots: [][]byte{},
|
|
CurrentEpochParticipation: []byte{},
|
|
PreviousEpochParticipation: []byte{},
|
|
|
|
// Eth1 data.
|
|
Eth1Data: ðpb.Eth1Data{},
|
|
Eth1DataVotes: []*ethpb.Eth1Data{},
|
|
Eth1DepositIndex: 0,
|
|
|
|
LatestExecutionPayloadHeader: &enginev1.ExecutionPayloadHeader{},
|
|
}
|
|
return state_native.InitializeFromProtoBellatrix(st)
|
|
}
|
|
|
|
func buildGenesisBeaconStateBellatrix(genesisTime time.Time, preState state.BeaconState, eth1Data *ethpb.Eth1Data) (state.BeaconState, error) {
|
|
if eth1Data == nil {
|
|
return nil, errors.New("no eth1data provided for genesis state")
|
|
}
|
|
|
|
randaoMixes := make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector)
|
|
for i := range randaoMixes {
|
|
h := make([]byte, 32)
|
|
copy(h, eth1Data.BlockHash)
|
|
randaoMixes[i] = h
|
|
}
|
|
|
|
zeroHash := params.BeaconConfig().ZeroHash[:]
|
|
|
|
activeIndexRoots := make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector)
|
|
for i := range activeIndexRoots {
|
|
activeIndexRoots[i] = zeroHash
|
|
}
|
|
|
|
blockRoots := make([][]byte, params.BeaconConfig().SlotsPerHistoricalRoot)
|
|
for i := range blockRoots {
|
|
blockRoots[i] = zeroHash
|
|
}
|
|
|
|
stateRoots := make([][]byte, params.BeaconConfig().SlotsPerHistoricalRoot)
|
|
for i := range stateRoots {
|
|
stateRoots[i] = zeroHash
|
|
}
|
|
|
|
slashings := make([]uint64, params.BeaconConfig().EpochsPerSlashingsVector)
|
|
|
|
genesisValidatorsRoot, err := stateutil.ValidatorRegistryRoot(preState.Validators())
|
|
if err != nil {
|
|
return nil, errors.Wrapf(err, "could not hash tree root genesis validators %v", err)
|
|
}
|
|
|
|
prevEpochParticipation, err := preState.PreviousEpochParticipation()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
currEpochParticipation, err := preState.CurrentEpochParticipation()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
scores, err := preState.InactivityScores()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
scoresMissing := len(preState.Validators()) - len(scores)
|
|
if scoresMissing > 0 {
|
|
for range scoresMissing {
|
|
scores = append(scores, 0)
|
|
}
|
|
}
|
|
st := ðpb.BeaconStateBellatrix{
|
|
// Misc fields.
|
|
Slot: 0,
|
|
GenesisTime: uint64(genesisTime.Unix()),
|
|
GenesisValidatorsRoot: genesisValidatorsRoot[:],
|
|
|
|
Fork: ðpb.Fork{
|
|
PreviousVersion: params.BeaconConfig().GenesisForkVersion,
|
|
CurrentVersion: params.BeaconConfig().GenesisForkVersion,
|
|
Epoch: 0,
|
|
},
|
|
|
|
// Validator registry fields.
|
|
Validators: preState.Validators(),
|
|
Balances: preState.Balances(),
|
|
PreviousEpochParticipation: prevEpochParticipation,
|
|
CurrentEpochParticipation: currEpochParticipation,
|
|
InactivityScores: scores,
|
|
|
|
// Randomness and committees.
|
|
RandaoMixes: randaoMixes,
|
|
|
|
// Finality.
|
|
PreviousJustifiedCheckpoint: ðpb.Checkpoint{
|
|
Epoch: 0,
|
|
Root: params.BeaconConfig().ZeroHash[:],
|
|
},
|
|
CurrentJustifiedCheckpoint: ðpb.Checkpoint{
|
|
Epoch: 0,
|
|
Root: params.BeaconConfig().ZeroHash[:],
|
|
},
|
|
JustificationBits: []byte{0},
|
|
FinalizedCheckpoint: ðpb.Checkpoint{
|
|
Epoch: 0,
|
|
Root: params.BeaconConfig().ZeroHash[:],
|
|
},
|
|
|
|
HistoricalRoots: [][]byte{},
|
|
BlockRoots: blockRoots,
|
|
StateRoots: stateRoots,
|
|
Slashings: slashings,
|
|
|
|
// Eth1 data.
|
|
Eth1Data: eth1Data,
|
|
Eth1DataVotes: []*ethpb.Eth1Data{},
|
|
Eth1DepositIndex: preState.Eth1DepositIndex(),
|
|
}
|
|
|
|
var scBits [fieldparams.SyncAggregateSyncCommitteeBytesLength]byte
|
|
bodyRoot, err := (ðpb.BeaconBlockBodyBellatrix{
|
|
RandaoReveal: make([]byte, 96),
|
|
Eth1Data: ðpb.Eth1Data{
|
|
DepositRoot: make([]byte, 32),
|
|
BlockHash: make([]byte, 32),
|
|
},
|
|
Graffiti: make([]byte, 32),
|
|
SyncAggregate: ðpb.SyncAggregate{
|
|
SyncCommitteeBits: scBits[:],
|
|
SyncCommitteeSignature: make([]byte, 96),
|
|
},
|
|
ExecutionPayload: &enginev1.ExecutionPayload{
|
|
ParentHash: make([]byte, 32),
|
|
FeeRecipient: make([]byte, 20),
|
|
StateRoot: make([]byte, 32),
|
|
ReceiptsRoot: make([]byte, 32),
|
|
LogsBloom: make([]byte, 256),
|
|
PrevRandao: make([]byte, 32),
|
|
ExtraData: make([]byte, 0),
|
|
BaseFeePerGas: make([]byte, 32),
|
|
BlockHash: make([]byte, 32),
|
|
Transactions: make([][]byte, 0),
|
|
},
|
|
}).HashTreeRoot()
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "could not hash tree root empty block body")
|
|
}
|
|
|
|
st.LatestBlockHeader = ðpb.BeaconBlockHeader{
|
|
ParentRoot: zeroHash,
|
|
StateRoot: zeroHash,
|
|
BodyRoot: bodyRoot[:],
|
|
}
|
|
|
|
var pubKeys [][]byte
|
|
for i := uint64(0); i < params.BeaconConfig().SyncCommitteeSize; i++ {
|
|
pubKeys = append(pubKeys, bytesutil.PadTo([]byte{}, params.BeaconConfig().BLSPubkeyLength))
|
|
}
|
|
st.CurrentSyncCommittee = ðpb.SyncCommittee{
|
|
Pubkeys: pubKeys,
|
|
AggregatePubkey: bytesutil.PadTo([]byte{}, params.BeaconConfig().BLSPubkeyLength),
|
|
}
|
|
st.NextSyncCommittee = ðpb.SyncCommittee{
|
|
Pubkeys: bytesutil.SafeCopy2dBytes(pubKeys),
|
|
AggregatePubkey: bytesutil.PadTo([]byte{}, params.BeaconConfig().BLSPubkeyLength),
|
|
}
|
|
|
|
st.LatestExecutionPayloadHeader = &enginev1.ExecutionPayloadHeader{
|
|
ParentHash: make([]byte, 32),
|
|
FeeRecipient: make([]byte, 20),
|
|
StateRoot: make([]byte, 32),
|
|
ReceiptsRoot: make([]byte, 32),
|
|
LogsBloom: make([]byte, 256),
|
|
PrevRandao: make([]byte, 32),
|
|
ExtraData: make([]byte, 0),
|
|
BaseFeePerGas: make([]byte, 32),
|
|
BlockHash: make([]byte, 32),
|
|
TransactionsRoot: make([]byte, 32),
|
|
}
|
|
|
|
bs, err := state_native.InitializeFromProtoBellatrix(st)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
is, err := bs.InactivityScores()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if bs.NumValidators() != len(is) {
|
|
return nil, errors.New("inactivity score mismatch with num vals")
|
|
}
|
|
return bs, nil
|
|
}
|