some fixes from review

This commit is contained in:
Ethan Buchman
2017-09-21 17:08:17 -04:00
parent 8ae2ffda89
commit 2131f8d330
6 changed files with 46 additions and 64 deletions

View File

@ -6,7 +6,7 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
cfg "github.com/tendermint/tendermint/config" 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/proxy"
"github.com/tendermint/tendermint/types" "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 // 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. // 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{ cmd := &cobra.Command{
Use: "node", Use: "node",
Short: "Run the tendermint node", Short: "Run the tendermint node",
RunE: func(cmd *cobra.Command, args []string) error { 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 // Create & start node
privVal, clientCreator := signerAndApp(config) n, err := nodeFunc(config, logger)
n, err := node.NewNode(config,
privVal,
clientCreator,
node.DefaultGenesisDocProviderFunc(config),
node.DefaultDBProvider,
logger.With("module", "node"))
if err != nil { if err != nil {
return fmt.Errorf("Failed to create node: %v", err) return fmt.Errorf("Failed to create node: %v", err)
} }

View File

@ -6,6 +6,7 @@ import (
"github.com/tendermint/tmlibs/cli" "github.com/tendermint/tmlibs/cli"
cmd "github.com/tendermint/tendermint/cmd/tendermint/commands" cmd "github.com/tendermint/tendermint/cmd/tendermint/commands"
nm "github.com/tendermint/tendermint/node"
) )
func main() { func main() {
@ -26,12 +27,14 @@ func main() {
// Users wishing to: // Users wishing to:
// * Use an external signer for their validators // * Use an external signer for their validators
// * Supply an in-proc abci app // * 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 // can copy this file and use something other than the
// DefaultSignerAndApp function // DefaultNewNode function
signerAndApp := cmd.DefaultSignerAndApp nodeFunc := nm.DefaultNewNode
// Create & start node // Create & start node
rootCmd.AddCommand(cmd.NewRunNodeCmd(signerAndApp)) rootCmd.AddCommand(cmd.NewRunNodeCmd(nodeFunc))
cmd := cli.PrepareBaseCmd(rootCmd, "TM", os.ExpandEnv("$HOME/.tendermint")) cmd := cli.PrepareBaseCmd(rootCmd, "TM", os.ExpandEnv("$HOME/.tendermint"))
cmd.Execute() cmd.Execute()

View File

@ -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. // Node is the highest level interface to a full Tendermint node.
@ -94,19 +109,6 @@ type Node struct {
txIndexer txindex.TxIndexer 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. // NewNode returns a new, ready to go, Tendermint Node.
func NewNode(config *cfg.Config, func NewNode(config *cfg.Config,
privValidator types.PrivValidator, privValidator types.PrivValidator,

View File

@ -15,8 +15,8 @@ func TestNodeStartStop(t *testing.T) {
config := cfg.ResetTestRoot("node_node_test") config := cfg.ResetTestRoot("node_node_test")
// Create & start node // Create & start node
n, err := NewNodeDefault(config, log.TestingLogger()) n, err := DefaultNewNode(config, log.TestingLogger())
assert.NoError(t, err, "expected no err on NewNodeDefault") assert.NoError(t, err, "expected no err on DefaultNewNode")
n.Start() n.Start()
t.Logf("Started node %v", n.sw.NodeInfo()) t.Logf("Started node %v", n.sw.NodeInfo())

View File

@ -121,19 +121,9 @@ func GenPrivValidatorFS(filePath string) *PrivValidatorFS {
// LoadPrivValidatorFS loads a PrivValidatorFS from the filePath. // LoadPrivValidatorFS loads a PrivValidatorFS from the filePath.
func LoadPrivValidatorFS(filePath string) *PrivValidatorFS { func LoadPrivValidatorFS(filePath string) *PrivValidatorFS {
privValJSONBytes, err := ioutil.ReadFile(filePath) return LoadPrivValidatorFSWithSigner(filePath, func(privVal PrivValidator) Signer {
if err != nil { return NewDefaultSigner(privVal.(*PrivValidatorFS).PrivKey)
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
} }
// LoadOrGenPrivValidatorFS loads a PrivValidatorFS from the given filePath // 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 // signer object. The PrivValidatorFS handles double signing prevention by persisting
// data to the filePath, while the Signer handles the signing. // data to the filePath, while the Signer handles the signing.
// If the filePath does not exist, the PrivValidatorFS must be created manually and saved. // 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) privValJSONBytes, err := ioutil.ReadFile(filePath)
if err != nil { if err != nil {
cmn.Exit(err.Error()) cmn.Exit(err.Error())
} }
privVal := PrivValidatorFS{} privVal := &PrivValidatorFS{}
err = json.Unmarshal(privValJSONBytes, &privVal) err = json.Unmarshal(privValJSONBytes, &privVal)
if err != nil { if err != nil {
cmn.Exit(cmn.Fmt("Error reading PrivValidator from %v: %v\n", filePath, err)) cmn.Exit(cmn.Fmt("Error reading PrivValidator from %v: %v\n", filePath, err))
} }
privVal.filePath = filePath privVal.filePath = filePath
privVal.Signer = signerFunc(privVal.PubKey) privVal.Signer = signerFunc(privVal)
return &privVal return privVal
} }
// Save persists the PrivValidatorFS to disk. // Save persists the PrivValidatorFS to disk.

View File

@ -29,19 +29,19 @@ func TestLoadValidator(t *testing.T) {
require.Nil(err, "%+v", err) require.Nil(err, "%+v", err)
serialized := fmt.Sprintf(`{ serialized := fmt.Sprintf(`{
"address": "%s", "address": "%s",
"pub_key": { "pub_key": {
"type": "ed25519", "type": "ed25519",
"data": "%s" "data": "%s"
}, },
"last_height": 0, "last_height": 0,
"last_round": 0, "last_round": 0,
"last_step": 0, "last_step": 0,
"last_signature": null, "last_signature": null,
"priv_key": { "priv_key": {
"type": "ed25519", "type": "ed25519",
"data": "%s" "data": "%s"
} }
}`, addrStr, pubStr, privStr) }`, addrStr, pubStr, privStr)
val := PrivValidatorFS{} val := PrivValidatorFS{}