Merge Pull Request #3777: Fix export panic
This commit is contained in:
parent
7ac01bd76a
commit
b1ce965de8
|
@ -89,6 +89,8 @@ decoded automatically.
|
|||
|
||||
### Gaia
|
||||
|
||||
* [\#3777](https://github.com/cosmso/cosmos-sdk/pull/3777) `gaiad export` no longer panics when the database is empty
|
||||
|
||||
### SDK
|
||||
|
||||
* \#3728 Truncate decimal multiplication & division in distribution to ensure
|
||||
|
|
|
@ -10,9 +10,9 @@ import (
|
|||
"github.com/spf13/viper"
|
||||
|
||||
"io/ioutil"
|
||||
"path"
|
||||
|
||||
"github.com/tendermint/tendermint/libs/cli"
|
||||
dbm "github.com/tendermint/tendermint/libs/db"
|
||||
tmtypes "github.com/tendermint/tendermint/types"
|
||||
|
||||
"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))
|
||||
|
||||
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)
|
||||
if err != nil {
|
||||
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)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
height := viper.GetInt64(flagHeight)
|
||||
forZeroHeight := viper.GetBool(flagForZeroHeight)
|
||||
jailWhiteList := viper.GetStringSlice(flagJailWhitelist)
|
||||
|
||||
appState, validators, err := appExporter(ctx.Logger, db, traceWriter, height, forZeroHeight, jailWhiteList)
|
||||
if err != nil {
|
||||
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
|
||||
},
|
||||
}
|
||||
|
||||
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().StringSlice(flagJailWhitelist, []string{}, "List of validators to not jail state export")
|
||||
return cmd
|
||||
}
|
||||
|
||||
func isEmptyState(home string) (bool, error) {
|
||||
files, err := ioutil.ReadDir(path.Join(home, "data"))
|
||||
if err != nil {
|
||||
return false, err
|
||||
func isEmptyState(db dbm.DB) bool {
|
||||
if db.Stats()["leveldb.sstables"] != "" {
|
||||
return false
|
||||
}
|
||||
|
||||
return len(files) == 0, nil
|
||||
return true
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue