Merge PR #4852: Cleanup Supply Genesis State

This commit is contained in:
Alexander Bezobchuk 2019-08-06 16:51:18 -04:00 committed by GitHub
parent c8ee82b40a
commit 0dcf158387
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 13 additions and 18 deletions

View File

@ -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))

View File

@ -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

View File

@ -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) {
@ -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()
}

View File

@ -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())
}

View File

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