-
Notifications
You must be signed in to change notification settings - Fork 206
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(x/swingset): include swing-store in genesis #8152
Changes from all commits
7a4fa83
33c4a51
592948d
3be2986
598abf7
55ba7f6
46d3466
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 |
---|---|---|
|
@@ -28,6 +28,7 @@ import ( | |
genutilcli "github.com/cosmos/cosmos-sdk/x/genutil/client/cli" | ||
"github.com/spf13/cast" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
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. I don't think 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. Yeah this is just imported so I can cast to the |
||
tmcli "github.com/tendermint/tendermint/libs/cli" | ||
"github.com/tendermint/tendermint/libs/log" | ||
dbm "github.com/tendermint/tm-db" | ||
|
@@ -40,7 +41,8 @@ import ( | |
type Sender func(needReply bool, str string) (string, error) | ||
|
||
var AppName = "agd" | ||
var OnStartHook func(log.Logger) | ||
var OnStartHook func(logger log.Logger) | ||
var OnExportHook func(logger log.Logger) | ||
|
||
// NewRootCmd creates a new root command for simd. It is called once in the | ||
// main function. | ||
|
@@ -133,6 +135,14 @@ func initRootCmd(sender Sender, rootCmd *cobra.Command, encodingConfig params.En | |
} | ||
server.AddCommands(rootCmd, gaia.DefaultNodeHome, ac.newApp, ac.appExport, addModuleInitFlags) | ||
|
||
hasVMController := sender != nil | ||
for _, command := range rootCmd.Commands() { | ||
if command.Name() == "export" { | ||
extendCosmosExportCommand(command, hasVMController) | ||
break | ||
} | ||
} | ||
|
||
// add keybase, auxiliary RPC, query, and tx child commands | ||
rootCmd.AddCommand( | ||
rpc.StatusCommand(), | ||
|
@@ -232,7 +242,16 @@ func (ac appCreator) newApp( | |
panic(err) | ||
} | ||
|
||
snapshotDir := filepath.Join(cast.ToString(appOpts.Get(flags.FlagHome)), "data", "snapshots") | ||
homePath := cast.ToString(appOpts.Get(flags.FlagHome)) | ||
mhofman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Set a default value for FlagSwingStoreExportDir based on the homePath | ||
// in case we need to InitGenesis with swing-store data | ||
viper, ok := appOpts.(*viper.Viper) | ||
if ok && cast.ToString(appOpts.Get(gaia.FlagSwingStoreExportDir)) == "" { | ||
viper.Set(gaia.FlagSwingStoreExportDir, filepath.Join(homePath, "config", ExportedSwingStoreDirectoryName)) | ||
} | ||
|
||
snapshotDir := filepath.Join(homePath, "data", "snapshots") | ||
snapshotDB, err := sdk.NewLevelDB("metadata", snapshotDir) | ||
if err != nil { | ||
panic(err) | ||
|
@@ -245,7 +264,7 @@ func (ac appCreator) newApp( | |
return gaia.NewAgoricApp( | ||
ac.sender, | ||
logger, db, traceStore, true, skipUpgradeHeights, | ||
cast.ToString(appOpts.Get(flags.FlagHome)), | ||
homePath, | ||
cast.ToUint(appOpts.Get(server.FlagInvCheckPeriod)), | ||
ac.encCfg, | ||
appOpts, | ||
|
@@ -263,6 +282,74 @@ func (ac appCreator) newApp( | |
) | ||
} | ||
|
||
const ( | ||
// FlagExportDir is the command-line flag for the "export" command specifying | ||
// where the output of the export should be placed. It contains both the | ||
// items names below: the genesis file, and a directory containing the | ||
// exported swing-store artifacts | ||
FlagExportDir = "export-dir" | ||
// ExportedGenesisFileName is the file name used to save the genesis in the export-dir | ||
ExportedGenesisFileName = "genesis.json" | ||
// ExportedSwingStoreDirectoryName is the directory name used to save the swing-store | ||
// export (artifacts only) in the export-dir | ||
ExportedSwingStoreDirectoryName = "swing-store" | ||
) | ||
|
||
// extendCosmosExportCommand monkey-patches the "export" command added by | ||
// cosmos-sdk to add a required "export-dir" command-line flag, and create the | ||
// genesis export in the specified directory. | ||
func extendCosmosExportCommand(cmd *cobra.Command, hasVMController bool) { | ||
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. Document, briefly, what this function should do. |
||
cmd.Flags().String(FlagExportDir, "", "The directory where to create the genesis export") | ||
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. Add note in the flag help string explaining difference from the swing store export dir flag, and in that one's help string explaining the difference from this one. 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. I didn't add a help string for Actually I realize now that I didn't actually declare it as an official flag anywhere, I just abused the flag mechanism to plumb through an app option. Where do you recommend I declare the 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. I second the confusion, but |
||
err := cmd.MarkFlagRequired(FlagExportDir) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
originalRunE := cmd.RunE | ||
|
||
extendedRunE := func(cmd *cobra.Command, args []string) error { | ||
serverCtx := server.GetServerContextFromCmd(cmd) | ||
|
||
exportDir, _ := cmd.Flags().GetString(FlagExportDir) | ||
err := os.MkdirAll(exportDir, os.ModePerm) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
genesisPath := filepath.Join(exportDir, ExportedGenesisFileName) | ||
swingStoreExportPath := filepath.Join(exportDir, ExportedSwingStoreDirectoryName) | ||
|
||
err = os.MkdirAll(swingStoreExportPath, os.ModePerm) | ||
if err != nil { | ||
return err | ||
} | ||
// We unconditionally set FlagSwingStoreExportDir as for export, it makes | ||
// little sense for users to control this location separately, and we don't | ||
// want to override any swing-store artifacts that may be associated to the | ||
// current genesis. | ||
serverCtx.Viper.Set(gaia.FlagSwingStoreExportDir, swingStoreExportPath) | ||
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. What I've discerned is that 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. FlagSwingStoreExportDir can only be used to set where to read the swing-store artifacts from for InitGenesis, not for export 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. Do they need to be different? It seems like the purpose is aligned. |
||
|
||
// This will fail is a genesis.json already exists in the export-dir | ||
genesisFile, err := os.OpenFile(genesisPath, os.O_CREATE|os.O_EXCL|os.O_WRONLY, os.ModePerm) | ||
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 will catch a preexisting genesis file, but is there also a potential issue if swingStoreExportPath was already created? I suspect not because it would be empty if this line succeeds, but it's worth confirming. 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. If the |
||
if err != nil { | ||
return err | ||
} | ||
defer genesisFile.Close() | ||
|
||
cmd.SetOut(genesisFile) | ||
|
||
return originalRunE(cmd, args) | ||
} | ||
|
||
// Only modify the command handler when we have a VM controller to handle | ||
// the full export logic. Otherwise, appExport will just exec the VM program | ||
// (OnExportHook), which will result in re-entering this flow with the VM | ||
// controller set. | ||
if hasVMController { | ||
cmd.RunE = extendedRunE | ||
} | ||
} | ||
|
||
func (ac appCreator) appExport( | ||
logger log.Logger, | ||
db dbm.DB, | ||
|
@@ -272,6 +359,9 @@ func (ac appCreator) appExport( | |
jailAllowedAddrs []string, | ||
appOpts servertypes.AppOptions, | ||
) (servertypes.ExportedApp, error) { | ||
if OnExportHook != nil { | ||
OnExportHook(logger) | ||
} | ||
|
||
homePath, ok := appOpts.Get(flags.FlagHome).(string) | ||
if !ok || homePath == "" { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's some log output in
launchVM
that could probably be improved for export.