-
Notifications
You must be signed in to change notification settings - Fork 3.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(cli/module)!: separate the app genesis state by module into folders when export/import/validate #13032
feat(cli/module)!: separate the app genesis state by module into folders when export/import/validate #13032
Changes from 14 commits
1732761
1184537
900467e
e793b3a
a4ecfec
b2e8d03
61080bf
757cd48
1f672e6
c199b2b
e008018
a0013d2
e8d5664
17d2f06
596598a
9ddb729
12c9fad
156b973
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ package server | |
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/spf13/cobra" | ||
tmjson "github.com/tendermint/tendermint/libs/json" | ||
|
@@ -20,6 +21,7 @@ const ( | |
FlagForZeroHeight = "for-zero-height" | ||
FlagJailAllowedAddrs = "jail-allowed-addrs" | ||
FlagModulesToExport = "modules-to-export" | ||
FlagSplitModules = "split-modules" | ||
) | ||
|
||
// ExportCmd dumps app state to JSON. | ||
|
@@ -67,8 +69,9 @@ func ExportCmd(appExporter types.AppExporter, defaultNodeHome string) *cobra.Com | |
forZeroHeight, _ := cmd.Flags().GetBool(FlagForZeroHeight) | ||
jailAllowedAddrs, _ := cmd.Flags().GetStringSlice(FlagJailAllowedAddrs) | ||
modulesToExport, _ := cmd.Flags().GetStringSlice(FlagModulesToExport) | ||
splitModules, _ := cmd.Flags().GetBool(FlagSplitModules) | ||
|
||
exported, err := appExporter(serverCtx.Logger, db, traceWriter, height, forZeroHeight, jailAllowedAddrs, serverCtx.Viper, modulesToExport) | ||
exported, err := appExporter(serverCtx.Logger, db, traceWriter, height, forZeroHeight, jailAllowedAddrs, serverCtx.Viper, modulesToExport, splitModules) | ||
if err != nil { | ||
return fmt.Errorf("error exporting state: %v", err) | ||
} | ||
|
@@ -96,17 +99,28 @@ func ExportCmd(appExporter types.AppExporter, defaultNodeHome string) *cobra.Com | |
}, | ||
} | ||
|
||
// 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 | ||
} | ||
if splitModules { | ||
wd, err := os.Getwd() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't correct, as it will export it in the current directory. You don't need it as we have |
||
if err != nil { | ||
return err | ||
} | ||
|
||
cmd.SetOut(cmd.OutOrStdout()) | ||
cmd.SetErr(cmd.OutOrStderr()) | ||
cmd.Println(string(sdk.MustSortJSON(encoded))) | ||
if err := doc.SaveAs(filepath.Join(wd, "genesis", "genesis.json")); err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should use |
||
return err | ||
} | ||
} else { | ||
// 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.SetErr(cmd.OutOrStderr()) | ||
cmd.Println(string(sdk.MustSortJSON(encoded))) | ||
} | ||
return nil | ||
}, | ||
} | ||
|
@@ -116,6 +130,7 @@ func ExportCmd(appExporter types.AppExporter, defaultNodeHome string) *cobra.Com | |
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") | ||
cmd.Flags().StringSlice(FlagModulesToExport, []string{}, "Comma-separated list of modules to export. If empty, will export all modules") | ||
cmd.Flags().Bool(FlagSplitModules, false, "Export module state and store it as separate file(s) in genesis/[module] folder") | ||
|
||
return cmd | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -4,6 +4,7 @@ import ( | |||||
"encoding/json" | ||||||
"fmt" | ||||||
"log" | ||||||
"os" | ||||||
|
||||||
tmproto "github.com/tendermint/tendermint/proto/tendermint/types" | ||||||
|
||||||
|
@@ -16,7 +17,12 @@ import ( | |||||
|
||||||
// ExportAppStateAndValidators exports the state of the application for a genesis | ||||||
// file. | ||||||
func (app *SimApp) ExportAppStateAndValidators(forZeroHeight bool, jailAllowedAddrs []string, modulesToExport []string) (servertypes.ExportedApp, error) { | ||||||
func (app *SimApp) ExportAppStateAndValidators( | ||||||
forZeroHeight bool, | ||||||
jailAllowedAddrs []string, | ||||||
modulesToExport []string, | ||||||
splitModules bool, | ||||||
) (servertypes.ExportedApp, error) { | ||||||
// as if they could withdraw from the start of the next block | ||||||
ctx := app.NewContext(true, tmproto.Header{Height: app.LastBlockHeight()}) | ||||||
|
||||||
|
@@ -28,11 +34,30 @@ func (app *SimApp) ExportAppStateAndValidators(forZeroHeight bool, jailAllowedAd | |||||
app.prepForZeroHeightGenesis(ctx, jailAllowedAddrs) | ||||||
} | ||||||
|
||||||
genState := app.ModuleManager.ExportGenesisForModules(ctx, app.appCodec, modulesToExport) | ||||||
appState, err := json.MarshalIndent(genState, "", " ") | ||||||
wd, err := os.Getwd() | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We shouldn't get the current directory. |
||||||
if err != nil { | ||||||
return servertypes.ExportedApp{}, err | ||||||
} | ||||||
app.ModuleManager.GenesisPath = wd | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
genState, err := app.ModuleManager.ExportGenesisForModules(ctx, app.appCodec, modulesToExport, splitModules) | ||||||
if err != nil { | ||||||
return servertypes.ExportedApp{}, err | ||||||
} | ||||||
|
||||||
var appState []byte | ||||||
if splitModules { | ||||||
jsonObj := make(map[string]json.RawMessage) | ||||||
jsonObj["module_genesis_state"] = []byte("true") | ||||||
appState, err = json.MarshalIndent(jsonObj, "", " ") | ||||||
if err != nil { | ||||||
return servertypes.ExportedApp{}, err | ||||||
} | ||||||
} else { | ||||||
appState, err = json.MarshalIndent(genState, "", " ") | ||||||
if err != nil { | ||||||
return servertypes.ExportedApp{}, err | ||||||
} | ||||||
} | ||||||
|
||||||
validators, err := staking.WriteValidators(ctx, app.StakingKeeper) | ||||||
return servertypes.ExportedApp{ | ||||||
|
Check warning
Code scanning / gosec
Returned error is not propagated up the stack.