Skip to content

Commit

Permalink
[WIP] Municipal Inflation: Add randomised simulation generation
Browse files Browse the repository at this point in the history
  • Loading branch information
pbukva committed Aug 9, 2023
1 parent f661c3b commit 7aafc7d
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 5 deletions.
18 changes: 17 additions & 1 deletion x/bank/simulation/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand All @@ -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{
Expand Down
4 changes: 3 additions & 1 deletion x/bank/simulation/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package simulation_test

import (
"encoding/json"
sdk "github.com/cosmos/cosmos-sdk/types"
"math/rand"
"testing"

Expand Down Expand Up @@ -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.
Expand Down
33 changes: 32 additions & 1 deletion x/mint/simulation/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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 {
Expand Down
6 changes: 4 additions & 2 deletions x/mint/types/minter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 7aafc7d

Please sign in to comment.