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 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

Next

go-kosu: add reset and show_node_info cmds

  • Loading branch information
gchaincl committed Aug 28, 2019
commit 3274bdb700ec9ed62c1547a8b6e6bff039ae0000
@@ -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
}
@@ -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,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)
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.