116 lines
3.3 KiB
Go
116 lines
3.3 KiB
Go
package server
|
|
|
|
// DONTCOVER
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
|
|
"github.com/spf13/cobra"
|
|
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
|
tmtypes "github.com/tendermint/tendermint/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
|
"github.com/cosmos/cosmos-sdk/server/types"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
)
|
|
|
|
const (
|
|
flagHeight = "height"
|
|
flagForZeroHeight = "for-zero-height"
|
|
flagJailWhitelist = "jail-whitelist"
|
|
)
|
|
|
|
// ExportCmd dumps app state to JSON.
|
|
func ExportCmd(appExporter types.AppExporter, defaultNodeHome string) *cobra.Command {
|
|
cmd := &cobra.Command{
|
|
Use: "export",
|
|
Short: "Export state to JSON",
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
serverCtx := GetServerContextFromCmd(cmd)
|
|
config := serverCtx.Config
|
|
|
|
homeDir, _ := cmd.Flags().GetString(flags.FlagHome)
|
|
config.SetRoot(homeDir)
|
|
|
|
db, err := openDB(config.RootDir)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if appExporter == nil {
|
|
if _, err := fmt.Fprintln(os.Stderr, "WARNING: App exporter not defined. Returning genesis file."); err != nil {
|
|
return err
|
|
}
|
|
|
|
genesis, err := ioutil.ReadFile(config.GenesisFile())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
fmt.Println(string(genesis))
|
|
return nil
|
|
}
|
|
|
|
traceWriterFile, _ := cmd.Flags().GetString(flagTraceStore)
|
|
traceWriter, err := openTraceWriter(traceWriterFile)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
height, _ := cmd.Flags().GetInt64(flagHeight)
|
|
forZeroHeight, _ := cmd.Flags().GetBool(flagForZeroHeight)
|
|
jailWhiteList, _ := cmd.Flags().GetStringSlice(flagJailWhitelist)
|
|
|
|
appState, validators, cp, err := appExporter(serverCtx.Logger, db, traceWriter, height, forZeroHeight, jailWhiteList)
|
|
if err != nil {
|
|
return fmt.Errorf("error exporting state: %v", err)
|
|
}
|
|
|
|
doc, err := tmtypes.GenesisDocFromFile(serverCtx.Config.GenesisFile())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
doc.AppState = appState
|
|
doc.Validators = validators
|
|
doc.ConsensusParams = &tmproto.ConsensusParams{
|
|
Block: tmproto.BlockParams{
|
|
MaxBytes: cp.Block.MaxBytes,
|
|
MaxGas: cp.Block.MaxGas,
|
|
TimeIotaMs: doc.ConsensusParams.Block.TimeIotaMs,
|
|
},
|
|
Evidence: tmproto.EvidenceParams{
|
|
MaxAgeNumBlocks: cp.Evidence.MaxAgeNumBlocks,
|
|
MaxAgeDuration: cp.Evidence.MaxAgeDuration,
|
|
MaxNum: cp.Evidence.MaxNum,
|
|
ProofTrialPeriod: cp.Evidence.ProofTrialPeriod,
|
|
},
|
|
Validator: tmproto.ValidatorParams{
|
|
PubKeyTypes: cp.Validator.PubKeyTypes,
|
|
},
|
|
}
|
|
|
|
// NOTE: for now we're just using standard JSON marshaling for the root GenesisDoc.
|
|
// These types are in Tendermint, don't support proto and as far as we know, don't need it.
|
|
// All of the protobuf/amino state is inside AppState
|
|
encoded, err := json.MarshalIndent(doc, "", " ")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
cmd.Println(string(sdk.MustSortJSON(encoded)))
|
|
return nil
|
|
},
|
|
}
|
|
|
|
cmd.Flags().String(flags.FlagHome, defaultNodeHome, "The application home directory")
|
|
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
|
|
}
|