## Description present app export genesis will export to stderr, not stdout, because [cobra `println` default is to stderr](https://pkg.go.dev/github.com/spf13/cobra#Command.Println) `gaiad export > genesis_export.json` will not work `gaiad export 2> genesis_export.json` will work the fix makes gaiad export to print to stdout. Closes: #XXXX --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
118 lines
3.5 KiB
Go
118 lines
3.5 KiB
Go
package server
|
|
|
|
// DONTCOVER
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/spf13/cobra"
|
|
tmjson "github.com/tendermint/tendermint/libs/json"
|
|
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"
|
|
FlagJailAllowedAddrs = "jail-allowed-addrs"
|
|
)
|
|
|
|
// 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)
|
|
|
|
if _, err := os.Stat(config.GenesisFile()); os.IsNotExist(err) {
|
|
return err
|
|
}
|
|
|
|
db, err := openDB(config.RootDir, GetAppDBBackend(serverCtx.Viper))
|
|
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 := os.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)
|
|
jailAllowedAddrs, _ := cmd.Flags().GetStringSlice(FlagJailAllowedAddrs)
|
|
|
|
exported, err := appExporter(serverCtx.Logger, db, traceWriter, height, forZeroHeight, jailAllowedAddrs, serverCtx.Viper)
|
|
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 = exported.AppState
|
|
doc.Validators = exported.Validators
|
|
doc.InitialHeight = exported.Height
|
|
doc.ConsensusParams = &tmtypes.ConsensusParams{
|
|
Block: tmtypes.BlockParams{
|
|
MaxBytes: exported.ConsensusParams.Block.MaxBytes,
|
|
MaxGas: exported.ConsensusParams.Block.MaxGas,
|
|
},
|
|
Evidence: tmtypes.EvidenceParams{
|
|
MaxAgeNumBlocks: exported.ConsensusParams.Evidence.MaxAgeNumBlocks,
|
|
MaxAgeDuration: exported.ConsensusParams.Evidence.MaxAgeDuration,
|
|
MaxBytes: exported.ConsensusParams.Evidence.MaxBytes,
|
|
},
|
|
Validator: tmtypes.ValidatorParams{
|
|
PubKeyTypes: exported.ConsensusParams.Validator.PubKeyTypes,
|
|
},
|
|
}
|
|
|
|
// 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)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
cmd.SetOut(cmd.OutOrStdout())
|
|
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(FlagJailAllowedAddrs, []string{}, "Comma-separated list of operator addresses of jailed validators to unjail")
|
|
|
|
return cmd
|
|
}
|