From 3274bdb700ec9ed62c1547a8b6e6bff039ae0000 Mon Sep 17 00:00:00 2001 From: Gustavo Chain Date: Wed, 28 Aug 2019 10:22:00 +0200 Subject: [PATCH 1/3] go-kosu: add reset and show_node_info cmds --- packages/go-kosu/abci/node.go | 72 ++++++++++++++++++++++++++++++ packages/go-kosu/abci/setup.go | 24 +++++++--- packages/go-kosu/cmd/kosud/main.go | 53 ++++++++++++++++++++-- 3 files changed, 140 insertions(+), 9 deletions(-) diff --git a/packages/go-kosu/abci/node.go b/packages/go-kosu/abci/node.go index 43662fe4..fd0f15b7 100644 --- a/packages/go-kosu/abci/node.go +++ b/packages/go-kosu/abci/node.go @@ -1,12 +1,18 @@ package abci import ( + "encoding/hex" + "fmt" + "io" + "io/ioutil" "os" + "github.com/tendermint/tendermint/config" tmflags "github.com/tendermint/tendermint/libs/cli/flags" log "github.com/tendermint/tendermint/libs/log" "github.com/tendermint/tendermint/node" "github.com/tendermint/tendermint/p2p" + "github.com/tendermint/tendermint/privval" pv "github.com/tendermint/tendermint/privval" "github.com/tendermint/tendermint/proxy" ) @@ -51,3 +57,69 @@ func (app *App) CreateNode() (*node.Node, error) { ) return node, err } + +// NodeInfo holds relevant node information +type NodeInfo struct { + PeerID string + NodeID string + PublicKey string + Moniker string +} + +// ShowNodeInfo returns the node's information given its config +func ShowNodeInfo(cfg *config.Config) (*NodeInfo, error) { + nodeKey, err := p2p.LoadNodeKey(cfg.NodeKeyFile()) + if err != nil { + return nil, err + } + + priv := privval.LoadFilePV( + cfg.PrivValidatorKeyFile(), + cfg.PrivValidatorStateFile(), + ).Key + + return &NodeInfo{ + PeerID: string(nodeKey.ID()), + NodeID: priv.Address.String(), + PublicKey: hex.EncodeToString(priv.PubKey.Bytes()), + Moniker: cfg.Moniker, + }, nil +} + +// ResetAll wipes the kosu home represented by cfg, keeping only the config and the genesis files +// ResetAll is idempotent, which means that running reset twice, should have no effect +// All the output will be written to w +func ResetAll(cfg *config.Config, w io.Writer) error { + if w == nil { + w = ioutil.Discard + } + + files := []string{ + cfg.P2P.AddrBookFile(), + cfg.PrivValidatorKeyFile(), + cfg.PrivValidatorStateFile(), + } + + if err := removeFiles(w, files...); err != nil { + return err + } + + fmt.Fprintf(w, "Deleting directory %s", cfg.DBDir()) + if err := os.RemoveAll(cfg.DBDir()); err != nil { + return err + } + + return nil +} + +func removeFiles(w io.Writer, files ...string) error { + for _, file := range files { + fmt.Fprintf(w, "Deleting %s\n", file) + if err := os.Remove(file); err != nil { + if !os.IsNotExist(err) { + return err + } + } + } + return nil +} diff --git a/packages/go-kosu/abci/setup.go b/packages/go-kosu/abci/setup.go index d02f5b70..4babc9bc 100644 --- a/packages/go-kosu/abci/setup.go +++ b/packages/go-kosu/abci/setup.go @@ -44,14 +44,26 @@ func createConfig(homedir string, logger log.Logger) error { } else { config.SetRoot(homedir) } + + // we first check if the config existed + needUpdate := false + cfgFile := config.RootDir + "/config/config.toml" + if _, err := os.Stat(cfgFile); os.IsNotExist(err) { + needUpdate = true + } + + // this will create a confir if it does not exists with default values cfg.EnsureRoot(config.RootDir) - // Update default config values - config.LogLevel = strings.Join( - []string{"app:info,witness:info", config.LogLevel}, ",", - ) - // WriteConfigFile will overwrite the default config written by .EnsureRoot - cfg.WriteConfigFile(config.RootDir+"/config/config.toml", config) + // update the cfg only if it didn't exist + if needUpdate { + config.LogLevel = strings.Join( + []string{"app:info,witness:info", config.LogLevel}, ",", + ) + + // WriteConfigFile will overwrite the default config written by .EnsureRoot + cfg.WriteConfigFile(cfgFile, config) + } // private validator privValKeyFile := config.PrivValidatorKeyFile() diff --git a/packages/go-kosu/cmd/kosud/main.go b/packages/go-kosu/cmd/kosud/main.go index ff731f4b..4190afb0 100644 --- a/packages/go-kosu/cmd/kosud/main.go +++ b/packages/go-kosu/cmd/kosud/main.go @@ -2,7 +2,9 @@ package main import ( "context" + "fmt" stdlog "log" + "os" "os/user" "path/filepath" "strings" @@ -125,13 +127,58 @@ func main() { }, } + showNodeInfoCmd := &cobra.Command{ + Use: "show_node_info", + Short: "Show this node's information", + Long: "Displays information unique to this node", + RunE: func(cmd *cobra.Command, _ []string) error { + kosuCfg, err := abci.LoadConfig(cfg.Home) + if err != nil { + return err + } + + info, err := abci.ShowNodeInfo(kosuCfg) + if err != nil { + return err + } + + fmt.Printf("Public Key: %s\n", info.PublicKey) + fmt.Printf("Node ID: %s\n", info.NodeID) + fmt.Printf("Peer ID: %s\n", info.PeerID) + fmt.Printf("Moniker: %s\n", info.Moniker) + return nil + }, + } + + unsafeResetCmd := &cobra.Command{ + Use: "reset", + Short: "Reset this node to genesis state", + Long: "Reset will wipe all the data and WAL, keeping only the config and the genesis file", + RunE: func(cmd *cobra.Command, _ []string) error { + kosuCfg, err := abci.LoadConfig(cfg.Home) + if err != nil { + return err + } + + if err := abci.ResetAll(kosuCfg, os.Stdout); err != nil { + return err + } + + return abci.InitTendermint(cfg.Home) + }, + } + rootCmd.PersistentFlags().StringVarP(&cfg.Home, "home", "H", "~/.kosu", "directory for config and data") rootCmd.PersistentFlags().BoolVarP(&cfg.Debug, "debug", "d", false, "enable debuging") rootCmd.Flags().StringVarP(&cfg.Web3, "web3", "E", "ws://localhost:8546", "URL of an Ethereum JSONRPC provider") - rootCmd.AddCommand(version.NewCommand()) - rootCmd.AddCommand(rpc.NewCommand()) - rootCmd.AddCommand(initCmd) + rootCmd.AddCommand( + version.NewCommand(), + rpc.NewCommand(), + initCmd, + showNodeInfoCmd, + unsafeResetCmd, + ) if err := rootCmd.Execute(); err != nil { stdlog.Fatal(err) From 2a44b253211d6e7cdfdcfeea52845b447707ab15 Mon Sep 17 00:00:00 2001 From: Gustavo Chain Date: Wed, 28 Aug 2019 20:55:40 +0200 Subject: [PATCH 2/3] go-kosu: mimic unsafe_reset_all --- packages/go-kosu/abci/node.go | 35 +++++------------------------- packages/go-kosu/cmd/kosud/main.go | 13 ++++++----- packages/go-kosu/go.sum | 1 + 3 files changed, 14 insertions(+), 35 deletions(-) diff --git a/packages/go-kosu/abci/node.go b/packages/go-kosu/abci/node.go index fd0f15b7..bb48dce7 100644 --- a/packages/go-kosu/abci/node.go +++ b/packages/go-kosu/abci/node.go @@ -2,11 +2,11 @@ package abci import ( "encoding/hex" - "fmt" "io" "io/ioutil" "os" + "github.com/tendermint/tendermint/cmd/tendermint/commands" "github.com/tendermint/tendermint/config" tmflags "github.com/tendermint/tendermint/libs/cli/flags" log "github.com/tendermint/tendermint/libs/log" @@ -89,37 +89,12 @@ func ShowNodeInfo(cfg *config.Config) (*NodeInfo, error) { // ResetAll wipes the kosu home represented by cfg, keeping only the config and the genesis files // ResetAll is idempotent, which means that running reset twice, should have no effect // All the output will be written to w -func ResetAll(cfg *config.Config, w io.Writer) error { +func ResetAll(cfg *config.Config, w io.Writer) { if w == nil { w = ioutil.Discard } - files := []string{ - cfg.P2P.AddrBookFile(), - cfg.PrivValidatorKeyFile(), - cfg.PrivValidatorStateFile(), - } - - if err := removeFiles(w, files...); err != nil { - return err - } - - fmt.Fprintf(w, "Deleting directory %s", cfg.DBDir()) - if err := os.RemoveAll(cfg.DBDir()); err != nil { - return err - } - - return nil -} - -func removeFiles(w io.Writer, files ...string) error { - for _, file := range files { - fmt.Fprintf(w, "Deleting %s\n", file) - if err := os.Remove(file); err != nil { - if !os.IsNotExist(err) { - return err - } - } - } - return nil + logger := log.NewTMLogger(w) + commands.ResetAll(cfg.DBDir(), cfg.P2P.AddrBookFile(), cfg.PrivValidatorKeyFile(), + cfg.PrivValidatorStateFile(), logger) } diff --git a/packages/go-kosu/cmd/kosud/main.go b/packages/go-kosu/cmd/kosud/main.go index 7605917b..75f4d3d8 100644 --- a/packages/go-kosu/cmd/kosud/main.go +++ b/packages/go-kosu/cmd/kosud/main.go @@ -19,6 +19,10 @@ import ( "go-kosu/witness" ) +const ( + dbName = "kosu" +) + // Config holds the program execution arguments type Config struct { Home string @@ -28,7 +32,7 @@ type Config struct { } func newDB(dir string, debug bool) (db.DB, error) { - gdb, err := db.NewGoLevelDB("kosu", dir) + gdb, err := db.NewGoLevelDB(dbName, dir) if err != nil { return nil, err } @@ -157,11 +161,10 @@ func main() { return err } - if err := abci.ResetAll(kosuCfg, os.Stdout); err != nil { - return err - } + abci.ResetAll(kosuCfg, os.Stdout) - return abci.InitTendermint(cfg.Home) + dbdir := path.Join(kosuCfg.RootDir, dbName+".db") + return os.RemoveAll(dbdir) }, } diff --git a/packages/go-kosu/go.sum b/packages/go-kosu/go.sum index 4102f77c..46eafc48 100644 --- a/packages/go-kosu/go.sum +++ b/packages/go-kosu/go.sum @@ -223,6 +223,7 @@ github.com/tendermint/tendermint v0.31.2-0.20190822092307-7b2d018f847e/go.mod h1 github.com/tendermint/tendermint v0.31.5 h1:vTet8tCq3B9/J9Yo11dNZ8pOB7NtSy++bVSfkP4KzR4= github.com/tendermint/tendermint v0.31.5/go.mod h1:ymcPyWblXCplCPQjbOYbrF1fWnpslATMVqiGgWbZrlc= github.com/tendermint/tendermint v0.32.1 h1:J8ddXMbCmG6GZjdCl/N1wgdXDU9uO91J2Y5CA9xYfGo= +github.com/tendermint/tendermint v0.32.3 h1:GEnWpGQ795h5oTFNbfBLsY0LW/CW2j6p6HtiYNfxsgg= github.com/tendermint/tm-db v0.1.1/go.mod h1:0cPKWu2Mou3IlxecH+MEUSYc1Ch537alLe6CpFrKzgw= github.com/tidwall/gjson v1.3.0 h1:kfpsw1W3trbg4Xm6doUtqSl9+LhLB6qJ9PkltVAQZYs= github.com/tidwall/gjson v1.3.0/go.mod h1:P256ACg0Mn+j1RXIDXoss50DeIABTYK1PULOJHhxOls= From e6242879ccda4485c55151129cdf28f88a1deeac Mon Sep 17 00:00:00 2001 From: Gustavo Chain Date: Wed, 28 Aug 2019 21:02:29 +0200 Subject: [PATCH 3/3] go-kosu: add missing import --- packages/go-kosu/cmd/kosud/main.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/go-kosu/cmd/kosud/main.go b/packages/go-kosu/cmd/kosud/main.go index 75f4d3d8..df50f3c7 100644 --- a/packages/go-kosu/cmd/kosud/main.go +++ b/packages/go-kosu/cmd/kosud/main.go @@ -6,6 +6,7 @@ import ( stdlog "log" "os" "os/user" + "path" "path/filepath" "strings" @@ -20,7 +21,7 @@ import ( ) const ( - dbName = "kosu" + dbName = "kosu" ) // Config holds the program execution arguments