refactor basecoind, change GenAppOptions

This commit is contained in:
Ethan Buchman 2018-04-05 13:09:02 +03:00
parent 9e6bc70e28
commit f79d06c9f3
5 changed files with 86 additions and 58 deletions

View File

@ -7,14 +7,9 @@ import (
"path/filepath"
"github.com/spf13/cobra"
"github.com/spf13/viper"
abci "github.com/tendermint/abci/types"
tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands"
cfg "github.com/tendermint/tendermint/config"
"github.com/tendermint/tmlibs/cli"
tmflags "github.com/tendermint/tmlibs/cli/flags"
cmn "github.com/tendermint/tmlibs/common"
dbm "github.com/tendermint/tmlibs/db"
"github.com/tendermint/tmlibs/log"
@ -25,40 +20,20 @@ import (
// basecoindCmd is the entry point for this binary
var (
context = server.NewContext(nil, nil)
basecoindCmd = &cobra.Command{
Use: "gaiad",
Short: "Gaia Daemon (server)",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if cmd.Name() == version.VersionCmd.Name() {
return nil
}
config, err := tcmd.ParseConfig()
if err != nil {
return err
}
logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout))
logger, err = tmflags.ParseLogLevel(config.LogLevel, logger, cfg.DefaultLogLevel())
if err != nil {
return err
}
if viper.GetBool(cli.TraceFlag) {
logger = log.NewTracingLogger(logger)
}
logger = logger.With("module", "main")
context.Config = config
context.Logger = logger
return nil
},
context = server.NewContext(nil, nil)
rootCmd = &cobra.Command{
Use: "basecoind",
Short: "Basecoin Daemon (server)",
PersistentPreRunE: server.PersistentPreRunEFn(context),
}
)
// defaultOptions sets up the app_options for the
// default genesis file
func defaultOptions(args []string) (json.RawMessage, string, cmn.HexBytes, error) {
addr, secret, err := server.GenerateCoinKey()
if err != nil {
return nil, "", nil, err
// defaultOptions sets up the app_state for the
// default genesis file. It is a server.GenAppState
// used for `basecoind init`.
func defaultOptions(args ...string) (json.RawMessage, error) {
if len(args) != 2 {
return nil, fmt.Errorf("Expected 2 args: address and coin denom")
}
opts := fmt.Sprintf(`{
"accounts": [{
@ -70,24 +45,25 @@ func defaultOptions(args []string) (json.RawMessage, string, cmn.HexBytes, error
}
]
}]
}`, addr)
return json.RawMessage(opts), secret, addr, nil
}`, args)
return json.RawMessage(opts), nil
}
func generateApp(rootDir string, logger log.Logger) (abci.Application, error) {
dbMain, err := dbm.NewGoLevelDB("basecoin", filepath.Join(rootDir, "data"))
dataDir := filepath.Join(rootDir, "data")
dbMain, err := dbm.NewGoLevelDB("basecoin", dataDir)
if err != nil {
return nil, err
}
dbAcc, err := dbm.NewGoLevelDB("basecoin-acc", filepath.Join(rootDir, "data"))
dbAcc, err := dbm.NewGoLevelDB("basecoin-acc", dataDir)
if err != nil {
return nil, err
}
dbIBC, err := dbm.NewGoLevelDB("basecoin-ibc", filepath.Join(rootDir, "data"))
dbIBC, err := dbm.NewGoLevelDB("basecoin-ibc", dataDir)
if err != nil {
return nil, err
}
dbStaking, err := dbm.NewGoLevelDB("basecoin-staking", filepath.Join(rootDir, "data"))
dbStaking, err := dbm.NewGoLevelDB("basecoin-staking", dataDir)
if err != nil {
return nil, err
}
@ -102,17 +78,17 @@ func generateApp(rootDir string, logger log.Logger) (abci.Application, error) {
}
func main() {
basecoindCmd.AddCommand(
rootCmd.AddCommand(
server.InitCmd(defaultOptions, context),
server.StartCmd(generateApp, context),
server.UnsafeResetAllCmd(context),
server.ShowNodeIdCmd(context),
server.ShowNodeIDCmd(context),
server.ShowValidatorCmd(context),
version.VersionCmd,
)
// prepare and add flags
rootDir := os.ExpandEnv("$HOME/.basecoind")
executor := cli.PrepareBaseCmd(basecoindCmd, "BC", rootDir)
executor := cli.PrepareBaseCmd(rootCmd, "BC", rootDir)
executor.Execute()
}

View File

@ -6,7 +6,6 @@ import (
"path/filepath"
abci "github.com/tendermint/abci/types"
cmn "github.com/tendermint/tmlibs/common"
dbm "github.com/tendermint/tmlibs/db"
"github.com/tendermint/tmlibs/log"
@ -107,7 +106,7 @@ func InitChainer(key sdk.StoreKey) func(sdk.Context, abci.RequestInitChain) abci
// GenInitOptions can be passed into InitCmd,
// returns a static string of a few key-values that can be parsed
// by InitChainer
func GenInitOptions(args []string) (json.RawMessage, string, cmn.HexBytes, error) {
func GenInitOptions(args ...string) (json.RawMessage, error) {
opts := []byte(`{
"values": [
{
@ -120,5 +119,5 @@ func GenInitOptions(args []string) (json.RawMessage, string, cmn.HexBytes, error
}
]
}`)
return opts, "", nil, nil
return opts, nil
}

View File

@ -13,12 +13,15 @@ import (
cmn "github.com/tendermint/tmlibs/common"
)
// testnetInformation contains the info necessary
// to setup a testnet including this account and validator.
type testnetInformation struct {
Secret string `json:"secret"`
Secret string `json:"secret"`
ChainID string `json:"chain_id"`
Account string `json:"account"`
Validator tmtypes.GenesisValidator `json:"validator"`
NodeID p2p.ID `json:"node_id"`
ChainID string `json:"chain_id"`
}
// InitCmd will initialize all files for tendermint,
@ -39,11 +42,11 @@ func InitCmd(gen GenAppState, ctx *Context) *cobra.Command {
return &cobraCmd
}
// GenAppState can parse command-line to
// generate default app_state for the genesis file.
// Also must return generated seed and address
// GenAppState takes a list of arguments and
// returns a default app_state to be included in
// in the genesis file.
// This is application-specific
type GenAppState func(args []string) (json.RawMessage, string, cmn.HexBytes, error)
type GenAppState func(args ...string) (json.RawMessage, error)
type initCmd struct {
genAppState GenAppState
@ -67,14 +70,22 @@ func (c initCmd) run(cmd *cobra.Command, args []string) error {
return nil
}
// generate secrete and address
addr, secret, err := GenerateCoinKey()
if err != nil {
return err
}
var DEFAULT_DENOM = "mycoin"
// Now, we want to add the custom app_state
appState, secret, address, err := c.genAppState(args)
appState, err := c.genAppState(addr.String(), DEFAULT_DENOM)
if err != nil {
return err
}
testnetInfo.Secret = secret
testnetInfo.Account = address.String()
testnetInfo.Account = addr.String()
// And add them to the genesis file
genFile := config.GenesisFile()

42
server/pre_run.go Normal file
View File

@ -0,0 +1,42 @@
package server
import (
"os"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/cosmos/cosmos-sdk/version"
tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands"
cfg "github.com/tendermint/tendermint/config"
"github.com/tendermint/tmlibs/cli"
tmflags "github.com/tendermint/tmlibs/cli/flags"
"github.com/tendermint/tmlibs/log"
)
// PersistentPreRunEFn returns a PersistentPreRunE function for cobra
// that initailizes the passed in context with a properly configured
// logger and config objecy
func PersistentPreRunEFn(context *Context) func(*cobra.Command, []string) error {
return func(cmd *cobra.Command, args []string) error {
if cmd.Name() == version.VersionCmd.Name() {
return nil
}
config, err := tcmd.ParseConfig()
if err != nil {
return err
}
logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout))
logger, err = tmflags.ParseLogLevel(config.LogLevel, logger, cfg.DefaultLogLevel())
if err != nil {
return err
}
if viper.GetBool(cli.TraceFlag) {
logger = log.NewTracingLogger(logger)
}
logger = logger.With("module", "main")
context.Config = config
context.Logger = logger
return nil
}
}

View File

@ -8,8 +8,8 @@ import (
"github.com/tendermint/tendermint/p2p"
)
// ShowNodeIdCmd - ported from Tendermint, dump node ID to stdout
func ShowNodeIdCmd(ctx *Context) *cobra.Command {
// ShowNodeIDCmd - ported from Tendermint, dump node ID to stdout
func ShowNodeIDCmd(ctx *Context) *cobra.Command {
cmd := showNodeId{ctx}
return &cobra.Command{
Use: "show_node_id",