forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
export.go
98 lines (81 loc) · 2.42 KB
/
export.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package server
// DONTCOVER
import (
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"io/ioutil"
"path"
"github.com/tendermint/tendermint/libs/cli"
tmtypes "github.com/tendermint/tendermint/types"
"github.com/cosmos/cosmos-sdk/codec"
)
const (
flagHeight = "height"
flagForZeroHeight = "for-zero-height"
flagJailWhitelist = "jail-whitelist"
)
// ExportCmd dumps app state to JSON.
func ExportCmd(ctx *Context, cdc *codec.Codec, appExporter AppExporter) *cobra.Command {
cmd := &cobra.Command{
Use: "export",
Short: "Export state to JSON",
RunE: func(cmd *cobra.Command, args []string) error {
config := ctx.Config
config.SetRoot(viper.GetString(cli.HomeFlag))
traceWriterFile := viper.GetString(flagTraceStore)
emptyState, err := isEmptyState(config.RootDir)
if err != nil {
return err
}
if emptyState || appExporter == nil {
fmt.Fprintln(os.Stderr, "WARNING: State is not initialized. Returning genesis file.")
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
}
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)
}
doc, err := tmtypes.GenesisDocFromFile(ctx.Config.GenesisFile())
if err != nil {
return err
}
doc.AppState = appState
doc.Validators = validators
encoded, err := codec.MarshalJSONIndent(cdc, doc)
if err != nil {
return err
}
fmt.Println(string(encoded))
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
}
return len(files) == 0, nil
}