Skip to content

Commit

Permalink
go/oasis-node/cmd/genesis: Add convert CLI command
Browse files Browse the repository at this point in the history
It can be used to convert a given genesis file to the canonical form.
  • Loading branch information
tjanez committed Sep 18, 2020
1 parent 8fecb7a commit 60d51ee
Showing 1 changed file with 74 additions and 12 deletions.
86 changes: 74 additions & 12 deletions go/oasis-node/cmd/genesis/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"math"
"os"
Expand All @@ -27,7 +28,7 @@ import (
tendermint "github.com/oasisprotocol/oasis-core/go/consensus/tendermint/api"
epochtime "github.com/oasisprotocol/oasis-core/go/epochtime/api"
genesis "github.com/oasisprotocol/oasis-core/go/genesis/api"
genesisFile "github.com/oasisprotocol/oasis-core/go/genesis/file"
genesisfile "github.com/oasisprotocol/oasis-core/go/genesis/file"
keymanager "github.com/oasisprotocol/oasis-core/go/keymanager/api"
cmdCommon "github.com/oasisprotocol/oasis-core/go/oasis-node/cmd/common"
"github.com/oasisprotocol/oasis-core/go/oasis-node/cmd/common/flags"
Expand All @@ -39,6 +40,9 @@ import (
)

const (
cfgGenesisCanonicalFile = "genesis.canonical_file"
cfgGenesisCanonicalFileShort = "C"

cfgEntity = "entity"
cfgRuntime = "runtime"
cfgNode = "node"
Expand Down Expand Up @@ -102,9 +106,10 @@ const (
)

var (
checkGenesisFlags = flag.NewFlagSet("", flag.ContinueOnError)
dumpGenesisFlags = flag.NewFlagSet("", flag.ContinueOnError)
initGenesisFlags = flag.NewFlagSet("", flag.ContinueOnError)
checkGenesisFlags = flag.NewFlagSet("", flag.ContinueOnError)
convertGenesisFlags = flag.NewFlagSet("", flag.ContinueOnError)
dumpGenesisFlags = flag.NewFlagSet("", flag.ContinueOnError)
initGenesisFlags = flag.NewFlagSet("", flag.ContinueOnError)

genesisCmd = &cobra.Command{
Use: "genesis",
Expand All @@ -129,6 +134,12 @@ var (
Run: doCheckGenesis,
}

convertGenesisCmd = &cobra.Command{
Use: "convert",
Short: "convert genesis file to canonical form",
Run: doConvertGenesis,
}

logger = logging.GetLogger("cmd/genesis")
)

Expand Down Expand Up @@ -588,30 +599,72 @@ func doDumpGenesis(cmd *cobra.Command, args []string) {
}
}

func loadGenesisDocument(genFilePath string) (*genesis.Document, error) {
provider, err := genesisfile.NewFileProvider(genFilePath)
if err != nil {
return nil, fmt.Errorf("failed to open genesis file: %w", err)
}
doc, err := provider.GetGenesisDocument()
if err != nil {
return nil, fmt.Errorf("failed to get genesis document: %w", err)
}
return doc, nil
}

func doCheckGenesis(cmd *cobra.Command, args []string) {
if err := cmdCommon.Init(); err != nil {
cmdCommon.EarlyLogAndExit(err)
}

filename := flags.GenesisFile()
provider, err := genesisFile.NewFileProvider(filename)
doc, err := loadGenesisDocument(flags.GenesisFile())
if err != nil {
logger.Error("failed to open genesis file", "err", err)
logger.Error("failed to load genesis document", "err", err)
os.Exit(1)
}
doc, err := provider.GetGenesisDocument()

err = doc.SanityCheck()
if err != nil {
logger.Error("failed to get genesis document", "err", err)
logger.Error("genesis document sanity check failed", "err", err)
os.Exit(1)
}
}

err = doc.SanityCheck()
func doConvertGenesis(cmd *cobra.Command, args []string) {
if err := cmdCommon.Init(); err != nil {
cmdCommon.EarlyLogAndExit(err)
}

doc, err := loadGenesisDocument(flags.GenesisFile())
if err != nil {
logger.Error("genesis document sanity check failed", "err", err)
logger.Error("failed to load genesis document", "err", err)
os.Exit(1)
}

// TODO: Pretty-print contents of genesis document.
data, err := json.MarshalIndent(doc, "", " ")
if err != nil {
logger.Error("failed to marshal genesis document into JSON",
"err", err,
)
os.Exit(1)
}

w, shouldClose, err := cmdCommon.GetOutputWriter(cmd, cfgGenesisCanonicalFile)
if err != nil {
logger.Error("failed to get writer for canonical genesis file",
"err", err,
)
os.Exit(1)
}
if shouldClose {
defer w.Close()
}

if _, err = w.Write(data); err != nil {
logger.Error("failed to write marshalled document to the canonical genesis file",
"err", err,
)
os.Exit(1)
}
}

// Register registers the genesis sub-command and all of it's children.
Expand All @@ -620,11 +673,13 @@ func Register(parentCmd *cobra.Command) {
dumpGenesisCmd.Flags().AddFlagSet(dumpGenesisFlags)
dumpGenesisCmd.PersistentFlags().AddFlagSet(cmdGrpc.ClientFlags)
checkGenesisCmd.Flags().AddFlagSet(checkGenesisFlags)
convertGenesisCmd.Flags().AddFlagSet(convertGenesisFlags)

for _, v := range []*cobra.Command{
initGenesisCmd,
dumpGenesisCmd,
checkGenesisCmd,
convertGenesisCmd,
} {
genesisCmd.AddCommand(v)
}
Expand All @@ -636,6 +691,13 @@ func init() {
_ = viper.BindPFlags(checkGenesisFlags)
checkGenesisFlags.AddFlagSet(flags.GenesisFileFlags)

_ = viper.BindPFlags(convertGenesisFlags)
convertGenesisFlags.AddFlagSet(flags.GenesisFileFlags)
convertGenesisFlags.StringP(
cfgGenesisCanonicalFile, cfgGenesisCanonicalFileShort,
"", "path to where to output the canonical genesis file (default: stdout)",
)

dumpGenesisFlags.Int64(cfgBlockHeight, consensus.HeightLatest, "block height at which to dump state")
_ = viper.BindPFlags(dumpGenesisFlags)
dumpGenesisFlags.AddFlagSet(flags.GenesisFileFlags)
Expand Down

0 comments on commit 60d51ee

Please sign in to comment.