diff --git a/beacon-chain/db/BUILD.bazel b/beacon-chain/db/BUILD.bazel index 5c88c6e2f..a55a9248a 100644 --- a/beacon-chain/db/BUILD.bazel +++ b/beacon-chain/db/BUILD.bazel @@ -7,7 +7,10 @@ go_library( "http_backup_handler.go", ], importpath = "github.com/prysmaticlabs/prysm/beacon-chain/db", - visibility = ["//beacon-chain:__subpackages__"], + visibility = [ + "//beacon-chain:__subpackages__", + "//tools:__subpackages__", + ], deps = [ "//beacon-chain/db/filters:go_default_library", "//beacon-chain/db/kv:go_default_library", diff --git a/tools/interop/export-genesis/BUILD.bazel b/tools/interop/export-genesis/BUILD.bazel new file mode 100644 index 000000000..50c55fd93 --- /dev/null +++ b/tools/interop/export-genesis/BUILD.bazel @@ -0,0 +1,18 @@ +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/interop/export-genesis", + visibility = ["//visibility:private"], + deps = [ + "//beacon-chain/db:go_default_library", + "@com_github_prysmaticlabs_go_ssz//:go_default_library", + ], +) + +go_binary( + name = "export-genesis", + embed = [":go_default_library"], + visibility = ["//visibility:public"], +) diff --git a/tools/interop/export-genesis/main.go b/tools/interop/export-genesis/main.go new file mode 100644 index 000000000..5a3b066dc --- /dev/null +++ b/tools/interop/export-genesis/main.go @@ -0,0 +1,45 @@ +package main + +import ( + "context" + "fmt" + "io/ioutil" + "os" + + "github.com/prysmaticlabs/go-ssz" + "github.com/prysmaticlabs/prysm/beacon-chain/db" +) + + +// A basic tool to extract genesis.ssz from existing beaconchain.db. +// ex: +// bazel run //tools/interop/export-genesis:export-genesis -- /tmp/data/beaconchaindata /tmp/genesis.ssz +func main() { + if len(os.Args) < 3 { + fmt.Println("Usage: ./main /path/to/datadir /path/to/output/genesis.ssz") + os.Exit(1) + } + + fmt.Printf("Reading db at %s and writing ssz output to %s.\n", os.Args[1], os.Args[2]) + + d, err := db.NewDB(os.Args[1]) + if err != nil { + panic(err) + } + defer d.Close() + gs, err := d.GenesisState(context.Background()) + if err != nil { + panic(err) + } + if gs == nil { + panic("nil genesis state") + } + b, err := ssz.Marshal(gs) + if err != nil { + panic(err) + } + if err := ioutil.WriteFile(os.Args[2], b, 0644); err != nil { + panic(err) + } + fmt.Println("done") +}