2018-04-26 08:53:07 -07:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-04-27 17:00:33 -07:00
|
|
|
|
2018-04-26 08:53:07 -07:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
|
2018-09-13 11:17:32 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2018-05-06 16:01:01 -07:00
|
|
|
tmtypes "github.com/tendermint/tendermint/types"
|
2018-08-23 02:34:59 -07:00
|
|
|
"io/ioutil"
|
|
|
|
"path"
|
2018-04-26 08:53:07 -07:00
|
|
|
)
|
|
|
|
|
2018-07-12 16:58:51 -07:00
|
|
|
// ExportCmd dumps app state to JSON.
|
2018-09-13 11:17:32 -07:00
|
|
|
func ExportCmd(ctx *Context, cdc *codec.Codec, appExporter AppExporter) *cobra.Command {
|
2018-04-27 17:00:33 -07:00
|
|
|
return &cobra.Command{
|
2018-04-26 08:53:07 -07:00
|
|
|
Use: "export",
|
|
|
|
Short: "Export state to JSON",
|
2018-04-27 17:00:33 -07:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
home := viper.GetString("home")
|
2018-10-10 15:45:41 -07:00
|
|
|
traceWriterFile := viper.GetString(flagTraceStore)
|
2018-08-23 02:34:59 -07:00
|
|
|
emptyState, err := isEmptyState(home)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if emptyState {
|
|
|
|
fmt.Println("WARNING: State is not initialized. Returning genesis file.")
|
|
|
|
genesisFile := path.Join(home, "config", "genesis.json")
|
|
|
|
genesis, err := ioutil.ReadFile(genesisFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Println(string(genesis))
|
|
|
|
return nil
|
|
|
|
}
|
2018-07-12 16:58:51 -07:00
|
|
|
|
2018-10-10 15:45:41 -07:00
|
|
|
db, err := openDB(home)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
traceWriter, err := openTraceWriter(traceWriterFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
appState, validators, err := appExporter(ctx.Logger, db, traceWriter)
|
2018-04-27 17:00:33 -07:00
|
|
|
if err != nil {
|
2018-06-13 12:13:22 -07:00
|
|
|
return errors.Errorf("error exporting state: %v\n", err)
|
2018-04-27 17:00:33 -07:00
|
|
|
}
|
2018-07-12 16:58:51 -07:00
|
|
|
|
2018-05-06 16:01:01 -07:00
|
|
|
doc, err := tmtypes.GenesisDocFromFile(ctx.Config.GenesisFile())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-07-12 16:58:51 -07:00
|
|
|
|
2018-07-14 05:39:10 -07:00
|
|
|
doc.AppState = appState
|
2018-06-06 09:38:13 -07:00
|
|
|
doc.Validators = validators
|
2018-07-12 16:58:51 -07:00
|
|
|
|
2018-09-13 11:17:32 -07:00
|
|
|
encoded, err := codec.MarshalJSONIndent(cdc, doc)
|
2018-05-06 16:01:01 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-07-12 16:58:51 -07:00
|
|
|
|
2018-05-06 16:01:01 -07:00
|
|
|
fmt.Println(string(encoded))
|
2018-04-27 17:00:33 -07:00
|
|
|
return nil
|
|
|
|
},
|
2018-04-26 08:53:07 -07:00
|
|
|
}
|
|
|
|
}
|
2018-08-23 02:34:59 -07:00
|
|
|
|
|
|
|
func isEmptyState(home string) (bool, error) {
|
|
|
|
files, err := ioutil.ReadDir(path.Join(home, "data"))
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return len(files) == 0, nil
|
|
|
|
}
|