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

E2E from Phase0 or Bellatrix #11802

Merged
merged 14 commits into from
Dec 21, 2022
1 change: 1 addition & 0 deletions beacon-chain/core/transition/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ go_library(
"//consensus-types/blocks:go_default_library",
"//consensus-types/interfaces:go_default_library",
"//consensus-types/primitives:go_default_library",
"//container/trie:go_default_library",
"//crypto/bls:go_default_library",
"//crypto/hash:go_default_library",
"//encoding/bytesutil:go_default_library",
Expand Down
38 changes: 38 additions & 0 deletions beacon-chain/core/transition/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
state_native "github.com/prysmaticlabs/prysm/v3/beacon-chain/state/state-native"
"github.com/prysmaticlabs/prysm/v3/beacon-chain/state/stateutil"
"github.com/prysmaticlabs/prysm/v3/config/params"
"github.com/prysmaticlabs/prysm/v3/container/trie"
ethpb "github.com/prysmaticlabs/prysm/v3/proto/prysm/v1alpha1"
)

Expand Down Expand Up @@ -75,6 +76,43 @@ func GenesisBeaconState(ctx context.Context, deposits []*ethpb.Deposit, genesisT
return OptimizedGenesisBeaconState(genesisTime, st, st.Eth1Data())
}

// PreminedGenesisBeaconState works almost exactly like GenesisBeaconState, except that it assumes that genesis deposits
// are not represented in the deposit contract and are only found in the genesis state validator registry. In order
// to ensure the deposit root and count match the empty deposit contract deployed in a testnet genesis block, the root
// of an empty deposit trie is computed and used as Eth1Data.deposit_root, and the deposit count is set to 0.
func PreminedGenesisBeaconState(ctx context.Context, deposits []*ethpb.Deposit, genesisTime uint64, eth1Data *ethpb.Eth1Data) (state.BeaconState, error) {
st, err := EmptyGenesisState()
if err != nil {
return nil, err
}

// Process initial deposits.
st, err = helpers.UpdateGenesisEth1Data(st, deposits, eth1Data)
if err != nil {
return nil, err
}
st, err = b.ProcessPreGenesisDeposits(ctx, st, deposits)
if err != nil {
return nil, errors.Wrap(err, "could not process validator deposits")
}

t, err := trie.NewTrie(params.BeaconConfig().DepositContractTreeDepth)
if err != nil {
return nil, err
}
dr, err := t.HashTreeRoot()
if err != nil {
return nil, err
}
if err := st.SetEth1Data(&ethpb.Eth1Data{DepositRoot: dr[:], BlockHash: eth1Data.BlockHash}); err != nil {
return nil, err
}
if err := st.SetEth1DepositIndex(0); err != nil {
return nil, err
}
return OptimizedGenesisBeaconState(genesisTime, st, st.Eth1Data())
}

