Browse Source

some fixes from review

pull/637/head
Ethan Buchman 7 years ago
parent
commit
2131f8d330
6 changed files with 46 additions and 64 deletions
  1. +3
    -16
      cmd/tendermint/commands/run_node.go
  2. +6
    -3
      cmd/tendermint/main.go
  3. +15
    -13
      node/node.go
  4. +2
    -2
      node/node_test.go
  5. +7
    -17
      types/priv_validator.go
  6. +13
    -13
      types/priv_validator_test.go

+ 3
- 16
cmd/tendermint/commands/run_node.go View File

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


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

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


+ 15
- 13
node/node.go 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.
@ -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,


+ 2
- 2
node/node_test.go View File

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


+ 7
- 17
types/priv_validator.go View File

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


+ 13
- 13
types/priv_validator_test.go View File

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


Loading…
Cancel
Save