From 2131f8d3309124922416305a99d5b1ebef02589f Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Thu, 21 Sep 2017 17:08:17 -0400 Subject: [PATCH] some fixes from review --- cmd/tendermint/commands/run_node.go | 19 +++---------------- cmd/tendermint/main.go | 9 ++++++--- node/node.go | 28 +++++++++++++++------------- node/node_test.go | 4 ++-- types/priv_validator.go | 24 +++++++----------------- types/priv_validator_test.go | 26 +++++++++++++------------- 6 files changed, 46 insertions(+), 64 deletions(-) diff --git a/cmd/tendermint/commands/run_node.go b/cmd/tendermint/commands/run_node.go index 6a793706f..b3ef821aa 100644 --- a/cmd/tendermint/commands/run_node.go +++ b/cmd/tendermint/commands/run_node.go @@ -6,7 +6,7 @@ import ( "github.com/spf13/cobra" cfg "github.com/tendermint/tendermint/config" - "github.com/tendermint/tendermint/node" + nm "github.com/tendermint/tendermint/node" "github.com/tendermint/tendermint/proxy" "github.com/tendermint/tendermint/types" ) @@ -53,26 +53,13 @@ func DefaultSignerAndApp(config *cfg.Config) (types.PrivValidator, proxy.ClientC // NewRunNodeCmd returns the command that allows the CLI to start a // node. It can be used with a custom PrivValidator and in-process ABCI application. -func NewRunNodeCmd(signerAndApp FuncSignerAndApp) *cobra.Command { +func NewRunNodeCmd(nodeFunc nm.NodeProvider) *cobra.Command { cmd := &cobra.Command{ Use: "node", Short: "Run the tendermint node", RunE: func(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 - privVal, clientCreator := signerAndApp(config) - n, err := node.NewNode(config, - privVal, - clientCreator, - node.DefaultGenesisDocProviderFunc(config), - node.DefaultDBProvider, - logger.With("module", "node")) + n, err := nodeFunc(config, logger) if err != nil { return fmt.Errorf("Failed to create node: %v", err) } diff --git a/cmd/tendermint/main.go b/cmd/tendermint/main.go index 1e2c00051..d1aba6f26 100644 --- a/cmd/tendermint/main.go +++ b/cmd/tendermint/main.go @@ -6,6 +6,7 @@ import ( "github.com/tendermint/tmlibs/cli" cmd "github.com/tendermint/tendermint/cmd/tendermint/commands" + nm "github.com/tendermint/tendermint/node" ) func main() { @@ -26,12 +27,14 @@ func main() { // Users wishing to: // * Use an external signer for their validators // * Supply an in-proc abci app + // * Supply a genesis doc file from another source + // * Provide their own DB implementation // can copy this file and use something other than the - // DefaultSignerAndApp function - signerAndApp := cmd.DefaultSignerAndApp + // DefaultNewNode function + nodeFunc := nm.DefaultNewNode // Create & start node - rootCmd.AddCommand(cmd.NewRunNodeCmd(signerAndApp)) + rootCmd.AddCommand(cmd.NewRunNodeCmd(nodeFunc)) cmd := cli.PrepareBaseCmd(rootCmd, "TM", os.ExpandEnv("$HOME/.tendermint")) cmd.Execute() diff --git a/node/node.go b/node/node.go index 0fb064514..834cb4c35 100644 --- a/node/node.go +++ b/node/node.go @@ -65,6 +65,21 @@ func DefaultGenesisDocProviderFunc(config *cfg.Config) GenesisDocProvider { } } +// NodeProvider takes a config and a logger and returns a ready to go Node. +type NodeProvider func(*cfg.Config, log.Logger) (*Node, error) + +// DefaultNewNode returns a Tendermint node with default settings for the +// PrivValidator, ClientCreator, GenesisDoc, and DBProvider. +// It implements NodeProvider. +func DefaultNewNode(config *cfg.Config, logger log.Logger) (*Node, error) { + return NewNode(config, + types.LoadOrGenPrivValidatorFS(config.PrivValidatorFile()), + proxy.DefaultClientCreator(config.ProxyApp, config.ABCI, config.DBDir()), + DefaultGenesisDocProviderFunc(config), + DefaultDBProvider, + logger) +} + //------------------------------------------------------------------------------ // Node is the highest level interface to a full Tendermint node. @@ -94,19 +109,6 @@ type Node struct { txIndexer txindex.TxIndexer } -// NewNodeDefault returns a Tendermint node with default settings for the -// PrivValidator, ClientCreator, GenesisDoc, and DBProvider, -func NewNodeDefault(config *cfg.Config, logger log.Logger) (*Node, error) { - // Get PrivValidator - privValidator := types.LoadOrGenPrivValidatorFS(config.PrivValidatorFile()) - return NewNode(config, - privValidator, - proxy.DefaultClientCreator(config.ProxyApp, config.ABCI, config.DBDir()), - DefaultGenesisDocProviderFunc(config), - DefaultDBProvider, - logger) -} - // NewNode returns a new, ready to go, Tendermint Node. func NewNode(config *cfg.Config, privValidator types.PrivValidator, diff --git a/node/node_test.go b/node/node_test.go index 1b6d27f47..641e606c3 100644 --- a/node/node_test.go +++ b/node/node_test.go @@ -15,8 +15,8 @@ func TestNodeStartStop(t *testing.T) { config := cfg.ResetTestRoot("node_node_test") // Create & start node - n, err := NewNodeDefault(config, log.TestingLogger()) - assert.NoError(t, err, "expected no err on NewNodeDefault") + n, err := DefaultNewNode(config, log.TestingLogger()) + assert.NoError(t, err, "expected no err on DefaultNewNode") n.Start() t.Logf("Started node %v", n.sw.NodeInfo()) diff --git a/types/priv_validator.go b/types/priv_validator.go index 4357c6f3b..47276f288 100644 --- a/types/priv_validator.go +++ b/types/priv_validator.go @@ -121,19 +121,9 @@ func GenPrivValidatorFS(filePath string) *PrivValidatorFS { // LoadPrivValidatorFS loads a PrivValidatorFS from the filePath. func LoadPrivValidatorFS(filePath string) *PrivValidatorFS { - privValJSONBytes, err := ioutil.ReadFile(filePath) - if err != nil { - cmn.Exit(err.Error()) - } - privVal := PrivValidatorFS{} - err = json.Unmarshal(privValJSONBytes, &privVal) - if err != nil { - cmn.Exit(cmn.Fmt("Error reading PrivValidator from %v: %v\n", filePath, err)) - } - - privVal.filePath = filePath - privVal.Signer = NewDefaultSigner(privVal.PrivKey) - return &privVal + return LoadPrivValidatorFSWithSigner(filePath, func(privVal PrivValidator) Signer { + return NewDefaultSigner(privVal.(*PrivValidatorFS).PrivKey) + }) } // LoadOrGenPrivValidatorFS loads a PrivValidatorFS from the given filePath @@ -153,20 +143,20 @@ func LoadOrGenPrivValidatorFS(filePath string) *PrivValidatorFS { // signer object. The PrivValidatorFS handles double signing prevention by persisting // data to the filePath, while the Signer handles the signing. // If the filePath does not exist, the PrivValidatorFS must be created manually and saved. -func LoadPrivValidatorFSWithSigner(filePath string, signerFunc func(crypto.PubKey) Signer) *PrivValidatorFS { +func LoadPrivValidatorFSWithSigner(filePath string, signerFunc func(PrivValidator) Signer) *PrivValidatorFS { privValJSONBytes, err := ioutil.ReadFile(filePath) if err != nil { cmn.Exit(err.Error()) } - privVal := PrivValidatorFS{} + privVal := &PrivValidatorFS{} err = json.Unmarshal(privValJSONBytes, &privVal) if err != nil { cmn.Exit(cmn.Fmt("Error reading PrivValidator from %v: %v\n", filePath, err)) } privVal.filePath = filePath - privVal.Signer = signerFunc(privVal.PubKey) - return &privVal + privVal.Signer = signerFunc(privVal) + return privVal } // Save persists the PrivValidatorFS to disk. diff --git a/types/priv_validator_test.go b/types/priv_validator_test.go index d56cb9ec9..5b6026740 100644 --- a/types/priv_validator_test.go +++ b/types/priv_validator_test.go @@ -29,19 +29,19 @@ func TestLoadValidator(t *testing.T) { require.Nil(err, "%+v", err) serialized := fmt.Sprintf(`{ - "address": "%s", - "pub_key": { - "type": "ed25519", - "data": "%s" - }, - "last_height": 0, - "last_round": 0, - "last_step": 0, - "last_signature": null, - "priv_key": { - "type": "ed25519", - "data": "%s" - } + "address": "%s", + "pub_key": { + "type": "ed25519", + "data": "%s" + }, + "last_height": 0, + "last_round": 0, + "last_step": 0, + "last_signature": null, + "priv_key": { + "type": "ed25519", + "data": "%s" + } }`, addrStr, pubStr, privStr) val := PrivValidatorFS{}