Browse Source

Add ability to construct new instance of Tendermint core from scratch

pull/637/head
Adrian Brink 7 years ago
committed by Ethan Buchman
parent
commit
4e13a19339
3 changed files with 19 additions and 45 deletions
  1. +7
    -39
      cmd/tendermint/commands/run_node.go
  2. +6
    -4
      cmd/tendermint/main.go
  3. +6
    -2
      types/priv_validator.go

+ 7
- 39
cmd/tendermint/commands/run_node.go View File

@ -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
}

+ 6
- 4
cmd/tendermint/main.go View File

@ -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()


+ 6
- 2
types/priv_validator.go View File

@ -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,
}
}


Loading…
Cancel
Save