init refactor
This commit is contained in:
+17
-20
@@ -21,7 +21,7 @@ import (
|
||||
)
|
||||
|
||||
// get cmd to initialize all files for tendermint and application
|
||||
func InitCmd(gen GenAppParams, ctx *Context) *cobra.Command {
|
||||
func InitCmd(ctx *Context, cdc *wire.Codec, gen GenAppParams) *cobra.Command {
|
||||
cobraCmd := cobra.Command{
|
||||
Use: "init",
|
||||
Short: "Initialize genesis files",
|
||||
@@ -30,12 +30,12 @@ func InitCmd(gen GenAppParams, ctx *Context) *cobra.Command {
|
||||
config := ctx.Config
|
||||
pubkey := ReadOrCreatePrivValidator(config)
|
||||
|
||||
chainID, validators, appState, err := gen(pubkey)
|
||||
chainID, validators, appState, message, err := gen(cdc, pubkey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = CreateGenesisFile(config, chainID, validators, appState)
|
||||
err = CreateGenesisFile(config, cdc, chainID, validators, appState)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -47,11 +47,13 @@ func InitCmd(gen GenAppParams, ctx *Context) *cobra.Command {
|
||||
|
||||
// print out some key information
|
||||
toPrint := struct {
|
||||
ChainID string `json:"chain_id"`
|
||||
NodeID string `json:"node_id"`
|
||||
ChainID string `json:"chain_id"`
|
||||
NodeID string `json:"node_id"`
|
||||
Message json.RawMessage `json:"message"`
|
||||
}{
|
||||
chainID,
|
||||
string(nodeKey.ID()),
|
||||
message,
|
||||
}
|
||||
out, err := wire.MarshalJSONIndent(cdc, toPrint)
|
||||
if err != nil {
|
||||
@@ -79,7 +81,7 @@ func ReadOrCreatePrivValidator(tmConfig *cfg.Config) crypto.PubKey {
|
||||
}
|
||||
|
||||
// create the genesis file
|
||||
func CreateGenesisFile(tmConfig *cfg.Config, chainID string, validators []tmtypes.GenesisValidator, appState json.RawMessage) error {
|
||||
func CreateGenesisFile(tmConfig *cfg.Config, cdc *wire.Codec, chainID string, validators []tmtypes.GenesisValidator, appState json.RawMessage) error {
|
||||
genFile := tmConfig.GenesisFile()
|
||||
if cmn.FileExists(genFile) {
|
||||
return fmt.Errorf("genesis config file already exists: %v", genFile)
|
||||
@@ -94,24 +96,16 @@ func CreateGenesisFile(tmConfig *cfg.Config, chainID string, validators []tmtype
|
||||
if err := genDoc.SaveAs(genFile); err != nil {
|
||||
return err
|
||||
}
|
||||
return addAppStateToGenesis(genFile, appState)
|
||||
return addAppStateToGenesis(cdc, genFile, appState)
|
||||
}
|
||||
|
||||
// Add one line to the genesis file
|
||||
func addAppStateToGenesis(genesisConfigPath string, appState json.RawMessage) error {
|
||||
func addAppStateToGenesis(cdc *wire.Codec, genesisConfigPath string, appState json.RawMessage) error {
|
||||
bz, err := ioutil.ReadFile(genesisConfigPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var doc map[string]json.RawMessage
|
||||
err = cdc.UnmarshalJSON(bz, &doc)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
doc["app_state"] = appState
|
||||
out, err := wire.MarshalJSONIndent(cdc, doc)
|
||||
out, err := AppendJSON(cdc, bz, "app_state", appState)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -122,10 +116,10 @@ func addAppStateToGenesis(genesisConfigPath string, appState json.RawMessage) er
|
||||
|
||||
// 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(crypto.PubKey) (chainID string, validators []tmtypes.GenesisValidator, appState json.RawMessage, err error)
|
||||
type GenAppParams func(*wire.Codec, crypto.PubKey) (chainID string, validators []tmtypes.GenesisValidator, appState, message json.RawMessage, err error)
|
||||
|
||||
// Create one account with a whole bunch of mycoin in it
|
||||
func SimpleGenAppState(pubKey crypto.PubKey) (chainID string, validators []tmtypes.GenesisValidator, appState json.RawMessage, err error) {
|
||||
func SimpleGenAppState(cdc *wire.Codec, pubKey crypto.PubKey) (chainID string, validators []tmtypes.GenesisValidator, appState, message json.RawMessage, err error) {
|
||||
|
||||
var addr sdk.Address
|
||||
var secret string
|
||||
@@ -133,7 +127,10 @@ func SimpleGenAppState(pubKey crypto.PubKey) (chainID string, validators []tmtyp
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
fmt.Printf("secret recovery key:\n%s\n", secret)
|
||||
|
||||
mm := map[string]string{"secret": secret}
|
||||
bz, err := cdc.MarshalJSON(mm)
|
||||
message = json.RawMessage(bz)
|
||||
|
||||
chainID = cmn.Fmt("test-chain-%v", cmn.RandStr(6))
|
||||
|
||||
|
||||
+1
-1
@@ -18,7 +18,7 @@ func TestInit(t *testing.T) {
|
||||
cfg, err := tcmd.ParseConfig()
|
||||
require.Nil(t, err)
|
||||
ctx := NewContext(cfg, logger)
|
||||
cmd := InitCmd(mock.GenInitOptions, ctx)
|
||||
cmd := InitCmd(ctx, cdc, mock.GenAppState)
|
||||
err = cmd.RunE(nil, nil)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
+18
-29
@@ -27,45 +27,34 @@ type AppCreator func(string, log.Logger) (abci.Application, error)
|
||||
|
||||
// StartCmd runs the service passed in, either
|
||||
// stand-alone, or in-process with tendermint
|
||||
func StartCmd(app AppCreator, ctx *Context) *cobra.Command {
|
||||
start := startCmd{
|
||||
appCreator: app,
|
||||
context: ctx,
|
||||
}
|
||||
func StartCmd(ctx *Context, appCreator AppCreator) *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "start",
|
||||
Short: "Run the full node",
|
||||
RunE: start.run,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
if !viper.GetBool(flagWithTendermint) {
|
||||
ctx.Logger.Info("Starting ABCI without Tendermint")
|
||||
return startStandAlone(ctx, appCreator)
|
||||
}
|
||||
ctx.Logger.Info("Starting ABCI with Tendermint")
|
||||
return startInProcess(ctx, appCreator)
|
||||
},
|
||||
}
|
||||
|
||||
// basic flags for abci app
|
||||
cmd.Flags().Bool(flagWithTendermint, true, "run abci app embedded in-process with tendermint")
|
||||
cmd.Flags().String(flagAddress, "tcp://0.0.0.0:46658", "Listen address")
|
||||
|
||||
// AddNodeFlags adds support for all
|
||||
// tendermint-specific command line options
|
||||
// AddNodeFlags adds support for all tendermint-specific command line options
|
||||
tcmd.AddNodeFlags(cmd)
|
||||
return cmd
|
||||
}
|
||||
|
||||
type startCmd struct {
|
||||
appCreator AppCreator
|
||||
context *Context
|
||||
}
|
||||
|
||||
func (s startCmd) run(cmd *cobra.Command, args []string) error {
|
||||
if !viper.GetBool(flagWithTendermint) {
|
||||
s.context.Logger.Info("Starting ABCI without Tendermint")
|
||||
return s.startStandAlone()
|
||||
}
|
||||
s.context.Logger.Info("Starting ABCI with Tendermint")
|
||||
return s.startInProcess()
|
||||
}
|
||||
|
||||
func (s startCmd) startStandAlone() error {
|
||||
func startStandAlone(ctx *Context, appCreator AppCreator) error {
|
||||
// Generate the app in the proper dir
|
||||
addr := viper.GetString(flagAddress)
|
||||
home := viper.GetString("home")
|
||||
app, err := s.appCreator(home, s.context.Logger)
|
||||
app, err := appCreator(home, ctx.Logger)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -74,7 +63,7 @@ func (s startCmd) startStandAlone() error {
|
||||
if err != nil {
|
||||
return errors.Errorf("Error creating listener: %v\n", err)
|
||||
}
|
||||
svr.SetLogger(s.context.Logger.With("module", "abci-server"))
|
||||
svr.SetLogger(ctx.Logger.With("module", "abci-server"))
|
||||
svr.Start()
|
||||
|
||||
// Wait forever
|
||||
@@ -85,10 +74,10 @@ func (s startCmd) startStandAlone() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s startCmd) startInProcess() error {
|
||||
cfg := s.context.Config
|
||||
func startInProcess(ctx *Context, appCreator AppCreator) error {
|
||||
cfg := ctx.Config
|
||||
home := cfg.RootDir
|
||||
app, err := s.appCreator(home, s.context.Logger)
|
||||
app, err := appCreator(home, ctx.Logger)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -99,7 +88,7 @@ func (s startCmd) startInProcess() error {
|
||||
proxy.NewLocalClientCreator(app),
|
||||
node.DefaultGenesisDocProviderFunc(cfg),
|
||||
node.DefaultDBProvider,
|
||||
s.context.Logger.With("module", "node"))
|
||||
ctx.Logger.With("module", "node"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ func TestStartStandAlone(t *testing.T) {
|
||||
cfg, err := tcmd.ParseConfig()
|
||||
require.Nil(t, err)
|
||||
ctx := NewContext(cfg, logger)
|
||||
initCmd := InitCmd(mock.GenInitOptions, ctx)
|
||||
initCmd := InitCmd(ctx, cdc, mock.GenAppState)
|
||||
err = initCmd.RunE(nil, nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
@@ -51,13 +51,13 @@ func TestStartWithTendermint(t *testing.T) {
|
||||
cfg, err := tcmd.ParseConfig()
|
||||
require.Nil(t, err)
|
||||
ctx := NewContext(cfg, logger)
|
||||
initCmd := InitCmd(mock.GenInitOptions, ctx)
|
||||
initCmd := InitCmd(ctx, cdc, mock.GenAppState)
|
||||
err = initCmd.RunE(nil, nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
// set up app and start up
|
||||
viper.Set(flagWithTendermint, true)
|
||||
startCmd := StartCmd(mock.NewApp, ctx)
|
||||
startCmd := StartCmd(ctx, mock.NewApp)
|
||||
startCmd.Flags().Set(flagAddress, FreeTCPAddr(t)) // set to a new free address
|
||||
timeout := time.Duration(5) * time.Second
|
||||
|
||||
|
||||
+17
-20
@@ -8,13 +8,10 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/mock"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
"github.com/stretchr/testify/require"
|
||||
tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands"
|
||||
"github.com/tendermint/tmlibs/cli"
|
||||
"github.com/tendermint/tmlibs/log"
|
||||
)
|
||||
|
||||
// Get a free address for a test tendermint server
|
||||
@@ -42,27 +39,27 @@ func setupViper(t *testing.T) func() {
|
||||
|
||||
// Begin the server pass up the channel to close
|
||||
// NOTE pass up the channel so it can be closed at the end of the process
|
||||
func StartServer(t *testing.T) chan error {
|
||||
defer setupViper(t)()
|
||||
//func StartServer(t *testing.T, cdc *wire.Codec) chan error {
|
||||
//defer setupViper(t)()
|
||||
|
||||
cfg, err := tcmd.ParseConfig()
|
||||
require.Nil(t, err)
|
||||
//cfg, err := tcmd.ParseConfig()
|
||||
//require.Nil(t, err)
|
||||
|
||||
// init server
|
||||
ctx := NewContext(cfg, log.NewNopLogger())
|
||||
initCmd := InitCmd(mock.GenAppState, ctx)
|
||||
err = initCmd.RunE(nil, nil)
|
||||
require.NoError(t, err)
|
||||
//// init server
|
||||
//ctx := NewContext(cfg, log.NewNopLogger())
|
||||
//initCmd := InitCmd(ctx, cdc, mock.GenAppState)
|
||||
//err = initCmd.RunE(nil, nil)
|
||||
//require.NoError(t, err)
|
||||
|
||||
// start server
|
||||
viper.Set(flagWithTendermint, true)
|
||||
startCmd := StartCmd(mock.NewApp, ctx)
|
||||
startCmd.Flags().Set(flagAddress, FreeTCPAddr(t)) // set to a new free address
|
||||
startCmd.Flags().Set("rpc.laddr", FreeTCPAddr(t)) // set to a new free address
|
||||
timeout := time.Duration(3) * time.Second
|
||||
//// start server
|
||||
//viper.Set(flagWithTendermint, true)
|
||||
//startCmd := StartCmd(mock.NewApp, ctx)
|
||||
//startCmd.Flags().Set(flagAddress, FreeTCPAddr(t)) // set to a new free address
|
||||
//startCmd.Flags().Set("rpc.laddr", FreeTCPAddr(t)) // set to a new free address
|
||||
//timeout := time.Duration(3) * time.Second
|
||||
|
||||
return RunOrTimeout(startCmd, timeout, t)
|
||||
}
|
||||
//return RunOrTimeout(startCmd, timeout, t)
|
||||
//}
|
||||
|
||||
// Run or Timout RunE of command passed in
|
||||
func RunOrTimeout(cmd *cobra.Command, timeout time.Duration, t *testing.T) chan error {
|
||||
|
||||
+25
-9
@@ -1,12 +1,14 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/version"
|
||||
"github.com/cosmos/cosmos-sdk/wire"
|
||||
tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands"
|
||||
cfg "github.com/tendermint/tendermint/config"
|
||||
"github.com/tendermint/tmlibs/cli"
|
||||
@@ -31,7 +33,7 @@ func NewContext(config *cfg.Config, logger log.Logger) *Context {
|
||||
return &Context{config, logger}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
//___________________________________________________________________________________
|
||||
|
||||
// PersistentPreRunEFn returns a PersistentPreRunE function for cobra
|
||||
// that initailizes the passed in context with a properly configured
|
||||
@@ -62,18 +64,32 @@ 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,
|
||||
context *Context) {
|
||||
appState GenAppParams, appCreator AppCreator) {
|
||||
|
||||
rootCmd.PersistentFlags().String("log_level", context.Config.LogLevel, "Log level")
|
||||
rootCmd.PersistentFlags().String("log_level", ctx.Config.LogLevel, "Log level")
|
||||
|
||||
rootCmd.AddCommand(
|
||||
InitCmd(appState, context),
|
||||
StartCmd(appCreator, context),
|
||||
UnsafeResetAllCmd(context),
|
||||
ShowNodeIDCmd(context),
|
||||
ShowValidatorCmd(context),
|
||||
InitCmd(ctx, cdc, appState),
|
||||
StartCmd(ctx, appCreator),
|
||||
UnsafeResetAllCmd(ctx),
|
||||
ShowNodeIDCmd(ctx),
|
||||
ShowValidatorCmd(ctx),
|
||||
version.VersionCmd,
|
||||
)
|
||||
}
|
||||
|
||||
//___________________________________________________________________________________
|
||||
|
||||
// append a new json field to existing json message
|
||||
func AppendJSON(cdc *wire.Codec, baseJSON []byte, key string, value json.RawMessage) (appended []byte, err error) {
|
||||
var jsonMap map[string]json.RawMessage
|
||||
err = cdc.UnmarshalJSON(baseJSON, &jsonMap)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
jsonMap[key] = value
|
||||
bz, err := wire.MarshalJSONIndent(cdc, jsonMap)
|
||||
return json.RawMessage(bz), err
|
||||
}
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/wire"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
//func AppendJSON(cdc *wire.Codec, baseJSON []byte, key string, value json.RawMessage) (appended []byte, err error) {
|
||||
|
||||
func TestAppendJSON(t *testing.T) {
|
||||
|
||||
foo := map[string]string{"foo": "foofoo"}
|
||||
bar := map[string]string{"barInner": "barbar"}
|
||||
|
||||
// create raw messages
|
||||
bz, err := cdc.MarshalJSON(foo)
|
||||
require.NoError(t, err)
|
||||
fooRaw := json.RawMessage(bz)
|
||||
|
||||
bz, err = cdc.MarshalJSON(bar)
|
||||
require.NoError(t, err)
|
||||
barRaw := json.RawMessage(bz)
|
||||
|
||||
// make the append
|
||||
cdc := wire.NewCodec()
|
||||
appBz, err := AppendJSON(cdc, fooRaw, "barOuter", barRaw)
|
||||
require.NoError(t, err)
|
||||
|
||||
// test the append
|
||||
var appended map[string]json.RawMessage
|
||||
err = cdc.UnmarshalJSON(appBz, &appended)
|
||||
require.NoError(t, err)
|
||||
|
||||
var resBar map[string]string
|
||||
err = cdc.UnmarshalJSON(appended["barOuter"], &resBar)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, bar, resBar, "appended: %v", appended)
|
||||
}
|
||||
Reference in New Issue
Block a user