# Module Genesis

# Pre-requisite Readings

# Type Definition

The subset of the genesis state defined from a given module is generally defined in a ./internal/types/genesis.go file, along with the DefaultGenesis and ValidateGenesis methods. The struct defining the module's subset of the genesis state is usually called GenesisState and contains all the module-related values that need to be initialized during the genesis process.

See an example of GenesisState type definition from the nameservice tutorial

Copy type GenesisState struct { WhoisRecords []Whois `json:"whois_records"` }

Next we present the main genesis-related methods that need to be implemented by module developers in order for their module to be used in Cosmos SDK applications.

# DefaultGenesis

The DefaultGenesis() method is a simple method that calls the constructor function for GenesisState with the default value for each parameter. See an example from the nameservice module:

Copy func DefaultGenesisState() GenesisState { return GenesisState{ WhoisRecords: []Whois{}, } }

# ValidateGenesis

The ValidateGenesis(genesisState GenesisState) method is called to verify that the provided genesisState is correct. It should perform validity checks on each of the parameter listed in GenesisState. See an example from the nameservice module:

Copy func ValidateGenesis(data GenesisState) error { for _, record := range data.WhoisRecords { if record.Owner == nil { return fmt.Errorf("invalid WhoisRecord: Value: %s. Error: Missing Owner", record.Value) } if record.Value == "" { return fmt.Errorf("invalid WhoisRecord: Owner: %s. Error: Missing Value", record.Owner) } if record.Price == nil { return fmt.Errorf("invalid WhoisRecord: Value: %s. Error: Missing Price", record.Value) } } return nil }

# Other Genesis Methods

Other than the methods related directly to GenesisState, module developers are expected to implement two other methods as part of the AppModuleGenesis interface (only if the module needs to initialize a subset of state in genesis). These methods are InitGenesis and ExportGenesis.

# InitGenesis

The InitGenesis method is executed during InitChain when the application is first started. Given a GenesisState, it initializes the subset of the state managed by the module by using the module's keeper setter function on each parameter within the GenesisState.

The module manager of the application is responsible for calling the InitGenesis method of each of the application's modules, in order. This order is set by the application developer via the manager's SetOrderGenesisMethod, which is called in the application's constructor function

See an example of InitGenesis from the nameservice tutorial

Copy func InitGenesis(ctx sdk.Context, keeper Keeper, data GenesisState) []abci.ValidatorUpdate { for _, record := range data.WhoisRecords { keeper.SetWhois(ctx, record.Value, record) } return []abci.ValidatorUpdate{} }

# ExportGenesis

The ExportGenesis method is executed whenever an export of the state is made. It takes the latest known version of the subset of the state managed by the module and creates a new GenesisState out of it. This is mainly used when the chain needs to be upgraded via a hard fork.

See an example of ExportGenesis from the nameservice tutorial.

Copy func ExportGenesis(ctx sdk.Context, k Keeper) GenesisState { var records []Whois iterator := k.GetNamesIterator(ctx) for ; iterator.Valid(); iterator.Next() { name := string(iterator.Key()) whois := k.GetWhois(ctx, name) records = append(records, whois) } return GenesisState{WhoisRecords: records} }

# Next

Learn about modules interfaces