diff --git a/x/bank/simulation/genesis.go b/x/bank/simulation/genesis.go index 9031d03364..de45fe2ab9 100644 --- a/x/bank/simulation/genesis.go +++ b/x/bank/simulation/genesis.go @@ -12,6 +12,16 @@ import ( "github.com/cosmos/cosmos-sdk/x/bank/types" ) +var ( + testSupplyVal, _ = sdk.NewIntFromString("1000000000000000000") + AdditionalTestSupply = sdk.NewCoins( + sdk.NewCoin("denom0", testSupplyVal), + sdk.NewCoin("denom1", testSupplyVal), + sdk.NewCoin("denom2", testSupplyVal), + sdk.NewCoin("denom3", testSupplyVal), + ) +) + // RandomGenesisDefaultSendParam computes randomized allow all send transfers param for the bank module func RandomGenesisDefaultSendParam(r *rand.Rand) bool { // 90% chance of transfers being enable or P(a) = 0.9 for success @@ -47,6 +57,11 @@ func RandomGenesisBalances(simState *module.SimulationState) []types.Balance { }) } + if len(genesisBalances) > 0 { + nb := genesisBalances[0].Coins.Add(AdditionalTestSupply...) + genesisBalances[0].Coins = nb + } + return genesisBalances } @@ -66,7 +81,8 @@ func RandomizedGenState(simState *module.SimulationState) { numAccs := int64(len(simState.Accounts)) totalSupply := sdk.NewInt(simState.InitialStake * (numAccs + simState.NumBonded)) - supply := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, totalSupply)) + + supply := AdditionalTestSupply.Add(sdk.NewCoin(sdk.DefaultBondDenom, totalSupply)) bankGenesis := types.GenesisState{ Params: types.Params{ diff --git a/x/bank/simulation/genesis_test.go b/x/bank/simulation/genesis_test.go index fc31ca38e9..a3f7ff385a 100644 --- a/x/bank/simulation/genesis_test.go +++ b/x/bank/simulation/genesis_test.go @@ -2,6 +2,7 @@ package simulation_test import ( "encoding/json" + sdk "github.com/cosmos/cosmos-sdk/types" "math/rand" "testing" @@ -43,7 +44,8 @@ func TestRandomizedGenState(t *testing.T) { require.Len(t, bankGenesis.Balances, 3) require.Equal(t, "cosmos1ghekyjucln7y67ntx7cf27m9dpuxxemn4c8g4r", bankGenesis.Balances[2].GetAddress().String()) require.Equal(t, "1000stake", bankGenesis.Balances[2].GetCoins().String()) - require.Equal(t, "6000stake", bankGenesis.Supply.String()) + expectedSupply := simulation.AdditionalTestSupply.Add(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(6000))) + require.Equal(t, expectedSupply.String(), bankGenesis.Supply.String()) } // TestRandomizedGenState tests abnormal scenarios of applying RandomizedGenState. diff --git a/x/mint/simulation/genesis.go b/x/mint/simulation/genesis.go index ce1fa89d96..783cc1d886 100644 --- a/x/mint/simulation/genesis.go +++ b/x/mint/simulation/genesis.go @@ -5,6 +5,7 @@ package simulation import ( "encoding/json" "fmt" + "github.com/cosmos/cosmos-sdk/x/bank/simulation" "math/rand" sdk "github.com/cosmos/cosmos-sdk/types" @@ -28,6 +29,34 @@ func GenInflationRate(r *rand.Rand) sdk.Dec { return sdk.NewDecWithPrec(int64(r.Intn(99)), 2) } +// GenMunicipalInflation randomized Municipal Inflation configuration +func GenMunicipalInflation(simState *module.SimulationState) []*types.MunicipalInflationPair { + r := simState.Rand + + coins := make([]*sdk.Coin, len(simulation.AdditionalTestSupply)) + for i := 0; i < len(simulation.AdditionalTestSupply); i++ { + coins[i] = &simulation.AdditionalTestSupply[i] + } + + len_ := r.Intn(len(coins) + 1) + municipalInflation := make([]*types.MunicipalInflationPair, len_) + for i := 0; i < len_; i++ { + lenCoins := len(coins) + lastIdx := lenCoins - 1 + rndIdx := r.Intn(lenCoins) + fmt.Println(">>>>>>>>>>>>>>>", coins, "rndIdx:", rndIdx) + c := coins[rndIdx] + coins[rndIdx] = coins[lastIdx] + coins = coins[:lastIdx] + + acc := &simState.Accounts[r.Intn(len(simState.Accounts))] + infl := sdk.NewDecWithPrec(r.Int63n(201), 2) + municipalInflation[i] = &types.MunicipalInflationPair{Denom: c.Denom, Inflation: types.NewMunicipalInflation(acc.Address.String(), infl)} + } + + return municipalInflation +} + // RandomizedGenState generates a random GenesisState for mint func RandomizedGenState(simState *module.SimulationState) { // minter @@ -48,7 +77,9 @@ func RandomizedGenState(simState *module.SimulationState) { blocksPerYear := uint64(60 * 60 * 8766 / 5) params := types.NewParams(mintDenom, inflationRateChange, blocksPerYear) - mintGenesis := types.NewGenesisState(types.InitialMinter(inflation), params) + minter := types.InitialMinter(inflation) + minter.MunicipalInflation = GenMunicipalInflation(simState) + mintGenesis := types.NewGenesisState(minter, params) bz, err := json.MarshalIndent(&mintGenesis, "", " ") if err != nil { diff --git a/x/mint/types/minter.go b/x/mint/types/minter.go index 484a8d27ea..2fdc1ff6fc 100644 --- a/x/mint/types/minter.go +++ b/x/mint/types/minter.go @@ -33,13 +33,15 @@ func DefaultInitialMinter() Minter { ) } -// validate minter +// ValidateMinter validate minter func ValidateMinter(minter Minter) error { if minter.Inflation.IsNegative() { return fmt.Errorf("mint parameter AnnualInflation should be positive, is %s", minter.Inflation.String()) } - return nil + + err := ValidateMunicipalInflations(&minter.MunicipalInflation) + return err } // NextInflationRate returns the new inflation rate for the next hour.