From 4e13a19339a033c405435b4180447d9f4cbbd147 Mon Sep 17 00:00:00 2001 From: Adrian Brink Date: Wed, 13 Sep 2017 08:32:53 +0200 Subject: [PATCH] Add ability to construct new instance of Tendermint core from scratch --- cmd/tendermint/commands/run_node.go | 46 +++++------------------------ cmd/tendermint/main.go | 10 ++++--- types/priv_validator.go | 8 +++-- 3 files changed, 19 insertions(+), 45 deletions(-) diff --git a/cmd/tendermint/commands/run_node.go b/cmd/tendermint/commands/run_node.go index fd33faefe..d52f2977c 100644 --- a/cmd/tendermint/commands/run_node.go +++ b/cmd/tendermint/commands/run_node.go @@ -2,6 +2,7 @@ package commands import ( "fmt" + "time" "github.com/spf13/cobra" @@ -12,10 +13,6 @@ import ( "github.com/tendermint/tendermint/types" ) -func init() { - AddNodeFlags(RunNodeCmd) -} - // AddNodeFlags exposes some common configuration options on the command-line // These are exposed for convenience of commands embedding a tendermint node func AddNodeFlags(cmd *cobra.Command) { @@ -44,24 +41,13 @@ func AddNodeFlags(cmd *cobra.Command) { cmd.Flags().Bool("consensus.create_empty_blocks", config.Consensus.CreateEmptyBlocks, "Set this to false to only produce blocks when there are txs or when the AppHash changes") } -// RunNodeCmd creates and starts a tendermint node. -var RunNodeCmd = &cobra.Command{ - Use: "node", - Short: "Run the tendermint node", - RunE: runNode, -} - // NewRunNodeCmd returns the command that allows the CLI to start a // node. It can be used with a custom PrivValidator. func NewRunNodeCmd(privVal *types.PrivValidator) *cobra.Command { - return &cobra.Command{ + cmd := &cobra.Command{ Use: "node", Short: "Run the tendermint node", RunE: func(cmd *cobra.Command, args []string) error { - // Wait until the genesis doc becomes available - // This is for Mintnet compatibility. - // TODO: If Mintnet gets deprecated or genesis_file is - // always available, remove. genDocFile := config.GenesisFile() for !cmn.FileExists(genDocFile) { logger.Info(cmn.Fmt("Waiting for genesis file %v...", genDocFile)) @@ -79,7 +65,8 @@ func NewRunNodeCmd(privVal *types.PrivValidator) *cobra.Command { if privVal == nil { n = node.NewNodeDefault(config, logger.With("module", "node")) } - n = node.NewNode(config, privVal, proxy.DefaultClientCreator(config.ProxyApp, config.ABCI, config.DBDir()), logger) + n = node.NewNode(config, privVal, proxy.DefaultClientCreator(config.ProxyApp, + config.ABCI, config.DBDir()), logger.With("module", "node")) if _, err := n.Start(); err != nil { return fmt.Errorf("Failed to start node: %v", err) @@ -93,6 +80,9 @@ func NewRunNodeCmd(privVal *types.PrivValidator) *cobra.Command { return nil }, } + + AddNodeFlags(cmd) + return cmd } // Users wishing to: @@ -101,25 +91,3 @@ func NewRunNodeCmd(privVal *types.PrivValidator) *cobra.Command { // should import github.com/tendermint/tendermint/node and implement // their own run_node to call node.NewNode (instead of node.NewNodeDefault) // with their custom priv validator and/or custom proxy.ClientCreator -func runNode(cmd *cobra.Command, args []string) error { - - genDocFile := config.GenesisFile() - genDoc, err := types.GenesisDocFromFile(genDocFile) - if err != nil { - return err - } - config.ChainID = genDoc.ChainID - - // Create & start node - n := node.NewNodeDefault(config, logger.With("module", "node")) - if _, err := n.Start(); err != nil { - return fmt.Errorf("Failed to start node: %v", err) - } else { - logger.Info("Started node", "nodeInfo", n.Switch().NodeInfo()) - } - - // Trap signal, run forever. - n.RunForever() - - return nil -} diff --git a/cmd/tendermint/main.go b/cmd/tendermint/main.go index d70df634d..e231f6c67 100644 --- a/cmd/tendermint/main.go +++ b/cmd/tendermint/main.go @@ -19,12 +19,14 @@ func main() { // NOTE: Implement your own type that implements the Signer interface // and then instantiate it here. - // signer := types.NewDefaultSigner(pk) - // privValidator := types.LoadPrivValidatorWithSigner(signer) - // rootCmd.AddCommand(NewRunNodeCmd(privValidator)) + /* + signer := types.NewDefaultSigner(pk) + privValidator := types.LoadPrivValidatorWithSigner(signer) + rootCmd.AddCommand(NewRunNodeCmd(privValidator)) + */ // Create & start node - rootCmd.AddCommand(RunNodeCmd) + rootCmd.AddCommand(NewRunNodeCmd(nil)) cmd := cli.PrepareBaseCmd(rootCmd, "TM", os.ExpandEnv("$HOME/.tendermint")) cmd.Execute() diff --git a/types/priv_validator.go b/types/priv_validator.go index ed0cde070..3944a9cdf 100644 --- a/types/priv_validator.go +++ b/types/priv_validator.go @@ -128,12 +128,16 @@ func GenPrivValidator() *PrivValidator { } } -func LoadPrivValidatorWithSigner(signer Signer) *PrivValidator { +// LoadPrivValidatorWithSigner instantiates a private validator with a custom +// signer object. Tendermint tracks state in the PrivValidator that might be +// saved to disk. Please supply a filepath where Tendermint can save the +// private validator. +func LoadPrivValidatorWithSigner(signer Signer, filePath string) *PrivValidator { return &PrivValidator{ Address: signer.PubKey().Address(), PubKey: signer.PubKey(), LastStep: stepNone, - filePath: "", + filePath: filePath, Signer: signer, } }