From abe912c61095615a92f4c14056815734609cf718 Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Mon, 18 Sep 2017 17:40:24 -0400 Subject: [PATCH] FuncSignerAndApp allows custom signer and abci app --- cmd/tendermint/commands/run_node.go | 30 +++++++++++----------- cmd/tendermint/main.go | 39 ++++++++++++++++------------- types/priv_validator.go | 6 ++--- 3 files changed, 40 insertions(+), 35 deletions(-) diff --git a/cmd/tendermint/commands/run_node.go b/cmd/tendermint/commands/run_node.go index 3eac5d981..b9d23a27a 100644 --- a/cmd/tendermint/commands/run_node.go +++ b/cmd/tendermint/commands/run_node.go @@ -5,6 +5,7 @@ import ( "github.com/spf13/cobra" + cfg "github.com/tendermint/tendermint/config" "github.com/tendermint/tendermint/node" "github.com/tendermint/tendermint/proxy" "github.com/tendermint/tendermint/types" @@ -38,9 +39,19 @@ 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") } +// FuncSignerAndApp takes a config and returns a PrivValidator and ClientCreator. +// It allows other projects to make Tendermint binaries with custom signers and applications. +type FuncSignerAndApp func(*cfg.Config) (*types.PrivValidator, proxy.ClientCreator) + +func DefaultSignerAndApp(config *cfg.Config) (*types.PrivValidator, proxy.ClientCreator) { + privValidator := types.LoadOrGenPrivValidator(config.PrivValidatorFile()) + clientCreator := proxy.DefaultClientCreator(config.ProxyApp, config.ABCI, config.DBDir()) + return privValidator, clientCreator +} + // 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 { +// node. It can be used with a custom PrivValidator and in-process ABCI application. +func NewRunNodeCmd(signerAndApp FuncSignerAndApp) *cobra.Command { cmd := &cobra.Command{ Use: "node", Short: "Run the tendermint node", @@ -53,12 +64,8 @@ func NewRunNodeCmd(privVal *types.PrivValidator) *cobra.Command { config.ChainID = genDoc.ChainID // Create & start node - var n *node.Node - 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.With("module", "node")) + privVal, clientCreator := signerAndApp(config) + n := node.NewNode(config, privVal, clientCreator, logger.With("module", "node")) if _, err := n.Start(); err != nil { return fmt.Errorf("Failed to start node: %v", err) @@ -76,10 +83,3 @@ func NewRunNodeCmd(privVal *types.PrivValidator) *cobra.Command { AddNodeFlags(cmd) return cmd } - -// Users wishing to: -// * Use an external signer for their validators -// * Supply an in-proc abci app -// 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 diff --git a/cmd/tendermint/main.go b/cmd/tendermint/main.go index e231f6c67..7cc1b2183 100644 --- a/cmd/tendermint/main.go +++ b/cmd/tendermint/main.go @@ -3,30 +3,35 @@ package main import ( "os" - // crypto "github.com/tendermint/go-crypto" - "github.com/tendermint/tmlibs/cli" - . "github.com/tendermint/tendermint/cmd/tendermint/commands" - // "github.com/tendermint/tendermint/types" + cmd "github.com/tendermint/tendermint/cmd/tendermint/commands" ) func main() { - rootCmd := RootCmd - rootCmd.AddCommand(GenValidatorCmd, InitFilesCmd, ProbeUpnpCmd, - ReplayCmd, ReplayConsoleCmd, ResetAllCmd, ResetPrivValidatorCmd, - ShowValidatorCmd, TestnetFilesCmd, VersionCmd) - - // 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)) - */ + rootCmd := cmd.RootCmd + rootCmd.AddCommand( + cmd.GenValidatorCmd, + cmd.InitFilesCmd, + cmd.ProbeUpnpCmd, + cmd.ReplayCmd, + cmd.ReplayConsoleCmd, + cmd.ResetAllCmd, + cmd.ResetPrivValidatorCmd, + cmd.ShowValidatorCmd, + cmd.TestnetFilesCmd, + cmd.VersionCmd) + + // NOTE: + // Users wishing to: + // * Use an external signer for their validators + // * Supply an in-proc abci app + // can copy this file and use something other than the + // default SignerAndApp function + signerAndApp := cmd.DefaultSignerAndApp // Create & start node - rootCmd.AddCommand(NewRunNodeCmd(nil)) + rootCmd.AddCommand(cmd.NewRunNodeCmd(signerAndApp)) cmd := cli.PrepareBaseCmd(rootCmd, "TM", os.ExpandEnv("$HOME/.tendermint")) cmd.Execute() diff --git a/types/priv_validator.go b/types/priv_validator.go index 3944a9cdf..72d05c49e 100644 --- a/types/priv_validator.go +++ b/types/priv_validator.go @@ -34,17 +34,17 @@ func voteToStep(vote *Vote) int8 { } } -// This is used to sign votes. +// Signer is an interface that defines how to sign votes. // It is the caller's duty to verify the msg before calling Sign, // eg. to avoid double signing. -// Currently, the only callers are SignVote and SignProposal -// Signer is an interface that describes how to sign votes. +// Currently, the only callers are SignVote and SignProposal. type Signer interface { PubKey() crypto.PubKey Sign(msg []byte) (crypto.Signature, error) } // DefaultSigner implements Signer. +// It uses a standard crypto.PrivKey. type DefaultSigner struct { priv crypto.PrivKey }