Skip to content

Commit b1ce965

Browse files
authored
Merge Pull Request #3777: Fix export panic
1 parent 7ac01bd commit b1ce965

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

PENDING.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ decoded automatically.
8989

9090
### Gaia
9191

92+
* [\#3777](https://github.com/cosmso/cosmos-sdk/pull/3777) `gaiad export` no longer panics when the database is empty
93+
9294
### SDK
9395

9496
* \#3728 Truncate decimal multiplication & division in distribution to ensure

server/export.go

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import (
1010
"github.com/spf13/viper"
1111

1212
"io/ioutil"
13-
"path"
1413

1514
"github.com/tendermint/tendermint/libs/cli"
15+
dbm "github.com/tendermint/tendermint/libs/db"
1616
tmtypes "github.com/tendermint/tendermint/types"
1717

1818
"github.com/cosmos/cosmos-sdk/codec"
@@ -34,35 +34,35 @@ func ExportCmd(ctx *Context, cdc *codec.Codec, appExporter AppExporter) *cobra.C
3434
config.SetRoot(viper.GetString(cli.HomeFlag))
3535

3636
traceWriterFile := viper.GetString(flagTraceStore)
37-
emptyState, err := isEmptyState(config.RootDir)
37+
38+
db, err := openDB(config.RootDir)
3839
if err != nil {
3940
return err
4041
}
4142

42-
if emptyState || appExporter == nil {
43-
_, err := fmt.Fprintln(os.Stderr, "WARNING: State is not initialized. Returning genesis file.")
44-
if err != nil {
43+
if isEmptyState(db) || appExporter == nil {
44+
if _, err := fmt.Fprintln(os.Stderr, "WARNING: State is not initialized. Returning genesis file."); err != nil {
4545
return err
4646
}
47+
4748
genesis, err := ioutil.ReadFile(config.GenesisFile())
4849
if err != nil {
4950
return err
5051
}
52+
5153
fmt.Println(string(genesis))
5254
return nil
5355
}
5456

55-
db, err := openDB(config.RootDir)
56-
if err != nil {
57-
return err
58-
}
5957
traceWriter, err := openTraceWriter(traceWriterFile)
6058
if err != nil {
6159
return err
6260
}
61+
6362
height := viper.GetInt64(flagHeight)
6463
forZeroHeight := viper.GetBool(flagForZeroHeight)
6564
jailWhiteList := viper.GetStringSlice(flagJailWhitelist)
65+
6666
appState, validators, err := appExporter(ctx.Logger, db, traceWriter, height, forZeroHeight, jailWhiteList)
6767
if err != nil {
6868
return fmt.Errorf("error exporting state: %v", err)
@@ -85,17 +85,16 @@ func ExportCmd(ctx *Context, cdc *codec.Codec, appExporter AppExporter) *cobra.C
8585
return nil
8686
},
8787
}
88+
8889
cmd.Flags().Int64(flagHeight, -1, "Export state from a particular height (-1 means latest height)")
8990
cmd.Flags().Bool(flagForZeroHeight, false, "Export state to start at height zero (perform preproccessing)")
9091
cmd.Flags().StringSlice(flagJailWhitelist, []string{}, "List of validators to not jail state export")
9192
return cmd
9293
}
9394

94-
func isEmptyState(home string) (bool, error) {
95-
files, err := ioutil.ReadDir(path.Join(home, "data"))
96-
if err != nil {
97-
return false, err
95+
func isEmptyState(db dbm.DB) bool {
96+
if db.Stats()["leveldb.sstables"] != "" {
97+
return false
9898
}
99-
100-
return len(files) == 0, nil
99+
return true
101100
}

0 commit comments

Comments
 (0)