2017-01-30 06:16:51 -08:00
|
|
|
package commands
|
2017-01-28 18:12:58 -08:00
|
|
|
|
|
|
|
import (
|
2017-02-16 09:03:01 -08:00
|
|
|
"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"
|
2017-03-08 23:19:07 -08:00
|
|
|
"github.com/spf13/cobra"
|
2017-05-12 11:29:13 -07:00
|
|
|
"github.com/spf13/viper"
|
2017-01-28 18:12:58 -08:00
|
|
|
|
2017-09-04 07:37:52 -07:00
|
|
|
"github.com/tendermint/abci/server"
|
2017-10-03 16:01:02 -07:00
|
|
|
abci "github.com/tendermint/abci/types"
|
2017-05-12 11:29:13 -07:00
|
|
|
"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
|
|
|
|
2017-10-03 16:01:02 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk"
|
2017-08-21 14:13:58 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/app"
|
2017-01-28 18:12:58 -08:00
|
|
|
)
|
|
|
|
|
2017-09-04 07:37:52 -07:00
|
|
|
// StartCmd - command to start running the abci app (and tendermint)!
|
2017-03-08 23:19:07 -08:00
|
|
|
var StartCmd = &cobra.Command{
|
|
|
|
Use: "start",
|
2017-09-04 07:37:52 -07:00
|
|
|
Short: "Start this full node",
|
2017-04-15 09:07:27 -07:00
|
|
|
RunE: startCmd,
|
2017-01-30 06:16:51 -08:00
|
|
|
}
|
|
|
|
|
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-10-03 16:01:02 -07:00
|
|
|
}
|
|
|
|
|
2017-07-05 03:57:52 -07:00
|
|
|
// nolint TODO: move to config file
|
2017-03-08 23:19:07 -08:00
|
|
|
const EyesCacheSize = 10000
|
|
|
|
|
2017-06-26 05:46:13 -07:00
|
|
|
//nolint
|
|
|
|
const (
|
|
|
|
FlagAddress = "address"
|
|
|
|
FlagWithoutTendermint = "without-tendermint"
|
|
|
|
)
|
2017-03-08 23:19:07 -08:00
|
|
|
|
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
|
2017-08-21 14:13:58 -07:00
|
|
|
Handler sdk.Handler
|
2017-07-04 04:43:25 -07:00
|
|
|
)
|
|
|
|
|
2017-06-26 05:46:13 -07:00
|
|
|
func init() {
|
2017-10-05 12:09:12 -07:00
|
|
|
addStartFlag(StartCmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
func addStartFlag(startCmd *cobra.Command) {
|
|
|
|
flags := startCmd.Flags()
|
2017-06-26 05:46:13 -07:00
|
|
|
flags.String(FlagAddress, "tcp://0.0.0.0:46658", "Listen address")
|
2017-09-04 07:37:52 -07:00
|
|
|
flags.Bool(FlagWithoutTendermint, false, "Only run abci app, assume external tendermint process")
|
2017-06-26 05:46:13 -07:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2017-10-03 16:01:02 -07: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 {
|
2017-05-12 11:29:13 -07:00
|
|
|
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(
|
2017-07-25 19:19:31 -07:00
|
|
|
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
|
2017-07-25 19:19:31 -07:00
|
|
|
basecoinApp := app.NewBasecoin(Handler, store, logger.With("module", "app"))
|
2017-10-03 16:01:02 -07:00
|
|
|
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
|
2017-07-25 19:19:31 -07:00
|
|
|
if basecoinApp.GetChainID() == "" {
|
2017-03-14 14:28:49 -07:00
|
|
|
// If genesis file exists, set key-value options
|
2017-05-12 11:29:13 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-25 19:19:31 -07:00
|
|
|
chainID := basecoinApp.GetChainID()
|
2017-06-26 05:46:13 -07:00
|
|
|
if viper.GetBool(FlagWithoutTendermint) {
|
2017-05-01 07:03:54 -07:00
|
|
|
logger.Info("Starting Basecoin without Tendermint", "chain_id", chainID)
|
2017-03-13 14:23:05 -07:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2017-10-03 16:01:02 -07:00
|
|
|
func startBasecoinABCI(basecoinApp abci.Application) error {
|
2017-01-29 11:41:21 -08:00
|
|
|
// Start the ABCI listener
|
2017-06-26 05:46:13 -07:00
|
|
|
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"))
|
2017-05-16 09:52:43 -07:00
|
|
|
svr.Start()
|
2017-03-08 23:19:07 -08:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-10-03 16:01:02 -07:00
|
|
|
func startTendermint(dir string, basecoinApp abci.Application) error {
|
2017-06-26 05:18:36 -07:00
|
|
|
cfg, err := tcmd.ParseConfig()
|
2017-05-22 04:30:42 -07:00
|
|
|
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
|
|
|
|
2017-05-12 11:29:13 -07: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
|
|
|
}
|