// OptimizedGenesisBeaconState is used to create a state that has already processed deposits. This is to efficiently
// create a mainnet state at chainstart.
func OptimizedGenesisBeaconState(genesisTime uint64, preState state.BeaconState, eth1Data *ethpb.Eth1Data) (state.BeaconState, error) {
Expand Down
3 changes: 3 additions & 0 deletions beacon-chain/execution/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,9 @@ func (s *Service) determineEarliestVotingBlock(ctx context.Context, followBlock
return 0, errors.Errorf("invalid genesis time provided. %d > %d", followBackDist, votingTime)
}
earliestValidTime := votingTime - followBackDist
if earliestValidTime < genesisTime {
return 0, nil
}
hdr, err := s.BlockByTimestamp(ctx, earliestValidTime)
if err != nil {
return 0, err
Expand Down
19 changes: 16 additions & 3 deletions beacon-chain/execution/testing/genesis.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package testing

import (
"fmt"
"math"
"math/big"

Expand All @@ -16,7 +17,7 @@ import (
const defaultMinerAddress = "0x878705ba3f8bc32fcf7f4caa1a35e72af65cf766"
const defaultTestChainId int64 = 1337
const defaultCoinbase = "0x0000000000000000000000000000000000000000"
const defaultDifficulty = "0x20000"
const defaultDifficulty = "1"
const defaultMixhash = "0x0000000000000000000000000000000000000000000000000000000000000000"
const defaultParenthash = "0x0000000000000000000000000000000000000000000000000000000000000000"
const defaultMinerBalance = "100000000000000000000000000000"
Expand Down Expand Up @@ -78,6 +79,10 @@ const DefaultCliqueSigner = "0x0000000000000000000000000000000000000000000000000
// like in an e2e test. The parameters are minimal but the full value is returned unmarshaled so that it can be
// customized as desired.
func GethTestnetGenesis(genesisTime uint64, cfg *clparams.BeaconChainConfig) core.Genesis {
ttd, ok := big.NewInt(0).SetString(clparams.BeaconConfig().TerminalTotalDifficulty, 10)
if !ok {
panic(fmt.Sprintf("unable to parse TerminalTotalDifficulty as an integer = %s", clparams.BeaconConfig().TerminalTotalDifficulty))
}
cc := &params.ChainConfig{
ChainID: big.NewInt(defaultTestChainId),
HomesteadBlock: bigz,
Expand All @@ -95,16 +100,24 @@ func GethTestnetGenesis(genesisTime uint64, cfg *clparams.BeaconChainConfig) cor
ArrowGlacierBlock: bigz,
GrayGlacierBlock: bigz,
MergeNetsplitBlock: bigz,
TerminalTotalDifficulty: bigz,
TerminalTotalDifficulty: ttd,
TerminalTotalDifficultyPassed: false,
Clique: &params.CliqueConfig{
Period: cfg.SecondsPerETH1Block,
Epoch: 20000,
},
}
da := defaultDepositContractAllocation(cfg.DepositContractAddress)
ma := minerAllocation()
extra, err := hexutil.Decode(DefaultCliqueSigner)
if err != nil {
panic(fmt.Sprintf("unable to decode DefaultCliqueSigner, with error %v", err.Error()))
}
return core.Genesis{
Config: cc,
Nonce: 0, // overridden for authorized signer votes in clique, so we should leave it empty?
Timestamp: genesisTime,
ExtraData: []byte(DefaultCliqueSigner),
ExtraData: extra,
GasLimit: math.MaxUint64 >> 1, // shift 1 back from the max, just in case
Difficulty: common.HexToHash(defaultDifficulty).Big(),
Mixhash: common.HexToHash(defaultMixhash),
Expand Down
1 change: 0 additions & 1 deletion cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
type Flags struct {
// Configuration related flags.
MinimalConfig bool // MinimalConfig as defined in the spec.
E2EConfig bool // E2EConfig made specifically for testing, do not use except in E2E.
MaxRPCPageSize int // MaxRPCPageSize is used for a cap of page sizes in RPC requests.
}

Expand Down
15 changes: 10 additions & 5 deletions config/params/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@ func isMinimal(lines []string) bool {
return false
}

func UnmarshalConfigFile(path string, conf *BeaconChainConfig) (*BeaconChainConfig, error) {
yamlFile, err := os.ReadFile(path) // #nosec G304
if err != nil {
return nil, errors.Wrap(err, "Failed to read chain config file.")
}
func UnmarshalConfig(yamlFile []byte, conf *BeaconChainConfig) (*BeaconChainConfig, error) {
// To track if config name is defined inside config file.
hasConfigName := false
// Convert 0x hex inputs to fixed bytes arrays
Expand Down Expand Up @@ -72,6 +68,14 @@ func UnmarshalConfigFile(path string, conf *BeaconChainConfig) (*BeaconChainConf
return conf, nil
}

func UnmarshalConfigFile(path string, conf *BeaconChainConfig) (*BeaconChainConfig, error) {
yamlFile, err := os.ReadFile(path) // #nosec G304
if err != nil {
return nil, errors.Wrap(err, "Failed to read chain config file.")
}
return UnmarshalConfig(yamlFile, conf)
}

// LoadChainConfigFile load, convert hex values into valid param yaml format,
// unmarshal , and apply beacon chain config file.
func LoadChainConfigFile(path string, conf *BeaconChainConfig) error {
Expand Down Expand Up @@ -201,6 +205,7 @@ func ConfigToYaml(cfg *BeaconChainConfig) []byte {
fmt.Sprintf("TERMINAL_TOTAL_DIFFICULTY: %s", cfg.TerminalTotalDifficulty),
fmt.Sprintf("TERMINAL_BLOCK_HASH: %#x", cfg.TerminalBlockHash),
fmt.Sprintf("TERMINAL_BLOCK_HASH_ACTIVATION_EPOCH: %d", cfg.TerminalBlockHashActivationEpoch),
fmt.Sprintf("DEPOSIT_CONTRACT_ADDRESS: %s", cfg.DepositContractAddress),
}

yamlFile := []byte(strings.Join(lines, "\n"))
Expand Down
Loading