Options -> AppState

This commit is contained in:
Ethan Buchman 2018-03-14 20:22:06 +01:00
parent 683663f680
commit 7f3a6e0c04
5 changed files with 27 additions and 76 deletions

View File

@ -6,8 +6,8 @@ import (
dbm "github.com/tendermint/tmlibs/db"
)
// TODO explain what the keybase is used for
// GetKeyBase initializes a keybase based on the configuration
// GetKeyBase initializes a keybase based on the given db.
// The KeyBase manages all activity requiring access to a key.
func GetKeyBase(db dbm.DB) keys.Keybase {
keybase := keys.New(
db,

View File

@ -13,13 +13,14 @@ import (
"github.com/cosmos/cosmos-sdk/client/lcd"
"github.com/cosmos/cosmos-sdk/client/rpc"
"github.com/cosmos/cosmos-sdk/client/tx"
coolcmd "github.com/cosmos/cosmos-sdk/examples/basecoin/x/cool/commands"
"github.com/cosmos/cosmos-sdk/version"
authcmd "github.com/cosmos/cosmos-sdk/x/auth/commands"
bankcmd "github.com/cosmos/cosmos-sdk/x/bank/commands"
"github.com/cosmos/cosmos-sdk/examples/basecoin/app"
"github.com/cosmos/cosmos-sdk/examples/basecoin/types"
coolcmd "github.com/cosmos/cosmos-sdk/examples/basecoin/x/cool/commands"
)
// gaiacliCmd is the entry point for this binary

View File

@ -59,6 +59,7 @@ func generateApp(rootDir string, logger log.Logger) (abci.Application, error) {
}
func main() {
// TODO: set logger through CLI
logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout)).
With("module", "main")

View File

@ -2,30 +2,24 @@ package server
import (
"encoding/json"
"fmt"
"io/ioutil"
"github.com/spf13/cobra"
"github.com/tendermint/go-crypto/keys"
"github.com/tendermint/go-crypto/keys/words"
cmn "github.com/tendermint/tmlibs/common"
dbm "github.com/tendermint/tmlibs/db"
"github.com/tendermint/tmlibs/log"
tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands"
cfg "github.com/tendermint/tendermint/config"
tmtypes "github.com/tendermint/tendermint/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// InitCmd will initialize all files for tendermint,
// along with proper app_options.
// along with proper app_state.
// The application can pass in a function to generate
// proper options. And may want to use GenerateCoinKey
// proper state. And may want to use GenerateCoinKey
// to create default account(s).
func InitCmd(gen GenOptions, logger log.Logger) *cobra.Command {
func InitCmd(gen GenAppState, logger log.Logger) *cobra.Command {
cmd := initCmd{
gen: gen,
logger: logger,
@ -37,39 +31,14 @@ func InitCmd(gen GenOptions, logger log.Logger) *cobra.Command {
}
}
// GenOptions can parse command-line and flag to
// generate default app_options for the genesis file.
// GenAppState can parse command-line and flag to
// generate default app_state for the genesis file.
// This is application-specific
type GenOptions func(args []string) (json.RawMessage, error)
// GenerateCoinKey returns the address of a public key,
// along with the secret phrase to recover the private key.
// You can give coins to this address and return the recovery
// phrase to the user to access them.
func GenerateCoinKey() (sdk.Address, string, error) {
// construct an in-memory key store
codec, err := words.LoadCodec("english")
if err != nil {
return nil, "", err
}
keybase := keys.New(
dbm.NewMemDB(),
codec,
)
// generate a private key, with recovery phrase
info, secret, err := keybase.Create("name", "pass", keys.AlgoEd25519)
if err != nil {
return nil, "", err
}
addr := info.PubKey.Address()
return addr, secret, nil
}
type GenAppState func(args []string) (json.RawMessage, error)
type initCmd struct {
gen GenOptions
logger log.Logger
genAppState GenAppState
logger log.Logger
}
func (c initCmd) run(cmd *cobra.Command, args []string) error {
@ -85,19 +54,19 @@ func (c initCmd) run(cmd *cobra.Command, args []string) error {
}
// no app_options, leave like tendermint
if c.gen == nil {
if c.genAppState == nil {
return nil
}
// Now, we want to add the custom app_options
options, err := c.gen(args)
// Now, we want to add the custom app_state
appState, err := c.genAppState(args)
if err != nil {
return err
}
// And add them to the genesis file
genFile := config.GenesisFile()
return addGenesisOptions(genFile, options)
return addGenesisState(genFile, appState)
}
// This was copied from tendermint/cmd/tendermint/commands/init.go
@ -141,7 +110,7 @@ func (c initCmd) initTendermintFiles(config *cfg.Config) error {
// so we can add one line.
type GenesisDoc map[string]json.RawMessage
func addGenesisOptions(filename string, options json.RawMessage) error {
func addGenesisState(filename string, appState json.RawMessage) error {
bz, err := ioutil.ReadFile(filename)
if err != nil {
return err
@ -153,7 +122,7 @@ func addGenesisOptions(filename string, options json.RawMessage) error {
return err
}
doc["app_state"] = options
doc["app_state"] = appState
out, err := json.MarshalIndent(doc, "", " ")
if err != nil {
return err
@ -161,23 +130,3 @@ func addGenesisOptions(filename string, options json.RawMessage) error {
return ioutil.WriteFile(filename, out, 0600)
}
// GetGenesisJSON returns a new tendermint genesis with Basecoin app_options
// that grant a large amount of "mycoin" to a single address
// TODO: A better UX for generating genesis files
func GetGenesisJSON(pubkey, chainID, denom, addr string, options string) string {
return fmt.Sprintf(`{
"accounts": [{
"address": "%s",
"coins": [
{
"denom": "%s",
"amount": 9007199254740992
}
]
}],
"plugin_options": [
"coin/issuer", {"app": "sigs", "addr": "%s"}%s
]
}`, addr, denom, addr, options)
}

View File

@ -23,14 +23,14 @@ const (
// appGenerator lets us lazily initialize app, using home dir
// and other flags (?) to start
type appGenerator func(string, log.Logger) (abci.Application, error)
type appCreator func(string, log.Logger) (abci.Application, error)
// StartCmd runs the service passed in, either
// stand-alone, or in-process with tendermint
func StartCmd(app appGenerator, logger log.Logger) *cobra.Command {
func StartCmd(app appCreator, logger log.Logger) *cobra.Command {
start := startCmd{
app: app,
logger: logger,
appCreator: appCreator,
logger: logger,
}
cmd := &cobra.Command{
Use: "start",
@ -48,8 +48,8 @@ func StartCmd(app appGenerator, logger log.Logger) *cobra.Command {
}
type startCmd struct {
app appGenerator
logger log.Logger
appCreator appCreator
logger log.Logger
}
func (s startCmd) run(cmd *cobra.Command, args []string) error {
@ -65,7 +65,7 @@ func (s startCmd) startStandAlone() error {
// Generate the app in the proper dir
addr := viper.GetString(flagAddress)
home := viper.GetString("home")
app, err := s.app(home, s.logger)
app, err := s.appCreator(home, s.logger)
if err != nil {
return err
}
@ -92,7 +92,7 @@ func (s startCmd) startInProcess() error {
}
home := cfg.RootDir
app, err := s.app(home, s.logger)
app, err := s.appCreator(home, s.logger)
if err != nil {
return err
}