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

A tool to update genesis time of an existing state #5664

Merged
merged 8 commits into from
Apr 28, 2020
19 changes: 19 additions & 0 deletions tools/update-genesis-time/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")

go_library(
name = "go_default_library",
srcs = ["main.go"],
importpath = "github.com/prysmaticlabs/prysm/tools/update-genesis-time",
visibility = ["//visibility:public"],
deps = [
"//proto/beacon/p2p/v1:go_default_library",
"//shared/roughtime:go_default_library",
"@com_github_prysmaticlabs_go_ssz//:go_default_library",
],
)

go_binary(
name = "update-genesis-time",
embed = [":go_default_library"],
visibility = ["//visibility:public"],
)
35 changes: 35 additions & 0 deletions tools/update-genesis-time/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
## Utility to Update Beacon State Genesis Time

This is a utility to help users update genesis time of an input beacon state

### Usage

_Name:_
**update-genesis-time** - this is a utility to update genesis time of a beacon state

_Usage:_
update-genesis-time [global options]

_Flags:_

- --input-ssz-state: Input filename of the SSZ marshaling of the genesis state
- --genesis-time: Unix timestamp used as the genesis time in the generated genesis state (defaults to now)

### Example

To use private key with default RPC:

```
bazel run //tools/update-genesis-time -- --input-ssz-state=/tmp/genesis.ssz
```

### Output

```
INFO: Elapsed time: 5.887s, Critical Path: 4.99s
INFO: 41 processes: 41 darwin-sandbox.
INFO: Build completed successfully, 44 total actions
INFO: Build completed successfully, 44 total actions
2020/04/28 11:55:21 No --genesis-time specified, defaulting to now
2020/04/28 11:55:21 Done writing to /tmp/genesis.ssz
```
51 changes: 51 additions & 0 deletions tools/update-genesis-time/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package main

import (
"flag"
"io/ioutil"
"log"

"github.com/prysmaticlabs/go-ssz"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/roughtime"
)

var (
genesisTime = flag.Uint64("genesis-time", 0, "Unix timestamp used as the genesis time in the generated genesis state (defaults to now)")
inputSSZState = flag.String("input-ssz-state", "", "Input filename of the SSZ marshaling of the genesis state")
)

func main() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add some documentation about what this is and why you would use it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Added a readme

flag.Parse()
if *inputSSZState == "" {
log.Fatal("Expected --input-ssz-state")
}

beaconState := &pb.BeaconState{}
if err := unmarshalFile(*inputSSZState, beaconState); err != nil {
log.Fatal(err)
}
if *genesisTime == 0 {
log.Print("No --genesis-time specified, defaulting to now")
beaconState.GenesisTime = uint64(roughtime.Now().Unix())
} else {
beaconState.GenesisTime = *genesisTime
}

encodedState, err := ssz.Marshal(beaconState)
if err != nil {
log.Fatalf("Could not ssz marshal the beacon state: %v", err)
}
if err := ioutil.WriteFile(*inputSSZState, encodedState, 0644); err != nil {
log.Fatalf("Could not write encoded beacon state to file: %v", err)
}
log.Printf("Done writing to %s", *inputSSZState)
}

func unmarshalFile(fPath string, data interface{}) error {
rawFile, err := ioutil.ReadFile(fPath)
if err != nil {
return err
}
return ssz.Unmarshal(rawFile, data)
}