Compare commits

...

4 Commits

Author SHA1 Message Date
prestonvanloon
7d95522fa3 The default state 2022-02-18 19:20:45 -06:00
prestonvanloon
d8cfcf2442 viz 2022-02-18 19:14:39 -06:00
prestonvanloon
0b4f9089c0 Add functionality to generate a genesis state on a specific fork 2022-02-18 19:07:15 -06:00
Nishant Das
ced24892a5 Cleanup Powchain Service (#10259)
* add cleanup

* gaz

* Fix test build
2022-02-18 14:13:31 +00:00
13 changed files with 85 additions and 103 deletions

View File

@@ -18,6 +18,7 @@ go_library(
"//beacon-chain:__subpackages__",
"//testing/spectest:__subpackages__",
"//testing/util:__pkg__",
"//tools:__subpackages__",
"//validator/client:__pkg__",
],
deps = [

View File

@@ -7,6 +7,7 @@ go_library(
visibility = [
"//beacon-chain:__subpackages__",
"//testing/spectest:__subpackages__",
"//tools:__subpackages__",
"//validator/client:__pkg__",
],
deps = [

View File

@@ -120,11 +120,6 @@ func (_ *Service) AllDeposits(_ context.Context, _ *big.Int) []*ethpb.Deposit {
return []*ethpb.Deposit{}
}
// ChainStartDeposits mocks out the powchain functionality for interop.
func (s *Service) ChainStartDeposits() []*ethpb.Deposit {
return s.chainStartDeposits
}
// ChainStartEth1Data mocks out the powchain functionality for interop.
func (_ *Service) ChainStartEth1Data() *ethpb.Eth1Data {
return &ethpb.Eth1Data{}

View File

@@ -428,27 +428,27 @@ func (s *Service) requestBatchedHeadersAndLogs(ctx context.Context) error {
}
func (s *Service) retrieveBlockHashAndTime(ctx context.Context, blkNum *big.Int) ([32]byte, uint64, error) {
hash, err := s.BlockHashByHeight(ctx, blkNum)
bHash, err := s.BlockHashByHeight(ctx, blkNum)
if err != nil {
return [32]byte{}, 0, errors.Wrap(err, "could not get eth1 block hash")
}
if hash == [32]byte{} {
if bHash == [32]byte{} {
return [32]byte{}, 0, errors.Wrap(err, "got empty block hash")
}
timeStamp, err := s.BlockTimeByHeight(ctx, blkNum)
if err != nil {
return [32]byte{}, 0, errors.Wrap(err, "could not get block timestamp")
}
return hash, timeStamp, nil
return bHash, timeStamp, nil
}
// checkBlockNumberForChainStart checks the given block number for if chainstart has occurred.
func (s *Service) checkBlockNumberForChainStart(ctx context.Context, blkNum *big.Int) error {
hash, timeStamp, err := s.retrieveBlockHashAndTime(ctx, blkNum)
bHash, timeStamp, err := s.retrieveBlockHashAndTime(ctx, blkNum)
if err != nil {
return err
}
s.checkForChainstart(ctx, hash, blkNum, timeStamp)
s.checkForChainstart(ctx, bHash, blkNum, timeStamp)
return nil
}

View File

@@ -333,7 +333,7 @@ func TestProcessETH2GenesisLog(t *testing.T) {
err = web3Service.ProcessETH1Block(context.Background(), big.NewInt(int64(logs[len(logs)-1].BlockNumber)))
require.NoError(t, err)
cachedDeposits := web3Service.ChainStartDeposits()
cachedDeposits := web3Service.chainStartData.ChainstartDeposits
require.Equal(t, depositsReqForChainStart, len(cachedDeposits))
// Receive the chain started event.
@@ -425,7 +425,7 @@ func TestProcessETH2GenesisLog_CorrectNumOfDeposits(t *testing.T) {
err = web3Service.processPastLogs(context.Background())
require.NoError(t, err)
cachedDeposits := web3Service.ChainStartDeposits()
cachedDeposits := web3Service.chainStartData.ChainstartDeposits
requiredDepsForChainstart := depositsReqForChainStart + depositOffset
require.Equal(t, requiredDepsForChainstart, len(cachedDeposits), "Did not cache the chain start deposits correctly")
@@ -529,7 +529,7 @@ func TestProcessETH2GenesisLog_LargePeriodOfNoLogs(t *testing.T) {
err = web3Service.processPastLogs(context.Background())
require.NoError(t, err)
cachedDeposits := web3Service.ChainStartDeposits()
cachedDeposits := web3Service.chainStartData.ChainstartDeposits
require.Equal(t, totalNumOfDeposits, len(cachedDeposits), "Did not cache the chain start deposits correctly")
// Receive the chain started event.

View File

@@ -81,7 +81,6 @@ var (
// ChainStartFetcher retrieves information pertaining to the chain start event
// of the beacon chain for usage across various services.
type ChainStartFetcher interface {
ChainStartDeposits() []*ethpb.Deposit
ChainStartEth1Data() *ethpb.Eth1Data
PreGenesisState() state.BeaconState
ClearPreGenesisData()
@@ -271,12 +270,6 @@ func (s *Service) Stop() error {
return nil
}
// ChainStartDeposits returns a slice of validator deposit data processed
// by the deposit contract and cached in the powchain service.
func (s *Service) ChainStartDeposits() []*ethpb.Deposit {
return s.chainStartData.ChainstartDeposits
}
// ClearPreGenesisData clears out the stored chainstart deposits and beacon state.
func (s *Service) ClearPreGenesisData() {
s.chainStartData.ChainstartDeposits = []*ethpb.Deposit{}
@@ -382,45 +375,6 @@ func (s *Service) ETH1ConnectionErrors() []error {
return errs
}
// DepositRoot returns the Merkle root of the latest deposit trie
// from the ETH1.0 deposit contract.
func (s *Service) DepositRoot() [32]byte {
return s.depositTrie.HashTreeRoot()
}
// DepositTrie returns the sparse Merkle trie used for storing
// deposits from the ETH1.0 deposit contract.
func (s *Service) DepositTrie() *trie.SparseMerkleTrie {
return s.depositTrie
}
// LatestBlockHeight in the ETH1.0 chain.
func (s *Service) LatestBlockHeight() *big.Int {
return big.NewInt(int64(s.latestEth1Data.BlockHeight))
}
// LatestBlockHash in the ETH1.0 chain.
func (s *Service) LatestBlockHash() common.Hash {
return bytesutil.ToBytes32(s.latestEth1Data.BlockHash)
}
// AreAllDepositsProcessed determines if all the logs from the deposit contract
// are processed.
func (s *Service) AreAllDepositsProcessed() (bool, error) {
s.processingLock.RLock()
defer s.processingLock.RUnlock()
countByte, err := s.depositContractCaller.GetDepositCount(&bind.CallOpts{})
if err != nil {
return false, errors.Wrap(err, "could not get deposit count")
}
count := bytesutil.FromBytes8(countByte)
deposits := s.cfg.depositCache.AllDeposits(s.ctx, nil)
if count != uint64(len(deposits)) {
return false, nil
}
return true, nil
}
// refers to the latest eth1 block which follows the condition: eth1_timestamp +
// SECONDS_PER_ETH1_BLOCK * ETH1_FOLLOW_DISTANCE <= current_unix_time
func (s *Service) followBlockHeight(_ context.Context) (uint64, error) {

View File

@@ -16,7 +16,6 @@ go_library(
"//beacon-chain/powchain/types:go_default_library",
"//beacon-chain/state:go_default_library",
"//beacon-chain/state/v1:go_default_library",
"//container/trie:go_default_library",
"//encoding/bytesutil:go_default_library",
"//proto/prysm/v1alpha1:go_default_library",
"@com_github_ethereum_go_ethereum//accounts/abi/bind/backends:go_default_library",

View File

@@ -10,7 +10,6 @@ import (
"github.com/prysmaticlabs/prysm/beacon-chain/powchain/types"
"github.com/prysmaticlabs/prysm/beacon-chain/state"
v1 "github.com/prysmaticlabs/prysm/beacon-chain/state/v1"
"github.com/prysmaticlabs/prysm/container/trie"
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
)
@@ -25,11 +24,6 @@ func (_ *FaultyMockPOWChain) Eth2GenesisPowchainInfo() (uint64, *big.Int) {
return 0, big.NewInt(0)
}
// LatestBlockHeight --
func (_ *FaultyMockPOWChain) LatestBlockHeight() *big.Int {
return big.NewInt(0)
}
// BlockExists --
func (f *FaultyMockPOWChain) BlockExists(_ context.Context, _ common.Hash) (bool, *big.Int, error) {
if f.HashesByHeight == nil {
@@ -54,21 +48,6 @@ func (_ *FaultyMockPOWChain) BlockByTimestamp(_ context.Context, _ uint64) (*typ
return &types.HeaderInfo{Number: big.NewInt(0)}, nil
}
// DepositRoot --
func (_ *FaultyMockPOWChain) DepositRoot() [32]byte {
return [32]byte{}
}
// DepositTrie --
func (_ *FaultyMockPOWChain) DepositTrie() *trie.SparseMerkleTrie {
return &trie.SparseMerkleTrie{}
}
// ChainStartDeposits --
func (_ *FaultyMockPOWChain) ChainStartDeposits() []*ethpb.Deposit {
return []*ethpb.Deposit{}
}
// ChainStartEth1Data --
func (_ *FaultyMockPOWChain) ChainStartEth1Data() *ethpb.Eth1Data {
return &ethpb.Eth1Data{}

View File

@@ -16,7 +16,6 @@ import (
"github.com/prysmaticlabs/prysm/async/event"
"github.com/prysmaticlabs/prysm/beacon-chain/powchain/types"
"github.com/prysmaticlabs/prysm/beacon-chain/state"
"github.com/prysmaticlabs/prysm/container/trie"
"github.com/prysmaticlabs/prysm/encoding/bytesutil"
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
)
@@ -58,11 +57,6 @@ func (m *POWChain) Eth2GenesisPowchainInfo() (uint64, *big.Int) {
return uint64(GenesisTime), blk
}
// DepositTrie --
func (_ *POWChain) DepositTrie() *trie.SparseMerkleTrie {
return &trie.SparseMerkleTrie{}
}
// BlockExists --
func (m *POWChain) BlockExists(_ context.Context, hash common.Hash) (bool, *big.Int, error) {
// Reverse the map of heights by hash.
@@ -107,17 +101,6 @@ func (m *POWChain) BlockByTimestamp(_ context.Context, time uint64) (*types.Head
return &types.HeaderInfo{Number: chosenNumber, Time: chosenTime}, nil
}
// DepositRoot --
func (_ *POWChain) DepositRoot() [32]byte {
root := []byte("depositroot")
return bytesutil.ToBytes32(root)
}
// ChainStartDeposits --
func (_ *POWChain) ChainStartDeposits() []*ethpb.Deposit {
return []*ethpb.Deposit{}
}
// ChainStartEth1Data --
func (m *POWChain) ChainStartEth1Data() *ethpb.Eth1Data {
return m.Eth1Data

View File

@@ -40,6 +40,7 @@ go_library(
"//proto/migration:__subpackages__",
"//testing/spectest:__subpackages__",
"//testing/util:__pkg__",
"//tools:__subpackages__",
],
deps = [
"//beacon-chain/state:go_default_library",

View File

@@ -40,8 +40,7 @@ go_library(
"//testing/benchmark:__pkg__",
"//testing/spectest:__subpackages__",
"//testing/util:__pkg__",
"//tools/benchmark-files-gen:__pkg__",
"//tools/pcli:__pkg__",
"//tools:__subpackages__",
],
deps = [
"//beacon-chain/state:go_default_library",

View File

@@ -10,10 +10,15 @@ go_library(
importpath = "github.com/prysmaticlabs/prysm/tools/genesis-state-gen",
visibility = ["//visibility:private"],
deps = [
"//beacon-chain/core/altair:go_default_library",
"//beacon-chain/core/execution:go_default_library",
"//beacon-chain/state/state-native/v2:go_default_library",
"//beacon-chain/state/v1:go_default_library",
"//config/params:go_default_library",
"//io/file:go_default_library",
"//proto/prysm/v1alpha1:go_default_library",
"//runtime/interop:go_default_library",
"@com_github_ferranbt_fastssz//:go_default_library",
"@com_github_ghodss_yaml//:go_default_library",
],
)

View File

@@ -11,7 +11,12 @@ import (
"os"
"strings"
ssz "github.com/ferranbt/fastssz"
"github.com/ghodss/yaml"
"github.com/prysmaticlabs/prysm/beacon-chain/core/altair"
"github.com/prysmaticlabs/prysm/beacon-chain/core/execution"
v2 "github.com/prysmaticlabs/prysm/beacon-chain/state/state-native/v2"
v1 "github.com/prysmaticlabs/prysm/beacon-chain/state/v1"
"github.com/prysmaticlabs/prysm/config/params"
"github.com/prysmaticlabs/prysm/io/file"
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
@@ -40,6 +45,13 @@ var (
sszOutputFile = flag.String("output-ssz", "", "Output filename of the SSZ marshaling of the generated genesis state")
yamlOutputFile = flag.String("output-yaml", "", "Output filename of the YAML marshaling of the generated genesis state")
jsonOutputFile = flag.String("output-json", "", "Output filename of the JSON marshaling of the generated genesis state")
forkName = flag.String("fork", "phase0", "Fork name to use for genesis state generation")
)
const (
phase0Fork = iota
altairFork
bellatrixFork
)
func main() {
@@ -54,7 +66,7 @@ func main() {
if !*useMainnetConfig {
params.OverrideBeaconConfig(params.MinimalSpecConfig())
}
var genesisState *ethpb.BeaconState
var phase0Genesis *ethpb.BeaconState
var err error
if *depositJSONFile != "" {
inputFile := *depositJSONFile
@@ -74,7 +86,7 @@ func main() {
}
}()
log.Printf("Generating genesis state from input JSON deposit data %s", inputFile)
genesisState, err = genesisStateFromJSONValidators(inputJSON, *genesisTime)
phase0Genesis, err = genesisStateFromJSONValidators(inputJSON, *genesisTime)
if err != nil {
log.Printf("Could not generate genesis beacon state: %v", err)
return
@@ -85,13 +97,66 @@ func main() {
return
}
// If no JSON input is specified, we create the state deterministically from interop keys.
genesisState, _, err = interop.GenerateGenesisState(context.Background(), *genesisTime, uint64(*numValidators))
phase0Genesis, _, err = interop.GenerateGenesisState(context.Background(), *genesisTime, uint64(*numValidators))
if err != nil {
log.Printf("Could not generate genesis beacon state: %v", err)
return
}
}
// Upgrade genesis state.
var genesisState ssz.Marshaler
ctx := context.Background()
var fork int
switch *forkName {
case "phase0":
fork = phase0Fork
case "altair":
fork = altairFork
case "bellatrix":
fork = bellatrixFork
default:
log.Fatalf("Unknown fork name %s", *forkName)
}
if *forkName != "phase0" {
log.Printf("Upgrading genesis state to fork %s.", *forkName)
} else {
genesisState = phase0Genesis
}
if fork >= altairFork {
wrappedGenesisState, err := v1.InitializeFromProtoUnsafe(phase0Genesis)
if err != nil {
log.Fatalf("Could not initialize genesis state: %v", err)
return
}
altairState, err := altair.UpgradeToAltair(ctx, wrappedGenesisState)
if err != nil {
log.Fatalf("Could not upgrade genesis state: %v", err)
return
}
var ok bool
genesisState, ok = altairState.InnerStateUnsafe().(*ethpb.BeaconStateAltair)
if !ok {
log.Fatalf("Could not convert to altair state")
return
}
}
if fork >= bellatrixFork {
altairState, err := v2.InitializeFromProtoUnsafe(genesisState.(*ethpb.BeaconStateAltair))
if err != nil {
log.Fatalf("Could not initialize genesis state: %v", err)
}
bellatrixState, err := execution.UpgradeToBellatrix(ctx, altairState)
if err != nil {
log.Fatalf("Could not upgrade genesis state: %v", err)
}
var ok bool
genesisState, ok = bellatrixState.InnerStateUnsafe().(*ethpb.BeaconStateBellatrix)
if !ok {
log.Fatalf("Could not convert to bellatrix state")
}
}
if *sszOutputFile != "" {
encodedState, err := genesisState.MarshalSSZ()
if err != nil {