-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A tool to update genesis time of an existing state (#5664)
* 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
1 parent
5ed72d4
commit 44611e0
Showing
3 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |