Skip to content

Latest commit

 

History

History
72 lines (59 loc) · 2.39 KB

genesis.md

File metadata and controls

72 lines (59 loc) · 2.39 KB

Genesis

The AppModule interface includes a number of functions for use in initializing and exporting GenesisState for the chain. The ModuleBasicManager calls these functions on each module when starting, stopping or exporting the chain. Here is a very basic implementation that you can expand upon.

Go to x/nameservice/genesis.go and add the following code:

package nameservice

import (
	"fmt"
	sdk "github.com/cosmos/cosmos-sdk/types"
	abci "github.com/tendermint/tendermint/abci/types"
)

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

func NewGenesisState(whoIsRecords []Whois) GenesisState {
	return GenesisState{WhoisRecords: nil}
}

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
}

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

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{}
}

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

A few notes about the above code:

  • ValidateGenesis() validates the provided genesis state to ensure that expected invariants hold
  • DefaultGenesisState() is used mostly for testing. This provides a minimal GenesisState.
  • InitGenesis() is called on chain start, this function imports genesis state into the keeper.
  • ExportGenesis() is called after stopping the chain, this function loads application state into a GenesisState stuct to later be exported to genesis.json alongside data from the other modules.

Now your module has everything it needs to be incorporated into your Cosmos SDK application!