fewer structs. remove viper from consensus

This commit is contained in:
Ethan Buchman
2017-05-01 20:09:29 -04:00
parent d8fb226ec4
commit 75b6c5215f
17 changed files with 371 additions and 299 deletions

View File

@@ -5,8 +5,8 @@ import (
"github.com/spf13/cobra"
cmn "github.com/tendermint/tmlibs/common"
"github.com/tendermint/tendermint/types"
cmn "github.com/tendermint/tmlibs/common"
)
var initFilesCmd = &cobra.Command{
@@ -20,13 +20,13 @@ func init() {
}
func initFiles(cmd *cobra.Command, args []string) {
privValFile := config.GetString("priv_validator_file")
privValFile := config.PrivValidatorFile
if _, err := os.Stat(privValFile); os.IsNotExist(err) {
privValidator := types.GenPrivValidator()
privValidator.SetFile(privValFile)
privValidator.Save()
genFile := config.GetString("genesis_file")
genFile := config.GenesisFile
if _, err := os.Stat(genFile); os.IsNotExist(err) {
genDoc := types.GenesisDoc{
@@ -40,8 +40,8 @@ func initFiles(cmd *cobra.Command, args []string) {
genDoc.SaveAs(genFile)
}
log.Notice("Initialized tendermint", "genesis", config.GetString("genesis_file"), "priv_validator", config.GetString("priv_validator_file"))
log.Notice("Initialized tendermint", "genesis", config.GenesisFile, "priv_validator", config.PrivValidatorFile)
} else {
log.Notice("Already initialized", "priv_validator", config.GetString("priv_validator_file"))
log.Notice("Already initialized", "priv_validator", config.PrivValidatorFile)
}
}

View File

@@ -1,36 +1,26 @@
package commands
import (
"fmt"
"github.com/spf13/cobra"
"github.com/tendermint/tendermint/consensus"
"github.com/spf13/cobra"
)
var replayCmd = &cobra.Command{
Use: "replay [walfile]",
Use: "replay",
Short: "Replay messages from WAL",
Run: func(cmd *cobra.Command, args []string) {
if len(args) > 1 {
consensus.RunReplayFile(config, args[1], false)
} else {
fmt.Println("replay requires an argument (walfile)")
}
config := getConfig()
consensus.RunReplayFile(config.Config, config.Consensus, false)
},
}
var replayConsoleCmd = &cobra.Command{
Use: "replay_console [walfile]",
Use: "replay_console",
Short: "Replay messages from WAL in a console",
Run: func(cmd *cobra.Command, args []string) {
if len(args) > 1 {
consensus.RunReplayFile(config, args[1], true)
} else {
fmt.Println("replay_console requires an argument (walfile)")
}
config := getConfig()
consensus.RunReplayFile(config.Config, config.Consensus, true)
},
}

View File

@@ -29,13 +29,15 @@ func init() {
// XXX: this is totally unsafe.
// it's only suitable for testnets.
func resetAll(cmd *cobra.Command, args []string) {
ResetAll(config.GetString("db_dir"), config.GetString("priv_validator_file"), log)
config := getConfig()
ResetAll(config.DBDir, config.PrivValidatorFile, log)
}
// XXX: this is totally unsafe.
// it's only suitable for testnets.
func resetPrivValidator(cmd *cobra.Command, args []string) {
resetPrivValidatorLocal(config.GetString("priv_validator_file"), log)
config := getConfig()
resetPrivValidatorLocal(config.PrivValidatorFile, log)
}
// Exported so other CLI tools can use it

View File

@@ -2,16 +2,34 @@ package commands
import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
tmcfg "github.com/tendermint/tendermint/config/tendermint"
"github.com/tendermint/tendermint/node"
"github.com/tendermint/tmlibs/logger"
)
var (
config = tmcfg.GetConfig("")
log = logger.New("module", "main")
viperConfig *viper.Viper
config *node.Config
log = logger.New("module", "main")
)
func init() {
// Set config to be used as defaults by flags.
// This will be overwritten by whatever is unmarshalled from viper
config = node.NewDefaultConfig("")
}
// unmarshal viper into the Tendermint config
func getConfig() *node.Config {
config := new(node.Config)
if err := viperConfig.Unmarshal(config); err != nil {
panic(err)
}
return config
}
//global flag
var logLevel string
@@ -19,13 +37,14 @@ var RootCmd = &cobra.Command{
Use: "tendermint",
Short: "Tendermint Core (BFT Consensus) in Go",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
// set the log level in the config and logger
config.Set("node.log_level", logLevel)
logger.SetLogLevel(logLevel)
// set the log level
config := getConfig()
logger.SetLogLevel(config.LogLevel)
},
}
func init() {
//parse flag and set config
RootCmd.PersistentFlags().StringVar(&logLevel, "log_level", config.GetString("node.log_level"), "Log level")
RootCmd.PersistentFlags().StringVar(&logLevel, "log_level", config.LogLevel, "Log level")
viperConfig.BindPFlag("log_level", RootCmd.Flags().Lookup("log_level"))
}

View File

@@ -14,10 +14,9 @@ import (
)
var runNodeCmd = &cobra.Command{
Use: "node",
Short: "Run the tendermint node",
PreRun: setConfigFlags,
RunE: runNode,
Use: "node",
Short: "Run the tendermint node",
RunE: runNode,
}
//flags
@@ -35,49 +34,55 @@ var (
)
func init() {
// bind flags
// configuration options
runNodeCmd.Flags().StringVar(&moniker, "moniker", config.GetString("node.moniker"),
// node flags
runNodeCmd.Flags().StringVar(&moniker, "moniker", config.Moniker,
"Node Name")
runNodeCmd.Flags().StringVar(&nodeLaddr, "node_laddr", config.GetString("node.listen_addr"),
"Node listen address. (0.0.0.0:0 means any interface, any port)")
runNodeCmd.Flags().StringVar(&seeds, "seeds", config.GetString("network.seeds"),
"Comma delimited host:port seed nodes")
runNodeCmd.Flags().BoolVar(&fastSync, "fast_sync", config.GetBool("blockchain.fast_sync"),
viperConfig.BindPFlag("moniker", runNodeCmd.Flags().Lookup("moniker"))
runNodeCmd.Flags().BoolVar(&fastSync, "fast_sync", config.FastSync,
"Fast blockchain syncing")
runNodeCmd.Flags().BoolVar(&skipUPNP, "skip_upnp", config.GetBool("network.skip_upnp"),
"Skip UPNP configuration")
runNodeCmd.Flags().StringVar(&rpcLaddr, "rpc_laddr", config.GetString("rpc.listen_addr"),
"RPC listen address. Port required")
runNodeCmd.Flags().StringVar(&grpcLaddr, "grpc_laddr", config.GetString("grpc.listen_addr"),
"GRPC listen address (BroadcastTx only). Port required")
runNodeCmd.Flags().StringVar(&proxyApp, "proxy_app", config.GetString("abci.proxy_app"),
viperConfig.BindPFlag("fast_sync", runNodeCmd.Flags().Lookup("fast_sync"))
// abci flags
runNodeCmd.Flags().StringVar(&proxyApp, "proxy_app", config.ProxyApp,
"Proxy app address, or 'nilapp' or 'dummy' for local testing.")
runNodeCmd.Flags().StringVar(&abciTransport, "abci", config.GetString("abci.mode"),
viperConfig.BindPFlag("proxy_app", runNodeCmd.Flags().Lookup("proxy_app"))
runNodeCmd.Flags().StringVar(&abciTransport, "abci", config.ABCI,
"Specify abci transport (socket | grpc)")
viperConfig.BindPFlag("abci", runNodeCmd.Flags().Lookup("abci"))
// rpc flags
runNodeCmd.Flags().StringVar(&rpcLaddr, "rpc_laddr", config.RPCListenAddress,
"RPC listen address. Port required")
viperConfig.BindPFlag("rpc_laddr", runNodeCmd.Flags().Lookup("rpc_laddr"))
runNodeCmd.Flags().StringVar(&grpcLaddr, "grpc_laddr", config.GRPCListenAddress,
"GRPC listen address (BroadcastTx only). Port required")
viperConfig.BindPFlag("grpc_laddr", runNodeCmd.Flags().Lookup("grpc_laddr"))
// p2p flags
runNodeCmd.Flags().StringVar(&nodeLaddr, "p2p.laddr", config.P2P.ListenAddress,
"Node listen address. (0.0.0.0:0 means any interface, any port)")
viperConfig.BindPFlag("p2p.laddr", runNodeCmd.Flags().Lookup("p2p.laddr"))
runNodeCmd.Flags().StringVar(&seeds, "p2p.seeds", config.P2P.Seeds,
"Comma delimited host:port seed nodes")
viperConfig.BindPFlag("p2p.seeds", runNodeCmd.Flags().Lookup("p2p.seeds"))
runNodeCmd.Flags().BoolVar(&skipUPNP, "p2p.skip_upnp", config.P2P.SkipUPNP,
"Skip UPNP configuration")
viperConfig.BindPFlag("p2p.skip_upnp", runNodeCmd.Flags().Lookup("p2p.skip_upnp"))
// feature flags
runNodeCmd.Flags().BoolVar(&pex, "pex", config.GetBool("pex_reactor"),
runNodeCmd.Flags().BoolVar(&pex, "p2p.pex", config.P2P.PexReactor,
"Enable Peer-Exchange (dev feature)")
RootCmd.AddCommand(runNodeCmd)
}
func setConfigFlags(cmd *cobra.Command, args []string) {
// Merge parsed flag values onto config
config.Set("node.moniker", moniker)
config.Set("node.listen_addr", nodeLaddr)
config.Set("network.seeds", seeds)
config.Set("network.skip_upnp", skipUPNP)
config.Set("network.pex_reactor", pex)
config.Set("blockchain.fast_sync", fastSync)
config.Set("rpc.listen_addr", rpcLaddr)
config.Set("rpc.grpc_listen_addr", grpcLaddr)
config.Set("abci.proxy_app", proxyApp)
config.Set("abci.mode", abciTransport)
}
// Users wishing to:
// * Use an external signer for their validators
// * Supply an in-proc abci app
@@ -90,7 +95,7 @@ func runNode(cmd *cobra.Command, args []string) error {
// This is for Mintnet compatibility.
// TODO: If Mintnet gets deprecated or genesis_file is
// always available, remove.
genDocFile := config.GetString("genesis_file")
genDocFile := config.GenesisFile
if !cmn.FileExists(genDocFile) {
log.Notice(cmn.Fmt("Waiting for genesis file %v...", genDocFile))
for {
@@ -109,12 +114,13 @@ func runNode(cmd *cobra.Command, args []string) error {
if genDoc.ChainID == "" {
return fmt.Errorf("Genesis doc %v must include non-empty chain_id", genDocFile)
}
config.Set("chain_id", genDoc.ChainID)
// config.SetChainID("chain_id", genDoc.ChainID) TODO
}
}
// Create & start node
n := node.NewNodeDefault(config) //tmConfig)
n := node.NewNodeDefault(getConfig())
if _, err := n.Start(); err != nil {
return fmt.Errorf("Failed to start node: %v", err)
} else {

View File

@@ -20,8 +20,8 @@ func init() {
}
func showValidator(cmd *cobra.Command, args []string) {
privValidatorFile := config.GetString("priv_validator_file")
privValidator := types.LoadOrGenPrivValidator(privValidatorFile)
config := getConfig()
privValidator := types.LoadOrGenPrivValidator(config.PrivValidatorFile)
pubKeyJSONBytes, _ := data.ToJSON(privValidator.PubKey)
fmt.Println(string(pubKeyJSONBytes))
}