From 0a4c1f00cbaf047a3caebc7f36fc4a5665921380 Mon Sep 17 00:00:00 2001 From: usmanmani1122 Date: Mon, 7 Oct 2024 12:51:10 +0500 Subject: [PATCH 01/10] testing --- golang/cosmos/app/app.go | 27 ++++++++++++++------- golang/cosmos/cmd/agd/main.go | 4 ++++ golang/cosmos/cmd/libdaemon/main.go | 4 ++++ golang/cosmos/daemon/cmd/root.go | 28 ++++++++++++++++++++++ golang/cosmos/daemon/main.go | 4 ++++ golang/cosmos/x/swingset/genesis.go | 2 ++ golang/cosmos/x/swingset/module.go | 12 +++++++++- packages/cosmic-swingset/src/chain-main.js | 4 ++++ 8 files changed, 76 insertions(+), 9 deletions(-) diff --git a/golang/cosmos/app/app.go b/golang/cosmos/app/app.go index 52eb932eaa0..ace015da709 100644 --- a/golang/cosmos/app/app.go +++ b/golang/cosmos/app/app.go @@ -137,13 +137,16 @@ import ( const appName = "agoric" -// FlagSwingStoreExportDir defines the config flag used to specify where a -// genesis swing-store export is expected. For start from genesis, the default -// value is config/swing-store in the home directory. For genesis export, the -// value is always a "swing-store" directory sibling to the exported -// genesis.json file. -// TODO: document this flag in config, likely alongside the genesis path -const FlagSwingStoreExportDir = "swing-store-export-dir" +const ( + // FlagSwingStoreExportDir defines the config flag used to specify where a + // genesis swing-store export is expected. For start from genesis, the default + // value is config/swing-store in the home directory. For genesis export, the + // value is always a "swing-store" directory sibling to the exported + // genesis.json file. + // TODO: document this flag in config, likely alongside the genesis path + FlagSwingStoreExportDir = "swing-store-export-dir" + FlagSwingStoreExportMode = "swing-store-export-mode" +) var ( // DefaultNodeHome default home directories for the application daemon @@ -672,6 +675,7 @@ func NewAgoricApp( app.EvidenceKeeper = *evidenceKeeper swingStoreExportDir := cast.ToString(appOpts.Get(FlagSwingStoreExportDir)) + swingStoreExportMode := cast.ToString(appOpts.Get(FlagSwingStoreExportMode)) // NOTE: Any module instantiated in the module manager that is later modified // must be passed by reference here. @@ -701,7 +705,14 @@ func NewAgoricApp( icaModule, packetforward.NewAppModule(app.PacketForwardKeeper), vstorage.NewAppModule(app.VstorageKeeper), - swingset.NewAppModule(app.SwingSetKeeper, &app.SwingStoreExportsHandler, setBootstrapNeeded, app.ensureControllerInited, swingStoreExportDir), + swingset.NewAppModule( + app.SwingSetKeeper, + &app.SwingStoreExportsHandler, + setBootstrapNeeded, + app.ensureControllerInited, + swingStoreExportDir, + swingStoreExportMode, + ), vibcModule, vbankModule, vtransferModule, diff --git a/golang/cosmos/cmd/agd/main.go b/golang/cosmos/cmd/agd/main.go index ced2e520a07..bb6cd5bafb5 100644 --- a/golang/cosmos/cmd/agd/main.go +++ b/golang/cosmos/cmd/agd/main.go @@ -3,6 +3,7 @@ package main import ( "context" "errors" + "fmt" "net/rpc" "net/rpc/jsonrpc" "os" @@ -86,6 +87,7 @@ func main() { args = append(args, os.Args[1:]...) binary := cast.ToString(appOpts.Get(daemoncmd.FlagSplitVm)) + fmt.Println("binary: ", binary, ", binary empty: ", binary == "") if binary == "" { binary, lookErr := FindCosmicSwingsetBinary() if lookErr != nil { @@ -153,5 +155,7 @@ func main() { return launchVM(agdServer, logger, appOpts) } + fmt.Println("RunWithController in main started") daemon.RunWithController(sendToNode) + fmt.Println("RunWithController in main ended") } diff --git a/golang/cosmos/cmd/libdaemon/main.go b/golang/cosmos/cmd/libdaemon/main.go index ee2f5363ab1..85a27663fe6 100644 --- a/golang/cosmos/cmd/libdaemon/main.go +++ b/golang/cosmos/cmd/libdaemon/main.go @@ -12,6 +12,7 @@ import "C" import ( "context" "encoding/json" + "fmt" "net/rpc" "os" "path/filepath" @@ -91,6 +92,7 @@ func RunAgCosmosDaemon(nodePort C.int, toNode C.sendFunc, cosmosArgs []*C.char) // fmt.Fprintln(os.Stderr, "Starting Cosmos", args) os.Args = args + fmt.Println("os.Args: ", os.Args) go func() { // We run in the background, but exit when the job is over. // swingset.SendToNode("hello from Initial Go!") @@ -102,7 +104,9 @@ func RunAgCosmosDaemon(nodePort C.int, toNode C.sendFunc, cosmosArgs []*C.char) exitCode = 99 return nil } + fmt.Println("RunWithController in RunAgCosmosDaemon started") daemon.RunWithController(sendToNode) + fmt.Println("RunWithController in RunAgCosmosDaemon exited") // fmt.Fprintln(os.Stderr, "Shutting down Cosmos") os.Exit(exitCode) }() diff --git a/golang/cosmos/daemon/cmd/root.go b/golang/cosmos/daemon/cmd/root.go index 367cea44c07..daab2eaf87f 100644 --- a/golang/cosmos/daemon/cmd/root.go +++ b/golang/cosmos/daemon/cmd/root.go @@ -386,6 +386,12 @@ const ( ExportedSwingStoreDirectoryName = "swing-store" ) +var allowedSwingSetExportModes = map[string]bool{ + "debug": true, + "operational": true, + "skip": true, +} + // 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 if the VM is running. @@ -396,6 +402,20 @@ func extendCosmosExportCommand(cmd *cobra.Command) { panic(err) } + var keys []string + for key := range allowedSwingSetExportModes { + keys = append(keys, key) + } + + cmd.Flags().String( + gaia.FlagSwingStoreExportMode, + "operational", + fmt.Sprintf( + "The mode for swingstore export (%s)", + strings.Join(keys, " | "), + ), + ) + originalRunE := cmd.RunE extendedRunE := func(cmd *cobra.Command, args []string) error { @@ -464,6 +484,14 @@ func (ac appCreator) appExport( return servertypes.ExportedApp{}, errors.New("application home is not set") } + swingStoreExportMode, ok := appOpts.Get(gaia.FlagSwingStoreExportMode).(string) + if !(ok && allowedSwingSetExportModes[swingStoreExportMode]) { + return servertypes.ExportedApp{}, fmt.Errorf( + "export mode '%s' is not supported", + swingStoreExportMode, + ) + } + var loadLatest bool if height == -1 { loadLatest = true diff --git a/golang/cosmos/daemon/main.go b/golang/cosmos/daemon/main.go index 4ae7e69acaa..caff113f42d 100644 --- a/golang/cosmos/daemon/main.go +++ b/golang/cosmos/daemon/main.go @@ -45,7 +45,10 @@ func RunWithController(sendToController vm.Sender) { config.Seal() rootCmd, _ := cmd.NewRootCmd(sendToController) + fmt.Println("rootCmd.Long", rootCmd.Long) + fmt.Println("rootCmd started") if err := svrcmd.Execute(rootCmd, "", app.DefaultNodeHome); err != nil { + fmt.Println("rootCmd err", err.Error()) switch e := err.(type) { case server.ErrorCode: os.Exit(e.Code) @@ -54,4 +57,5 @@ func RunWithController(sendToController vm.Sender) { os.Exit(1) } } + fmt.Println("rootCmd ended") } diff --git a/golang/cosmos/x/swingset/genesis.go b/golang/cosmos/x/swingset/genesis.go index 56c6e06e5ba..a22c369580a 100644 --- a/golang/cosmos/x/swingset/genesis.go +++ b/golang/cosmos/x/swingset/genesis.go @@ -13,6 +13,7 @@ import ( agoric "github.com/Agoric/agoric-sdk/golang/cosmos/types" "github.com/Agoric/agoric-sdk/golang/cosmos/x/swingset/keeper" "github.com/Agoric/agoric-sdk/golang/cosmos/x/swingset/types" + "github.com/cosmos/cosmos-sdk/server" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -136,6 +137,7 @@ func ExportGenesis(ctx sdk.Context, k Keeper, swingStoreExportsHandler *SwingSto State: k.GetState(ctx), SwingStoreExportData: nil, } + fmt.Println("ctx.Value(server.ServerContextKey): ", ctx.Value(server.ServerContextKey)) snapshotHeight := uint64(ctx.BlockHeight()) diff --git a/golang/cosmos/x/swingset/module.go b/golang/cosmos/x/swingset/module.go index c8c646313f6..ae2223b65a5 100644 --- a/golang/cosmos/x/swingset/module.go +++ b/golang/cosmos/x/swingset/module.go @@ -80,10 +80,18 @@ type AppModule struct { setBootstrapNeeded func() ensureControllerInited func(sdk.Context) swingStoreExportDir string + swingStoreExportMode string } // NewAppModule creates a new AppModule Object -func NewAppModule(k Keeper, swingStoreExportsHandler *SwingStoreExportsHandler, setBootstrapNeeded func(), ensureControllerInited func(sdk.Context), swingStoreExportDir string) AppModule { +func NewAppModule( + k Keeper, + swingStoreExportsHandler *SwingStoreExportsHandler, + setBootstrapNeeded func(), + ensureControllerInited func(sdk.Context), + swingStoreExportDir string, + swingStoreExportMode string, +) AppModule { am := AppModule{ AppModuleBasic: AppModuleBasic{}, keeper: k, @@ -91,6 +99,7 @@ func NewAppModule(k Keeper, swingStoreExportsHandler *SwingStoreExportsHandler, setBootstrapNeeded: setBootstrapNeeded, ensureControllerInited: ensureControllerInited, swingStoreExportDir: swingStoreExportDir, + swingStoreExportMode: swingStoreExportMode, } return am } @@ -173,6 +182,7 @@ func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data json. func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage { am.checkSwingStoreExportSetup() + fmt.Println("am.swingStoreExportMode: ", am.swingStoreExportMode) gs := ExportGenesis(ctx, am.keeper, am.swingStoreExportsHandler, am.swingStoreExportDir) return cdc.MustMarshalJSON(gs) } diff --git a/packages/cosmic-swingset/src/chain-main.js b/packages/cosmic-swingset/src/chain-main.js index 83f38f2aa6a..0576db06529 100644 --- a/packages/cosmic-swingset/src/chain-main.js +++ b/packages/cosmic-swingset/src/chain-main.js @@ -215,6 +215,7 @@ export default async function main(progname, args, { env, homedir, agcc }) { function registerPortHandler(portHandler) { lastPort += 1; const port = lastPort; + console.log(`Registering handler on port ${port}`); portHandlers[port] = async (...phArgs) => E.resolve(portHandler(...phArgs)).catch(e => { console.error('portHandler threw', e); @@ -252,7 +253,9 @@ export default async function main(progname, args, { env, homedir, agcc }) { // Need to keep the process alive until Go exits. whenHellFreezesOver = new Promise(() => {}); + console.log('[JS] runAgCosmosDaemon started'); agcc.runAgCosmosDaemon(nodePort, fromGo, [progname, ...args]); + console.log('[JS] runAgCosmosDaemon finished'); /** * @type {undefined | { @@ -746,6 +749,7 @@ export default async function main(progname, args, { env, homedir, agcc }) { async function toSwingSet(action, _replier) { // console.log(`toSwingSet`, action); + console.log(`toSwingSet running with action ${action.type}`); await null; From e5b1b866f7d04fb9148ba18ed90a66f9406a7013 Mon Sep 17 00:00:00 2001 From: usmanmani1122 Date: Sun, 13 Oct 2024 15:58:53 +0500 Subject: [PATCH 02/10] pass flag value ahead to swingset --- golang/cosmos/x/swingset/genesis.go | 26 +++++++++++++++++++------- golang/cosmos/x/swingset/module.go | 9 +++++++-- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/golang/cosmos/x/swingset/genesis.go b/golang/cosmos/x/swingset/genesis.go index a22c369580a..f384106bb6c 100644 --- a/golang/cosmos/x/swingset/genesis.go +++ b/golang/cosmos/x/swingset/genesis.go @@ -13,7 +13,6 @@ import ( agoric "github.com/Agoric/agoric-sdk/golang/cosmos/types" "github.com/Agoric/agoric-sdk/golang/cosmos/x/swingset/keeper" "github.com/Agoric/agoric-sdk/golang/cosmos/x/swingset/types" - "github.com/cosmos/cosmos-sdk/server" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -131,24 +130,37 @@ func InitGenesis(ctx sdk.Context, k Keeper, swingStoreExportsHandler *SwingStore return false } -func ExportGenesis(ctx sdk.Context, k Keeper, swingStoreExportsHandler *SwingStoreExportsHandler, swingStoreExportDir string) *types.GenesisState { +func ExportGenesis( + ctx sdk.Context, + k Keeper, + swingStoreExportsHandler *SwingStoreExportsHandler, + swingStoreExportDir string, + swingStoreExportMode string, +) *types.GenesisState { gs := &types.GenesisState{ Params: k.GetParams(ctx), State: k.GetState(ctx), SwingStoreExportData: nil, } - fmt.Println("ctx.Value(server.ServerContextKey): ", ctx.Value(server.ServerContextKey)) snapshotHeight := uint64(ctx.BlockHeight()) + if swingStoreExportMode == "debug" { + snapshotHeight = 0 + } - eventHandler := swingStoreGenesisEventHandler{exportDir: swingStoreExportDir, snapshotHeight: snapshotHeight, swingStore: k.GetSwingStore(ctx), hasher: sha256.New()} + eventHandler := swingStoreGenesisEventHandler{ + exportDir: swingStoreExportDir, + snapshotHeight: snapshotHeight, + swingStore: k.GetSwingStore(ctx), + hasher: sha256.New(), + } err := swingStoreExportsHandler.InitiateExport( - // The export will fail if the export of a historical height was requested + // The export will fail if the export of a historical height was requested outside of debug mode snapshotHeight, eventHandler, keeper.SwingStoreExportOptions{ - ArtifactMode: keeper.SwingStoreArtifactModeOperational, + ArtifactMode: swingStoreExportMode, ExportDataMode: keeper.SwingStoreExportDataModeSkip, }, ) @@ -178,7 +190,7 @@ func (eventHandler swingStoreGenesisEventHandler) OnExportStarted(height uint64, } func (eventHandler swingStoreGenesisEventHandler) OnExportRetrieved(provider keeper.SwingStoreExportProvider) error { - if eventHandler.snapshotHeight != provider.BlockHeight { + if !(eventHandler.snapshotHeight == 0 || eventHandler.snapshotHeight == provider.BlockHeight) { return fmt.Errorf("snapshot block height (%d) doesn't match requested height (%d)", provider.BlockHeight, eventHandler.snapshotHeight) } diff --git a/golang/cosmos/x/swingset/module.go b/golang/cosmos/x/swingset/module.go index ae2223b65a5..6a3465e88bf 100644 --- a/golang/cosmos/x/swingset/module.go +++ b/golang/cosmos/x/swingset/module.go @@ -182,7 +182,12 @@ func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data json. func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage { am.checkSwingStoreExportSetup() - fmt.Println("am.swingStoreExportMode: ", am.swingStoreExportMode) - gs := ExportGenesis(ctx, am.keeper, am.swingStoreExportsHandler, am.swingStoreExportDir) + gs := ExportGenesis( + ctx, + am.keeper, + am.swingStoreExportsHandler, + am.swingStoreExportDir, + am.swingStoreExportMode, + ) return cdc.MustMarshalJSON(gs) } From 10cfea3115c4cc466558e937a563ab7f0274d647 Mon Sep 17 00:00:00 2001 From: usmanmani1122 Date: Mon, 28 Oct 2024 18:19:58 +0500 Subject: [PATCH 03/10] handle export data modes --- golang/cosmos/daemon/cmd/root.go | 8 ++-- golang/cosmos/x/swingset/genesis.go | 74 +++++++++++++++++++++++------ 2 files changed, 64 insertions(+), 18 deletions(-) diff --git a/golang/cosmos/daemon/cmd/root.go b/golang/cosmos/daemon/cmd/root.go index daab2eaf87f..62708cb3c4f 100644 --- a/golang/cosmos/daemon/cmd/root.go +++ b/golang/cosmos/daemon/cmd/root.go @@ -387,9 +387,9 @@ const ( ) var allowedSwingSetExportModes = map[string]bool{ - "debug": true, - "operational": true, - "skip": true, + swingsetkeeper.SwingStoreArtifactModeDebug: true, + swingsetkeeper.SwingStoreArtifactModeOperational: true, + swingsetkeeper.SwingStoreArtifactModeNone: true, } // extendCosmosExportCommand monkey-patches the "export" command added by @@ -409,7 +409,7 @@ func extendCosmosExportCommand(cmd *cobra.Command) { cmd.Flags().String( gaia.FlagSwingStoreExportMode, - "operational", + swingsetkeeper.SwingStoreArtifactModeOperational, fmt.Sprintf( "The mode for swingstore export (%s)", strings.Join(keys, " | "), diff --git a/golang/cosmos/x/swingset/genesis.go b/golang/cosmos/x/swingset/genesis.go index f384106bb6c..49d2bede114 100644 --- a/golang/cosmos/x/swingset/genesis.go +++ b/golang/cosmos/x/swingset/genesis.go @@ -1,13 +1,13 @@ package swingset import ( - // "os" "bytes" "crypto/sha256" "encoding/hex" "encoding/json" "fmt" "hash" + "io" "strings" agoric "github.com/Agoric/agoric-sdk/golang/cosmos/types" @@ -153,6 +153,12 @@ func ExportGenesis( snapshotHeight: snapshotHeight, swingStore: k.GetSwingStore(ctx), hasher: sha256.New(), + exportMode: swingStoreExportMode, + } + + exportDataMode := keeper.SwingStoreExportDataModeSkip + if swingStoreExportMode == keeper.SwingStoreArtifactModeDebug { + exportDataMode = keeper.SwingStoreExportDataModeAll } err := swingStoreExportsHandler.InitiateExport( @@ -161,7 +167,7 @@ func ExportGenesis( eventHandler, keeper.SwingStoreExportOptions{ ArtifactMode: swingStoreExportMode, - ExportDataMode: keeper.SwingStoreExportDataModeSkip, + ExportDataMode: exportDataMode, }, ) if err != nil { @@ -183,6 +189,7 @@ type swingStoreGenesisEventHandler struct { snapshotHeight uint64 swingStore sdk.KVStore hasher hash.Hash + exportMode string } func (eventHandler swingStoreGenesisEventHandler) OnExportStarted(height uint64, retrieveSwingStoreExport func() error) error { @@ -194,21 +201,60 @@ func (eventHandler swingStoreGenesisEventHandler) OnExportRetrieved(provider kee return fmt.Errorf("snapshot block height (%d) doesn't match requested height (%d)", provider.BlockHeight, eventHandler.snapshotHeight) } + artifactsEnded := false + + var getExportDataReader = func() (agoric.KVEntryReader, error) { + exportDataIterator := eventHandler.swingStore.Iterator(nil, nil) + kvReader := agoric.NewKVIteratorReader(exportDataIterator) + eventHandler.hasher.Reset() + encoder := json.NewEncoder(eventHandler.hasher) + encoder.SetEscapeHTML(false) + + return agoric.NewKVHookingReader(kvReader, func(entry agoric.KVEntry) error { + return encoder.Encode(entry) + }, func() error { + return nil + }), nil + } + artifactsProvider := keeper.SwingStoreExportProvider{ - GetExportDataReader: func() (agoric.KVEntryReader, error) { - exportDataIterator := eventHandler.swingStore.Iterator(nil, nil) - kvReader := agoric.NewKVIteratorReader(exportDataIterator) - eventHandler.hasher.Reset() - encoder := json.NewEncoder(eventHandler.hasher) - encoder.SetEscapeHTML(false) + GetExportDataReader: getExportDataReader, + ReadNextArtifact: func() (types.SwingStoreArtifact, error) { + var ( + artifact types.SwingStoreArtifact + err error + encodedExportData bytes.Buffer + ) - return agoric.NewKVHookingReader(kvReader, func(entry agoric.KVEntry) error { - return encoder.Encode(entry) - }, func() error { - return nil - }), nil + if !artifactsEnded { + artifact, err = provider.ReadNextArtifact() + } else { + return types.SwingStoreArtifact{}, io.EOF + } + + if err == io.EOF { + artifactsEnded = true + if eventHandler.exportMode == keeper.SwingStoreArtifactModeDebug { + err = nil + exportDataReader, err := provider.GetExportDataReader() + + if err == nil { + err = agoric.EncodeKVEntryReaderToJsonl( + exportDataReader, + &encodedExportData, + ) + if err == nil { + artifact = types.SwingStoreArtifact{ + Data: encodedExportData.Bytes(), + Name: keeper.UntrustedExportDataArtifactName, + } + } + } + } + } + + return artifact, err }, - ReadNextArtifact: provider.ReadNextArtifact, } return keeper.WriteSwingStoreExportToDirectory(artifactsProvider, eventHandler.exportDir) From c1d3cdb5dd0d5cb44e3a6bae43a091f5771a384b Mon Sep 17 00:00:00 2001 From: usmanmani1122 Date: Mon, 28 Oct 2024 18:24:56 +0500 Subject: [PATCH 04/10] revert logs --- golang/cosmos/cmd/agd/main.go | 4 ---- golang/cosmos/cmd/libdaemon/main.go | 4 ---- golang/cosmos/daemon/main.go | 4 ---- golang/cosmos/x/swingset/genesis.go | 10 ++++------ packages/cosmic-swingset/src/chain-main.js | 4 ---- 5 files changed, 4 insertions(+), 22 deletions(-) diff --git a/golang/cosmos/cmd/agd/main.go b/golang/cosmos/cmd/agd/main.go index bb6cd5bafb5..ced2e520a07 100644 --- a/golang/cosmos/cmd/agd/main.go +++ b/golang/cosmos/cmd/agd/main.go @@ -3,7 +3,6 @@ package main import ( "context" "errors" - "fmt" "net/rpc" "net/rpc/jsonrpc" "os" @@ -87,7 +86,6 @@ func main() { args = append(args, os.Args[1:]...) binary := cast.ToString(appOpts.Get(daemoncmd.FlagSplitVm)) - fmt.Println("binary: ", binary, ", binary empty: ", binary == "") if binary == "" { binary, lookErr := FindCosmicSwingsetBinary() if lookErr != nil { @@ -155,7 +153,5 @@ func main() { return launchVM(agdServer, logger, appOpts) } - fmt.Println("RunWithController in main started") daemon.RunWithController(sendToNode) - fmt.Println("RunWithController in main ended") } diff --git a/golang/cosmos/cmd/libdaemon/main.go b/golang/cosmos/cmd/libdaemon/main.go index 85a27663fe6..ee2f5363ab1 100644 --- a/golang/cosmos/cmd/libdaemon/main.go +++ b/golang/cosmos/cmd/libdaemon/main.go @@ -12,7 +12,6 @@ import "C" import ( "context" "encoding/json" - "fmt" "net/rpc" "os" "path/filepath" @@ -92,7 +91,6 @@ func RunAgCosmosDaemon(nodePort C.int, toNode C.sendFunc, cosmosArgs []*C.char) // fmt.Fprintln(os.Stderr, "Starting Cosmos", args) os.Args = args - fmt.Println("os.Args: ", os.Args) go func() { // We run in the background, but exit when the job is over. // swingset.SendToNode("hello from Initial Go!") @@ -104,9 +102,7 @@ func RunAgCosmosDaemon(nodePort C.int, toNode C.sendFunc, cosmosArgs []*C.char) exitCode = 99 return nil } - fmt.Println("RunWithController in RunAgCosmosDaemon started") daemon.RunWithController(sendToNode) - fmt.Println("RunWithController in RunAgCosmosDaemon exited") // fmt.Fprintln(os.Stderr, "Shutting down Cosmos") os.Exit(exitCode) }() diff --git a/golang/cosmos/daemon/main.go b/golang/cosmos/daemon/main.go index caff113f42d..4ae7e69acaa 100644 --- a/golang/cosmos/daemon/main.go +++ b/golang/cosmos/daemon/main.go @@ -45,10 +45,7 @@ func RunWithController(sendToController vm.Sender) { config.Seal() rootCmd, _ := cmd.NewRootCmd(sendToController) - fmt.Println("rootCmd.Long", rootCmd.Long) - fmt.Println("rootCmd started") if err := svrcmd.Execute(rootCmd, "", app.DefaultNodeHome); err != nil { - fmt.Println("rootCmd err", err.Error()) switch e := err.(type) { case server.ErrorCode: os.Exit(e.Code) @@ -57,5 +54,4 @@ func RunWithController(sendToController vm.Sender) { os.Exit(1) } } - fmt.Println("rootCmd ended") } diff --git a/golang/cosmos/x/swingset/genesis.go b/golang/cosmos/x/swingset/genesis.go index 49d2bede114..c50e726c733 100644 --- a/golang/cosmos/x/swingset/genesis.go +++ b/golang/cosmos/x/swingset/genesis.go @@ -143,8 +143,11 @@ func ExportGenesis( SwingStoreExportData: nil, } + exportDataMode := keeper.SwingStoreExportDataModeSkip snapshotHeight := uint64(ctx.BlockHeight()) - if swingStoreExportMode == "debug" { + + if swingStoreExportMode == keeper.SwingStoreArtifactModeDebug { + exportDataMode = keeper.SwingStoreExportDataModeAll snapshotHeight = 0 } @@ -156,11 +159,6 @@ func ExportGenesis( exportMode: swingStoreExportMode, } - exportDataMode := keeper.SwingStoreExportDataModeSkip - if swingStoreExportMode == keeper.SwingStoreArtifactModeDebug { - exportDataMode = keeper.SwingStoreExportDataModeAll - } - err := swingStoreExportsHandler.InitiateExport( // The export will fail if the export of a historical height was requested outside of debug mode snapshotHeight, diff --git a/packages/cosmic-swingset/src/chain-main.js b/packages/cosmic-swingset/src/chain-main.js index 6dc7cab3484..10b669da82a 100644 --- a/packages/cosmic-swingset/src/chain-main.js +++ b/packages/cosmic-swingset/src/chain-main.js @@ -215,7 +215,6 @@ export default async function main(progname, args, { env, homedir, agcc }) { function registerPortHandler(portHandler) { lastPort += 1; const port = lastPort; - console.log(`Registering handler on port ${port}`); portHandlers[port] = async (...phArgs) => E.resolve(portHandler(...phArgs)).catch(e => { console.error('portHandler threw', e); @@ -253,9 +252,7 @@ export default async function main(progname, args, { env, homedir, agcc }) { // Need to keep the process alive until Go exits. whenHellFreezesOver = new Promise(() => {}); - console.log('[JS] runAgCosmosDaemon started'); agcc.runAgCosmosDaemon(nodePort, fromGo, [progname, ...args]); - console.log('[JS] runAgCosmosDaemon finished'); /** * @type {undefined | { @@ -749,7 +746,6 @@ export default async function main(progname, args, { env, homedir, agcc }) { async function toSwingSet(action, _replier) { // console.log(`toSwingSet`, action); - console.log(`toSwingSet running with action ${action.type}`); await null; From 8811fc0c264775209999aef8dd1526db86e65293 Mon Sep 17 00:00:00 2001 From: usmanmani1122 Date: Tue, 29 Oct 2024 01:51:38 +0500 Subject: [PATCH 05/10] nit --- golang/cosmos/x/swingset/genesis.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/golang/cosmos/x/swingset/genesis.go b/golang/cosmos/x/swingset/genesis.go index c50e726c733..f00cc2e4baa 100644 --- a/golang/cosmos/x/swingset/genesis.go +++ b/golang/cosmos/x/swingset/genesis.go @@ -195,7 +195,8 @@ func (eventHandler swingStoreGenesisEventHandler) OnExportStarted(height uint64, } func (eventHandler swingStoreGenesisEventHandler) OnExportRetrieved(provider keeper.SwingStoreExportProvider) error { - if !(eventHandler.snapshotHeight == 0 || eventHandler.snapshotHeight == provider.BlockHeight) { + if !((eventHandler.exportMode == keeper.SwingStoreArtifactModeDebug && eventHandler.snapshotHeight == 0) || + eventHandler.snapshotHeight == provider.BlockHeight) { return fmt.Errorf("snapshot block height (%d) doesn't match requested height (%d)", provider.BlockHeight, eventHandler.snapshotHeight) } @@ -234,7 +235,7 @@ func (eventHandler swingStoreGenesisEventHandler) OnExportRetrieved(provider kee artifactsEnded = true if eventHandler.exportMode == keeper.SwingStoreArtifactModeDebug { err = nil - exportDataReader, err := provider.GetExportDataReader() + exportDataReader, err := getExportDataReader() if err == nil { err = agoric.EncodeKVEntryReaderToJsonl( From 1ad8be5a7bb753b91b01a3b6cb2c20a8893145f8 Mon Sep 17 00:00:00 2001 From: usmanmani1122 Date: Thu, 31 Oct 2024 16:44:04 +0500 Subject: [PATCH 06/10] disable vm launch on skip mode --- golang/cosmos/daemon/cmd/root.go | 53 +++++++++-------- golang/cosmos/x/swingset/genesis.go | 57 ++++++++++--------- .../keeper/swing_store_exports_handler.go | 2 +- 3 files changed, 60 insertions(+), 52 deletions(-) diff --git a/golang/cosmos/daemon/cmd/root.go b/golang/cosmos/daemon/cmd/root.go index 62708cb3c4f..cf664db856f 100644 --- a/golang/cosmos/daemon/cmd/root.go +++ b/golang/cosmos/daemon/cmd/root.go @@ -389,7 +389,7 @@ const ( var allowedSwingSetExportModes = map[string]bool{ swingsetkeeper.SwingStoreArtifactModeDebug: true, swingsetkeeper.SwingStoreArtifactModeOperational: true, - swingsetkeeper.SwingStoreArtifactModeNone: true, + swingsetkeeper.SwingStoreArtifactModeSkip: true, } // extendCosmosExportCommand monkey-patches the "export" command added by @@ -422,25 +422,32 @@ func extendCosmosExportCommand(cmd *cobra.Command) { serverCtx := server.GetServerContextFromCmd(cmd) exportDir, _ := cmd.Flags().GetString(FlagExportDir) + swingStoreExportMode, _ := cmd.Flags().GetString(gaia.FlagSwingStoreExportMode) + 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 + // Since skip mode doesn't perform any swing store export + // There is no point in creating the export directory + if swingStoreExportMode != swingsetkeeper.SwingStoreArtifactModeSkip { + 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) } - // 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) - if hasVMController(serverCtx) { + if hasVMController(serverCtx) || swingStoreExportMode == swingsetkeeper.SwingStoreArtifactModeSkip { // Capture the export in the genesisPath. // This will fail if a genesis.json already exists in the export-dir genesisFile, err := os.OpenFile( @@ -473,7 +480,16 @@ func (ac appCreator) appExport( jailAllowedAddrs []string, appOpts servertypes.AppOptions, ) (servertypes.ExportedApp, error) { - if OnExportHook != nil { + swingStoreExportMode, ok := appOpts.Get(gaia.FlagSwingStoreExportMode).(string) + if !(ok && allowedSwingSetExportModes[swingStoreExportMode]) { + return servertypes.ExportedApp{}, fmt.Errorf( + "export mode '%s' is not supported", + swingStoreExportMode, + ) + } + + // We don't have to launch VM in case the swing store export is not required + if !(swingStoreExportMode == swingsetkeeper.SwingStoreArtifactModeSkip || OnExportHook == nil) { if err := OnExportHook(ac.agdServer, logger, appOpts); err != nil { return servertypes.ExportedApp{}, err } @@ -484,18 +500,7 @@ func (ac appCreator) appExport( return servertypes.ExportedApp{}, errors.New("application home is not set") } - swingStoreExportMode, ok := appOpts.Get(gaia.FlagSwingStoreExportMode).(string) - if !(ok && allowedSwingSetExportModes[swingStoreExportMode]) { - return servertypes.ExportedApp{}, fmt.Errorf( - "export mode '%s' is not supported", - swingStoreExportMode, - ) - } - - var loadLatest bool - if height == -1 { - loadLatest = true - } + loadLatest := height == -1 gaiaApp := gaia.NewAgoricApp( ac.sender, ac.agdServer, diff --git a/golang/cosmos/x/swingset/genesis.go b/golang/cosmos/x/swingset/genesis.go index f00cc2e4baa..483545947ec 100644 --- a/golang/cosmos/x/swingset/genesis.go +++ b/golang/cosmos/x/swingset/genesis.go @@ -138,9 +138,10 @@ func ExportGenesis( swingStoreExportMode string, ) *types.GenesisState { gs := &types.GenesisState{ - Params: k.GetParams(ctx), - State: k.GetState(ctx), - SwingStoreExportData: nil, + Params: k.GetParams(ctx), + State: k.GetState(ctx), + SwingStoreExportData: nil, + SwingStoreExportDataHash: "", } exportDataMode := keeper.SwingStoreExportDataModeSkip @@ -151,33 +152,35 @@ func ExportGenesis( snapshotHeight = 0 } - eventHandler := swingStoreGenesisEventHandler{ - exportDir: swingStoreExportDir, - snapshotHeight: snapshotHeight, - swingStore: k.GetSwingStore(ctx), - hasher: sha256.New(), - exportMode: swingStoreExportMode, - } + if swingStoreExportMode != keeper.SwingStoreArtifactModeSkip { + eventHandler := swingStoreGenesisEventHandler{ + exportDir: swingStoreExportDir, + snapshotHeight: snapshotHeight, + swingStore: k.GetSwingStore(ctx), + hasher: sha256.New(), + exportMode: swingStoreExportMode, + } - err := swingStoreExportsHandler.InitiateExport( - // The export will fail if the export of a historical height was requested outside of debug mode - snapshotHeight, - eventHandler, - keeper.SwingStoreExportOptions{ - ArtifactMode: swingStoreExportMode, - ExportDataMode: exportDataMode, - }, - ) - if err != nil { - panic(err) - } + err := swingStoreExportsHandler.InitiateExport( + // The export will fail if the export of a historical height was requested outside of debug mode + snapshotHeight, + eventHandler, + keeper.SwingStoreExportOptions{ + ArtifactMode: swingStoreExportMode, + ExportDataMode: exportDataMode, + }, + ) + if err != nil { + panic(err) + } - err = keeper.WaitUntilSwingStoreExportDone() - if err != nil { - panic(err) - } + err = keeper.WaitUntilSwingStoreExportDone() + if err != nil { + panic(err) + } - gs.SwingStoreExportDataHash = fmt.Sprintf("sha256:%x", eventHandler.hasher.Sum(nil)) + gs.SwingStoreExportDataHash = fmt.Sprintf("sha256:%x", eventHandler.hasher.Sum(nil)) + } return gs } diff --git a/golang/cosmos/x/swingset/keeper/swing_store_exports_handler.go b/golang/cosmos/x/swingset/keeper/swing_store_exports_handler.go index 0a1fe798f27..e9608bf2360 100644 --- a/golang/cosmos/x/swingset/keeper/swing_store_exports_handler.go +++ b/golang/cosmos/x/swingset/keeper/swing_store_exports_handler.go @@ -160,7 +160,7 @@ type swingStoreRestoreExportAction struct { const ( // SwingStoreArtifactModeNone means that no artifacts are part of the // export / import. - SwingStoreArtifactModeNone = "none" + SwingStoreArtifactModeSkip = "skip" // SwingStoreArtifactModeOperational represents the minimal set of artifacts // needed to operate a node. From 3928c4ad860357afb5061cd8e78d85ea8da9c4a8 Mon Sep 17 00:00:00 2001 From: usmanmani1122 Date: Sat, 2 Nov 2024 13:23:14 +0500 Subject: [PATCH 07/10] close reade --- golang/cosmos/x/swingset/genesis.go | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/golang/cosmos/x/swingset/genesis.go b/golang/cosmos/x/swingset/genesis.go index 483545947ec..f571f496824 100644 --- a/golang/cosmos/x/swingset/genesis.go +++ b/golang/cosmos/x/swingset/genesis.go @@ -237,19 +237,18 @@ func (eventHandler swingStoreGenesisEventHandler) OnExportRetrieved(provider kee if err == io.EOF { artifactsEnded = true if eventHandler.exportMode == keeper.SwingStoreArtifactModeDebug { - err = nil - exportDataReader, err := getExportDataReader() + exportDataReader, _ := getExportDataReader() + defer exportDataReader.Close() + + err = agoric.EncodeKVEntryReaderToJsonl( + exportDataReader, + &encodedExportData, + ) if err == nil { - err = agoric.EncodeKVEntryReaderToJsonl( - exportDataReader, - &encodedExportData, - ) - if err == nil { - artifact = types.SwingStoreArtifact{ - Data: encodedExportData.Bytes(), - Name: keeper.UntrustedExportDataArtifactName, - } + artifact = types.SwingStoreArtifact{ + Data: encodedExportData.Bytes(), + Name: keeper.UntrustedExportDataArtifactName, } } } From 483c56650e5133038f7d4e321a9eff3ada0802b1 Mon Sep 17 00:00:00 2001 From: usmanmani1122 Date: Mon, 11 Nov 2024 10:06:50 +0000 Subject: [PATCH 08/10] addressed Mathieu comments --- golang/cosmos/app/app.go | 8 ++- golang/cosmos/daemon/cmd/root.go | 10 ++-- golang/cosmos/x/swingset/genesis.go | 59 ++++++++++--------- .../keeper/swing_store_exports_handler.go | 2 +- 4 files changed, 44 insertions(+), 35 deletions(-) diff --git a/golang/cosmos/app/app.go b/golang/cosmos/app/app.go index 0778544177c..65f1251ce6c 100644 --- a/golang/cosmos/app/app.go +++ b/golang/cosmos/app/app.go @@ -144,7 +144,13 @@ const ( // value is always a "swing-store" directory sibling to the exported // genesis.json file. // TODO: document this flag in config, likely alongside the genesis path - FlagSwingStoreExportDir = "swing-store-export-dir" + FlagSwingStoreExportDir = "swing-store-export-dir" + // FlagSwingStoreExportMode defines the export mode for the swing store + // Alongside the default mode `operational`, there are two other modes + // + // 1- `skip` mode will skip the swing store export altogether + // + // 2- `debug` mode will export the complete store, starting from height zero FlagSwingStoreExportMode = "swing-store-export-mode" ) diff --git a/golang/cosmos/daemon/cmd/root.go b/golang/cosmos/daemon/cmd/root.go index cf664db856f..baa8b2d61e4 100644 --- a/golang/cosmos/daemon/cmd/root.go +++ b/golang/cosmos/daemon/cmd/root.go @@ -388,8 +388,8 @@ const ( var allowedSwingSetExportModes = map[string]bool{ swingsetkeeper.SwingStoreArtifactModeDebug: true, + swingsetkeeper.SwingStoreArtifactModeNone: true, swingsetkeeper.SwingStoreArtifactModeOperational: true, - swingsetkeeper.SwingStoreArtifactModeSkip: true, } // extendCosmosExportCommand monkey-patches the "export" command added by @@ -431,9 +431,9 @@ func extendCosmosExportCommand(cmd *cobra.Command) { genesisPath := filepath.Join(exportDir, ExportedGenesisFileName) - // Since skip mode doesn't perform any swing store export + // Since none mode doesn't perform any swing store export // There is no point in creating the export directory - if swingStoreExportMode != swingsetkeeper.SwingStoreArtifactModeSkip { + if swingStoreExportMode != swingsetkeeper.SwingStoreArtifactModeNone { swingStoreExportPath := filepath.Join(exportDir, ExportedSwingStoreDirectoryName) err = os.MkdirAll(swingStoreExportPath, os.ModePerm) @@ -447,7 +447,7 @@ func extendCosmosExportCommand(cmd *cobra.Command) { serverCtx.Viper.Set(gaia.FlagSwingStoreExportDir, swingStoreExportPath) } - if hasVMController(serverCtx) || swingStoreExportMode == swingsetkeeper.SwingStoreArtifactModeSkip { + if hasVMController(serverCtx) || swingStoreExportMode == swingsetkeeper.SwingStoreArtifactModeNone { // Capture the export in the genesisPath. // This will fail if a genesis.json already exists in the export-dir genesisFile, err := os.OpenFile( @@ -489,7 +489,7 @@ func (ac appCreator) appExport( } // We don't have to launch VM in case the swing store export is not required - if !(swingStoreExportMode == swingsetkeeper.SwingStoreArtifactModeSkip || OnExportHook == nil) { + if swingStoreExportMode != swingsetkeeper.SwingStoreArtifactModeNone && OnExportHook != nil { if err := OnExportHook(ac.agdServer, logger, appOpts); err != nil { return servertypes.ExportedApp{}, err } diff --git a/golang/cosmos/x/swingset/genesis.go b/golang/cosmos/x/swingset/genesis.go index f571f496824..573e6abfd92 100644 --- a/golang/cosmos/x/swingset/genesis.go +++ b/golang/cosmos/x/swingset/genesis.go @@ -138,26 +138,28 @@ func ExportGenesis( swingStoreExportMode string, ) *types.GenesisState { gs := &types.GenesisState{ - Params: k.GetParams(ctx), - State: k.GetState(ctx), - SwingStoreExportData: nil, - SwingStoreExportDataHash: "", + Params: k.GetParams(ctx), + State: k.GetState(ctx), + SwingStoreExportData: nil, } + // This will only be used in non skip mode + artifactMode := swingStoreExportMode exportDataMode := keeper.SwingStoreExportDataModeSkip + hasher := sha256.New() snapshotHeight := uint64(ctx.BlockHeight()) - if swingStoreExportMode == keeper.SwingStoreArtifactModeDebug { + if artifactMode == keeper.SwingStoreArtifactModeDebug { exportDataMode = keeper.SwingStoreExportDataModeAll snapshotHeight = 0 } - if swingStoreExportMode != keeper.SwingStoreArtifactModeSkip { + if artifactMode != keeper.SwingStoreArtifactModeNone { eventHandler := swingStoreGenesisEventHandler{ exportDir: swingStoreExportDir, snapshotHeight: snapshotHeight, swingStore: k.GetSwingStore(ctx), - hasher: sha256.New(), + hasher: hasher, exportMode: swingStoreExportMode, } @@ -166,7 +168,7 @@ func ExportGenesis( snapshotHeight, eventHandler, keeper.SwingStoreExportOptions{ - ArtifactMode: swingStoreExportMode, + ArtifactMode: artifactMode, ExportDataMode: exportDataMode, }, ) @@ -178,10 +180,10 @@ func ExportGenesis( if err != nil { panic(err) } - - gs.SwingStoreExportDataHash = fmt.Sprintf("sha256:%x", eventHandler.hasher.Sum(nil)) } + gs.SwingStoreExportDataHash = fmt.Sprintf("sha256:%x", hasher.Sum(nil)) + return gs } @@ -198,29 +200,26 @@ func (eventHandler swingStoreGenesisEventHandler) OnExportStarted(height uint64, } func (eventHandler swingStoreGenesisEventHandler) OnExportRetrieved(provider keeper.SwingStoreExportProvider) error { - if !((eventHandler.exportMode == keeper.SwingStoreArtifactModeDebug && eventHandler.snapshotHeight == 0) || - eventHandler.snapshotHeight == provider.BlockHeight) { + if eventHandler.exportMode != keeper.SwingStoreArtifactModeDebug && eventHandler.snapshotHeight != provider.BlockHeight { return fmt.Errorf("snapshot block height (%d) doesn't match requested height (%d)", provider.BlockHeight, eventHandler.snapshotHeight) } artifactsEnded := false - var getExportDataReader = func() (agoric.KVEntryReader, error) { - exportDataIterator := eventHandler.swingStore.Iterator(nil, nil) - kvReader := agoric.NewKVIteratorReader(exportDataIterator) - eventHandler.hasher.Reset() - encoder := json.NewEncoder(eventHandler.hasher) - encoder.SetEscapeHTML(false) - - return agoric.NewKVHookingReader(kvReader, func(entry agoric.KVEntry) error { - return encoder.Encode(entry) - }, func() error { - return nil - }), nil - } - artifactsProvider := keeper.SwingStoreExportProvider{ - GetExportDataReader: getExportDataReader, + GetExportDataReader: func() (agoric.KVEntryReader, error) { + exportDataIterator := eventHandler.swingStore.Iterator(nil, nil) + kvReader := agoric.NewKVIteratorReader(exportDataIterator) + eventHandler.hasher.Reset() + encoder := json.NewEncoder(eventHandler.hasher) + encoder.SetEscapeHTML(false) + + return agoric.NewKVHookingReader(kvReader, func(entry agoric.KVEntry) error { + return encoder.Encode(entry) + }, func() error { + return nil + }), nil + }, ReadNextArtifact: func() (types.SwingStoreArtifact, error) { var ( artifact types.SwingStoreArtifact @@ -237,7 +236,7 @@ func (eventHandler swingStoreGenesisEventHandler) OnExportRetrieved(provider kee if err == io.EOF { artifactsEnded = true if eventHandler.exportMode == keeper.SwingStoreArtifactModeDebug { - exportDataReader, _ := getExportDataReader() + exportDataReader, _ := provider.GetExportDataReader() defer exportDataReader.Close() @@ -258,5 +257,9 @@ func (eventHandler swingStoreGenesisEventHandler) OnExportRetrieved(provider kee }, } + if eventHandler.exportMode == keeper.SwingStoreArtifactModeDebug { + artifactsProvider.BlockHeight = provider.BlockHeight + } + return keeper.WriteSwingStoreExportToDirectory(artifactsProvider, eventHandler.exportDir) } diff --git a/golang/cosmos/x/swingset/keeper/swing_store_exports_handler.go b/golang/cosmos/x/swingset/keeper/swing_store_exports_handler.go index e9608bf2360..0a1fe798f27 100644 --- a/golang/cosmos/x/swingset/keeper/swing_store_exports_handler.go +++ b/golang/cosmos/x/swingset/keeper/swing_store_exports_handler.go @@ -160,7 +160,7 @@ type swingStoreRestoreExportAction struct { const ( // SwingStoreArtifactModeNone means that no artifacts are part of the // export / import. - SwingStoreArtifactModeSkip = "skip" + SwingStoreArtifactModeNone = "none" // SwingStoreArtifactModeOperational represents the minimal set of artifacts // needed to operate a node. From 530a2b543e045946d3c1c05b4f93fa1ddfd3a648 Mon Sep 17 00:00:00 2001 From: usmanmani1122 Date: Tue, 12 Nov 2024 07:56:33 +0000 Subject: [PATCH 09/10] use new constants --- golang/cosmos/app/app.go | 2 +- golang/cosmos/daemon/cmd/root.go | 14 +++++++------- golang/cosmos/x/swingset/genesis.go | 10 +++++----- .../swingset/keeper/swing_store_exports_handler.go | 14 ++++++++++++++ 4 files changed, 27 insertions(+), 13 deletions(-) diff --git a/golang/cosmos/app/app.go b/golang/cosmos/app/app.go index 65f1251ce6c..6f2d7d225b9 100644 --- a/golang/cosmos/app/app.go +++ b/golang/cosmos/app/app.go @@ -150,7 +150,7 @@ const ( // // 1- `skip` mode will skip the swing store export altogether // - // 2- `debug` mode will export the complete store, starting from height zero + // 2- `debug` mode will export all the available store FlagSwingStoreExportMode = "swing-store-export-mode" ) diff --git a/golang/cosmos/daemon/cmd/root.go b/golang/cosmos/daemon/cmd/root.go index baa8b2d61e4..29063763656 100644 --- a/golang/cosmos/daemon/cmd/root.go +++ b/golang/cosmos/daemon/cmd/root.go @@ -387,9 +387,9 @@ const ( ) var allowedSwingSetExportModes = map[string]bool{ - swingsetkeeper.SwingStoreArtifactModeDebug: true, - swingsetkeeper.SwingStoreArtifactModeNone: true, - swingsetkeeper.SwingStoreArtifactModeOperational: true, + swingsetkeeper.SwingStoreExportModeDebug: true, + swingsetkeeper.SwingStoreExportModeOperational: true, + swingsetkeeper.SwingStoreExportModeSkip: true, } // extendCosmosExportCommand monkey-patches the "export" command added by @@ -409,7 +409,7 @@ func extendCosmosExportCommand(cmd *cobra.Command) { cmd.Flags().String( gaia.FlagSwingStoreExportMode, - swingsetkeeper.SwingStoreArtifactModeOperational, + swingsetkeeper.SwingStoreExportModeOperational, fmt.Sprintf( "The mode for swingstore export (%s)", strings.Join(keys, " | "), @@ -433,7 +433,7 @@ func extendCosmosExportCommand(cmd *cobra.Command) { // Since none mode doesn't perform any swing store export // There is no point in creating the export directory - if swingStoreExportMode != swingsetkeeper.SwingStoreArtifactModeNone { + if swingStoreExportMode != swingsetkeeper.SwingStoreExportModeSkip { swingStoreExportPath := filepath.Join(exportDir, ExportedSwingStoreDirectoryName) err = os.MkdirAll(swingStoreExportPath, os.ModePerm) @@ -447,7 +447,7 @@ func extendCosmosExportCommand(cmd *cobra.Command) { serverCtx.Viper.Set(gaia.FlagSwingStoreExportDir, swingStoreExportPath) } - if hasVMController(serverCtx) || swingStoreExportMode == swingsetkeeper.SwingStoreArtifactModeNone { + if hasVMController(serverCtx) || swingStoreExportMode == swingsetkeeper.SwingStoreExportModeSkip { // Capture the export in the genesisPath. // This will fail if a genesis.json already exists in the export-dir genesisFile, err := os.OpenFile( @@ -489,7 +489,7 @@ func (ac appCreator) appExport( } // We don't have to launch VM in case the swing store export is not required - if swingStoreExportMode != swingsetkeeper.SwingStoreArtifactModeNone && OnExportHook != nil { + if swingStoreExportMode != swingsetkeeper.SwingStoreExportModeSkip && OnExportHook != nil { if err := OnExportHook(ac.agdServer, logger, appOpts); err != nil { return servertypes.ExportedApp{}, err } diff --git a/golang/cosmos/x/swingset/genesis.go b/golang/cosmos/x/swingset/genesis.go index 573e6abfd92..e5a8d34d44e 100644 --- a/golang/cosmos/x/swingset/genesis.go +++ b/golang/cosmos/x/swingset/genesis.go @@ -149,12 +149,12 @@ func ExportGenesis( hasher := sha256.New() snapshotHeight := uint64(ctx.BlockHeight()) - if artifactMode == keeper.SwingStoreArtifactModeDebug { + if swingStoreExportMode == keeper.SwingStoreExportModeDebug { exportDataMode = keeper.SwingStoreExportDataModeAll snapshotHeight = 0 } - if artifactMode != keeper.SwingStoreArtifactModeNone { + if swingStoreExportMode != keeper.SwingStoreExportModeSkip { eventHandler := swingStoreGenesisEventHandler{ exportDir: swingStoreExportDir, snapshotHeight: snapshotHeight, @@ -200,7 +200,7 @@ func (eventHandler swingStoreGenesisEventHandler) OnExportStarted(height uint64, } func (eventHandler swingStoreGenesisEventHandler) OnExportRetrieved(provider keeper.SwingStoreExportProvider) error { - if eventHandler.exportMode != keeper.SwingStoreArtifactModeDebug && eventHandler.snapshotHeight != provider.BlockHeight { + if eventHandler.exportMode != keeper.SwingStoreExportModeDebug && eventHandler.snapshotHeight != provider.BlockHeight { return fmt.Errorf("snapshot block height (%d) doesn't match requested height (%d)", provider.BlockHeight, eventHandler.snapshotHeight) } @@ -235,7 +235,7 @@ func (eventHandler swingStoreGenesisEventHandler) OnExportRetrieved(provider kee if err == io.EOF { artifactsEnded = true - if eventHandler.exportMode == keeper.SwingStoreArtifactModeDebug { + if eventHandler.exportMode == keeper.SwingStoreExportModeDebug { exportDataReader, _ := provider.GetExportDataReader() defer exportDataReader.Close() @@ -257,7 +257,7 @@ func (eventHandler swingStoreGenesisEventHandler) OnExportRetrieved(provider kee }, } - if eventHandler.exportMode == keeper.SwingStoreArtifactModeDebug { + if eventHandler.exportMode == keeper.SwingStoreExportModeDebug { artifactsProvider.BlockHeight = provider.BlockHeight } diff --git a/golang/cosmos/x/swingset/keeper/swing_store_exports_handler.go b/golang/cosmos/x/swingset/keeper/swing_store_exports_handler.go index 0a1fe798f27..cc0841eacd3 100644 --- a/golang/cosmos/x/swingset/keeper/swing_store_exports_handler.go +++ b/golang/cosmos/x/swingset/keeper/swing_store_exports_handler.go @@ -195,6 +195,20 @@ const ( SwingStoreExportDataModeAll = "all" ) +const ( + // SwingStoreExportModeSkip indicates swing store data should be + // excluded from the export. + SwingStoreExportModeSkip = "skip" + + // SwingStoreExportModeOperational (default) indicates export should + // have the minimal set of artifacts needed to operate a node. + SwingStoreExportModeOperational = "operational" + + // SwingStoreExportModeDebug indicates export should have the maximal + // set of artifacts available in the JS swing-store. + SwingStoreExportModeDebug = "debug" +) + // SwingStoreExportOptions are configurable options provided to the JS swing-store export type SwingStoreExportOptions struct { // ArtifactMode controls the set of artifacts that should be included in the From cda65f970a21d7f690572af148a249ed7fd5ab77 Mon Sep 17 00:00:00 2001 From: usmanmani1122 Date: Thu, 21 Nov 2024 04:51:52 +0000 Subject: [PATCH 10/10] move constants to genesis file --- golang/cosmos/daemon/cmd/root.go | 14 +++++------ golang/cosmos/x/swingset/genesis.go | 24 +++++++++++++++---- .../keeper/swing_store_exports_handler.go | 14 ----------- 3 files changed, 26 insertions(+), 26 deletions(-) diff --git a/golang/cosmos/daemon/cmd/root.go b/golang/cosmos/daemon/cmd/root.go index 29063763656..07a46217348 100644 --- a/golang/cosmos/daemon/cmd/root.go +++ b/golang/cosmos/daemon/cmd/root.go @@ -387,9 +387,9 @@ const ( ) var allowedSwingSetExportModes = map[string]bool{ - swingsetkeeper.SwingStoreExportModeDebug: true, - swingsetkeeper.SwingStoreExportModeOperational: true, - swingsetkeeper.SwingStoreExportModeSkip: true, + swingset.SwingStoreExportModeDebug: true, + swingset.SwingStoreExportModeOperational: true, + swingset.SwingStoreExportModeSkip: true, } // extendCosmosExportCommand monkey-patches the "export" command added by @@ -409,7 +409,7 @@ func extendCosmosExportCommand(cmd *cobra.Command) { cmd.Flags().String( gaia.FlagSwingStoreExportMode, - swingsetkeeper.SwingStoreExportModeOperational, + swingset.SwingStoreExportModeOperational, fmt.Sprintf( "The mode for swingstore export (%s)", strings.Join(keys, " | "), @@ -433,7 +433,7 @@ func extendCosmosExportCommand(cmd *cobra.Command) { // Since none mode doesn't perform any swing store export // There is no point in creating the export directory - if swingStoreExportMode != swingsetkeeper.SwingStoreExportModeSkip { + if swingStoreExportMode != swingset.SwingStoreExportModeSkip { swingStoreExportPath := filepath.Join(exportDir, ExportedSwingStoreDirectoryName) err = os.MkdirAll(swingStoreExportPath, os.ModePerm) @@ -447,7 +447,7 @@ func extendCosmosExportCommand(cmd *cobra.Command) { serverCtx.Viper.Set(gaia.FlagSwingStoreExportDir, swingStoreExportPath) } - if hasVMController(serverCtx) || swingStoreExportMode == swingsetkeeper.SwingStoreExportModeSkip { + if hasVMController(serverCtx) || swingStoreExportMode == swingset.SwingStoreExportModeSkip { // Capture the export in the genesisPath. // This will fail if a genesis.json already exists in the export-dir genesisFile, err := os.OpenFile( @@ -489,7 +489,7 @@ func (ac appCreator) appExport( } // We don't have to launch VM in case the swing store export is not required - if swingStoreExportMode != swingsetkeeper.SwingStoreExportModeSkip && OnExportHook != nil { + if swingStoreExportMode != swingset.SwingStoreExportModeSkip && OnExportHook != nil { if err := OnExportHook(ac.agdServer, logger, appOpts); err != nil { return servertypes.ExportedApp{}, err } diff --git a/golang/cosmos/x/swingset/genesis.go b/golang/cosmos/x/swingset/genesis.go index e5a8d34d44e..e8308e6e94e 100644 --- a/golang/cosmos/x/swingset/genesis.go +++ b/golang/cosmos/x/swingset/genesis.go @@ -16,6 +16,20 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" ) +const ( + // SwingStoreExportModeSkip indicates swing store data should be + // excluded from the export. + SwingStoreExportModeSkip = "skip" + + // SwingStoreExportModeOperational (default) indicates export should + // have the minimal set of artifacts needed to operate a node. + SwingStoreExportModeOperational = "operational" + + // SwingStoreExportModeDebug indicates export should have the maximal + // set of artifacts available in the JS swing-store. + SwingStoreExportModeDebug = "debug" +) + func ValidateGenesis(data *types.GenesisState) error { if data == nil { return fmt.Errorf("swingset genesis data cannot be nil") @@ -149,12 +163,12 @@ func ExportGenesis( hasher := sha256.New() snapshotHeight := uint64(ctx.BlockHeight()) - if swingStoreExportMode == keeper.SwingStoreExportModeDebug { + if swingStoreExportMode == SwingStoreExportModeDebug { exportDataMode = keeper.SwingStoreExportDataModeAll snapshotHeight = 0 } - if swingStoreExportMode != keeper.SwingStoreExportModeSkip { + if swingStoreExportMode != SwingStoreExportModeSkip { eventHandler := swingStoreGenesisEventHandler{ exportDir: swingStoreExportDir, snapshotHeight: snapshotHeight, @@ -200,7 +214,7 @@ func (eventHandler swingStoreGenesisEventHandler) OnExportStarted(height uint64, } func (eventHandler swingStoreGenesisEventHandler) OnExportRetrieved(provider keeper.SwingStoreExportProvider) error { - if eventHandler.exportMode != keeper.SwingStoreExportModeDebug && eventHandler.snapshotHeight != provider.BlockHeight { + if eventHandler.exportMode != SwingStoreExportModeDebug && eventHandler.snapshotHeight != provider.BlockHeight { return fmt.Errorf("snapshot block height (%d) doesn't match requested height (%d)", provider.BlockHeight, eventHandler.snapshotHeight) } @@ -235,7 +249,7 @@ func (eventHandler swingStoreGenesisEventHandler) OnExportRetrieved(provider kee if err == io.EOF { artifactsEnded = true - if eventHandler.exportMode == keeper.SwingStoreExportModeDebug { + if eventHandler.exportMode == SwingStoreExportModeDebug { exportDataReader, _ := provider.GetExportDataReader() defer exportDataReader.Close() @@ -257,7 +271,7 @@ func (eventHandler swingStoreGenesisEventHandler) OnExportRetrieved(provider kee }, } - if eventHandler.exportMode == keeper.SwingStoreExportModeDebug { + if eventHandler.exportMode == SwingStoreExportModeDebug { artifactsProvider.BlockHeight = provider.BlockHeight } diff --git a/golang/cosmos/x/swingset/keeper/swing_store_exports_handler.go b/golang/cosmos/x/swingset/keeper/swing_store_exports_handler.go index cc0841eacd3..0a1fe798f27 100644 --- a/golang/cosmos/x/swingset/keeper/swing_store_exports_handler.go +++ b/golang/cosmos/x/swingset/keeper/swing_store_exports_handler.go @@ -195,20 +195,6 @@ const ( SwingStoreExportDataModeAll = "all" ) -const ( - // SwingStoreExportModeSkip indicates swing store data should be - // excluded from the export. - SwingStoreExportModeSkip = "skip" - - // SwingStoreExportModeOperational (default) indicates export should - // have the minimal set of artifacts needed to operate a node. - SwingStoreExportModeOperational = "operational" - - // SwingStoreExportModeDebug indicates export should have the maximal - // set of artifacts available in the JS swing-store. - SwingStoreExportModeDebug = "debug" -) - // SwingStoreExportOptions are configurable options provided to the JS swing-store export type SwingStoreExportOptions struct { // ArtifactMode controls the set of artifacts that should be included in the