Merge pull request #729 from cosmos/cwgoes/misc-minor-fixes

Misc minor fixes
This commit is contained in:
Ethan Buchman 2018-03-31 18:54:19 +03:00 committed by GitHub
commit 5dd24b4b0a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 83 additions and 23 deletions

View File

@ -84,10 +84,7 @@ test_unit:
test_cover:
@rm -rf examples/basecoin/vendor/
@rm -rf examples/democoin/vendor/
@rm -rf client/lcd/keys.db ~/.tendermint_test
@bash tests/test_cover.sh
@rm -rf client/lcd/keys.db ~/.tendermint_test
benchmark:
@go test -bench=. $(PACKAGES)

View File

@ -2,6 +2,7 @@ package keys
import (
"fmt"
"path/filepath"
"github.com/spf13/viper"
@ -32,7 +33,7 @@ type KeyOutput struct {
func GetKeyBase() (keys.Keybase, error) {
if keybase == nil {
rootDir := viper.GetString(cli.HomeFlag)
db, err := dbm.NewGoLevelDB(KeyDBName, rootDir)
db, err := dbm.NewGoLevelDB(KeyDBName, filepath.Join(rootDir, "keys"))
if err != nil {
return nil, err
}

View File

@ -25,6 +25,7 @@ import (
ctypes "github.com/tendermint/tendermint/rpc/core/types"
tmrpc "github.com/tendermint/tendermint/rpc/lib/server"
tmtypes "github.com/tendermint/tendermint/types"
"github.com/tendermint/tmlibs/cli"
dbm "github.com/tendermint/tmlibs/db"
"github.com/tendermint/tmlibs/log"
@ -320,6 +321,7 @@ func TestTxs(t *testing.T) {
// strt TM and the LCD in process, listening on their respective sockets
func startTMAndLCD() (*nm.Node, net.Listener, error) {
viper.Set(cli.HomeFlag, os.TempDir())
kb, err := keys.GetKeyBase() // dbm.NewMemDB()) // :(
if err != nil {
return nil, nil, err

View File

@ -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) {

View File

@ -9,6 +9,7 @@ import (
abci "github.com/tendermint/abci/types"
"github.com/tendermint/tmlibs/cli"
cmn "github.com/tendermint/tmlibs/common"
"github.com/tendermint/tmlibs/log"
"github.com/cosmos/cosmos-sdk/baseapp"
@ -26,10 +27,10 @@ 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)
@ -45,7 +46,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) {

View File

@ -3,8 +3,12 @@ package main
import (
"fmt"
"os"
"path/filepath"
"github.com/spf13/viper"
"github.com/tendermint/abci/server"
"github.com/tendermint/tmlibs/cli"
cmn "github.com/tendermint/tmlibs/common"
dbm "github.com/tendermint/tmlibs/db"
"github.com/tendermint/tmlibs/log"
@ -17,7 +21,8 @@ func main() {
logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout)).With("module", "main")
db, err := dbm.NewGoLevelDB("basecoind", "data")
rootDir := viper.GetString(cli.HomeFlag)
db, err := dbm.NewGoLevelDB("basecoind", filepath.Join(rootDir, "data"))
if err != nil {
fmt.Println(err)
os.Exit(1)

View File

@ -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
}

View File

@ -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)

View File

@ -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
}