Skip to content

Commit

Permalink
A tool to update genesis time of an existing state (#5664)
Browse files Browse the repository at this point in the history
* Add a tool to update genesis time
* Minor touchups
* Merge branch 'master' into update-genesis-time
* Added a readme
* Merge refs/heads/master into update-genesis-time
* Merge branch 'update-genesis-time' of github.com:prysmaticlabs/prysm into update-genesis-time
* Merge refs/heads/master into update-genesis-time
* Merge refs/heads/master into update-genesis-time
  • Loading branch information
terencechain authored Apr 28, 2020
1 parent 5ed72d4 commit 44611e0
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
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() {
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)
}

0 comments on commit 44611e0

Please sign in to comment.