2018-04-26 08:53:07 -07:00
|
|
|
package server
|
|
|
|
|
2019-02-08 13:45:41 -08:00
|
|
|
// DONTCOVER
|
|
|
|
|
2018-04-26 08:53:07 -07:00
|
|
|
import (
|
|
|
|
"fmt"
|
2019-07-23 10:00:26 -07:00
|
|
|
"io/ioutil"
|
2019-02-08 13:45:41 -08:00
|
|
|
"os"
|
2018-04-27 17:00:33 -07:00
|
|
|
|
2018-04-26 08:53:07 -07:00
|
|
|
"github.com/spf13/cobra"
|
2020-09-03 03:11:46 -07:00
|
|
|
tmjson "github.com/tendermint/tendermint/libs/json"
|
2020-08-14 10:58:53 -07:00
|
|
|
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
2018-12-10 06:27:25 -08:00
|
|
|
tmtypes "github.com/tendermint/tendermint/types"
|
|
|
|
|
2019-05-28 01:44:04 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
2020-07-27 10:57:15 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/server/types"
|
2019-07-23 10:00:26 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2018-04-26 08:53:07 -07:00
|
|
|
)
|
|
|
|
|
2018-11-13 07:14:09 -08:00
|
|
|
const (
|
2020-08-25 08:55:19 -07:00
|
|
|
FlagHeight = "height"
|
|
|
|
FlagForZeroHeight = "for-zero-height"
|
|
|
|
FlagJailAllowedAddrs = "jail-allowed-addrs"
|
2018-11-13 07:14:09 -08:00
|
|
|
)
|
|
|
|
|
2018-07-12 16:58:51 -07:00
|
|
|
// ExportCmd dumps app state to JSON.
|
2020-07-27 10:57:15 -07:00
|
|
|
func ExportCmd(appExporter types.AppExporter, defaultNodeHome string) *cobra.Command {
|
2018-11-13 07:14:09 -08:00
|
|
|
cmd := &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 {
|
2020-07-07 08:40:46 -07:00
|
|
|
serverCtx := GetServerContextFromCmd(cmd)
|
|
|
|
config := serverCtx.Config
|
2019-02-08 13:45:41 -08:00
|
|
|
|
2020-07-05 09:56:17 -07:00
|
|
|
homeDir, _ := cmd.Flags().GetString(flags.FlagHome)
|
|
|
|
config.SetRoot(homeDir)
|
2019-03-01 13:04:36 -08:00
|
|
|
|
2020-09-03 03:11:46 -07:00
|
|
|
if _, err := os.Stat(config.GenesisFile()); os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-03-01 13:04:36 -08:00
|
|
|
db, err := openDB(config.RootDir)
|
2018-08-23 02:34:59 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-25 12:22:28 -08:00
|
|
|
if appExporter == nil {
|
|
|
|
if _, err := fmt.Fprintln(os.Stderr, "WARNING: App exporter not defined. Returning genesis file."); err != nil {
|
2019-02-25 03:35:06 -08:00
|
|
|
return err
|
|
|
|
}
|
2019-03-01 13:04:36 -08:00
|
|
|
|
2019-02-08 13:45:41 -08:00
|
|
|
genesis, err := ioutil.ReadFile(config.GenesisFile())
|
2018-08-23 02:34:59 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-03-01 13:04:36 -08:00
|
|
|
|
2018-08-23 02:34:59 -07:00
|
|
|
fmt.Println(string(genesis))
|
|
|
|
return nil
|
|
|
|
}
|
2018-07-12 16:58:51 -07:00
|
|
|
|
2020-07-05 09:56:17 -07:00
|
|
|
traceWriterFile, _ := cmd.Flags().GetString(flagTraceStore)
|
2018-10-10 15:45:41 -07:00
|
|
|
traceWriter, err := openTraceWriter(traceWriterFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-03-01 13:04:36 -08:00
|
|
|
|
2020-08-25 08:55:19 -07:00
|
|
|
height, _ := cmd.Flags().GetInt64(FlagHeight)
|
|
|
|
forZeroHeight, _ := cmd.Flags().GetBool(FlagForZeroHeight)
|
|
|
|
jailAllowedAddrs, _ := cmd.Flags().GetStringSlice(FlagJailAllowedAddrs)
|
2019-03-01 13:04:36 -08:00
|
|
|
|
2020-11-02 11:10:14 -08:00
|
|
|
exported, err := appExporter(serverCtx.Logger, db, traceWriter, height, forZeroHeight, jailAllowedAddrs, serverCtx.Viper)
|
2018-04-27 17:00:33 -07:00
|
|
|
if err != nil {
|
2019-02-08 11:37:46 -08:00
|
|
|
return fmt.Errorf("error exporting state: %v", err)
|
2018-04-27 17:00:33 -07:00
|
|
|
}
|
2018-07-12 16:58:51 -07:00
|
|
|
|
2020-07-07 08:40:46 -07:00
|
|
|
doc, err := tmtypes.GenesisDocFromFile(serverCtx.Config.GenesisFile())
|
2018-05-06 16:01:01 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-07-12 16:58:51 -07:00
|
|
|
|
2020-09-03 03:11:46 -07:00
|
|
|
doc.AppState = exported.AppState
|
|
|
|
doc.Validators = exported.Validators
|
|
|
|
doc.InitialHeight = exported.Height
|
2020-08-14 10:58:53 -07:00
|
|
|
doc.ConsensusParams = &tmproto.ConsensusParams{
|
|
|
|
Block: tmproto.BlockParams{
|
2020-09-03 03:11:46 -07:00
|
|
|
MaxBytes: exported.ConsensusParams.Block.MaxBytes,
|
|
|
|
MaxGas: exported.ConsensusParams.Block.MaxGas,
|
2020-06-26 01:26:11 -07:00
|
|
|
TimeIotaMs: doc.ConsensusParams.Block.TimeIotaMs,
|
2020-04-16 08:10:39 -07:00
|
|
|
},
|
2020-08-14 10:58:53 -07:00
|
|
|
Evidence: tmproto.EvidenceParams{
|
2020-09-08 09:33:53 -07:00
|
|
|
MaxAgeNumBlocks: exported.ConsensusParams.Evidence.MaxAgeNumBlocks,
|
|
|
|
MaxAgeDuration: exported.ConsensusParams.Evidence.MaxAgeDuration,
|
2020-10-13 23:20:52 -07:00
|
|
|
MaxBytes: exported.ConsensusParams.Evidence.MaxBytes,
|
2020-04-16 08:10:39 -07:00
|
|
|
},
|
2020-08-14 10:58:53 -07:00
|
|
|
Validator: tmproto.ValidatorParams{
|
2020-09-03 03:11:46 -07:00
|
|
|
PubKeyTypes: exported.ConsensusParams.Validator.PubKeyTypes,
|
2020-04-16 08:10:39 -07:00
|
|
|
},
|
|
|
|
}
|
2018-07-12 16:58:51 -07:00
|
|
|
|
2020-09-03 03:11:46 -07:00
|
|
|
// NOTE: Tendermint uses a custom JSON decoder for GenesisDoc
|
|
|
|
// (except for stuff inside AppState). Inside AppState, we're free
|
|
|
|
// to encode as protobuf or amino.
|
|
|
|
encoded, err := tmjson.Marshal(doc)
|
2018-05-06 16:01:01 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-07-12 16:58:51 -07:00
|
|
|
|
2020-06-26 01:26:11 -07:00
|
|
|
cmd.Println(string(sdk.MustSortJSON(encoded)))
|
2018-04-27 17:00:33 -07:00
|
|
|
return nil
|
|
|
|
},
|
2018-04-26 08:53:07 -07:00
|
|
|
}
|
2021-04-16 17:21:32 -07:00
|
|
|
|
2020-07-20 08:12:33 -07:00
|
|
|
cmd.Flags().String(flags.FlagHome, defaultNodeHome, "The application home directory")
|
2020-08-25 08:55:19 -07:00
|
|
|
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)")
|
2020-11-13 04:53:18 -08:00
|
|
|
cmd.Flags().StringSlice(FlagJailAllowedAddrs, []string{}, "Comma-separated list of operator addresses of jailed validators to unjail")
|
2018-08-23 02:34:59 -07:00
|
|
|
|
2020-02-25 12:22:28 -08:00
|
|
|
return cmd
|
2018-08-23 02:34:59 -07:00
|
|
|
}
|