stake init overhaul

This commit is contained in:
rigelrozanski
2018-04-26 14:26:39 -04:00
parent c8f5fcb27b
commit 201908949a
13 changed files with 189 additions and 67 deletions
+28 -13
View File
@@ -11,6 +11,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
crypto "github.com/tendermint/go-crypto"
@@ -26,7 +27,7 @@ import (
// TODO flag to retrieve genesis file / config file from a URL?
// get cmd to initialize all files for tendermint and application
func InitCmd(ctx *Context, cdc *wire.Codec, gen GenAppParams, appendState AppendAppState) *cobra.Command {
func InitCmd(ctx *Context, cdc *wire.Codec, appInit AppInit) *cobra.Command {
flagOverwrite, flagPieceFile := "overwrite", "piece-file"
cmd := &cobra.Command{
Use: "init",
@@ -37,7 +38,7 @@ func InitCmd(ctx *Context, cdc *wire.Codec, gen GenAppParams, appendState Append
config := ctx.Config
pubkey := ReadOrCreatePrivValidator(config)
chainID, validators, appState, cliPrint, err := gen(cdc, pubkey)
chainID, validators, appState, cliPrint, err := appInit.GenAppParams(cdc, pubkey)
if err != nil {
return err
}
@@ -100,11 +101,12 @@ func InitCmd(ctx *Context, cdc *wire.Codec, gen GenAppParams, appendState Append
return nil
},
}
if appendState != nil {
cmd.AddCommand(FromPiecesCmd(ctx, cdc, appendState))
if appInit.AppendAppState != nil {
cmd.AddCommand(FromPiecesCmd(ctx, cdc, appInit))
cmd.Flags().StringP(flagPieceFile, "a", "", "create an append file for others to import")
}
cmd.Flags().BoolP(flagOverwrite, "o", false, "overwrite the config file")
cmd.Flags().AddFlagSet(appInit.Flags)
return cmd
}
@@ -118,7 +120,7 @@ type GenesisPiece struct {
}
// get cmd to initialize all files for tendermint and application
func FromPiecesCmd(ctx *Context, cdc *wire.Codec, appendState AppendAppState) *cobra.Command {
func FromPiecesCmd(ctx *Context, cdc *wire.Codec, appInit AppInit) *cobra.Command {
return &cobra.Command{
Use: "from-pieces [directory]",
Short: "Create genesis from directory of genesis pieces",
@@ -142,7 +144,7 @@ func FromPiecesCmd(ctx *Context, cdc *wire.Codec, appendState AppendAppState) *c
os.Remove(genFile)
// deterministically walk the directory for genesis-piece files to import
filepath.Walk(pieceDir, appendPiece(ctx, cdc, appendState, nodeKeyFile, genFile))
filepath.Walk(pieceDir, appendPiece(ctx, cdc, appInit, nodeKeyFile, genFile))
return nil
},
@@ -150,7 +152,7 @@ func FromPiecesCmd(ctx *Context, cdc *wire.Codec, appendState AppendAppState) *c
}
// append a genesis-piece
func appendPiece(ctx *Context, cdc *wire.Codec, appendState AppendAppState, nodeKeyFile, genFile string) filepath.WalkFunc {
func appendPiece(ctx *Context, cdc *wire.Codec, appInit AppInit, nodeKeyFile, genFile string) filepath.WalkFunc {
return func(pieceFile string, _ os.FileInfo, err error) error {
if err != nil {
return err
@@ -203,7 +205,7 @@ func appendPiece(ctx *Context, cdc *wire.Codec, appendState AppendAppState, node
validators = append(validators, piece.Validators...)
// combine the app state
appState, err = appendState(cdc, appState, piece.AppState)
appState, err = appInit.AppendAppState(cdc, appState, piece.AppState)
if err != nil {
return err
}
@@ -273,17 +275,30 @@ func addAppStateToGenesis(cdc *wire.Codec, genesisConfigPath string, appState js
//_____________________________________________________________________
// GenAppParams creates the core parameters initialization. It takes in a
// pubkey meant to represent the pubkey of the validator of this machine.
type GenAppParams func(*wire.Codec, crypto.PubKey) (chainID string, validators []tmtypes.GenesisValidator, appState, cliPrint json.RawMessage, err error)
// Core functionality passed from the application to the server init command
type AppInit struct {
// append appState1 with appState2
type AppendAppState func(cdc *wire.Codec, appState1, appState2 json.RawMessage) (appState json.RawMessage, err error)
// flags required for GenAppParams
Flags *pflag.FlagSet
// GenAppParams creates the core parameters initialization. It takes in a
// pubkey meant to represent the pubkey of the validator of this machine.
GenAppParams func(*wire.Codec, crypto.PubKey) (chainID string, validators []tmtypes.GenesisValidator, appState, cliPrint json.RawMessage, err error)
// append appState1 with appState2
AppendAppState func(cdc *wire.Codec, appState1, appState2 json.RawMessage) (appState json.RawMessage, err error)
}
// simple default application init
var DefaultAppInit = AppInit{
GenAppParams: SimpleGenAppParams,
}
// Create one account with a whole bunch of mycoin in it
func SimpleGenAppParams(cdc *wire.Codec, pubKey crypto.PubKey) (chainID string, validators []tmtypes.GenesisValidator, appState, cliPrint json.RawMessage, err error) {
var addr sdk.Address
var secret string
addr, secret, err = GenerateCoinKey()
if err != nil {
+4 -1
View File
@@ -20,7 +20,10 @@ func TestInit(t *testing.T) {
require.Nil(t, err)
ctx := NewContext(cfg, logger)
cdc := wire.NewCodec()
cmd := InitCmd(ctx, cdc, mock.GenAppParams, nil)
appInit := AppInit{
GenAppParams: mock.GenAppParams,
}
cmd := InitCmd(ctx, cdc, appInit)
err = cmd.RunE(nil, nil)
require.NoError(t, err)
}
+8 -2
View File
@@ -27,7 +27,10 @@ func TestStartStandAlone(t *testing.T) {
require.Nil(t, err)
ctx := NewContext(cfg, logger)
cdc := wire.NewCodec()
initCmd := InitCmd(ctx, cdc, mock.GenAppParams, nil)
appInit := AppInit{
GenAppParams: mock.GenAppParams,
}
initCmd := InitCmd(ctx, cdc, appInit)
err = initCmd.RunE(nil, nil)
require.NoError(t, err)
@@ -54,7 +57,10 @@ func TestStartWithTendermint(t *testing.T) {
require.Nil(t, err)
ctx := NewContext(cfg, logger)
cdc := wire.NewCodec()
initCmd := InitCmd(ctx, cdc, mock.GenAppParams, nil)
appInit := AppInit{
GenAppParams: mock.GenAppParams,
}
initCmd := InitCmd(ctx, cdc, appInit)
err = initCmd.RunE(nil, nil)
require.NoError(t, err)
+3 -3
View File
@@ -67,13 +67,13 @@ func PersistentPreRunEFn(context *Context) func(*cobra.Command, []string) error
// add server commands
func AddCommands(
ctx *Context, cdc *wire.Codec,
rootCmd *cobra.Command,
appState GenAppParams, appCreator AppCreator) {
rootCmd *cobra.Command, appInit AppInit,
appCreator AppCreator) {
rootCmd.PersistentFlags().String("log_level", ctx.Config.LogLevel, "Log level")
rootCmd.AddCommand(
InitCmd(ctx, cdc, appState, nil),
InitCmd(ctx, cdc, appInit),
StartCmd(ctx, appCreator),
UnsafeResetAllCmd(ctx),
ShowNodeIDCmd(ctx),