basecoind init --testnet (closes #718)
This commit is contained in:
parent
2e5943e1bf
commit
27d24610ab
|
@ -10,6 +10,7 @@ import (
|
|||
|
||||
abci "github.com/tendermint/abci/types"
|
||||
"github.com/tendermint/tmlibs/cli"
|
||||
cmn "github.com/tendermint/tmlibs/common"
|
||||
dbm "github.com/tendermint/tmlibs/db"
|
||||
"github.com/tendermint/tmlibs/log"
|
||||
|
||||
|
@ -28,14 +29,11 @@ var (
|
|||
|
||||
// defaultOptions sets up the app_options for the
|
||||
// default genesis file
|
||||
func defaultOptions(args []string) (json.RawMessage, error) {
|
||||
func defaultOptions(args []string) (json.RawMessage, string, cmn.HexBytes, error) {
|
||||
addr, secret, err := server.GenerateCoinKey()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, "", nil, err
|
||||
}
|
||||
fmt.Println("Secret phrase to access coins:")
|
||||
fmt.Println(secret)
|
||||
|
||||
opts := fmt.Sprintf(`{
|
||||
"accounts": [{
|
||||
"address": "%s",
|
||||
|
@ -47,7 +45,7 @@ func defaultOptions(args []string) (json.RawMessage, error) {
|
|||
]
|
||||
}]
|
||||
}`, addr)
|
||||
return json.RawMessage(opts), nil
|
||||
return json.RawMessage(opts), secret, addr, nil
|
||||
}
|
||||
|
||||
func generateApp(rootDir string, logger log.Logger) (abci.Application, error) {
|
||||
|
|
|
@ -6,6 +6,7 @@ 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"
|
||||
|
||||
|
@ -106,7 +107,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, error) {
|
||||
func GenInitOptions(args []string) (json.RawMessage, string, cmn.HexBytes, error) {
|
||||
opts := []byte(`{
|
||||
"values": [
|
||||
{
|
||||
|
@ -119,5 +120,5 @@ func GenInitOptions(args []string) (json.RawMessage, error) {
|
|||
}
|
||||
]
|
||||
}`)
|
||||
return opts, nil
|
||||
return opts, "", nil, nil
|
||||
}
|
||||
|
|
|
@ -21,7 +21,7 @@ func TestInitApp(t *testing.T) {
|
|||
require.NoError(t, err)
|
||||
|
||||
// initialize it future-way
|
||||
opts, err := GenInitOptions(nil)
|
||||
opts, _, _, err := GenInitOptions(nil)
|
||||
require.NoError(t, err)
|
||||
req := abci.RequestInitChain{AppStateBytes: opts}
|
||||
app.InitChain(req)
|
||||
|
|
|
@ -2,18 +2,32 @@ package server
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
|
||||
cmn "github.com/tendermint/tmlibs/common"
|
||||
"github.com/tendermint/tmlibs/log"
|
||||
|
||||
tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands"
|
||||
cfg "github.com/tendermint/tendermint/config"
|
||||
"github.com/tendermint/tendermint/p2p"
|
||||
tmtypes "github.com/tendermint/tendermint/types"
|
||||
)
|
||||
|
||||
const (
|
||||
flagTestnet = "testnet"
|
||||
)
|
||||
|
||||
type testnetInformation struct {
|
||||
Secret string `json:"secret"`
|
||||
Account string `json:"account"`
|
||||
Validator tmtypes.GenesisValidator `json:"validator"`
|
||||
NodeID p2p.ID `json:"node_id"`
|
||||
}
|
||||
|
||||
// InitCmd will initialize all files for tendermint,
|
||||
// along with proper app_state.
|
||||
// The application can pass in a function to generate
|
||||
|
@ -24,17 +38,20 @@ func InitCmd(gen GenAppState, logger log.Logger) *cobra.Command {
|
|||
genAppState: gen,
|
||||
logger: logger,
|
||||
}
|
||||
return &cobra.Command{
|
||||
cobraCmd := cobra.Command{
|
||||
Use: "init",
|
||||
Short: "Initialize genesis files",
|
||||
RunE: cmd.run,
|
||||
}
|
||||
cobraCmd.Flags().Bool(flagTestnet, false, "Output testnet information in JSON")
|
||||
return &cobraCmd
|
||||
}
|
||||
|
||||
// GenAppState can parse command-line and flag to
|
||||
// generate default app_state for the genesis file.
|
||||
// Also must return generated seed and address
|
||||
// This is application-specific
|
||||
type GenAppState func(args []string) (json.RawMessage, error)
|
||||
type GenAppState func(args []string) (json.RawMessage, string, cmn.HexBytes, error)
|
||||
|
||||
type initCmd struct {
|
||||
genAppState GenAppState
|
||||
|
@ -42,13 +59,20 @@ type initCmd struct {
|
|||
}
|
||||
|
||||
func (c initCmd) run(cmd *cobra.Command, args []string) error {
|
||||
// Store testnet information as we go
|
||||
var testnetInfo testnetInformation
|
||||
|
||||
if viper.GetBool(flagTestnet) {
|
||||
c.logger = log.NewFilter(c.logger, log.AllowError())
|
||||
}
|
||||
|
||||
// Run the basic tendermint initialization,
|
||||
// set up a default genesis with no app_options
|
||||
config, err := tcmd.ParseConfig()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = c.initTendermintFiles(config)
|
||||
err = c.initTendermintFiles(config, &testnetInfo)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -59,19 +83,38 @@ func (c initCmd) run(cmd *cobra.Command, args []string) error {
|
|||
}
|
||||
|
||||
// Now, we want to add the custom app_state
|
||||
appState, err := c.genAppState(args)
|
||||
appState, secret, address, err := c.genAppState(args)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
testnetInfo.Secret = secret
|
||||
testnetInfo.Account = address.String()
|
||||
|
||||
// And add them to the genesis file
|
||||
genFile := config.GenesisFile()
|
||||
return addGenesisState(genFile, appState)
|
||||
if err := addGenesisState(genFile, appState); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if viper.GetBool(flagTestnet) {
|
||||
nodeKey, err := p2p.LoadOrGenNodeKey(config.NodeKeyFile())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
testnetInfo.NodeID = nodeKey.ID()
|
||||
out, err := json.MarshalIndent(testnetInfo, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println(string(out))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// This was copied from tendermint/cmd/tendermint/commands/init.go
|
||||
// so we could pass in the config and the logger.
|
||||
func (c initCmd) initTendermintFiles(config *cfg.Config) error {
|
||||
func (c initCmd) initTendermintFiles(config *cfg.Config, info *testnetInformation) error {
|
||||
// private validator
|
||||
privValFile := config.PrivValidatorFile()
|
||||
var privValidator *tmtypes.PrivValidatorFS
|
||||
|
@ -102,6 +145,18 @@ func (c initCmd) initTendermintFiles(config *cfg.Config) error {
|
|||
}
|
||||
c.logger.Info("Generated genesis file", "path", genFile)
|
||||
}
|
||||
|
||||
// reload the config file and find our validator info
|
||||
loadedDoc, err := tmtypes.GenesisDocFromFile(genFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, validator := range loadedDoc.Validators {
|
||||
if validator.PubKey == privValidator.GetPubKey() {
|
||||
info.Validator = validator
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue