testnet
GitHub

ExportAppStateAndValidators

ExportAppStateAndValidators(forZeroHeight bool, jailAllowedAddrs []string, modulesToExport []string) (servertypes.ExportedApp, error)

Exports the entire application state and validator set at the current block height into a format suitable for a genesis.json file. This function is critical for network upgrades, hard forks, and creating new testnets from an existing state. It iterates through all registered modules, calls their respective ExportGenesis methods, and aggregates the results.

Parameters

Name Type Required Description
forZeroHeight bool If true, the state is prepared for a new chain starting at height 0. This involves special handling like withdrawing all staking rewards.
jailAllowedAddrs []string A list of validator addresses that are allowed to be jailed in the exported state. If empty, no validators are jailed.
modulesToExport []string A list of module names to export. If empty, all modules will be exported.

Returns

Type: servertypes.ExportedApp

An ExportedApp struct containing the marshaled app state as JSON, the list of validators, the export height, and the consensus parameters.

Errors

Code Name Description
error Export Error Returns an error if any module fails to export its genesis state or if the final state cannot be marshaled to JSON.

Examples

Exporting State for a Network Upgrade

This conceptual example shows how `ExportAppStateAndValidators` would be invoked within a CLI command like `marood export`. The function is called with `forZeroHeight=false` for a standard state export that preserves the current block height.

// This function is typically called from a CLI command, not directly in application code.
// Example of how it might be used in a command:

// marood export --for-zero-height=false > genesis_export.json

func exportStateCmd() *cobra.Command {
    cmd := &cobra.Command{
        Use:   "export",
        Short: "Export state to JSON",
        RunE: func(cmd *cobra.Command, args []string) error {
            serverCtx := server.GetServerContextFromCmd(cmd)
            app := serverCtx.App.(servertypes.Application)

            // Assume 'marooApp' is the concrete type of our application
            marooApp, ok := app.(*maroo.MarooApp)
            if !ok {
                return fmt.Errorf("application is not a MarooApp")
            }

            exportedApp, err := marooApp.ExportAppStateAndValidators(false, []string{}, []string{})
            if err != nil {
                return err
            }

            // The result is then typically printed to stdout
            fmt.Println(string(exportedApp.AppState))
            return nil
        },
    }
    return cmd
}
ESC
Type to search