Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

go-kosu: add reset and show_node_info cmds #239

Merged
merged 5 commits into from Aug 28, 2019
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -1,12 +1,18 @@
package abci

import (
"encoding/hex"
"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"
"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,44 @@ 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) {
if w == nil {
w = ioutil.Discard
}

logger := log.NewTMLogger(w)
commands.ResetAll(cfg.DBDir(), cfg.P2P.AddrBookFile(), cfg.PrivValidatorKeyFile(),
cfg.PrivValidatorStateFile(), logger)
}
@@ -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()
@@ -2,8 +2,11 @@ package main

import (
"context"
"fmt"
stdlog "log"
"os"
"os/user"
"path"
"path/filepath"
"strings"

@@ -17,6 +20,10 @@ import (
"go-kosu/witness"
)

const (
dbName = "kosu"
)

// Config holds the program execution arguments
type Config struct {
Home string
@@ -26,7 +33,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
}
@@ -122,13 +129,57 @@ 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
}

abci.ResetAll(kosuCfg, os.Stdout)

dbdir := path.Join(kosuCfg.RootDir, dbName+".db")
return os.RemoveAll(dbdir)
},
}

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)
@@ -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=
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.