diff --git a/blockchain/reactor.go b/blockchain/reactor.go index a788c4aca..1ea5650ed 100644 --- a/blockchain/reactor.go +++ b/blockchain/reactor.go @@ -6,13 +6,14 @@ import ( "reflect" "time" - cmn "github.com/tendermint/tmlibs/common" - cfg "github.com/tendermint/go-config" - "github.com/tendermint/tendermint/p2p" + "github.com/spf13/viper" + "github.com/tendermint/go-wire" + "github.com/tendermint/tendermint/p2p" "github.com/tendermint/tendermint/proxy" sm "github.com/tendermint/tendermint/state" "github.com/tendermint/tendermint/types" + cmn "github.com/tendermint/tmlibs/common" ) const ( @@ -43,7 +44,7 @@ type consensusReactor interface { type BlockchainReactor struct { p2p.BaseReactor - config cfg.Config + config *viper.Viper state *sm.State proxyAppConn proxy.AppConnConsensus // same as consensus.proxyAppConn store *BlockStore @@ -57,7 +58,7 @@ type BlockchainReactor struct { } // NewBlockchainReactor returns new reactor instance. -func NewBlockchainReactor(config cfg.Config, state *sm.State, proxyAppConn proxy.AppConnConsensus, store *BlockStore, fastSync bool) *BlockchainReactor { +func NewBlockchainReactor(config *viper.Viper, state *sm.State, proxyAppConn proxy.AppConnConsensus, store *BlockStore, fastSync bool) *BlockchainReactor { if state.LastBlockHeight == store.Height()-1 { store.height-- // XXX HACK, make this better } diff --git a/cmd/tendermint/commands/probe_upnp.go b/cmd/tendermint/commands/probe_upnp.go index eb55d6690..330dcf17d 100644 --- a/cmd/tendermint/commands/probe_upnp.go +++ b/cmd/tendermint/commands/probe_upnp.go @@ -12,14 +12,14 @@ import ( var probeUpnpCmd = &cobra.Command{ Use: "probe_upnp", Short: "Test UPnP functionality", - Run: probeUpnp, + RunE: probeUpnp, } func init() { RootCmd.AddCommand(probeUpnpCmd) } -func probeUpnp(cmd *cobra.Command, args []string) { +func probeUpnp(cmd *cobra.Command, args []string) error { capabilities, err := upnp.Probe() if err != nil { @@ -28,9 +28,9 @@ func probeUpnp(cmd *cobra.Command, args []string) { fmt.Println("Probe success!") jsonBytes, err := json.Marshal(capabilities) if err != nil { - panic(err) + return err } fmt.Println(string(jsonBytes)) } - + return nil } diff --git a/cmd/tendermint/commands/reset_priv_validator.go b/cmd/tendermint/commands/reset_priv_validator.go index b1fb334bc..a611935bb 100644 --- a/cmd/tendermint/commands/reset_priv_validator.go +++ b/cmd/tendermint/commands/reset_priv_validator.go @@ -4,8 +4,8 @@ import ( "os" "github.com/spf13/cobra" + "github.com/spf13/viper" - cfg "github.com/tendermint/go-config" "github.com/tendermint/log15" "github.com/tendermint/tendermint/types" ) @@ -36,27 +36,30 @@ func resetAll(cmd *cobra.Command, args []string) { // XXX: this is totally unsafe. // it's only suitable for testnets. func resetPrivValidator(cmd *cobra.Command, args []string) { - ResetPrivValidator(config, log) + resetPrivValidatorLocal(config, log) } // Exported so other CLI tools can use it -func ResetAll(c cfg.Config, l log15.Logger) { - ResetPrivValidator(c, l) - os.RemoveAll(c.GetString("db_dir")) +func ResetAll(c *viper.Viper, l log15.Logger) { + resetPrivValidatorLocal(c, l) + dataDir := c.GetString("db_dir") + os.RemoveAll(dataDir) + l.Notice("Removed all data", "dir", dataDir) } -func ResetPrivValidator(c cfg.Config, l log15.Logger) { +func resetPrivValidatorLocal(c *viper.Viper, l log15.Logger) { + // Get PrivValidator var privValidator *types.PrivValidator - privValidatorFile := config.GetString("priv_validator_file") + privValidatorFile := c.GetString("priv_validator_file") if _, err := os.Stat(privValidatorFile); err == nil { privValidator = types.LoadPrivValidator(privValidatorFile) privValidator.Reset() - log.Notice("Reset PrivValidator", "file", privValidatorFile) + l.Notice("Reset PrivValidator", "file", privValidatorFile) } else { privValidator = types.GenPrivValidator() privValidator.SetFile(privValidatorFile) privValidator.Save() - log.Notice("Generated PrivValidator", "file", privValidatorFile) + l.Notice("Generated PrivValidator", "file", privValidatorFile) } } diff --git a/cmd/tendermint/commands/root.go b/cmd/tendermint/commands/root.go index 120d53c04..de5b71bd1 100644 --- a/cmd/tendermint/commands/root.go +++ b/cmd/tendermint/commands/root.go @@ -3,8 +3,8 @@ package commands import ( "github.com/spf13/cobra" - "github.com/tendermint/tmlibs/logger" tmcfg "github.com/tendermint/tendermint/config/tendermint" + "github.com/tendermint/tmlibs/logger" ) var ( diff --git a/cmd/tendermint/commands/run_node.go b/cmd/tendermint/commands/run_node.go index 5e0fef32c..b2b23af5c 100644 --- a/cmd/tendermint/commands/run_node.go +++ b/cmd/tendermint/commands/run_node.go @@ -1,21 +1,22 @@ package commands import ( + "fmt" "io/ioutil" "time" "github.com/spf13/cobra" - . "github.com/tendermint/tmlibs/common" "github.com/tendermint/tendermint/node" "github.com/tendermint/tendermint/types" + cmn "github.com/tendermint/tmlibs/common" ) var runNodeCmd = &cobra.Command{ Use: "node", Short: "Run the tendermint node", PreRun: setConfigFlags, - Run: runNode, + RunE: runNode, } //flags @@ -82,30 +83,30 @@ func setConfigFlags(cmd *cobra.Command, args []string) { // 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 -func runNode(cmd *cobra.Command, args []string) { +func runNode(cmd *cobra.Command, args []string) error { // Wait until the genesis doc becomes available // This is for Mintnet compatibility. // TODO: If Mintnet gets deprecated or genesis_file is // always available, remove. genDocFile := config.GetString("genesis_file") - if !FileExists(genDocFile) { - log.Notice(Fmt("Waiting for genesis file %v...", genDocFile)) + if !cmn.FileExists(genDocFile) { + log.Notice(cmn.Fmt("Waiting for genesis file %v...", genDocFile)) for { time.Sleep(time.Second) - if !FileExists(genDocFile) { + if !cmn.FileExists(genDocFile) { continue } jsonBlob, err := ioutil.ReadFile(genDocFile) if err != nil { - Exit(Fmt("Couldn't read GenesisDoc file: %v", err)) + return fmt.Errorf("Couldn't read GenesisDoc file: %v", err) } genDoc, err := types.GenesisDocFromJSON(jsonBlob) if err != nil { - Exit(Fmt("Error reading GenesisDoc: %v", err)) + return fmt.Errorf("Error reading GenesisDoc: %v", err) } if genDoc.ChainID == "" { - Exit(Fmt("Genesis doc %v must include non-empty chain_id", genDocFile)) + return fmt.Errorf("Genesis doc %v must include non-empty chain_id", genDocFile) } config.Set("chain_id", genDoc.ChainID) } @@ -114,11 +115,13 @@ func runNode(cmd *cobra.Command, args []string) { // Create & start node n := node.NewNodeDefault(config) if _, err := n.Start(); err != nil { - Exit(Fmt("Failed to start node: %v", err)) + return fmt.Errorf("Failed to start node: %v", err) } else { log.Notice("Started node", "nodeInfo", n.Switch().NodeInfo()) } // Trap signal, run forever. n.RunForever() + + return nil } diff --git a/config/tendermint/config.go b/config/tendermint/config.go index 93650eaef..9011e0154 100644 --- a/config/tendermint/config.go +++ b/config/tendermint/config.go @@ -5,8 +5,8 @@ import ( "path" "strings" - . "github.com/tendermint/tmlibs/common" - cfg "github.com/tendermint/go-config" + "github.com/spf13/viper" + cmn "github.com/tendermint/tmlibs/common" ) func getTMRoot(rootDir string) string { @@ -25,85 +25,89 @@ func getTMRoot(rootDir string) string { func initTMRoot(rootDir string) { rootDir = getTMRoot(rootDir) - EnsureDir(rootDir, 0700) - EnsureDir(rootDir+"/data", 0700) + cmn.EnsureDir(rootDir, 0700) + cmn.EnsureDir(rootDir+"/data", 0700) configFilePath := path.Join(rootDir, "config.toml") // Write default config file if missing. - if !FileExists(configFilePath) { + if !cmn.FileExists(configFilePath) { // Ask user for moniker // moniker := cfg.Prompt("Type hostname: ", "anonymous") - MustWriteFile(configFilePath, []byte(defaultConfig("anonymous")), 0644) + cmn.MustWriteFile(configFilePath, []byte(defaultConfig("anonymous")), 0644) } } -func GetConfig(rootDir string) cfg.Config { +func GetConfig(rootDir string) *viper.Viper { rootDir = getTMRoot(rootDir) initTMRoot(rootDir) - configFilePath := path.Join(rootDir, "config.toml") - mapConfig, err := cfg.ReadMapConfigFromFile(configFilePath) + config := viper.New() + config.SetConfigName("config") + config.SetConfigType("toml") + config.AddConfigPath(rootDir) + err := config.ReadInConfig() if err != nil { - Exit(Fmt("Could not read config: %v", err)) + cmn.Exit(cmn.Fmt("Could not read config from directory %v: %v", rootDir, err)) } + //config.WatchConfig() // Set defaults or panic - if mapConfig.IsSet("chain_id") { - Exit("Cannot set 'chain_id' via config.toml") + if config.IsSet("chain_id") { + cmn.Exit("Cannot set 'chain_id' via config.toml") } - if mapConfig.IsSet("revision_file") { - Exit("Cannot set 'revision_file' via config.toml. It must match what's in the Makefile") + if config.IsSet("revision_file") { + cmn.Exit("Cannot set 'revision_file' via config.toml. It must match what's in the Makefile") } - mapConfig.SetRequired("chain_id") // blows up if you try to use it before setting. - mapConfig.SetDefault("genesis_file", rootDir+"/genesis.json") - mapConfig.SetDefault("proxy_app", "tcp://127.0.0.1:46658") - mapConfig.SetDefault("abci", "socket") - mapConfig.SetDefault("moniker", "anonymous") - mapConfig.SetDefault("node_laddr", "tcp://0.0.0.0:46656") - mapConfig.SetDefault("seeds", "") - // mapConfig.SetDefault("seeds", "goldenalchemist.chaintest.net:46656") - mapConfig.SetDefault("fast_sync", true) - mapConfig.SetDefault("skip_upnp", false) - mapConfig.SetDefault("addrbook_file", rootDir+"/addrbook.json") - mapConfig.SetDefault("addrbook_strict", true) // disable to allow connections locally - mapConfig.SetDefault("pex_reactor", false) // enable for peer exchange - mapConfig.SetDefault("priv_validator_file", rootDir+"/priv_validator.json") - mapConfig.SetDefault("db_backend", "leveldb") - mapConfig.SetDefault("db_dir", rootDir+"/data") - mapConfig.SetDefault("log_level", "info") - mapConfig.SetDefault("rpc_laddr", "tcp://0.0.0.0:46657") - mapConfig.SetDefault("grpc_laddr", "") - mapConfig.SetDefault("prof_laddr", "") - mapConfig.SetDefault("revision_file", rootDir+"/revision") - mapConfig.SetDefault("cs_wal_file", rootDir+"/data/cs.wal/wal") - mapConfig.SetDefault("cs_wal_light", false) - mapConfig.SetDefault("filter_peers", false) - - mapConfig.SetDefault("block_size", 10000) // max number of txs - mapConfig.SetDefault("block_part_size", 65536) // part size 64K - mapConfig.SetDefault("disable_data_hash", false) + //mapConfig.SetRequired("chain_id") // blows up if you try to use it before setting. + config.SetDefault("genesis_file", rootDir+"/genesis.json") + config.SetDefault("proxy_app", "tcp://127.0.0.1:46658") + config.SetDefault("abci", "socket") + config.SetDefault("moniker", "anonymous") + config.SetDefault("node_laddr", "tcp://0.0.0.0:46656") + config.SetDefault("seeds", "") + // config.SetDefault("seeds", "goldenalchemist.chaintest.net:46656") + config.SetDefault("fast_sync", true) + config.SetDefault("skip_upnp", false) + config.SetDefault("addrbook_file", rootDir+"/addrbook.json") + config.SetDefault("addrbook_strict", true) // disable to allow connections locally + config.SetDefault("pex_reactor", false) // enable for peer exchange + config.SetDefault("priv_validator_file", rootDir+"/priv_validator.json") + config.SetDefault("db_backend", "leveldb") + config.SetDefault("db_dir", rootDir+"/data") + config.SetDefault("log_level", "info") + config.SetDefault("rpc_laddr", "tcp://0.0.0.0:46657") + config.SetDefault("grpc_laddr", "") + config.SetDefault("prof_laddr", "") + config.SetDefault("revision_file", rootDir+"/revision") + config.SetDefault("cs_wal_file", rootDir+"/data/cs.wal/wal") + config.SetDefault("cs_wal_light", false) + config.SetDefault("filter_peers", false) + + config.SetDefault("block_size", 10000) // max number of txs + config.SetDefault("block_part_size", 65536) // part size 64K + config.SetDefault("disable_data_hash", false) // all timeouts are in ms - mapConfig.SetDefault("timeout_handshake", 10000) - mapConfig.SetDefault("timeout_propose", 3000) - mapConfig.SetDefault("timeout_propose_delta", 500) - mapConfig.SetDefault("timeout_prevote", 1000) - mapConfig.SetDefault("timeout_prevote_delta", 500) - mapConfig.SetDefault("timeout_precommit", 1000) - mapConfig.SetDefault("timeout_precommit_delta", 500) - mapConfig.SetDefault("timeout_commit", 1000) + config.SetDefault("timeout_handshake", 10000) + config.SetDefault("timeout_propose", 3000) + config.SetDefault("timeout_propose_delta", 500) + config.SetDefault("timeout_prevote", 1000) + config.SetDefault("timeout_prevote_delta", 500) + config.SetDefault("timeout_precommit", 1000) + config.SetDefault("timeout_precommit_delta", 500) + config.SetDefault("timeout_commit", 1000) // make progress asap (no `timeout_commit`) on full precommit votes - mapConfig.SetDefault("skip_timeout_commit", false) - mapConfig.SetDefault("mempool_recheck", true) - mapConfig.SetDefault("mempool_recheck_empty", true) - mapConfig.SetDefault("mempool_broadcast", true) - mapConfig.SetDefault("mempool_wal_dir", rootDir+"/data/mempool.wal") + config.SetDefault("skip_timeout_commit", false) + config.SetDefault("mempool_recheck", true) + config.SetDefault("mempool_recheck_empty", true) + config.SetDefault("mempool_broadcast", true) + config.SetDefault("mempool_wal_dir", rootDir+"/data/mempool.wal") - mapConfig.SetDefault("tx_index", "kv") + config.SetDefault("tx_index", "kv") - return mapConfig + return config } var defaultConfigTmpl = `# This is a TOML config file. diff --git a/config/tendermint_test/config.go b/config/tendermint_test/config.go index fcdc572b9..1a87d5261 100644 --- a/config/tendermint_test/config.go +++ b/config/tendermint_test/config.go @@ -3,12 +3,14 @@ package tendermint_test import ( + "fmt" "os" "path" "strings" + "github.com/spf13/viper" + . "github.com/tendermint/tmlibs/common" - cfg "github.com/tendermint/go-config" "github.com/tendermint/tmlibs/logger" ) @@ -43,7 +45,6 @@ func initTMRoot(rootDir string) { // Write default config file if missing. if !FileExists(configFilePath) { // Ask user for moniker - // moniker := cfg.Prompt("Type hostname: ", "anonymous") MustWriteFile(configFilePath, []byte(defaultConfig("anonymous")), 0644) } if !FileExists(genesisFilePath) { @@ -53,65 +54,70 @@ func initTMRoot(rootDir string) { MustWriteFile(privFilePath, []byte(defaultPrivValidator), 0644) } -func ResetConfig(localPath string) cfg.Config { +func ResetConfig(localPath string) *viper.Viper { rootDir := os.Getenv("HOME") + "/.tendermint_test/" + localPath initTMRoot(rootDir) - configFilePath := path.Join(rootDir, "config.toml") - mapConfig, err := cfg.ReadMapConfigFromFile(configFilePath) + config := viper.New() + config.SetConfigName("config") + config.SetConfigType("toml") + config.AddConfigPath(rootDir) + err := config.ReadInConfig() if err != nil { Exit(Fmt("Could not read config: %v", err)) } + //config.WatchConfig() // Set defaults or panic - if mapConfig.IsSet("chain_id") { - Exit("Cannot set 'chain_id' via config.toml") + if config.IsSet("chain_id") { + Exit(fmt.Sprintf("Cannot set 'chain_id' via config.toml:\n %v\n %v\n ", config.Get("chain_id"), rootDir)) } - mapConfig.SetDefault("chain_id", "tendermint_test") - mapConfig.SetDefault("genesis_file", rootDir+"/genesis.json") - mapConfig.SetDefault("proxy_app", "dummy") - mapConfig.SetDefault("abci", "socket") - mapConfig.SetDefault("moniker", "anonymous") - mapConfig.SetDefault("node_laddr", "tcp://0.0.0.0:36656") - mapConfig.SetDefault("fast_sync", false) - mapConfig.SetDefault("skip_upnp", true) - mapConfig.SetDefault("addrbook_file", rootDir+"/addrbook.json") - mapConfig.SetDefault("addrbook_strict", true) // disable to allow connections locally - mapConfig.SetDefault("pex_reactor", false) // enable for peer exchange - mapConfig.SetDefault("priv_validator_file", rootDir+"/priv_validator.json") - mapConfig.SetDefault("db_backend", "memdb") - mapConfig.SetDefault("db_dir", rootDir+"/data") - mapConfig.SetDefault("log_level", "info") - mapConfig.SetDefault("rpc_laddr", "tcp://0.0.0.0:36657") - mapConfig.SetDefault("grpc_laddr", "tcp://0.0.0.0:36658") - mapConfig.SetDefault("prof_laddr", "") - mapConfig.SetDefault("revision_file", rootDir+"/revision") - mapConfig.SetDefault("cs_wal_file", rootDir+"/data/cs.wal/wal") - mapConfig.SetDefault("cs_wal_light", false) - mapConfig.SetDefault("filter_peers", false) - - mapConfig.SetDefault("block_size", 10000) - mapConfig.SetDefault("block_part_size", 65536) // part size 64K - mapConfig.SetDefault("disable_data_hash", false) - mapConfig.SetDefault("timeout_handshake", 10000) - mapConfig.SetDefault("timeout_propose", 2000) - mapConfig.SetDefault("timeout_propose_delta", 1) - mapConfig.SetDefault("timeout_prevote", 10) - mapConfig.SetDefault("timeout_prevote_delta", 1) - mapConfig.SetDefault("timeout_precommit", 10) - mapConfig.SetDefault("timeout_precommit_delta", 1) - mapConfig.SetDefault("timeout_commit", 10) - mapConfig.SetDefault("skip_timeout_commit", true) - mapConfig.SetDefault("mempool_recheck", true) - mapConfig.SetDefault("mempool_recheck_empty", true) - mapConfig.SetDefault("mempool_broadcast", true) - mapConfig.SetDefault("mempool_wal_dir", "") - - mapConfig.SetDefault("tx_index", "kv") - - logger.SetLogLevel(mapConfig.GetString("log_level")) - - return mapConfig + + config.SetDefault("chain_id", "tendermint_test") + config.SetDefault("genesis_file", rootDir+"/genesis.json") + config.SetDefault("proxy_app", "dummy") + config.SetDefault("abci", "socket") + config.SetDefault("moniker", "anonymous") + config.SetDefault("node_laddr", "tcp://0.0.0.0:36656") + config.SetDefault("fast_sync", false) + config.SetDefault("skip_upnp", true) + config.SetDefault("addrbook_file", rootDir+"/addrbook.json") + config.SetDefault("addrbook_strict", true) // disable to allow connections locally + config.SetDefault("pex_reactor", false) // enable for peer exchange + config.SetDefault("priv_validator_file", rootDir+"/priv_validator.json") + config.SetDefault("db_backend", "memdb") + config.SetDefault("db_dir", rootDir+"/data") + config.SetDefault("log_level", "info") + config.SetDefault("rpc_laddr", "tcp://0.0.0.0:36657") + config.SetDefault("grpc_laddr", "tcp://0.0.0.0:36658") + config.SetDefault("prof_laddr", "") + config.SetDefault("revision_file", rootDir+"/revision") + config.SetDefault("cs_wal_file", rootDir+"/data/cs.wal/wal") + config.SetDefault("cs_wal_light", false) + config.SetDefault("filter_peers", false) + + config.SetDefault("block_size", 10000) + config.SetDefault("block_part_size", 65536) // part size 64K + config.SetDefault("disable_data_hash", false) + config.SetDefault("timeout_handshake", 10000) + config.SetDefault("timeout_propose", 2000) + config.SetDefault("timeout_propose_delta", 1) + config.SetDefault("timeout_prevote", 10) + config.SetDefault("timeout_prevote_delta", 1) + config.SetDefault("timeout_precommit", 10) + config.SetDefault("timeout_precommit_delta", 1) + config.SetDefault("timeout_commit", 10) + config.SetDefault("skip_timeout_commit", true) + config.SetDefault("mempool_recheck", true) + config.SetDefault("mempool_recheck_empty", true) + config.SetDefault("mempool_broadcast", true) + config.SetDefault("mempool_wal_dir", "") + + config.SetDefault("tx_index", "kv") + + logger.SetLogLevel(config.GetString("log_level")) + + return config } var defaultConfigTmpl = `# This is a TOML config file. diff --git a/consensus/byzantine_test.go b/consensus/byzantine_test.go index acd9e90e2..b9edf4635 100644 --- a/consensus/byzantine_test.go +++ b/consensus/byzantine_test.go @@ -5,13 +5,13 @@ import ( "testing" "time" - "github.com/tendermint/tendermint/config/tendermint_test" + "github.com/spf13/viper" - . "github.com/tendermint/tmlibs/common" - cfg "github.com/tendermint/go-config" - "github.com/tendermint/tmlibs/events" + "github.com/tendermint/tendermint/config/tendermint_test" "github.com/tendermint/tendermint/p2p" "github.com/tendermint/tendermint/types" + . "github.com/tendermint/tmlibs/common" + "github.com/tendermint/tmlibs/events" ) func init() { @@ -36,7 +36,7 @@ func TestByzantine(t *testing.T) { switches := make([]*p2p.Switch, N) for i := 0; i < N; i++ { - switches[i] = p2p.NewSwitch(cfg.NewMapConfig(nil)) + switches[i] = p2p.NewSwitch(viper.New()) } reactors := make([]p2p.Reactor, N) diff --git a/consensus/common_test.go b/consensus/common_test.go index 77cc8172a..469fc9c6b 100644 --- a/consensus/common_test.go +++ b/consensus/common_test.go @@ -11,23 +11,24 @@ import ( "testing" "time" + "github.com/spf13/viper" + abcicli "github.com/tendermint/abci/client" abci "github.com/tendermint/abci/types" - . "github.com/tendermint/tmlibs/common" - cfg "github.com/tendermint/go-config" - dbm "github.com/tendermint/tmlibs/db" - "github.com/tendermint/tendermint/p2p" bc "github.com/tendermint/tendermint/blockchain" "github.com/tendermint/tendermint/config/tendermint_test" mempl "github.com/tendermint/tendermint/mempool" + "github.com/tendermint/tendermint/p2p" sm "github.com/tendermint/tendermint/state" "github.com/tendermint/tendermint/types" + . "github.com/tendermint/tmlibs/common" + dbm "github.com/tendermint/tmlibs/db" "github.com/tendermint/abci/example/counter" "github.com/tendermint/abci/example/dummy" ) -var config cfg.Config // NOTE: must be reset for each _test.go file +var config *viper.Viper // NOTE: must be reset for each _test.go file var ensureTimeout = time.Duration(2) func ensureDir(dir string, mode os.FileMode) { @@ -233,7 +234,7 @@ func newConsensusState(state *sm.State, pv *types.PrivValidator, app abci.Applic return newConsensusStateWithConfig(config, state, pv, app) } -func newConsensusStateWithConfig(thisConfig cfg.Config, state *sm.State, pv *types.PrivValidator, app abci.Application) *ConsensusState { +func newConsensusStateWithConfig(thisConfig *viper.Viper, state *sm.State, pv *types.PrivValidator, app abci.Application) *ConsensusState { // Get BlockStore blockDB := dbm.NewMemDB() blockStore := bc.NewBlockStore(blockDB) @@ -256,7 +257,7 @@ func newConsensusStateWithConfig(thisConfig cfg.Config, state *sm.State, pv *typ return cs } -func loadPrivValidator(conf cfg.Config) *types.PrivValidator { +func loadPrivValidator(conf *viper.Viper) *types.PrivValidator { privValidatorFile := conf.GetString("priv_validator_file") ensureDir(path.Dir(privValidatorFile), 0700) privValidator := types.LoadOrGenPrivValidator(privValidatorFile) diff --git a/consensus/replay.go b/consensus/replay.go index 931e8893d..5bc0c2611 100644 --- a/consensus/replay.go +++ b/consensus/replay.go @@ -10,11 +10,12 @@ import ( "strings" "time" + "github.com/spf13/viper" + abci "github.com/tendermint/abci/types" - auto "github.com/tendermint/tmlibs/autofile" - . "github.com/tendermint/tmlibs/common" - cfg "github.com/tendermint/go-config" "github.com/tendermint/go-wire" + auto "github.com/tendermint/tmlibs/autofile" + cmn "github.com/tendermint/tmlibs/common" "github.com/tendermint/tendermint/proxy" sm "github.com/tendermint/tendermint/state" @@ -108,7 +109,7 @@ func (cs *ConsensusState) catchupReplay(csHeight int) error { gr.Close() } if found { - return errors.New(Fmt("WAL should not contain #ENDHEIGHT %d.", csHeight)) + return errors.New(cmn.Fmt("WAL should not contain #ENDHEIGHT %d.", csHeight)) } // Search for last height marker @@ -143,7 +144,7 @@ func (cs *ConsensusState) catchupReplay(csHeight int) error { } // TODO (0.10.0): uncomment - // return errors.New(Fmt("Cannot replay height %d. WAL does not contain #ENDHEIGHT for %d.", csHeight, csHeight-1)) + // return errors.New(cmn.Fmt("Cannot replay height %d. WAL does not contain #ENDHEIGHT for %d.", csHeight, csHeight-1)) } log.Notice("Catchup by replaying consensus messages", "height", csHeight) @@ -199,14 +200,14 @@ func makeHeightSearchFunc(height int) auto.SearchFunc { // we were last and using the WAL to recover there type Handshaker struct { - config cfg.Config + config *viper.Viper state *sm.State store types.BlockStore nBlocks int // number of blocks applied to the state } -func NewHandshaker(config cfg.Config, state *sm.State, store types.BlockStore) *Handshaker { +func NewHandshaker(config *viper.Viper, state *sm.State, store types.BlockStore) *Handshaker { return &Handshaker{config, state, store, 0} } @@ -221,7 +222,7 @@ func (h *Handshaker) Handshake(proxyApp proxy.AppConns) error { // handshake is done via info request on the query conn res, err := proxyApp.Query().InfoSync() if err != nil { - return errors.New(Fmt("Error calling Info: %v", err)) + return errors.New(cmn.Fmt("Error calling Info: %v", err)) } blockHeight := int(res.LastBlockHeight) // XXX: beware overflow @@ -238,7 +239,7 @@ func (h *Handshaker) Handshake(proxyApp proxy.AppConns) error { return nil } else if err != nil { - return errors.New(Fmt("Error on replay: %v", err)) + return errors.New(cmn.Fmt("Error on replay: %v", err)) } log.Notice("Completed ABCI Handshake - Tendermint and App are synced", "appHeight", blockHeight, "appHash", appHash) @@ -266,11 +267,11 @@ func (h *Handshaker) ReplayBlocks(appHash []byte, appBlockHeight int, proxyApp p } else if storeBlockHeight < stateBlockHeight { // the state should never be ahead of the store (this is under tendermint's control) - PanicSanity(Fmt("StateBlockHeight (%d) > StoreBlockHeight (%d)", stateBlockHeight, storeBlockHeight)) + cmn.PanicSanity(cmn.Fmt("StateBlockHeight (%d) > StoreBlockHeight (%d)", stateBlockHeight, storeBlockHeight)) } else if storeBlockHeight > stateBlockHeight+1 { // store should be at most one ahead of the state (this is under tendermint's control) - PanicSanity(Fmt("StoreBlockHeight (%d) > StateBlockHeight + 1 (%d)", storeBlockHeight, stateBlockHeight+1)) + cmn.PanicSanity(cmn.Fmt("StoreBlockHeight (%d) > StateBlockHeight + 1 (%d)", storeBlockHeight, stateBlockHeight+1)) } // Now either store is equal to state, or one ahead. @@ -313,7 +314,7 @@ func (h *Handshaker) ReplayBlocks(appHash []byte, appBlockHeight int, proxyApp p } - PanicSanity("Should never happen") + cmn.PanicSanity("Should never happen") return nil, nil } @@ -368,7 +369,7 @@ func (h *Handshaker) replayBlock(height int, proxyApp proxy.AppConnConsensus) ([ func (h *Handshaker) checkAppHash(appHash []byte) error { if !bytes.Equal(h.state.AppHash, appHash) { - panic(errors.New(Fmt("Tendermint state.AppHash does not match AppHash after replay. Got %X, expected %X", appHash, h.state.AppHash)).Error()) + panic(errors.New(cmn.Fmt("Tendermint state.AppHash does not match AppHash after replay. Got %X, expected %X", appHash, h.state.AppHash)).Error()) return nil } return nil diff --git a/consensus/replay_file.go b/consensus/replay_file.go index 3de0e3d10..1d2a91f31 100644 --- a/consensus/replay_file.go +++ b/consensus/replay_file.go @@ -8,24 +8,25 @@ import ( "strconv" "strings" - . "github.com/tendermint/tmlibs/common" - cfg "github.com/tendermint/go-config" - dbm "github.com/tendermint/tmlibs/db" + "github.com/spf13/viper" + bc "github.com/tendermint/tendermint/blockchain" mempl "github.com/tendermint/tendermint/mempool" "github.com/tendermint/tendermint/proxy" sm "github.com/tendermint/tendermint/state" "github.com/tendermint/tendermint/types" + cmn "github.com/tendermint/tmlibs/common" + dbm "github.com/tendermint/tmlibs/db" ) //-------------------------------------------------------- // replay messages interactively or all at once -func RunReplayFile(config cfg.Config, walFile string, console bool) { +func RunReplayFile(config *viper.Viper, walFile string, console bool) { consensusState := newConsensusStateForReplay(config) if err := consensusState.ReplayFile(walFile, console); err != nil { - Exit(Fmt("Error during consensus replay: %v", err)) + cmn.Exit(cmn.Fmt("Error during consensus replay: %v", err)) } } @@ -114,7 +115,7 @@ func (pb *playback) replayReset(count int, newStepCh chan interface{}) error { pb.fp = fp pb.scanner = bufio.NewScanner(fp) count = pb.count - count - log.Notice(Fmt("Reseting from %d to %d", pb.count, count)) + log.Notice(cmn.Fmt("Reseting from %d to %d", pb.count, count)) pb.count = 0 pb.cs = newCS for i := 0; pb.scanner.Scan() && i < count; i++ { @@ -149,9 +150,9 @@ func (pb *playback) replayConsoleLoop() int { bufReader := bufio.NewReader(os.Stdin) line, more, err := bufReader.ReadLine() if more { - Exit("input is too long") + cmn.Exit("input is too long") } else if err != nil { - Exit(err.Error()) + cmn.Exit(err.Error()) } tokens := strings.Split(string(line), " ") @@ -236,7 +237,7 @@ func (pb *playback) replayConsoleLoop() int { //-------------------------------------------------------------------------------- // convenience for replay mode -func newConsensusStateForReplay(config cfg.Config) *ConsensusState { +func newConsensusStateForReplay(config *viper.Viper) *ConsensusState { // Get BlockStore blockStoreDB := dbm.NewDB("blockstore", config.GetString("db_backend"), config.GetString("db_dir")) blockStore := bc.NewBlockStore(blockStoreDB) @@ -249,7 +250,7 @@ func newConsensusStateForReplay(config cfg.Config) *ConsensusState { proxyApp := proxy.NewAppConns(config, proxy.DefaultClientCreator(config), NewHandshaker(config, state, blockStore)) _, err := proxyApp.Start() if err != nil { - Exit(Fmt("Error starting proxy app conns: %v", err)) + cmn.Exit(cmn.Fmt("Error starting proxy app conns: %v", err)) } // add the chainid to the global config @@ -258,7 +259,7 @@ func newConsensusStateForReplay(config cfg.Config) *ConsensusState { // Make event switch eventSwitch := types.NewEventSwitch() if _, err := eventSwitch.Start(); err != nil { - Exit(Fmt("Failed to start event switch: %v", err)) + cmn.Exit(cmn.Fmt("Failed to start event switch: %v", err)) } mempool := mempl.NewMempool(config, proxyApp.Mempool()) diff --git a/consensus/replay_test.go b/consensus/replay_test.go index 32415d6e3..e486af83e 100644 --- a/consensus/replay_test.go +++ b/consensus/replay_test.go @@ -12,17 +12,17 @@ import ( "testing" "time" - "github.com/tendermint/tendermint/config/tendermint_test" + "github.com/spf13/viper" "github.com/tendermint/abci/example/dummy" - cmn "github.com/tendermint/tmlibs/common" - cfg "github.com/tendermint/go-config" "github.com/tendermint/go-crypto" - dbm "github.com/tendermint/tmlibs/db" "github.com/tendermint/go-wire" + "github.com/tendermint/tendermint/config/tendermint_test" "github.com/tendermint/tendermint/proxy" sm "github.com/tendermint/tendermint/state" "github.com/tendermint/tendermint/types" + cmn "github.com/tendermint/tmlibs/common" + dbm "github.com/tendermint/tmlibs/db" ) func init() { @@ -408,7 +408,7 @@ func buildAppStateFromChain(proxyApp proxy.AppConns, } -func buildTMStateFromChain(config cfg.Config, state *sm.State, chain []*types.Block, mode uint) []byte { +func buildTMStateFromChain(config *viper.Viper, state *sm.State, chain []*types.Block, mode uint) []byte { // run the whole chain against this client to build up the tendermint state clientCreator := proxy.NewLocalClientCreator(dummy.NewPersistentDummyApplication(path.Join(config.GetString("db_dir"), "1"))) proxyApp := proxy.NewAppConns(config, clientCreator, nil) // sm.NewHandshaker(config, state, store, ReplayLastBlock)) @@ -602,7 +602,7 @@ func makeBlockchain(t *testing.T, chainID string, nBlocks int, privVal *types.Pr } // fresh state and mock store -func stateAndStore(config cfg.Config, pubKey crypto.PubKey) (*sm.State, *mockBlockStore) { +func stateAndStore(config *viper.Viper, pubKey crypto.PubKey) (*sm.State, *mockBlockStore) { stateDB := dbm.NewMemDB() return sm.MakeGenesisState(stateDB, &types.GenesisDoc{ ChainID: config.GetString("chain_id"), @@ -617,13 +617,13 @@ func stateAndStore(config cfg.Config, pubKey crypto.PubKey) (*sm.State, *mockBlo // mock block store type mockBlockStore struct { - config cfg.Config + config *viper.Viper chain []*types.Block commits []*types.Commit } // TODO: NewBlockStore(db.NewMemDB) ... -func NewMockBlockStore(config cfg.Config) *mockBlockStore { +func NewMockBlockStore(config *viper.Viper) *mockBlockStore { return &mockBlockStore{config, nil, nil} } diff --git a/consensus/state.go b/consensus/state.go index 8077dcec1..28836a079 100644 --- a/consensus/state.go +++ b/consensus/state.go @@ -10,13 +10,13 @@ import ( "time" "github.com/ebuchman/fail-test" + "github.com/spf13/viper" - . "github.com/tendermint/tmlibs/common" - cfg "github.com/tendermint/go-config" "github.com/tendermint/go-wire" "github.com/tendermint/tendermint/proxy" sm "github.com/tendermint/tendermint/state" "github.com/tendermint/tendermint/types" + cmn "github.com/tendermint/tmlibs/common" ) //----------------------------------------------------------------------------- @@ -56,7 +56,7 @@ func (tp *TimeoutParams) Commit(t time.Time) time.Time { } // InitTimeoutParamsFromConfig initializes parameters from config -func InitTimeoutParamsFromConfig(config cfg.Config) *TimeoutParams { +func InitTimeoutParamsFromConfig(config *viper.Viper) *TimeoutParams { return &TimeoutParams{ Propose0: config.GetInt("timeout_propose"), ProposeDelta: config.GetInt("timeout_propose_delta"), @@ -222,9 +222,9 @@ type PrivValidator interface { // Tracks consensus state across block heights and rounds. type ConsensusState struct { - BaseService + cmn.BaseService - config cfg.Config + config *viper.Viper proxyAppConn proxy.AppConnConsensus blockStore types.BlockStore mempool types.Mempool @@ -255,7 +255,7 @@ type ConsensusState struct { done chan struct{} } -func NewConsensusState(config cfg.Config, state *sm.State, proxyAppConn proxy.AppConnConsensus, blockStore types.BlockStore, mempool types.Mempool) *ConsensusState { +func NewConsensusState(config *viper.Viper, state *sm.State, proxyAppConn proxy.AppConnConsensus, blockStore types.BlockStore, mempool types.Mempool) *ConsensusState { cs := &ConsensusState{ config: config, proxyAppConn: proxyAppConn, @@ -276,7 +276,7 @@ func NewConsensusState(config cfg.Config, state *sm.State, proxyAppConn proxy.Ap // Don't call scheduleRound0 yet. // We do that upon Start(). cs.reconstructLastCommit(state) - cs.BaseService = *NewBaseService(log, "ConsensusState", cs) + cs.BaseService = *cmn.NewBaseService(log, "ConsensusState", cs) return cs } @@ -290,7 +290,7 @@ func (cs *ConsensusState) SetEventSwitch(evsw types.EventSwitch) { func (cs *ConsensusState) String() string { // better not to access shared variables - return Fmt("ConsensusState") //(H:%v R:%v S:%v", cs.Height, cs.Round, cs.Step) + return cmn.Fmt("ConsensusState") //(H:%v R:%v S:%v", cs.Height, cs.Round, cs.Step) } func (cs *ConsensusState) GetState() *sm.State { @@ -398,7 +398,7 @@ func (cs *ConsensusState) Wait() { // Open file to log all consensus messages and timeouts for deterministic accountability func (cs *ConsensusState) OpenWAL(walFile string) (err error) { - err = EnsureDir(path.Dir(walFile), 0700) + err = cmn.EnsureDir(path.Dir(walFile), 0700) if err != nil { log.Error("Error ensuring ConsensusState wal dir", "error", err.Error()) return err @@ -519,11 +519,11 @@ func (cs *ConsensusState) reconstructLastCommit(state *sm.State) { } added, err := lastPrecommits.AddVote(precommit) if !added || err != nil { - PanicCrisis(Fmt("Failed to reconstruct LastCommit: %v", err)) + cmn.PanicCrisis(cmn.Fmt("Failed to reconstruct LastCommit: %v", err)) } } if !lastPrecommits.HasTwoThirdsMajority() { - PanicSanity("Failed to reconstruct LastCommit: Does not have +2/3 maj") + cmn.PanicSanity("Failed to reconstruct LastCommit: Does not have +2/3 maj") } cs.LastCommit = lastPrecommits } @@ -532,13 +532,13 @@ func (cs *ConsensusState) reconstructLastCommit(state *sm.State) { // The round becomes 0 and cs.Step becomes RoundStepNewHeight. func (cs *ConsensusState) updateToState(state *sm.State) { if cs.CommitRound > -1 && 0 < cs.Height && cs.Height != state.LastBlockHeight { - PanicSanity(Fmt("updateToState() expected state height of %v but found %v", + cmn.PanicSanity(cmn.Fmt("updateToState() expected state height of %v but found %v", cs.Height, state.LastBlockHeight)) } if cs.state != nil && cs.state.LastBlockHeight+1 != cs.Height { // This might happen when someone else is mutating cs.state. // Someone forgot to pass in state.Copy() somewhere?! - PanicSanity(Fmt("Inconsistent cs.state.LastBlockHeight+1 %v vs cs.Height %v", + cmn.PanicSanity(cmn.Fmt("Inconsistent cs.state.LastBlockHeight+1 %v vs cs.Height %v", cs.state.LastBlockHeight+1, cs.Height)) } @@ -555,7 +555,7 @@ func (cs *ConsensusState) updateToState(state *sm.State) { lastPrecommits := (*types.VoteSet)(nil) if cs.CommitRound > -1 && cs.Votes != nil { if !cs.Votes.Precommits(cs.CommitRound).HasTwoThirdsMajority() { - PanicSanity("updateToState(state) called but last Precommit round didn't have +2/3") + cmn.PanicSanity("updateToState(state) called but last Precommit round didn't have +2/3") } lastPrecommits = cs.Votes.Precommits(cs.CommitRound) } @@ -723,7 +723,7 @@ func (cs *ConsensusState) handleTimeout(ti timeoutInfo, rs RoundState) { types.FireEventTimeoutWait(cs.evsw, cs.RoundStateEvent()) cs.enterNewRound(ti.Height, ti.Round+1) default: - panic(Fmt("Invalid timeout step: %v", ti.Step)) + panic(cmn.Fmt("Invalid timeout step: %v", ti.Step)) } } @@ -738,7 +738,7 @@ func (cs *ConsensusState) handleTimeout(ti timeoutInfo, rs RoundState) { // NOTE: cs.StartTime was already set for height. func (cs *ConsensusState) enterNewRound(height int, round int) { if cs.Height != height || round < cs.Round || (cs.Round == round && cs.Step != RoundStepNewHeight) { - log.Debug(Fmt("enterNewRound(%v/%v): Invalid args. Current step: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step)) + log.Debug(cmn.Fmt("enterNewRound(%v/%v): Invalid args. Current step: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step)) return } @@ -746,7 +746,7 @@ func (cs *ConsensusState) enterNewRound(height int, round int) { log.Warn("Need to set a buffer and log.Warn() here for sanity.", "startTime", cs.StartTime, "now", now) } - log.Notice(Fmt("enterNewRound(%v/%v). Current: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step)) + log.Notice(cmn.Fmt("enterNewRound(%v/%v). Current: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step)) // Increment validators if necessary validators := cs.Validators @@ -780,10 +780,10 @@ func (cs *ConsensusState) enterNewRound(height int, round int) { // Enter: from NewRound(height,round). func (cs *ConsensusState) enterPropose(height int, round int) { if cs.Height != height || round < cs.Round || (cs.Round == round && RoundStepPropose <= cs.Step) { - log.Debug(Fmt("enterPropose(%v/%v): Invalid args. Current step: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step)) + log.Debug(cmn.Fmt("enterPropose(%v/%v): Invalid args. Current step: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step)) return } - log.Info(Fmt("enterPropose(%v/%v). Current: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step)) + log.Info(cmn.Fmt("enterPropose(%v/%v). Current: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step)) defer func() { // Done enterPropose: @@ -850,7 +850,7 @@ func (cs *ConsensusState) defaultDecideProposal(height, round int) { cs.sendInternalMessage(msgInfo{&BlockPartMessage{cs.Height, cs.Round, part}, ""}) } log.Info("Signed proposal", "height", height, "round", round, "proposal", proposal) - log.Debug(Fmt("Signed proposal block: %v", block)) + log.Debug(cmn.Fmt("Signed proposal block: %v", block)) } else { if !cs.replayMode { log.Warn("enterPropose: Error signing proposal", "height", height, "round", round, "error", err) @@ -906,7 +906,7 @@ func (cs *ConsensusState) createProposalBlock() (block *types.Block, blockParts // Otherwise vote nil. func (cs *ConsensusState) enterPrevote(height int, round int) { if cs.Height != height || round < cs.Round || (cs.Round == round && RoundStepPrevote <= cs.Step) { - log.Debug(Fmt("enterPrevote(%v/%v): Invalid args. Current step: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step)) + log.Debug(cmn.Fmt("enterPrevote(%v/%v): Invalid args. Current step: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step)) return } @@ -924,7 +924,7 @@ func (cs *ConsensusState) enterPrevote(height int, round int) { // TODO: catchup event? } - log.Info(Fmt("enterPrevote(%v/%v). Current: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step)) + log.Info(cmn.Fmt("enterPrevote(%v/%v). Current: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step)) // Sign and broadcast vote as necessary cs.doPrevote(height, round) @@ -967,13 +967,13 @@ func (cs *ConsensusState) defaultDoPrevote(height int, round int) { // Enter: any +2/3 prevotes at next round. func (cs *ConsensusState) enterPrevoteWait(height int, round int) { if cs.Height != height || round < cs.Round || (cs.Round == round && RoundStepPrevoteWait <= cs.Step) { - log.Debug(Fmt("enterPrevoteWait(%v/%v): Invalid args. Current step: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step)) + log.Debug(cmn.Fmt("enterPrevoteWait(%v/%v): Invalid args. Current step: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step)) return } if !cs.Votes.Prevotes(round).HasTwoThirdsAny() { - PanicSanity(Fmt("enterPrevoteWait(%v/%v), but Prevotes does not have any +2/3 votes", height, round)) + cmn.PanicSanity(cmn.Fmt("enterPrevoteWait(%v/%v), but Prevotes does not have any +2/3 votes", height, round)) } - log.Info(Fmt("enterPrevoteWait(%v/%v). Current: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step)) + log.Info(cmn.Fmt("enterPrevoteWait(%v/%v). Current: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step)) defer func() { // Done enterPrevoteWait: @@ -993,11 +993,11 @@ func (cs *ConsensusState) enterPrevoteWait(height int, round int) { // else, precommit nil otherwise. func (cs *ConsensusState) enterPrecommit(height int, round int) { if cs.Height != height || round < cs.Round || (cs.Round == round && RoundStepPrecommit <= cs.Step) { - log.Debug(Fmt("enterPrecommit(%v/%v): Invalid args. Current step: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step)) + log.Debug(cmn.Fmt("enterPrecommit(%v/%v): Invalid args. Current step: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step)) return } - log.Info(Fmt("enterPrecommit(%v/%v). Current: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step)) + log.Info(cmn.Fmt("enterPrecommit(%v/%v). Current: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step)) defer func() { // Done enterPrecommit: @@ -1024,7 +1024,7 @@ func (cs *ConsensusState) enterPrecommit(height int, round int) { // the latest POLRound should be this round polRound, _ := cs.Votes.POLInfo() if polRound < round { - PanicSanity(Fmt("This POLRound should be %v but got %", round, polRound)) + cmn.PanicSanity(cmn.Fmt("This POLRound should be %v but got %", round, polRound)) } // +2/3 prevoted nil. Unlock and precommit nil. @@ -1058,7 +1058,7 @@ func (cs *ConsensusState) enterPrecommit(height int, round int) { log.Notice("enterPrecommit: +2/3 prevoted proposal block. Locking", "hash", blockID.Hash) // Validate the block. if err := cs.state.ValidateBlock(cs.ProposalBlock); err != nil { - PanicConsensus(Fmt("enterPrecommit: +2/3 prevoted for an invalid block: %v", err)) + cmn.PanicConsensus(cmn.Fmt("enterPrecommit: +2/3 prevoted for an invalid block: %v", err)) } cs.LockedRound = round cs.LockedBlock = cs.ProposalBlock @@ -1087,13 +1087,13 @@ func (cs *ConsensusState) enterPrecommit(height int, round int) { // Enter: any +2/3 precommits for next round. func (cs *ConsensusState) enterPrecommitWait(height int, round int) { if cs.Height != height || round < cs.Round || (cs.Round == round && RoundStepPrecommitWait <= cs.Step) { - log.Debug(Fmt("enterPrecommitWait(%v/%v): Invalid args. Current step: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step)) + log.Debug(cmn.Fmt("enterPrecommitWait(%v/%v): Invalid args. Current step: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step)) return } if !cs.Votes.Precommits(round).HasTwoThirdsAny() { - PanicSanity(Fmt("enterPrecommitWait(%v/%v), but Precommits does not have any +2/3 votes", height, round)) + cmn.PanicSanity(cmn.Fmt("enterPrecommitWait(%v/%v), but Precommits does not have any +2/3 votes", height, round)) } - log.Info(Fmt("enterPrecommitWait(%v/%v). Current: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step)) + log.Info(cmn.Fmt("enterPrecommitWait(%v/%v). Current: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step)) defer func() { // Done enterPrecommitWait: @@ -1109,10 +1109,10 @@ func (cs *ConsensusState) enterPrecommitWait(height int, round int) { // Enter: +2/3 precommits for block func (cs *ConsensusState) enterCommit(height int, commitRound int) { if cs.Height != height || RoundStepCommit <= cs.Step { - log.Debug(Fmt("enterCommit(%v/%v): Invalid args. Current step: %v/%v/%v", height, commitRound, cs.Height, cs.Round, cs.Step)) + log.Debug(cmn.Fmt("enterCommit(%v/%v): Invalid args. Current step: %v/%v/%v", height, commitRound, cs.Height, cs.Round, cs.Step)) return } - log.Info(Fmt("enterCommit(%v/%v). Current: %v/%v/%v", height, commitRound, cs.Height, cs.Round, cs.Step)) + log.Info(cmn.Fmt("enterCommit(%v/%v). Current: %v/%v/%v", height, commitRound, cs.Height, cs.Round, cs.Step)) defer func() { // Done enterCommit: @@ -1128,7 +1128,7 @@ func (cs *ConsensusState) enterCommit(height int, commitRound int) { blockID, ok := cs.Votes.Precommits(commitRound).TwoThirdsMajority() if !ok { - PanicSanity("RunActionCommit() expects +2/3 precommits") + cmn.PanicSanity("RunActionCommit() expects +2/3 precommits") } // The Locked* fields no longer matter. @@ -1155,7 +1155,7 @@ func (cs *ConsensusState) enterCommit(height int, commitRound int) { // If we have the block AND +2/3 commits for it, finalize. func (cs *ConsensusState) tryFinalizeCommit(height int) { if cs.Height != height { - PanicSanity(Fmt("tryFinalizeCommit() cs.Height: %v vs height: %v", cs.Height, height)) + cmn.PanicSanity(cmn.Fmt("tryFinalizeCommit() cs.Height: %v vs height: %v", cs.Height, height)) } blockID, ok := cs.Votes.Precommits(cs.CommitRound).TwoThirdsMajority() @@ -1176,7 +1176,7 @@ func (cs *ConsensusState) tryFinalizeCommit(height int) { // Increment height and goto RoundStepNewHeight func (cs *ConsensusState) finalizeCommit(height int) { if cs.Height != height || cs.Step != RoundStepCommit { - log.Debug(Fmt("finalizeCommit(%v): Invalid args. Current step: %v/%v/%v", height, cs.Height, cs.Round, cs.Step)) + log.Debug(cmn.Fmt("finalizeCommit(%v): Invalid args. Current step: %v/%v/%v", height, cs.Height, cs.Round, cs.Step)) return } @@ -1184,21 +1184,21 @@ func (cs *ConsensusState) finalizeCommit(height int) { block, blockParts := cs.ProposalBlock, cs.ProposalBlockParts if !ok { - PanicSanity(Fmt("Cannot finalizeCommit, commit does not have two thirds majority")) + cmn.PanicSanity(cmn.Fmt("Cannot finalizeCommit, commit does not have two thirds majority")) } if !blockParts.HasHeader(blockID.PartsHeader) { - PanicSanity(Fmt("Expected ProposalBlockParts header to be commit header")) + cmn.PanicSanity(cmn.Fmt("Expected ProposalBlockParts header to be commit header")) } if !block.HashesTo(blockID.Hash) { - PanicSanity(Fmt("Cannot finalizeCommit, ProposalBlock does not hash to commit hash")) + cmn.PanicSanity(cmn.Fmt("Cannot finalizeCommit, ProposalBlock does not hash to commit hash")) } if err := cs.state.ValidateBlock(block); err != nil { - PanicConsensus(Fmt("+2/3 committed an invalid block: %v", err)) + cmn.PanicConsensus(cmn.Fmt("+2/3 committed an invalid block: %v", err)) } - log.Notice(Fmt("Finalizing commit of block with %d txs", block.NumTxs), + log.Notice(cmn.Fmt("Finalizing commit of block with %d txs", block.NumTxs), "height", block.Height, "hash", block.Hash(), "root", block.AppHash) - log.Info(Fmt("%v", block)) + log.Info(cmn.Fmt("%v", block)) fail.Fail() // XXX @@ -1393,7 +1393,7 @@ func (cs *ConsensusState) addVote(vote *types.Vote, peerKey string) (added bool, } added, err = cs.LastCommit.AddVote(vote) if added { - log.Info(Fmt("Added to lastPrecommits: %v", cs.LastCommit.StringShort())) + log.Info(cmn.Fmt("Added to lastPrecommits: %v", cs.LastCommit.StringShort())) types.FireEventVote(cs.evsw, types.EventDataVote{vote}) // if we can skip timeoutCommit and have all the votes now, @@ -1474,7 +1474,7 @@ func (cs *ConsensusState) addVote(vote *types.Vote, peerKey string) (added bool, cs.enterPrecommitWait(height, vote.Round) } default: - PanicSanity(Fmt("Unexpected vote type %X", vote.Type)) // Should not happen. + cmn.PanicSanity(cmn.Fmt("Unexpected vote type %X", vote.Type)) // Should not happen. } } // Either duplicate, or error upon cs.Votes.AddByIndex() diff --git a/glide.lock b/glide.lock index 954064e5c..7720c946b 100644 --- a/glide.lock +++ b/glide.lock @@ -1,18 +1,18 @@ -hash: 9096fc4e29eff8fac3708ed30deacac67b7a181f0192b177006105dccdb16686 -updated: 2017-04-21T18:39:37.154946943-04:00 +hash: 6f8962f6ca0e25b8e43bc6e496bd46c9ff3a79dcf789578efeeaee2fffc39c6e +updated: 2017-04-25T14:48:36.999444976-04:00 imports: - name: github.com/btcsuite/btcd version: 583684b21bfbde9b5fc4403916fd7c807feb0289 subpackages: - btcec -- name: github.com/BurntSushi/toml - version: 99064174e013895bbd9b025c31100bd1d9b590ca - name: github.com/davecgh/go-spew version: 6d212800a42e8ab5c146b8ace3490ee17e5225f9 subpackages: - spew - name: github.com/ebuchman/fail-test version: 95f809107225be108efcf10a3509e4ea6ceef3c4 +- name: github.com/fsnotify/fsnotify + version: 4da3e2cfbabc9f751898f250b49f2439785783a1 - name: github.com/go-stack/stack version: 100eb0c0a9c5b306ca2fb4f165df21d80ada4b82 - name: github.com/gogo/protobuf @@ -27,24 +27,53 @@ imports: version: d9eb7a3d35ec988b8585d4a0068e462c27d28380 - name: github.com/gorilla/websocket version: 3ab3a8b8831546bd18fd182c20687ca853b2bb13 +- name: github.com/hashicorp/hcl + version: 630949a3c5fa3c613328e1b8256052cbc2327c9b + subpackages: + - hcl/ast + - hcl/parser + - hcl/scanner + - hcl/strconv + - hcl/token + - json/parser + - json/scanner + - json/token - name: github.com/inconshreveable/mousetrap version: 76626ae9c91c4f2a10f34cad8ce83ea42c93bb75 - name: github.com/jmhodges/levigo version: c42d9e0ca023e2198120196f842701bb4c55d7b9 +- name: github.com/magiconair/properties + version: 51463bfca2576e06c62a8504b5c0f06d61312647 - name: github.com/mattn/go-colorable version: d228849504861217f796da67fae4f6e347643f15 - name: github.com/mattn/go-isatty version: 30a891c33c7cde7b02a981314b4228ec99380cca +- name: github.com/mitchellh/mapstructure + version: 53818660ed4955e899c0bcafa97299a388bd7c8e +- name: github.com/pelletier/go-buffruneio + version: c37440a7cf42ac63b919c752ca73a85067e05992 +- name: github.com/pelletier/go-toml + version: 13d49d4606eb801b8f01ae542b4afc4c6ee3d84a - name: github.com/pkg/errors version: 645ef00459ed84a119197bfb8d8205042c6df63d - name: github.com/pmezard/go-difflib version: d8ed2627bdf02c080bf22230dbb337003b7aba2d subpackages: - difflib +- name: github.com/spf13/afero + version: 9be650865eab0c12963d8753212f4f9c66cdcf12 + subpackages: + - mem +- name: github.com/spf13/cast + version: acbeb36b902d72a7a4c18e8f3241075e7ab763e4 - name: github.com/spf13/cobra version: fcd0c5a1df88f5d6784cb4feead962c3f3d0b66c +- name: github.com/spf13/jwalterweatherman + version: fa7ca7e836cf3a8bb4ebf799f472c12d7e903d66 - name: github.com/spf13/pflag version: 9ff6c6923cfffbcd502984b8e0c80539a94968b7 +- name: github.com/spf13/viper + version: 5d46e70da8c0b6f812e0b170b7a985753b5c63cb - name: github.com/stretchr/testify version: 69483b4bd14f5845b5a1e55bca19e954e827f1d0 subpackages: @@ -66,7 +95,7 @@ imports: - leveldb/table - leveldb/util - name: github.com/tendermint/abci - version: be61e273cebeb64866a83c578f92b22cf2169cdd + version: c709d3cc857929a8dd36a90da3640122d7e75770 subpackages: - client - example/counter @@ -78,10 +107,6 @@ imports: subpackages: - edwards25519 - extra25519 -- name: github.com/tendermint/go-common - version: f9e3db037330c8a8d61d3966de8473eaf01154fa -- name: github.com/tendermint/go-config - version: 620dcbbd7d587cf3599dedbf329b64311b0c307a - name: github.com/tendermint/go-crypto version: 9b95da8fa4187f6799558d89b271dc8ab6485615 - name: github.com/tendermint/go-wire @@ -136,6 +161,11 @@ imports: version: d75a52659825e75fff6158388dddc6a5b04f9ba5 subpackages: - unix +- name: golang.org/x/text + version: f4b4367115ec2de254587813edaa901bc1c723a8 + subpackages: + - transform + - unicode/norm - name: google.golang.org/grpc version: cbcceb2942a489498cf22b2f918536e819d33f0a subpackages: @@ -149,4 +179,6 @@ imports: - stats - tap - transport +- name: gopkg.in/yaml.v2 + version: cd8b52f8269e0feb286dfeef29f8fe4d5b397e0b testImports: [] diff --git a/glide.yaml b/glide.yaml index a5570d038..f06a16b3b 100644 --- a/glide.yaml +++ b/glide.yaml @@ -10,6 +10,7 @@ import: - package: github.com/gorilla/websocket - package: github.com/pkg/errors - package: github.com/spf13/cobra +- package: github.com/spf13/viper - package: github.com/stretchr/testify subpackages: - require @@ -19,8 +20,6 @@ import: - client - example/dummy - types -- package: github.com/tendermint/go-config - version: develop - package: github.com/tendermint/go-crypto version: develop - package: github.com/tendermint/go-wire diff --git a/mempool/mempool.go b/mempool/mempool.go index eedf6fb9c..621f0931e 100644 --- a/mempool/mempool.go +++ b/mempool/mempool.go @@ -7,13 +7,14 @@ import ( "sync/atomic" "time" + "github.com/spf13/viper" + abci "github.com/tendermint/abci/types" + "github.com/tendermint/tendermint/proxy" + "github.com/tendermint/tendermint/types" auto "github.com/tendermint/tmlibs/autofile" "github.com/tendermint/tmlibs/clist" . "github.com/tendermint/tmlibs/common" - cfg "github.com/tendermint/go-config" - "github.com/tendermint/tendermint/proxy" - "github.com/tendermint/tendermint/types" ) /* @@ -47,7 +48,7 @@ TODO: Better handle abci client errors. (make it automatically handle connection const cacheSize = 100000 type Mempool struct { - config cfg.Config + config *viper.Viper proxyMtx sync.Mutex proxyAppConn proxy.AppConnMempool @@ -66,7 +67,7 @@ type Mempool struct { wal *auto.AutoFile } -func NewMempool(config cfg.Config, proxyAppConn proxy.AppConnMempool) *Mempool { +func NewMempool(config *viper.Viper, proxyAppConn proxy.AppConnMempool) *Mempool { mempool := &Mempool{ config: config, proxyAppConn: proxyAppConn, diff --git a/mempool/reactor.go b/mempool/reactor.go index 4d1638888..6bea53b0c 100644 --- a/mempool/reactor.go +++ b/mempool/reactor.go @@ -6,12 +6,13 @@ import ( "reflect" "time" + "github.com/spf13/viper" + abci "github.com/tendermint/abci/types" - "github.com/tendermint/tmlibs/clist" - cfg "github.com/tendermint/go-config" - "github.com/tendermint/tendermint/p2p" "github.com/tendermint/go-wire" + "github.com/tendermint/tendermint/p2p" "github.com/tendermint/tendermint/types" + "github.com/tendermint/tmlibs/clist" ) const ( @@ -24,12 +25,12 @@ const ( // MempoolReactor handles mempool tx broadcasting amongst peers. type MempoolReactor struct { p2p.BaseReactor - config cfg.Config + config *viper.Viper Mempool *Mempool evsw types.EventSwitch } -func NewMempoolReactor(config cfg.Config, mempool *Mempool) *MempoolReactor { +func NewMempoolReactor(config *viper.Viper, mempool *Mempool) *MempoolReactor { memR := &MempoolReactor{ config: config, Mempool: mempool, diff --git a/node/node.go b/node/node.go index f24c1c4f2..1c66c00e0 100644 --- a/node/node.go +++ b/node/node.go @@ -7,19 +7,18 @@ import ( "net/http" "strings" + "github.com/spf13/viper" + abci "github.com/tendermint/abci/types" - cmn "github.com/tendermint/tmlibs/common" - cfg "github.com/tendermint/go-config" crypto "github.com/tendermint/go-crypto" - dbm "github.com/tendermint/tmlibs/db" - p2p "github.com/tendermint/tendermint/p2p" - rpc "github.com/tendermint/tendermint/rpc" - rpcserver "github.com/tendermint/tendermint/rpc/server" wire "github.com/tendermint/go-wire" bc "github.com/tendermint/tendermint/blockchain" "github.com/tendermint/tendermint/consensus" mempl "github.com/tendermint/tendermint/mempool" + p2p "github.com/tendermint/tendermint/p2p" "github.com/tendermint/tendermint/proxy" + rpc "github.com/tendermint/tendermint/rpc" + rpcserver "github.com/tendermint/tendermint/rpc/server" rpccore "github.com/tendermint/tendermint/rpc/tendermint/core" grpccore "github.com/tendermint/tendermint/rpc/tendermint/grpc" sm "github.com/tendermint/tendermint/state" @@ -28,6 +27,8 @@ import ( "github.com/tendermint/tendermint/state/txindex/null" "github.com/tendermint/tendermint/types" "github.com/tendermint/tendermint/version" + cmn "github.com/tendermint/tmlibs/common" + dbm "github.com/tendermint/tmlibs/db" _ "net/http/pprof" ) @@ -36,7 +37,7 @@ type Node struct { cmn.BaseService // config - config cfg.Config // user config + config *viper.Viper // user config genesisDoc *types.GenesisDoc // initial validator set privValidator *types.PrivValidator // local node's validator key @@ -57,14 +58,14 @@ type Node struct { txIndexer txindex.TxIndexer } -func NewNodeDefault(config cfg.Config) *Node { +func NewNodeDefault(config *viper.Viper) *Node { // Get PrivValidator privValidatorFile := config.GetString("priv_validator_file") privValidator := types.LoadOrGenPrivValidator(privValidatorFile) return NewNode(config, privValidator, proxy.DefaultClientCreator(config)) } -func NewNode(config cfg.Config, privValidator *types.PrivValidator, clientCreator proxy.ClientCreator) *Node { +func NewNode(config *viper.Viper, privValidator *types.PrivValidator, clientCreator proxy.ClientCreator) *Node { // Get BlockStore blockStoreDB := dbm.NewDB("blockstore", config.GetString("db_backend"), config.GetString("db_dir")) @@ -134,7 +135,11 @@ func NewNode(config cfg.Config, privValidator *types.PrivValidator, clientCreato consensusReactor := consensus.NewConsensusReactor(consensusState, fastSync) // Make p2p network switch - sw := p2p.NewSwitch(config.GetConfig("p2p")) + p2pConfig := viper.New() + if config.IsSet("p2p") { //TODO verify this necessary, where is this ever set? + p2pConfig = config.Get("p2p").(*viper.Viper) + } + sw := p2p.NewSwitch(p2pConfig) sw.AddReactor("MEMPOOL", mempoolReactor) sw.AddReactor("BLOCKCHAIN", bcReactor) sw.AddReactor("CONSENSUS", consensusReactor) diff --git a/p2p/config.go b/p2p/config.go index a8b7e343b..2e70d16df 100644 --- a/p2p/config.go +++ b/p2p/config.go @@ -1,7 +1,7 @@ package p2p import ( - cfg "github.com/tendermint/go-config" + "github.com/spf13/viper" ) const ( @@ -24,7 +24,7 @@ const ( configFuzzProbSleep = "fuzz_prob_sleep" ) -func setConfigDefaults(config cfg.Config) { +func setConfigDefaults(config *viper.Viper) { // Switch default config config.SetDefault(configKeyDialTimeoutSeconds, 3) config.SetDefault(configKeyHandshakeTimeoutSeconds, 20) diff --git a/p2p/switch.go b/p2p/switch.go index 8c771df1c..7e9c0a9c6 100644 --- a/p2p/switch.go +++ b/p2p/switch.go @@ -7,7 +7,8 @@ import ( "net" "time" - cfg "github.com/tendermint/go-config" + "github.com/spf13/viper" + crypto "github.com/tendermint/go-crypto" "github.com/tendermint/log15" . "github.com/tendermint/tmlibs/common" @@ -61,7 +62,7 @@ incoming messages are received on the reactor. type Switch struct { BaseService - config cfg.Config + config *viper.Viper listeners []Listener reactors map[string]Reactor chDescs []*ChannelDescriptor @@ -80,7 +81,7 @@ var ( ErrSwitchMaxPeersPerIPRange = errors.New("IP range has too many peers") ) -func NewSwitch(config cfg.Config) *Switch { +func NewSwitch(config *viper.Viper) *Switch { setConfigDefaults(config) sw := &Switch{ @@ -529,7 +530,7 @@ func makeSwitch(i int, network, version string, initSwitch func(int, *Switch) *S privKey := crypto.GenPrivKeyEd25519() // new switch, add reactors // TODO: let the config be passed in? - s := initSwitch(i, NewSwitch(cfg.NewMapConfig(nil))) + s := initSwitch(i, NewSwitch(viper.New())) s.SetNodeInfo(&NodeInfo{ PubKey: privKey.PubKey().Unwrap().(crypto.PubKeyEd25519), Moniker: Fmt("switch%d", i), @@ -572,7 +573,7 @@ func (sw *Switch) addPeerWithConnectionAndConfig(conn net.Conn, config *PeerConf return nil } -func peerConfigFromGoConfig(config cfg.Config) *PeerConfig { +func peerConfigFromGoConfig(config *viper.Viper) *PeerConfig { return &PeerConfig{ AuthEnc: config.GetBool(configKeyAuthEnc), Fuzz: config.GetBool(configFuzzEnable), diff --git a/p2p/switch_test.go b/p2p/switch_test.go index 1f1fe69f0..38529d754 100644 --- a/p2p/switch_test.go +++ b/p2p/switch_test.go @@ -8,20 +8,21 @@ import ( "testing" "time" + "github.com/spf13/viper" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - . "github.com/tendermint/tmlibs/common" - cfg "github.com/tendermint/go-config" crypto "github.com/tendermint/go-crypto" wire "github.com/tendermint/go-wire" + . "github.com/tendermint/tmlibs/common" ) var ( - config cfg.Config + config *viper.Viper ) func init() { - config = cfg.NewMapConfig(nil) + config = viper.New() setConfigDefaults(config) } diff --git a/proxy/app_conn_test.go b/proxy/app_conn_test.go index 7c072625a..f9c6afc98 100644 --- a/proxy/app_conn_test.go +++ b/proxy/app_conn_test.go @@ -4,11 +4,11 @@ import ( "strings" "testing" - . "github.com/tendermint/tmlibs/common" abcicli "github.com/tendermint/abci/client" "github.com/tendermint/abci/example/dummy" "github.com/tendermint/abci/server" "github.com/tendermint/abci/types" + cmn "github.com/tendermint/tmlibs/common" ) //---------------------------------------- @@ -44,43 +44,43 @@ func (app *appConnTest) InfoSync() (types.ResponseInfo, error) { var SOCKET = "socket" func TestEcho(t *testing.T) { - sockPath := Fmt("unix:///tmp/echo_%v.sock", RandStr(6)) + sockPath := cmn.Fmt("unix:///tmp/echo_%v.sock", cmn.RandStr(6)) clientCreator := NewRemoteClientCreator(sockPath, SOCKET, true) // Start server s, err := server.NewSocketServer(sockPath, dummy.NewDummyApplication()) if err != nil { - Exit(err.Error()) + cmn.Exit(err.Error()) } defer s.Stop() // Start client cli, err := clientCreator.NewABCIClient() if err != nil { - Exit(err.Error()) + cmn.Exit(err.Error()) } proxy := NewAppConnTest(cli) t.Log("Connected") for i := 0; i < 1000; i++ { - proxy.EchoAsync(Fmt("echo-%v", i)) + proxy.EchoAsync(cmn.Fmt("echo-%v", i)) } proxy.FlushSync() } func BenchmarkEcho(b *testing.B) { b.StopTimer() // Initialize - sockPath := Fmt("unix:///tmp/echo_%v.sock", RandStr(6)) + sockPath := cmn.Fmt("unix:///tmp/echo_%v.sock", cmn.RandStr(6)) clientCreator := NewRemoteClientCreator(sockPath, SOCKET, true) // Start server s, err := server.NewSocketServer(sockPath, dummy.NewDummyApplication()) if err != nil { - Exit(err.Error()) + cmn.Exit(err.Error()) } defer s.Stop() // Start client cli, err := clientCreator.NewABCIClient() if err != nil { - Exit(err.Error()) + cmn.Exit(err.Error()) } proxy := NewAppConnTest(cli) b.Log("Connected") @@ -98,18 +98,18 @@ func BenchmarkEcho(b *testing.B) { } func TestInfo(t *testing.T) { - sockPath := Fmt("unix:///tmp/echo_%v.sock", RandStr(6)) + sockPath := cmn.Fmt("unix:///tmp/echo_%v.sock", cmn.RandStr(6)) clientCreator := NewRemoteClientCreator(sockPath, SOCKET, true) // Start server s, err := server.NewSocketServer(sockPath, dummy.NewDummyApplication()) if err != nil { - Exit(err.Error()) + cmn.Exit(err.Error()) } defer s.Stop() // Start client cli, err := clientCreator.NewABCIClient() if err != nil { - Exit(err.Error()) + cmn.Exit(err.Error()) } proxy := NewAppConnTest(cli) t.Log("Connected") diff --git a/proxy/client.go b/proxy/client.go index ea9051989..4c50f38b3 100644 --- a/proxy/client.go +++ b/proxy/client.go @@ -4,10 +4,11 @@ import ( "fmt" "sync" + "github.com/spf13/viper" + abcicli "github.com/tendermint/abci/client" "github.com/tendermint/abci/example/dummy" "github.com/tendermint/abci/types" - cfg "github.com/tendermint/go-config" ) // NewABCIClient returns newly connected client @@ -63,7 +64,7 @@ func (r *remoteClientCreator) NewABCIClient() (abcicli.Client, error) { //----------------------------------------------------------------- // default -func DefaultClientCreator(config cfg.Config) ClientCreator { +func DefaultClientCreator(config *viper.Viper) ClientCreator { addr := config.GetString("proxy_app") transport := config.GetString("abci") diff --git a/proxy/multi_app_conn.go b/proxy/multi_app_conn.go index b70a0baf0..9f70713eb 100644 --- a/proxy/multi_app_conn.go +++ b/proxy/multi_app_conn.go @@ -1,8 +1,8 @@ package proxy import ( + "github.com/spf13/viper" cmn "github.com/tendermint/tmlibs/common" - cfg "github.com/tendermint/go-config" ) //----------------------------- @@ -16,7 +16,7 @@ type AppConns interface { Query() AppConnQuery } -func NewAppConns(config cfg.Config, clientCreator ClientCreator, handshaker Handshaker) AppConns { +func NewAppConns(config *viper.Viper, clientCreator ClientCreator, handshaker Handshaker) AppConns { return NewMultiAppConn(config, clientCreator, handshaker) } @@ -34,7 +34,7 @@ type Handshaker interface { type multiAppConn struct { cmn.BaseService - config cfg.Config + config *viper.Viper handshaker Handshaker @@ -46,7 +46,7 @@ type multiAppConn struct { } // Make all necessary abci connections to the application -func NewMultiAppConn(config cfg.Config, clientCreator ClientCreator, handshaker Handshaker) *multiAppConn { +func NewMultiAppConn(config *viper.Viper, clientCreator ClientCreator, handshaker Handshaker) *multiAppConn { multiAppConn := &multiAppConn{ config: config, handshaker: handshaker, diff --git a/rpc/tendermint/core/pipe.go b/rpc/tendermint/core/pipe.go index d4a1d127e..b10ff4737 100644 --- a/rpc/tendermint/core/pipe.go +++ b/rpc/tendermint/core/pipe.go @@ -1,11 +1,11 @@ package core import ( - cfg "github.com/tendermint/go-config" + "github.com/spf13/viper" crypto "github.com/tendermint/go-crypto" - p2p "github.com/tendermint/tendermint/p2p" "github.com/tendermint/tendermint/consensus" + p2p "github.com/tendermint/tendermint/p2p" "github.com/tendermint/tendermint/proxy" "github.com/tendermint/tendermint/state/txindex" "github.com/tendermint/tendermint/types" @@ -34,7 +34,7 @@ var ( // external, thread safe interfaces eventSwitch types.EventSwitch proxyAppQuery proxy.AppConnQuery - config cfg.Config + config *viper.Viper // interfaces defined in types and above blockStore types.BlockStore @@ -49,7 +49,7 @@ var ( txIndexer txindex.TxIndexer ) -func SetConfig(c cfg.Config) { +func SetConfig(c *viper.Viper) { config = c } diff --git a/rpc/tendermint/core/routes.go b/rpc/tendermint/core/routes.go index 1ed874f28..7ed18ef7f 100644 --- a/rpc/tendermint/core/routes.go +++ b/rpc/tendermint/core/routes.go @@ -3,8 +3,8 @@ package core import ( data "github.com/tendermint/go-wire/data" rpc "github.com/tendermint/tendermint/rpc/server" - "github.com/tendermint/tendermint/rpc/types" ctypes "github.com/tendermint/tendermint/rpc/tendermint/core/types" + "github.com/tendermint/tendermint/rpc/types" "github.com/tendermint/tendermint/types" ) @@ -39,7 +39,9 @@ var Routes = map[string]*rpc.RPCFunc{ // control API "dial_seeds": rpc.NewRPCFunc(UnsafeDialSeedsResult, "seeds"), "unsafe_flush_mempool": rpc.NewRPCFunc(UnsafeFlushMempool, ""), - "unsafe_set_config": rpc.NewRPCFunc(UnsafeSetConfigResult, "type,key,value"), + + // config is not in general thread safe. expose specifics if you need em + // "unsafe_set_config": rpc.NewRPCFunc(UnsafeSetConfigResult, "type,key,value"), // profiler API "unsafe_start_cpu_profiler": rpc.NewRPCFunc(UnsafeStartCPUProfilerResult, "filename"), diff --git a/rpc/tendermint/test/client_test.go b/rpc/tendermint/test/client_test.go index 1164bdd79..11ee61a56 100644 --- a/rpc/tendermint/test/client_test.go +++ b/rpc/tendermint/test/client_test.go @@ -10,6 +10,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + abci "github.com/tendermint/abci/types" rpc "github.com/tendermint/tendermint/rpc/client" "github.com/tendermint/tendermint/rpc/tendermint/core" @@ -66,8 +67,8 @@ func TestJSONBroadcastTxSync(t *testing.T) { } func testBroadcastTxSync(t *testing.T, client rpc.HTTPClient) { - config.Set("block_size", 0) - defer config.Set("block_size", -1) + mem := node.MempoolReactor().Mempool + initMemSize := mem.Size() tmResult := new(ctypes.TMResult) tx := randBytes(t) _, err := client.Call("broadcast_tx_sync", map[string]interface{}{"tx": tx}, tmResult) @@ -75,8 +76,7 @@ func testBroadcastTxSync(t *testing.T, client rpc.HTTPClient) { res := (*tmResult).(*ctypes.ResultBroadcastTx) require.Equal(t, abci.CodeType_OK, res.Code) - mem := node.MempoolReactor().Mempool - require.Equal(t, 1, mem.Size()) + require.Equal(t, initMemSize+1, mem.Size()) txs := mem.Reap(1) require.EqualValues(t, tx, txs[0]) mem.Flush() @@ -357,51 +357,52 @@ func TestWSDoubleFire(t *testing.T) { }*/ //-------------------------------------------------------------------------------- +//TODO needs to be refactored so we don't use a mutable config but rather update specific values we're interested in // unsafe_set_config -var stringVal = "my string" -var intVal = 987654321 -var boolVal = true - -// don't change these -var testCasesUnsafeSetConfig = [][]string{ - []string{"string", "key1", stringVal}, - []string{"int", "key2", fmt.Sprintf("%v", intVal)}, - []string{"bool", "key3", fmt.Sprintf("%v", boolVal)}, -} - -func TestURIUnsafeSetConfig(t *testing.T) { - for _, testCase := range testCasesUnsafeSetConfig { - tmResult := new(ctypes.TMResult) - _, err := GetURIClient().Call("unsafe_set_config", map[string]interface{}{ - "type": testCase[0], - "key": testCase[1], - "value": testCase[2], - }, tmResult) - require.Nil(t, err) - } - testUnsafeSetConfig(t) -} - -func TestJSONUnsafeSetConfig(t *testing.T) { - for _, testCase := range testCasesUnsafeSetConfig { - tmResult := new(ctypes.TMResult) - _, err := GetJSONClient().Call("unsafe_set_config", - map[string]interface{}{"type": testCase[0], "key": testCase[1], "value": testCase[2]}, - tmResult) - require.Nil(t, err) - } - testUnsafeSetConfig(t) -} - -func testUnsafeSetConfig(t *testing.T) { - require := require.New(t) - s := config.GetString("key1") - require.Equal(stringVal, s) - - i := config.GetInt("key2") - require.Equal(intVal, i) - - b := config.GetBool("key3") - require.Equal(boolVal, b) -} +//var stringVal = "my string" +//var intVal = 987654321 +//var boolVal = true +// +//// don't change these +//var testCasesUnsafeSetConfig = [][]string{ +// []string{"string", "key1", stringVal}, +// []string{"int", "key2", fmt.Sprintf("%v", intVal)}, +// []string{"bool", "key3", fmt.Sprintf("%v", boolVal)}, +//} +// +//func TestURIUnsafeSetConfig(t *testing.T) { +// for _, testCase := range testCasesUnsafeSetConfig { +// tmResult := new(ctypes.TMResult) +// _, err := GetURIClient().Call("unsafe_set_config", map[string]interface{}{ +// "type": testCase[0], +// "key": testCase[1], +// "value": testCase[2], +// }, tmResult) +// require.Nil(t, err) +// } +// testUnsafeSetConfig(t) +//} +// +//func TestJSONUnsafeSetConfig(t *testing.T) { +// for _, testCase := range testCasesUnsafeSetConfig { +// tmResult := new(ctypes.TMResult) +// _, err := GetJSONClient().Call("unsafe_set_config", +// map[string]interface{}{"type": testCase[0], "key": testCase[1], "value": testCase[2]}, +// tmResult) +// require.Nil(t, err) +// } +// testUnsafeSetConfig(t) +//} +// +//func testUnsafeSetConfig(t *testing.T) { +// require := require.New(t) +// s := config.GetString("key1") +// require.Equal(stringVal, s) +// +// i := config.GetInt("key2") +// require.Equal(intVal, i) +// +// b := config.GetBool("key3") +// require.Equal(boolVal, b) +//} diff --git a/rpc/tendermint/test/helpers.go b/rpc/tendermint/test/helpers.go index e5a2fb4e2..4e281f8c3 100644 --- a/rpc/tendermint/test/helpers.go +++ b/rpc/tendermint/test/helpers.go @@ -9,24 +9,22 @@ import ( "testing" "time" + "github.com/spf13/viper" "github.com/stretchr/testify/require" - logger "github.com/tendermint/tmlibs/logger" wire "github.com/tendermint/go-wire" + logger "github.com/tendermint/tmlibs/logger" abci "github.com/tendermint/abci/types" - cfg "github.com/tendermint/go-config" - client "github.com/tendermint/tendermint/rpc/client" "github.com/tendermint/tendermint/config/tendermint_test" nm "github.com/tendermint/tendermint/node" "github.com/tendermint/tendermint/proxy" + client "github.com/tendermint/tendermint/rpc/client" ctypes "github.com/tendermint/tendermint/rpc/tendermint/core/types" core_grpc "github.com/tendermint/tendermint/rpc/tendermint/grpc" "github.com/tendermint/tendermint/types" ) -var ( - config cfg.Config -) +var config *viper.Viper const tmLogLevel = "error" @@ -56,7 +54,7 @@ func makeAddrs() (string, string, string) { } // GetConfig returns a config for the test cases as a singleton -func GetConfig() cfg.Config { +func GetConfig() *viper.Viper { if config == nil { pathname := makePathname() config = tendermint_test.ResetConfig(pathname) diff --git a/state/errors.go b/state/errors.go index 1ea7ed3d9..50c5a2c04 100644 --- a/state/errors.go +++ b/state/errors.go @@ -1,7 +1,7 @@ package state import ( - . "github.com/tendermint/tmlibs/common" + cmn "github.com/tendermint/tmlibs/common" ) type ( @@ -36,20 +36,20 @@ type ( ) func (e ErrUnknownBlock) Error() string { - return Fmt("Could not find block #%d", e.Height) + return cmn.Fmt("Could not find block #%d", e.Height) } func (e ErrBlockHashMismatch) Error() string { - return Fmt("App block hash (%X) does not match core block hash (%X) for height %d", e.AppHash, e.CoreHash, e.Height) + return cmn.Fmt("App block hash (%X) does not match core block hash (%X) for height %d", e.AppHash, e.CoreHash, e.Height) } func (e ErrAppBlockHeightTooHigh) Error() string { - return Fmt("App block height (%d) is higher than core (%d)", e.AppHeight, e.CoreHeight) + return cmn.Fmt("App block height (%d) is higher than core (%d)", e.AppHeight, e.CoreHeight) } func (e ErrLastStateMismatch) Error() string { - return Fmt("Latest tendermint block (%d) LastAppHash (%X) does not match app's AppHash (%X)", e.Height, e.Core, e.App) + return cmn.Fmt("Latest tendermint block (%d) LastAppHash (%X) does not match app's AppHash (%X)", e.Height, e.Core, e.App) } func (e ErrStateMismatch) Error() string { - return Fmt("State after replay does not match saved state. Got ----\n%v\nExpected ----\n%v\n", e.Got, e.Expected) + return cmn.Fmt("State after replay does not match saved state. Got ----\n%v\nExpected ----\n%v\n", e.Got, e.Expected) } diff --git a/state/execution.go b/state/execution.go index 643dd4d05..b06974cd8 100644 --- a/state/execution.go +++ b/state/execution.go @@ -6,11 +6,11 @@ import ( fail "github.com/ebuchman/fail-test" abci "github.com/tendermint/abci/types" - . "github.com/tendermint/tmlibs/common" crypto "github.com/tendermint/go-crypto" "github.com/tendermint/tendermint/proxy" "github.com/tendermint/tendermint/state/txindex" "github.com/tendermint/tendermint/types" + cmn "github.com/tendermint/tmlibs/common" ) //-------------------------------------------------- @@ -126,7 +126,7 @@ func updateValidators(validators *types.ValidatorSet, changedValidators []*abci. power := int64(v.Power) // mind the overflow from uint64 if power < 0 { - return errors.New(Fmt("Power (%d) overflows int64", v.Power)) + return errors.New(cmn.Fmt("Power (%d) overflows int64", v.Power)) } _, val := validators.GetByAddress(address) @@ -134,20 +134,20 @@ func updateValidators(validators *types.ValidatorSet, changedValidators []*abci. // add val added := validators.Add(types.NewValidator(pubkey, power)) if !added { - return errors.New(Fmt("Failed to add new validator %X with voting power %d", address, power)) + return errors.New(cmn.Fmt("Failed to add new validator %X with voting power %d", address, power)) } } else if v.Power == 0 { // remove val _, removed := validators.Remove(address) if !removed { - return errors.New(Fmt("Failed to remove validator %X)")) + return errors.New(cmn.Fmt("Failed to remove validator %X)")) } } else { // update val val.VotingPower = power updated := validators.Update(val) if !updated { - return errors.New(Fmt("Failed to update validator %X with voting power %d", address, power)) + return errors.New(cmn.Fmt("Failed to update validator %X with voting power %d", address, power)) } } } @@ -156,8 +156,8 @@ func updateValidators(validators *types.ValidatorSet, changedValidators []*abci. // return a bit array of validators that signed the last commit // NOTE: assumes commits have already been authenticated -func commitBitArrayFromBlock(block *types.Block) *BitArray { - signed := NewBitArray(len(block.LastCommit.Precommits)) +func commitBitArrayFromBlock(block *types.Block) *cmn.BitArray { + signed := cmn.NewBitArray(len(block.LastCommit.Precommits)) for i, precommit := range block.LastCommit.Precommits { if precommit != nil { signed.SetIndex(i, true) // val_.LastCommitHeight = block.Height - 1 @@ -187,7 +187,7 @@ func (s *State) validateBlock(block *types.Block) error { } } else { if len(block.LastCommit.Precommits) != s.LastValidators.Size() { - return errors.New(Fmt("Invalid block commit size. Expected %v, got %v", + return errors.New(cmn.Fmt("Invalid block commit size. Expected %v, got %v", s.LastValidators.Size(), len(block.LastCommit.Precommits))) } err := s.LastValidators.VerifyCommit( diff --git a/state/state.go b/state/state.go index ccdf508ac..2625cbc71 100644 --- a/state/state.go +++ b/state/state.go @@ -6,10 +6,11 @@ import ( "sync" "time" + "github.com/spf13/viper" abci "github.com/tendermint/abci/types" - . "github.com/tendermint/tmlibs/common" - cfg "github.com/tendermint/go-config" + cmn "github.com/tendermint/tmlibs/common" dbm "github.com/tendermint/tmlibs/db" + "github.com/tendermint/go-wire" "github.com/tendermint/tendermint/state/txindex" "github.com/tendermint/tendermint/state/txindex/null" @@ -64,7 +65,7 @@ func loadState(db dbm.DB, key []byte) *State { wire.ReadBinaryPtr(&s, r, 0, n, err) if *err != nil { // DATA HAS BEEN CORRUPTED OR THE SPEC HAS CHANGED - Exit(Fmt("LoadState: Data has been corrupted or its spec has changed: %v\n", *err)) + cmn.Exit(cmn.Fmt("LoadState: Data has been corrupted or its spec has changed: %v\n", *err)) } // TODO: ensure that buf is completely read. } @@ -108,7 +109,7 @@ func (s *State) LoadABCIResponses() *ABCIResponses { wire.ReadBinaryPtr(abciResponses, r, 0, n, err) if *err != nil { // DATA HAS BEEN CORRUPTED OR THE SPEC HAS CHANGED - Exit(Fmt("LoadABCIResponses: Data has been corrupted or its spec has changed: %v\n", *err)) + cmn.Exit(cmn.Fmt("LoadABCIResponses: Data has been corrupted or its spec has changed: %v\n", *err)) } // TODO: ensure that buf is completely read. } @@ -123,7 +124,7 @@ func (s *State) Bytes() []byte { buf, n, err := new(bytes.Buffer), new(int), new(error) wire.WriteBinary(s, buf, n, err) if *err != nil { - PanicCrisis(*err) + cmn.PanicCrisis(*err) } return buf.Bytes() } @@ -168,7 +169,7 @@ func (s *State) GetValidators() (*types.ValidatorSet, *types.ValidatorSet) { // Load the most recent state from "state" db, // or create a new one (and save) from genesis. -func GetState(config cfg.Config, stateDB dbm.DB) *State { +func GetState(config *viper.Viper, stateDB dbm.DB) *State { state := LoadState(stateDB) if state == nil { state = MakeGenesisStateFromFile(stateDB, config.GetString("genesis_file")) @@ -203,7 +204,7 @@ func (a *ABCIResponses) Bytes() []byte { buf, n, err := new(bytes.Buffer), new(int), new(error) wire.WriteBinary(*a, buf, n, err) if *err != nil { - PanicCrisis(*err) + cmn.PanicCrisis(*err) } return buf.Bytes() } @@ -217,11 +218,11 @@ func (a *ABCIResponses) Bytes() []byte { func MakeGenesisStateFromFile(db dbm.DB, genDocFile string) *State { genDocJSON, err := ioutil.ReadFile(genDocFile) if err != nil { - Exit(Fmt("Couldn't read GenesisDoc file: %v", err)) + cmn.Exit(cmn.Fmt("Couldn't read GenesisDoc file: %v", err)) } genDoc, err := types.GenesisDocFromJSON(genDocJSON) if err != nil { - Exit(Fmt("Error reading GenesisDoc: %v", err)) + cmn.Exit(cmn.Fmt("Error reading GenesisDoc: %v", err)) } return MakeGenesisState(db, genDoc) } @@ -231,7 +232,7 @@ func MakeGenesisStateFromFile(db dbm.DB, genDocFile string) *State { // Used in tests. func MakeGenesisState(db dbm.DB, genDoc *types.GenesisDoc) *State { if len(genDoc.Validators) == 0 { - Exit(Fmt("The genesis file has no validators")) + cmn.Exit(cmn.Fmt("The genesis file has no validators")) } if genDoc.GenesisTime.IsZero() { diff --git a/test/test_libs.sh b/test/test_libs.sh index 1d57ae049..3f4315cd9 100644 --- a/test/test_libs.sh +++ b/test/test_libs.sh @@ -14,8 +14,8 @@ fi # some libs are tested with go, others with make # TODO: should be all make (post repo merge) -LIBS_GO_TEST=(tmlibs/clist tmlibs/common go-config go-crypto tmlibs/db tmlibs/events go-merkle tendermint/p2p) -LIBS_MAKE_TEST=(tendermint/rpc go-wire abci) +LIBS_GO_TEST=(tmlibs go-crypto) +LIBS_MAKE_TEST=(go-wire abci) for lib in "${LIBS_GO_TEST[@]}"; do