diff --git a/tools/update-genesis-time/BUILD.bazel b/tools/update-genesis-time/BUILD.bazel new file mode 100644 index 000000000000..9d6b3ac523b4 --- /dev/null +++ b/tools/update-genesis-time/BUILD.bazel @@ -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"], +) diff --git a/tools/update-genesis-time/README.md b/tools/update-genesis-time/README.md new file mode 100644 index 000000000000..f4e765e9c44a --- /dev/null +++ b/tools/update-genesis-time/README.md @@ -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 +``` diff --git a/tools/update-genesis-time/main.go b/tools/update-genesis-time/main.go new file mode 100644 index 000000000000..f71af31caac3 --- /dev/null +++ b/tools/update-genesis-time/main.go @@ -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) +}