Default genesisTime to now when generating a genesis state. (#3615)

* Default genesisTime to now when generating a genesis state.

* Use roughtime for genesis creation timestamp
This commit is contained in:
Jim McDonald 2019-09-27 16:49:55 +01:00 committed by Raul Jordan
parent 14bc8d7637
commit e1e36e1424
6 changed files with 15 additions and 9 deletions

View File

@ -25,7 +25,7 @@ You can use `bazel run //tools/genesis-state-gen` to create a deterministic gene
### Usage
- **--genesis-time** uint: Unix timestamp used as the genesis time in the generated genesis state
- **--genesis-time** uint: Unix timestamp used as the genesis time in the generated genesis state (defaults to now)
- **--mainnet-config** bool: Select whether genesis state should be generated with mainnet or minimal (default) params
- **--num-validators** int: Number of validators to deterministically include in the generated genesis state
- **--output-ssz** string: Output filename of the SSZ marshaling of the generated genesis state
@ -50,7 +50,6 @@ bazel run //beacon-chain -- \
--deposit-contract 0xD775140349E6A5D12524C6ccc3d6A1d4519D4029 \
--clear-db \
--interop-num-validators 64 \
--interop-genesis-time=$(date +%s) \
--interop-eth1data-votes
```

View File

@ -83,6 +83,10 @@ func NewColdStartService(ctx context.Context, cfg *Config) *Service {
if err != nil {
log.Fatalf("Could not generate interop genesis state: %v", err)
}
if s.genesisTime == 0 {
// Generated genesis time; fetch it
s.genesisTime = genesisState.GenesisTime
}
if err := s.saveGenesisState(ctx, genesisState); err != nil {
log.Fatalf("Could not save interop genesis state %v", err)
}

View File

@ -415,12 +415,11 @@ func (b *BeaconNode) registerRPCService(ctx *cli.Context) error {
return err
}
genesisTime := ctx.GlobalUint64(flags.InteropGenesisTimeFlag.Name)
genesisValidators := ctx.GlobalUint64(flags.InteropNumValidatorsFlag.Name)
genesisStatePath := ctx.GlobalString(flags.InteropGenesisStateFlag.Name)
var depositFetcher depositcache.DepositFetcher
var chainStartFetcher powchain.ChainStartFetcher
if genesisTime > 0 && genesisValidators > 0 || genesisStatePath != "" {
if genesisValidators > 0 || genesisStatePath != "" {
var interopService *interopcoldstart.Service
if err := b.services.FetchService(&interopService); err != nil {
return err
@ -500,7 +499,7 @@ func (b *BeaconNode) registerInteropServices(ctx *cli.Context) error {
genesisValidators := ctx.GlobalUint64(flags.InteropNumValidatorsFlag.Name)
genesisStatePath := ctx.GlobalString(flags.InteropGenesisStateFlag.Name)
if genesisTime > 0 && genesisValidators > 0 || genesisStatePath != "" {
if genesisValidators > 0 || genesisStatePath != "" {
svc := interopcoldstart.NewColdStartService(context.Background(), &interopcoldstart.Config{
GenesisTime: genesisTime,
NumValidators: genesisValidators,
@ -510,8 +509,6 @@ func (b *BeaconNode) registerInteropServices(ctx *cli.Context) error {
})
return b.services.RegisterService(svc)
} else if genesisTime+genesisValidators > 0 {
log.Errorf("%s and %s must be used together", flags.InteropNumValidatorsFlag.Name, flags.InteropGenesisTimeFlag.Name)
}
return nil
}

View File

@ -15,6 +15,7 @@ go_library(
"//shared/bls:go_default_library",
"//shared/hashutil:go_default_library",
"//shared/params:go_default_library",
"//shared/roughtime:go_default_library",
"//shared/trieutil:go_default_library",
"@com_github_pkg_errors//:go_default_library",
"@com_github_prysmaticlabs_go_ssz//:go_default_library",

View File

@ -9,6 +9,7 @@ import (
"github.com/prysmaticlabs/prysm/shared/bls"
"github.com/prysmaticlabs/prysm/shared/hashutil"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/roughtime"
"github.com/prysmaticlabs/prysm/shared/trieutil"
)
@ -22,6 +23,7 @@ var (
)
// GenerateGenesisState deterministically given a genesis time and number of validators.
// If a genesis time of 0 is supplied it is set to the current time.
func GenerateGenesisState(genesisTime, numValidators uint64) (*pb.BeaconState, []*ethpb.Deposit, error) {
privKeys, pubKeys, err := DeterministicallyGenerateKeys(0 /*startIndex*/, numValidators)
if err != nil {
@ -43,6 +45,9 @@ func GenerateGenesisState(genesisTime, numValidators uint64) (*pb.BeaconState, [
return nil, nil, errors.Wrap(err, "could not generate deposits from the deposit data provided")
}
root := trie.Root()
if genesisTime == 0 {
genesisTime = uint64(roughtime.Now().Unix())
}
beaconState, err := state.GenesisBeaconState(deposits, genesisTime, &ethpb.Eth1Data{
DepositRoot: root[:],
DepositCount: uint64(len(deposits)),

View File

@ -19,7 +19,7 @@ const (
var (
numValidators = flag.Int("num-validators", 0, "Number of validators to deterministically include in the generated genesis state")
useMainnetConfig = flag.Bool("mainnet-config", false, "Select whether genesis state should be generated with mainnet or minimal (default) params")
genesisTime = flag.Uint64("genesis-time", 0, "Unix timestamp used as the genesis time in the generated genesis state")
genesisTime = flag.Uint64("genesis-time", 0, "Unix timestamp used as the genesis time in the generated genesis state (defaults to now)")
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")
@ -31,7 +31,7 @@ func main() {
log.Fatal("Expected --num-validators to have been provided, received 0")
}
if *genesisTime == 0 {
log.Print("No --genesis-time specified, defaulting to 0 as the unix timestamp")
log.Print("No --genesis-time specified, defaulting to now")
}
if *sszOutputFile == "" && *yamlOutputFile == "" && *jsonOutputFile == "" {
log.Fatal("Expected --output-ssz, --output-yaml, or --output-json to have been provided, received nil")