init refactor
This commit is contained in:
parent
6748aa7bc6
commit
55c1e1dcfc
|
@ -2,7 +2,6 @@ package app
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
|
||||||
|
|
||||||
abci "github.com/tendermint/abci/types"
|
abci "github.com/tendermint/abci/types"
|
||||||
crypto "github.com/tendermint/go-crypto"
|
crypto "github.com/tendermint/go-crypto"
|
||||||
|
@ -183,7 +182,7 @@ func (ga *GenesisAccount) ToAccount() (acc *auth.BaseAccount) {
|
||||||
|
|
||||||
// GaiaGenAppState expects two args: an account address
|
// GaiaGenAppState expects two args: an account address
|
||||||
// and a coin denomination, and gives lots of coins to that address.
|
// and a coin denomination, and gives lots of coins to that address.
|
||||||
func GaiaGenAppState(pubKey crypto.PubKey) (chainID string, validators []tmtypes.GenesisValidator, appState json.RawMessage, err error) {
|
func GaiaGenAppState(cdc *wire.Codec, pubKey crypto.PubKey) (chainID string, validators []tmtypes.GenesisValidator, appState, message json.RawMessage, err error) {
|
||||||
|
|
||||||
var addr sdk.Address
|
var addr sdk.Address
|
||||||
var secret string
|
var secret string
|
||||||
|
@ -191,7 +190,10 @@ func GaiaGenAppState(pubKey crypto.PubKey) (chainID string, validators []tmtypes
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
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))
|
chainID = cmn.Fmt("test-chain-%v", cmn.RandStr(6))
|
||||||
|
|
||||||
|
|
|
@ -15,16 +15,6 @@ import (
|
||||||
"github.com/cosmos/cosmos-sdk/server"
|
"github.com/cosmos/cosmos-sdk/server"
|
||||||
)
|
)
|
||||||
|
|
||||||
// rootCmd is the entry point for this binary
|
|
||||||
var (
|
|
||||||
context = server.NewDefaultContext()
|
|
||||||
rootCmd = &cobra.Command{
|
|
||||||
Use: "gaiad",
|
|
||||||
Short: "Gaia Daemon (server)",
|
|
||||||
PersistentPreRunE: server.PersistentPreRunEFn(context),
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
func generateApp(rootDir string, logger log.Logger) (abci.Application, error) {
|
func generateApp(rootDir string, logger log.Logger) (abci.Application, error) {
|
||||||
dataDir := filepath.Join(rootDir, "data")
|
dataDir := filepath.Join(rootDir, "data")
|
||||||
db, err := dbm.NewGoLevelDB("gaia", dataDir)
|
db, err := dbm.NewGoLevelDB("gaia", dataDir)
|
||||||
|
@ -36,7 +26,15 @@ func generateApp(rootDir string, logger log.Logger) (abci.Application, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
server.AddCommands(rootCmd, app.GaiaGenAppState, generateApp, context)
|
cdc := app.MakeCodec()
|
||||||
|
ctx := server.NewDefaultContext()
|
||||||
|
rootCmd := &cobra.Command{
|
||||||
|
Use: "gaiad",
|
||||||
|
Short: "Gaia Daemon (server)",
|
||||||
|
PersistentPreRunE: server.PersistentPreRunEFn(ctx),
|
||||||
|
}
|
||||||
|
|
||||||
|
server.AddCommands(ctx, cdc, rootCmd, app.GaiaGenAppState, generateApp)
|
||||||
|
|
||||||
// prepare and add flags
|
// prepare and add flags
|
||||||
rootDir := os.ExpandEnv("$HOME/.gaiad")
|
rootDir := os.ExpandEnv("$HOME/.gaiad")
|
||||||
|
|
|
@ -15,15 +15,23 @@ import (
|
||||||
"github.com/cosmos/cosmos-sdk/server"
|
"github.com/cosmos/cosmos-sdk/server"
|
||||||
)
|
)
|
||||||
|
|
||||||
// rootCmd is the entry point for this binary
|
func main() {
|
||||||
var (
|
cdc := app.MakeCodec()
|
||||||
context = server.NewDefaultContext()
|
ctx := server.NewDefaultContext()
|
||||||
rootCmd = &cobra.Command{
|
|
||||||
|
rootCmd := &cobra.Command{
|
||||||
Use: "basecoind",
|
Use: "basecoind",
|
||||||
Short: "Basecoin Daemon (server)",
|
Short: "Basecoin Daemon (server)",
|
||||||
PersistentPreRunE: server.PersistentPreRunEFn(context),
|
PersistentPreRunE: server.PersistentPreRunEFn(ctx),
|
||||||
}
|
}
|
||||||
)
|
|
||||||
|
server.AddCommands(ctx, cdc, rootCmd, server.SimpleGenAppState, generateApp)
|
||||||
|
|
||||||
|
// prepare and add flags
|
||||||
|
rootDir := os.ExpandEnv("$HOME/.basecoind")
|
||||||
|
executor := cli.PrepareBaseCmd(rootCmd, "BC", rootDir)
|
||||||
|
executor.Execute()
|
||||||
|
}
|
||||||
|
|
||||||
func generateApp(rootDir string, logger log.Logger) (abci.Application, error) {
|
func generateApp(rootDir string, logger log.Logger) (abci.Application, error) {
|
||||||
dataDir := filepath.Join(rootDir, "data")
|
dataDir := filepath.Join(rootDir, "data")
|
||||||
|
@ -34,12 +42,3 @@ func generateApp(rootDir string, logger log.Logger) (abci.Application, error) {
|
||||||
bapp := app.NewBasecoinApp(logger, db)
|
bapp := app.NewBasecoinApp(logger, db)
|
||||||
return bapp, nil
|
return bapp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
|
||||||
server.AddCommands(rootCmd, server.DefaultGenAppState, generateApp, context)
|
|
||||||
|
|
||||||
// prepare and add flags
|
|
||||||
rootDir := os.ExpandEnv("$HOME/.basecoind")
|
|
||||||
executor := cli.PrepareBaseCmd(rootCmd, "BC", rootDir)
|
|
||||||
executor.Execute()
|
|
||||||
}
|
|
||||||
|
|
|
@ -8,42 +8,29 @@ import (
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
abci "github.com/tendermint/abci/types"
|
abci "github.com/tendermint/abci/types"
|
||||||
|
crypto "github.com/tendermint/go-crypto"
|
||||||
|
tmtypes "github.com/tendermint/tendermint/types"
|
||||||
"github.com/tendermint/tmlibs/cli"
|
"github.com/tendermint/tmlibs/cli"
|
||||||
dbm "github.com/tendermint/tmlibs/db"
|
dbm "github.com/tendermint/tmlibs/db"
|
||||||
"github.com/tendermint/tmlibs/log"
|
"github.com/tendermint/tmlibs/log"
|
||||||
|
|
||||||
"github.com/cosmos/cosmos-sdk/examples/democoin/app"
|
"github.com/cosmos/cosmos-sdk/examples/democoin/app"
|
||||||
"github.com/cosmos/cosmos-sdk/server"
|
"github.com/cosmos/cosmos-sdk/server"
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
"github.com/cosmos/cosmos-sdk/wire"
|
||||||
)
|
)
|
||||||
|
|
||||||
// rootCmd is the entry point for this binary
|
// coolGenAppState sets up the app_state and appends the cool app state
|
||||||
var (
|
func CoolGenAppState(cdc *wire.Codec, pubKey crypto.PubKey) (chainID string, validators []tmtypes.GenesisValidator, appState, message json.RawMessage, err error) {
|
||||||
context = server.NewDefaultContext()
|
chainID, validators, appState, message, err = server.SimpleGenAppState(cdc, pubKey)
|
||||||
rootCmd = &cobra.Command{
|
|
||||||
Use: "democoind",
|
|
||||||
Short: "Democoin Daemon (server)",
|
|
||||||
PersistentPreRunE: server.PersistentPreRunEFn(context),
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
// defaultAppState sets up the app_state for the
|
|
||||||
// default genesis file
|
|
||||||
func defaultAppState(args []string, addr sdk.Address, coinDenom string) (json.RawMessage, error) {
|
|
||||||
baseJSON, err := server.DefaultGenAppState(args, addr, coinDenom)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return
|
||||||
}
|
}
|
||||||
var jsonMap map[string]json.RawMessage
|
key := "cool"
|
||||||
err = json.Unmarshal(baseJSON, &jsonMap)
|
value := json.RawMessage(`{
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
jsonMap["cool"] = json.RawMessage(`{
|
|
||||||
"trend": "ice-cold"
|
"trend": "ice-cold"
|
||||||
}`)
|
}`)
|
||||||
bz, err := json.Marshal(jsonMap)
|
appState, err = server.AppendJSON(cdc, appState, key, value)
|
||||||
return json.RawMessage(bz), err
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateApp(rootDir string, logger log.Logger) (abci.Application, error) {
|
func generateApp(rootDir string, logger log.Logger) (abci.Application, error) {
|
||||||
|
@ -56,7 +43,16 @@ func generateApp(rootDir string, logger log.Logger) (abci.Application, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
server.AddCommands(rootCmd, defaultAppState, generateApp, context)
|
cdc := app.MakeCodec()
|
||||||
|
ctx := server.NewDefaultContext()
|
||||||
|
|
||||||
|
rootCmd := &cobra.Command{
|
||||||
|
Use: "democoind",
|
||||||
|
Short: "Democoin Daemon (server)",
|
||||||
|
PersistentPreRunE: server.PersistentPreRunEFn(ctx),
|
||||||
|
}
|
||||||
|
|
||||||
|
server.AddCommands(ctx, cdc, rootCmd, CoolGenAppState, generateApp)
|
||||||
|
|
||||||
// prepare and add flags
|
// prepare and add flags
|
||||||
rootDir := os.ExpandEnv("$HOME/.democoind")
|
rootDir := os.ExpandEnv("$HOME/.democoind")
|
||||||
|
|
14
mock/app.go
14
mock/app.go
|
@ -14,11 +14,11 @@ import (
|
||||||
|
|
||||||
bam "github.com/cosmos/cosmos-sdk/baseapp"
|
bam "github.com/cosmos/cosmos-sdk/baseapp"
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
|
"github.com/cosmos/cosmos-sdk/wire"
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewApp creates a simple mock kvstore app for testing.
|
// NewApp creates a simple mock kvstore app for testing. It should work
|
||||||
// It should work similar to a real app.
|
// similar to a real app. Make sure rootDir is empty before running the test,
|
||||||
// Make sure rootDir is empty before running the test,
|
|
||||||
// in order to guarantee consistent results
|
// in order to guarantee consistent results
|
||||||
func NewApp(rootDir string, logger log.Logger) (abci.Application, error) {
|
func NewApp(rootDir string, logger log.Logger) (abci.Application, error) {
|
||||||
db, err := dbm.NewGoLevelDB("mock", filepath.Join(rootDir, "data"))
|
db, err := dbm.NewGoLevelDB("mock", filepath.Join(rootDir, "data"))
|
||||||
|
@ -108,16 +108,16 @@ func InitChainer(key sdk.StoreKey) func(sdk.Context, abci.RequestInitChain) abci
|
||||||
|
|
||||||
// GenAppState can be passed into InitCmd, returns a static string of a few
|
// GenAppState can be passed into InitCmd, returns a static string of a few
|
||||||
// key-values that can be parsed by InitChainer
|
// key-values that can be parsed by InitChainer
|
||||||
func GenAppState(pubKey crypto.PubKey) (chainID string, validators []tmtypes.GenesisValidator, appState json.RawMessage, err error) {
|
func GenAppState(_ *wire.Codec, pubKey crypto.PubKey) (chainID string, validators []tmtypes.GenesisValidator, appState, message json.RawMessage, err error) {
|
||||||
|
|
||||||
chainID = cmn.Fmt("test-chain-%v", cmn.RandStr(6))
|
chainID = fmt.Sprintf("test-chain-%v", cmn.RandStr(6))
|
||||||
|
|
||||||
validators = []tmtypes.GenesisValidator{{
|
validators = []tmtypes.GenesisValidator{{
|
||||||
PubKey: pubKey,
|
PubKey: pubKey,
|
||||||
Power: 10,
|
Power: 10,
|
||||||
}}
|
}}
|
||||||
|
|
||||||
appState = json.RawMessage(fmt.Sprintf(`{
|
appState = json.RawMessage(`{
|
||||||
"values": [
|
"values": [
|
||||||
{
|
{
|
||||||
"key": "hello",
|
"key": "hello",
|
||||||
|
@ -128,6 +128,6 @@ func GenAppState(pubKey crypto.PubKey) (chainID string, validators []tmtypes.Gen
|
||||||
"value": "bar"
|
"value": "bar"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}`))
|
}`)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@ import (
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
abci "github.com/tendermint/abci/types"
|
abci "github.com/tendermint/abci/types"
|
||||||
|
crypto "github.com/tendermint/go-crypto"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TestInitApp makes sure we can initialize this thing without an error
|
// TestInitApp makes sure we can initialize this thing without an error
|
||||||
|
@ -21,9 +22,13 @@ func TestInitApp(t *testing.T) {
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
// initialize it future-way
|
// initialize it future-way
|
||||||
opts, err := GenInitOptions(nil, nil, "")
|
pubKey := crypto.GenPrivKeyEd25519().PubKey()
|
||||||
|
_, _, appState, _, err := GenAppState(nil, pubKey)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
req := abci.RequestInitChain{AppStateBytes: opts}
|
//TODO test validators in the init chain?
|
||||||
|
req := abci.RequestInitChain{
|
||||||
|
AppStateBytes: appState,
|
||||||
|
}
|
||||||
app.InitChain(req)
|
app.InitChain(req)
|
||||||
app.Commit()
|
app.Commit()
|
||||||
|
|
||||||
|
|
|
@ -21,7 +21,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// get cmd to initialize all files for tendermint and application
|
// 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{
|
cobraCmd := cobra.Command{
|
||||||
Use: "init",
|
Use: "init",
|
||||||
Short: "Initialize genesis files",
|
Short: "Initialize genesis files",
|
||||||
|
@ -30,12 +30,12 @@ func InitCmd(gen GenAppParams, ctx *Context) *cobra.Command {
|
||||||
config := ctx.Config
|
config := ctx.Config
|
||||||
pubkey := ReadOrCreatePrivValidator(config)
|
pubkey := ReadOrCreatePrivValidator(config)
|
||||||
|
|
||||||
chainID, validators, appState, err := gen(pubkey)
|
chainID, validators, appState, message, err := gen(cdc, pubkey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = CreateGenesisFile(config, chainID, validators, appState)
|
err = CreateGenesisFile(config, cdc, chainID, validators, appState)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -47,11 +47,13 @@ func InitCmd(gen GenAppParams, ctx *Context) *cobra.Command {
|
||||||
|
|
||||||
// print out some key information
|
// print out some key information
|
||||||
toPrint := struct {
|
toPrint := struct {
|
||||||
ChainID string `json:"chain_id"`
|
ChainID string `json:"chain_id"`
|
||||||
NodeID string `json:"node_id"`
|
NodeID string `json:"node_id"`
|
||||||
|
Message json.RawMessage `json:"message"`
|
||||||
}{
|
}{
|
||||||
chainID,
|
chainID,
|
||||||
string(nodeKey.ID()),
|
string(nodeKey.ID()),
|
||||||
|
message,
|
||||||
}
|
}
|
||||||
out, err := wire.MarshalJSONIndent(cdc, toPrint)
|
out, err := wire.MarshalJSONIndent(cdc, toPrint)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -79,7 +81,7 @@ func ReadOrCreatePrivValidator(tmConfig *cfg.Config) crypto.PubKey {
|
||||||
}
|
}
|
||||||
|
|
||||||
// create the genesis file
|
// 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()
|
genFile := tmConfig.GenesisFile()
|
||||||
if cmn.FileExists(genFile) {
|
if cmn.FileExists(genFile) {
|
||||||
return fmt.Errorf("genesis config file already exists: %v", 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 {
|
if err := genDoc.SaveAs(genFile); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return addAppStateToGenesis(genFile, appState)
|
return addAppStateToGenesis(cdc, genFile, appState)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add one line to the genesis file
|
// 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)
|
bz, err := ioutil.ReadFile(genesisConfigPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
out, err := AppendJSON(cdc, bz, "app_state", appState)
|
||||||
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)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -122,10 +116,10 @@ func addAppStateToGenesis(genesisConfigPath string, appState json.RawMessage) er
|
||||||
|
|
||||||
// GenAppParams creates the core parameters initialization. It takes in a
|
// GenAppParams creates the core parameters initialization. It takes in a
|
||||||
// pubkey meant to represent the pubkey of the validator of this machine.
|
// 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
|
// 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 addr sdk.Address
|
||||||
var secret string
|
var secret string
|
||||||
|
@ -133,7 +127,10 @@ func SimpleGenAppState(pubKey crypto.PubKey) (chainID string, validators []tmtyp
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
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))
|
chainID = cmn.Fmt("test-chain-%v", cmn.RandStr(6))
|
||||||
|
|
||||||
|
|
|
@ -18,7 +18,7 @@ func TestInit(t *testing.T) {
|
||||||
cfg, err := tcmd.ParseConfig()
|
cfg, err := tcmd.ParseConfig()
|
||||||
require.Nil(t, err)
|
require.Nil(t, err)
|
||||||
ctx := NewContext(cfg, logger)
|
ctx := NewContext(cfg, logger)
|
||||||
cmd := InitCmd(mock.GenInitOptions, ctx)
|
cmd := InitCmd(ctx, cdc, mock.GenAppState)
|
||||||
err = cmd.RunE(nil, nil)
|
err = cmd.RunE(nil, nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,45 +27,34 @@ type AppCreator func(string, log.Logger) (abci.Application, error)
|
||||||
|
|
||||||
// StartCmd runs the service passed in, either
|
// StartCmd runs the service passed in, either
|
||||||
// stand-alone, or in-process with tendermint
|
// stand-alone, or in-process with tendermint
|
||||||
func StartCmd(app AppCreator, ctx *Context) *cobra.Command {
|
func StartCmd(ctx *Context, appCreator AppCreator) *cobra.Command {
|
||||||
start := startCmd{
|
|
||||||
appCreator: app,
|
|
||||||
context: ctx,
|
|
||||||
}
|
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "start",
|
Use: "start",
|
||||||
Short: "Run the full node",
|
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
|
// basic flags for abci app
|
||||||
cmd.Flags().Bool(flagWithTendermint, true, "run abci app embedded in-process with tendermint")
|
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")
|
cmd.Flags().String(flagAddress, "tcp://0.0.0.0:46658", "Listen address")
|
||||||
|
|
||||||
// AddNodeFlags adds support for all
|
// AddNodeFlags adds support for all tendermint-specific command line options
|
||||||
// tendermint-specific command line options
|
|
||||||
tcmd.AddNodeFlags(cmd)
|
tcmd.AddNodeFlags(cmd)
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
type startCmd struct {
|
func startStandAlone(ctx *Context, appCreator AppCreator) error {
|
||||||
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 {
|
|
||||||
// Generate the app in the proper dir
|
// Generate the app in the proper dir
|
||||||
addr := viper.GetString(flagAddress)
|
addr := viper.GetString(flagAddress)
|
||||||
home := viper.GetString("home")
|
home := viper.GetString("home")
|
||||||
app, err := s.appCreator(home, s.context.Logger)
|
app, err := appCreator(home, ctx.Logger)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -74,7 +63,7 @@ func (s startCmd) startStandAlone() error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Errorf("Error creating listener: %v\n", err)
|
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()
|
svr.Start()
|
||||||
|
|
||||||
// Wait forever
|
// Wait forever
|
||||||
|
@ -85,10 +74,10 @@ func (s startCmd) startStandAlone() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s startCmd) startInProcess() error {
|
func startInProcess(ctx *Context, appCreator AppCreator) error {
|
||||||
cfg := s.context.Config
|
cfg := ctx.Config
|
||||||
home := cfg.RootDir
|
home := cfg.RootDir
|
||||||
app, err := s.appCreator(home, s.context.Logger)
|
app, err := appCreator(home, ctx.Logger)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -99,7 +88,7 @@ func (s startCmd) startInProcess() error {
|
||||||
proxy.NewLocalClientCreator(app),
|
proxy.NewLocalClientCreator(app),
|
||||||
node.DefaultGenesisDocProviderFunc(cfg),
|
node.DefaultGenesisDocProviderFunc(cfg),
|
||||||
node.DefaultDBProvider,
|
node.DefaultDBProvider,
|
||||||
s.context.Logger.With("module", "node"))
|
ctx.Logger.With("module", "node"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,7 +25,7 @@ func TestStartStandAlone(t *testing.T) {
|
||||||
cfg, err := tcmd.ParseConfig()
|
cfg, err := tcmd.ParseConfig()
|
||||||
require.Nil(t, err)
|
require.Nil(t, err)
|
||||||
ctx := NewContext(cfg, logger)
|
ctx := NewContext(cfg, logger)
|
||||||
initCmd := InitCmd(mock.GenInitOptions, ctx)
|
initCmd := InitCmd(ctx, cdc, mock.GenAppState)
|
||||||
err = initCmd.RunE(nil, nil)
|
err = initCmd.RunE(nil, nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
@ -51,13 +51,13 @@ func TestStartWithTendermint(t *testing.T) {
|
||||||
cfg, err := tcmd.ParseConfig()
|
cfg, err := tcmd.ParseConfig()
|
||||||
require.Nil(t, err)
|
require.Nil(t, err)
|
||||||
ctx := NewContext(cfg, logger)
|
ctx := NewContext(cfg, logger)
|
||||||
initCmd := InitCmd(mock.GenInitOptions, ctx)
|
initCmd := InitCmd(ctx, cdc, mock.GenAppState)
|
||||||
err = initCmd.RunE(nil, nil)
|
err = initCmd.RunE(nil, nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
// set up app and start up
|
// set up app and start up
|
||||||
viper.Set(flagWithTendermint, true)
|
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
|
startCmd.Flags().Set(flagAddress, FreeTCPAddr(t)) // set to a new free address
|
||||||
timeout := time.Duration(5) * time.Second
|
timeout := time.Duration(5) * time.Second
|
||||||
|
|
||||||
|
|
|
@ -8,13 +8,10 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/cosmos/cosmos-sdk/mock"
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands"
|
|
||||||
"github.com/tendermint/tmlibs/cli"
|
"github.com/tendermint/tmlibs/cli"
|
||||||
"github.com/tendermint/tmlibs/log"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Get a free address for a test tendermint server
|
// 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
|
// 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
|
// NOTE pass up the channel so it can be closed at the end of the process
|
||||||
func StartServer(t *testing.T) chan error {
|
//func StartServer(t *testing.T, cdc *wire.Codec) chan error {
|
||||||
defer setupViper(t)()
|
//defer setupViper(t)()
|
||||||
|
|
||||||
cfg, err := tcmd.ParseConfig()
|
//cfg, err := tcmd.ParseConfig()
|
||||||
require.Nil(t, err)
|
//require.Nil(t, err)
|
||||||
|
|
||||||
// init server
|
//// init server
|
||||||
ctx := NewContext(cfg, log.NewNopLogger())
|
//ctx := NewContext(cfg, log.NewNopLogger())
|
||||||
initCmd := InitCmd(mock.GenAppState, ctx)
|
//initCmd := InitCmd(ctx, cdc, mock.GenAppState)
|
||||||
err = initCmd.RunE(nil, nil)
|
//err = initCmd.RunE(nil, nil)
|
||||||
require.NoError(t, err)
|
//require.NoError(t, err)
|
||||||
|
|
||||||
// start server
|
//// start server
|
||||||
viper.Set(flagWithTendermint, true)
|
//viper.Set(flagWithTendermint, true)
|
||||||
startCmd := StartCmd(mock.NewApp, ctx)
|
//startCmd := StartCmd(mock.NewApp, ctx)
|
||||||
startCmd.Flags().Set(flagAddress, FreeTCPAddr(t)) // set to a new free address
|
//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
|
//startCmd.Flags().Set("rpc.laddr", FreeTCPAddr(t)) // set to a new free address
|
||||||
timeout := time.Duration(3) * time.Second
|
//timeout := time.Duration(3) * time.Second
|
||||||
|
|
||||||
return RunOrTimeout(startCmd, timeout, t)
|
//return RunOrTimeout(startCmd, timeout, t)
|
||||||
}
|
//}
|
||||||
|
|
||||||
// Run or Timout RunE of command passed in
|
// Run or Timout RunE of command passed in
|
||||||
func RunOrTimeout(cmd *cobra.Command, timeout time.Duration, t *testing.T) chan error {
|
func RunOrTimeout(cmd *cobra.Command, timeout time.Duration, t *testing.T) chan error {
|
||||||
|
|
|
@ -1,12 +1,14 @@
|
||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
|
|
||||||
"github.com/cosmos/cosmos-sdk/version"
|
"github.com/cosmos/cosmos-sdk/version"
|
||||||
|
"github.com/cosmos/cosmos-sdk/wire"
|
||||||
tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands"
|
tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands"
|
||||||
cfg "github.com/tendermint/tendermint/config"
|
cfg "github.com/tendermint/tendermint/config"
|
||||||
"github.com/tendermint/tmlibs/cli"
|
"github.com/tendermint/tmlibs/cli"
|
||||||
|
@ -31,7 +33,7 @@ func NewContext(config *cfg.Config, logger log.Logger) *Context {
|
||||||
return &Context{config, logger}
|
return &Context{config, logger}
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------
|
//___________________________________________________________________________________
|
||||||
|
|
||||||
// PersistentPreRunEFn returns a PersistentPreRunE function for cobra
|
// PersistentPreRunEFn returns a PersistentPreRunE function for cobra
|
||||||
// that initailizes the passed in context with a properly configured
|
// 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
|
// add server commands
|
||||||
func AddCommands(
|
func AddCommands(
|
||||||
|
ctx *Context, cdc *wire.Codec,
|
||||||
rootCmd *cobra.Command,
|
rootCmd *cobra.Command,
|
||||||
appState GenAppParams, appCreator AppCreator,
|
appState GenAppParams, appCreator AppCreator) {
|
||||||
context *Context) {
|
|
||||||
|
|
||||||
rootCmd.PersistentFlags().String("log_level", context.Config.LogLevel, "Log level")
|
rootCmd.PersistentFlags().String("log_level", ctx.Config.LogLevel, "Log level")
|
||||||
|
|
||||||
rootCmd.AddCommand(
|
rootCmd.AddCommand(
|
||||||
InitCmd(appState, context),
|
InitCmd(ctx, cdc, appState),
|
||||||
StartCmd(appCreator, context),
|
StartCmd(ctx, appCreator),
|
||||||
UnsafeResetAllCmd(context),
|
UnsafeResetAllCmd(ctx),
|
||||||
ShowNodeIDCmd(context),
|
ShowNodeIDCmd(ctx),
|
||||||
ShowValidatorCmd(context),
|
ShowValidatorCmd(ctx),
|
||||||
version.VersionCmd,
|
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)
|
||||||
|
}
|
Loading…
Reference in New Issue