cosmos-sdk/server/commands/start.go

180 lines
4.5 KiB
Go
Raw Normal View History

package commands
2017-01-28 18:12:58 -08:00
import (
"fmt"
2017-01-29 21:27:57 -08:00
"os"
"path"
2017-01-28 18:12:58 -08:00
2017-04-15 09:07:27 -07:00
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
2017-01-28 18:12:58 -08:00
"github.com/tendermint/abci/server"
abci "github.com/tendermint/abci/types"
"github.com/tendermint/tmlibs/cli"
cmn "github.com/tendermint/tmlibs/common"
2017-01-29 11:41:21 -08:00
2017-06-26 05:18:36 -07:00
tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands"
2017-01-29 11:41:21 -08:00
"github.com/tendermint/tendermint/node"
"github.com/tendermint/tendermint/proxy"
2017-05-12 12:47:18 -07:00
"github.com/tendermint/tendermint/types"
2017-01-29 11:41:21 -08:00
sdk "github.com/cosmos/cosmos-sdk"
"github.com/cosmos/cosmos-sdk/app"
2017-01-28 18:12:58 -08:00
)
// StartCmd - command to start running the abci app (and tendermint)!
var StartCmd = &cobra.Command{
Use: "start",
Short: "Start this full node",
2017-04-15 09:07:27 -07:00
RunE: startCmd,
}
2017-10-11 11:06:57 -07:00
// GetTickStartCmd - initialize a command as the start command with tick
func GetTickStartCmd(tick app.Ticker) *cobra.Command {
2017-10-09 02:18:40 -07:00
startCmd := &cobra.Command{
Use: "start",
Short: "Start this full node",
RunE: startCmd,
}
startCmd.RunE = tickStartCmd(tick)
addStartFlag(startCmd)
return startCmd
}
2017-07-05 03:57:52 -07:00
// nolint TODO: move to config file
const EyesCacheSize = 10000
//nolint
const (
FlagAddress = "address"
FlagWithoutTendermint = "without-tendermint"
)
2017-07-04 04:43:25 -07:00
var (
2017-07-05 03:57:52 -07:00
// Handler - use a global to store the handler, so we can set it in main.
2017-07-04 04:43:25 -07:00
// TODO: figure out a cleaner way to register plugins
Handler sdk.Handler
2017-07-04 04:43:25 -07:00
)
func init() {
2017-10-05 12:09:12 -07:00
addStartFlag(StartCmd)
}
func addStartFlag(startCmd *cobra.Command) {
flags := startCmd.Flags()
flags.String(FlagAddress, "tcp://0.0.0.0:46658", "Listen address")
flags.Bool(FlagWithoutTendermint, false, "Only run abci app, assume external tendermint process")
// add all standard 'tendermint node' flags
2017-10-05 12:09:12 -07:00
tcmd.AddNodeFlags(startCmd)
2017-01-30 08:16:00 -08:00
}
//returns the start command which uses the tick
func tickStartCmd(tick app.Ticker) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
rootDir := viper.GetString(cli.HomeFlag)
store, err := app.NewStore(
path.Join(rootDir, "data", "merkleeyes.db"),
EyesCacheSize,
logger.With("module", "store"),
)
if err != nil {
return err
}
// Create Basecoin app
basecoinApp := app.NewBasecoinTick(Handler, store, logger.With("module", "app"), tick)
return start(rootDir, store, basecoinApp)
}
}
2017-04-15 09:07:27 -07:00
func startCmd(cmd *cobra.Command, args []string) error {
rootDir := viper.GetString(cli.HomeFlag)
2017-01-28 18:12:58 -08:00
2017-07-27 08:17:22 -07:00
store, err := app.NewStore(
path.Join(rootDir, "data", "merkleeyes.db"),
EyesCacheSize,
logger.With("module", "store"),
)
2017-07-27 08:17:22 -07:00
if err != nil {
return err
}
2017-01-28 18:12:58 -08:00
// Create Basecoin app
basecoinApp := app.NewBasecoin(Handler, store, logger.With("module", "app"))
return start(rootDir, store, basecoinApp)
}
func start(rootDir string, store *app.Store, basecoinApp *app.Basecoin) error {
2017-01-29 15:32:38 -08:00
2017-03-14 14:28:49 -07:00
// if chain_id has not been set yet, load the genesis.
// else, assume it's been loaded
if basecoinApp.GetChainID() == "" {
2017-03-14 14:28:49 -07:00
// If genesis file exists, set key-value options
genesisFile := path.Join(rootDir, "genesis.json")
2017-03-14 14:28:49 -07:00
if _, err := os.Stat(genesisFile); err == nil {
err := basecoinApp.LoadGenesis(genesisFile)
if err != nil {
2017-04-15 09:07:27 -07:00
return errors.Errorf("Error in LoadGenesis: %v\n", err)
2017-03-14 14:28:49 -07:00
}
} else {
fmt.Printf("No genesis file at %s, skipping...\n", genesisFile)
2017-01-28 18:12:58 -08:00
}
}
chainID := basecoinApp.GetChainID()
if viper.GetBool(FlagWithoutTendermint) {
2017-05-01 07:03:54 -07:00
logger.Info("Starting Basecoin without Tendermint", "chain_id", chainID)
// run just the abci app/server
2017-04-15 09:07:27 -07:00
return startBasecoinABCI(basecoinApp)
2017-01-29 11:41:21 -08:00
}
2017-07-05 03:57:52 -07:00
logger.Info("Starting Basecoin with Tendermint", "chain_id", chainID)
// start the app with tendermint in-process
return startTendermint(rootDir, basecoinApp)
2017-01-29 11:41:21 -08:00
}
func startBasecoinABCI(basecoinApp abci.Application) error {
2017-01-29 11:41:21 -08:00
// Start the ABCI listener
addr := viper.GetString(FlagAddress)
svr, err := server.NewServer(addr, "socket", basecoinApp)
2017-01-28 18:12:58 -08:00
if err != nil {
2017-04-15 09:07:27 -07:00
return errors.Errorf("Error creating listener: %v\n", err)
2017-01-28 18:12:58 -08:00
}
2017-05-01 07:03:54 -07:00
svr.SetLogger(logger.With("module", "abci-server"))
svr.Start()
2017-01-28 18:12:58 -08:00
// Wait forever
cmn.TrapSignal(func() {
// Cleanup
svr.Stop()
})
2017-04-15 09:07:27 -07:00
return nil
2017-01-29 11:41:21 -08:00
}
func startTendermint(dir string, basecoinApp abci.Application) error {
2017-06-26 05:18:36 -07:00
cfg, err := tcmd.ParseConfig()
if err != nil {
return err
}
2017-05-01 07:03:54 -07:00
2017-01-29 11:41:21 -08:00
// Create & start tendermint node
2017-10-02 11:40:42 -07:00
n, err := node.NewNode(cfg,
types.LoadOrGenPrivValidatorFS(cfg.PrivValidatorFile()),
proxy.NewLocalClientCreator(basecoinApp),
node.DefaultGenesisDocProviderFunc(cfg),
node.DefaultDBProvider,
logger.With("module", "node"))
if err != nil {
return err
}
2017-01-29 11:41:21 -08:00
_, err = n.Start()
2017-03-18 17:48:48 -07:00
if err != nil {
2017-04-17 18:28:03 -07:00
return err
2017-03-18 17:48:48 -07:00
}
2017-01-29 11:41:21 -08:00
2017-05-12 12:47:18 -07:00
// Trap signal, run forever.
n.RunForever()
2017-04-15 09:07:27 -07:00
return nil
2017-01-28 18:12:58 -08:00
}