Merge Pull Request #3777: Fix export panic
This commit is contained in:
parent
7ac01bd76a
commit
b1ce965de8
|
@ -89,6 +89,8 @@ decoded automatically.
|
||||||
|
|
||||||
### Gaia
|
### Gaia
|
||||||
|
|
||||||
|
* [\#3777](https://github.com/cosmso/cosmos-sdk/pull/3777) `gaiad export` no longer panics when the database is empty
|
||||||
|
|
||||||
### SDK
|
### SDK
|
||||||
|
|
||||||
* \#3728 Truncate decimal multiplication & division in distribution to ensure
|
* \#3728 Truncate decimal multiplication & division in distribution to ensure
|
||||||
|
|
|
@ -10,9 +10,9 @@ import (
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"path"
|
|
||||||
|
|
||||||
"github.com/tendermint/tendermint/libs/cli"
|
"github.com/tendermint/tendermint/libs/cli"
|
||||||
|
dbm "github.com/tendermint/tendermint/libs/db"
|
||||||
tmtypes "github.com/tendermint/tendermint/types"
|
tmtypes "github.com/tendermint/tendermint/types"
|
||||||
|
|
||||||
"github.com/cosmos/cosmos-sdk/codec"
|
"github.com/cosmos/cosmos-sdk/codec"
|
||||||
|
@ -34,35 +34,35 @@ func ExportCmd(ctx *Context, cdc *codec.Codec, appExporter AppExporter) *cobra.C
|
||||||
config.SetRoot(viper.GetString(cli.HomeFlag))
|
config.SetRoot(viper.GetString(cli.HomeFlag))
|
||||||
|
|
||||||
traceWriterFile := viper.GetString(flagTraceStore)
|
traceWriterFile := viper.GetString(flagTraceStore)
|
||||||
emptyState, err := isEmptyState(config.RootDir)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if emptyState || appExporter == nil {
|
|
||||||
_, err := fmt.Fprintln(os.Stderr, "WARNING: State is not initialized. Returning genesis file.")
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
genesis, err := ioutil.ReadFile(config.GenesisFile())
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
fmt.Println(string(genesis))
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
db, err := openDB(config.RootDir)
|
db, err := openDB(config.RootDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if isEmptyState(db) || appExporter == nil {
|
||||||
|
if _, err := fmt.Fprintln(os.Stderr, "WARNING: State is not initialized. Returning genesis file."); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
genesis, err := ioutil.ReadFile(config.GenesisFile())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(string(genesis))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
traceWriter, err := openTraceWriter(traceWriterFile)
|
traceWriter, err := openTraceWriter(traceWriterFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
height := viper.GetInt64(flagHeight)
|
height := viper.GetInt64(flagHeight)
|
||||||
forZeroHeight := viper.GetBool(flagForZeroHeight)
|
forZeroHeight := viper.GetBool(flagForZeroHeight)
|
||||||
jailWhiteList := viper.GetStringSlice(flagJailWhitelist)
|
jailWhiteList := viper.GetStringSlice(flagJailWhitelist)
|
||||||
|
|
||||||
appState, validators, err := appExporter(ctx.Logger, db, traceWriter, height, forZeroHeight, jailWhiteList)
|
appState, validators, err := appExporter(ctx.Logger, db, traceWriter, height, forZeroHeight, jailWhiteList)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error exporting state: %v", err)
|
return fmt.Errorf("error exporting state: %v", err)
|
||||||
|
@ -85,17 +85,16 @@ func ExportCmd(ctx *Context, cdc *codec.Codec, appExporter AppExporter) *cobra.C
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd.Flags().Int64(flagHeight, -1, "Export state from a particular height (-1 means latest height)")
|
cmd.Flags().Int64(flagHeight, -1, "Export state from a particular height (-1 means latest height)")
|
||||||
cmd.Flags().Bool(flagForZeroHeight, false, "Export state to start at height zero (perform preproccessing)")
|
cmd.Flags().Bool(flagForZeroHeight, false, "Export state to start at height zero (perform preproccessing)")
|
||||||
cmd.Flags().StringSlice(flagJailWhitelist, []string{}, "List of validators to not jail state export")
|
cmd.Flags().StringSlice(flagJailWhitelist, []string{}, "List of validators to not jail state export")
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func isEmptyState(home string) (bool, error) {
|
func isEmptyState(db dbm.DB) bool {
|
||||||
files, err := ioutil.ReadDir(path.Join(home, "data"))
|
if db.Stats()["leveldb.sstables"] != "" {
|
||||||
if err != nil {
|
return false
|
||||||
return false, err
|
|
||||||
}
|
}
|
||||||
|
return true
|
||||||
return len(files) == 0, nil
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue