Skip to content

Commit

Permalink
Merge PR #4852: Cleanup Supply Genesis State
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderbez authored Aug 6, 2019
1 parent c8ee82b commit 0dcf158
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 18 deletions.
2 changes: 1 addition & 1 deletion simapp/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func GenBankGenesisState(cdc *codec.Codec, r *rand.Rand, ap simulation.AppParams
func GenSupplyGenesisState(cdc *codec.Codec, amount, numInitiallyBonded, numAccs int64, genesisState map[string]json.RawMessage) {
totalSupply := sdk.NewInt(amount * (numAccs + numInitiallyBonded))
supplyGenesis := supply.NewGenesisState(
supply.NewSupply(sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, totalSupply))),
sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, totalSupply)),
)

fmt.Printf("Generated supply parameters:\n%s\n", codec.MustMarshalJSONIndent(cdc, supplyGenesis))
Expand Down
1 change: 1 addition & 0 deletions x/genutil/legacy/v036/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func Migrate(appState genutil.AppMap) genutil.AppMap {
appState[v036staking.ModuleName] = v036Codec.MustMarshalJSON(v036staking.Migrate(stakingGenState))
}

// migrate supply state
appState[v036supply.ModuleName] = v036Codec.MustMarshalJSON(v036supply.EmptyGenesisState())

return appState
Expand Down
10 changes: 5 additions & 5 deletions x/supply/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
// CONTRACT: all types of accounts must have been already initialized/created
func InitGenesis(ctx sdk.Context, keeper Keeper, ak types.AccountKeeper, data GenesisState) {
// manually set the total supply based on accounts if not provided
if data.Supply.GetTotal().Empty() {
if data.Supply.Empty() {
var totalSupply sdk.Coins
ak.IterateAccounts(ctx,
func(acc authexported.Account) (stop bool) {
Expand All @@ -20,19 +20,19 @@ func InitGenesis(ctx sdk.Context, keeper Keeper, ak types.AccountKeeper, data Ge
},
)

data.Supply = data.Supply.SetTotal(totalSupply)
data.Supply = totalSupply
}

keeper.SetSupply(ctx, data.Supply)
keeper.SetSupply(ctx, types.NewSupply(data.Supply))
}

// ExportGenesis returns a GenesisState for a given context and keeper.
func ExportGenesis(ctx sdk.Context, keeper Keeper) GenesisState {
return NewGenesisState(keeper.GetSupply(ctx))
return NewGenesisState(keeper.GetSupply(ctx).GetTotal())
}

// ValidateGenesis performs basic validation of supply genesis data returning an
// error for any failed validation criteria.
func ValidateGenesis(data GenesisState) error {
return data.Supply.ValidateBasic()
return types.NewSupply(data.Supply).ValidateBasic()
}
8 changes: 4 additions & 4 deletions x/supply/internal/types/genesis.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package types

import (
"github.com/cosmos/cosmos-sdk/x/supply/exported"
sdk "github.com/cosmos/cosmos-sdk/types"
)

// GenesisState is the supply state that must be provided at genesis.
type GenesisState struct {
Supply exported.SupplyI `json:"supply" yaml:"supply"`
Supply sdk.Coins `json:"supply" yaml:"supply"`
}

// NewGenesisState creates a new genesis state.
func NewGenesisState(supply exported.SupplyI) GenesisState {
func NewGenesisState(supply sdk.Coins) GenesisState {
return GenesisState{supply}
}

// DefaultGenesisState returns a default genesis state
func DefaultGenesisState() GenesisState {
return NewGenesisState(DefaultSupply())
return NewGenesisState(DefaultSupply().GetTotal())
}
10 changes: 2 additions & 8 deletions x/supply/legacy/v0_36/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,13 @@ import (
const ModuleName = "supply"

type (
Supply struct {
Total sdk.Coins `json:"total"`
}

GenesisState struct {
Supply Supply `json:"supply"`
Supply sdk.Coins `json:"supply" yaml:"supply"`
}
)

func EmptyGenesisState() GenesisState {
return GenesisState{
Supply: Supply{
Total: sdk.NewCoins(), // leave this empty as it's filled on initialization
},
Supply: sdk.NewCoins(), // leave this empty as it's filled on initialization
}
}

0 comments on commit 0dcf158

Please sign in to comment.