Files
cosmos-sdk/x/bank/simulation/genesis.go
T
Rafael Tenfen 91a916ec1a refactor: add option to set bondDenom on simulations (#14529)
## Description




- Add new property `BondDenom` to `SimulationState` struct in `types/module/simulation.go`
- Changed the `x/modules/simulation/genesis.go` mostly to use the new property

---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing)
- [x] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
2023-01-08 18:21:51 +00:00

98 lines
4.3 KiB
Go

package simulation
import (
"encoding/json"
"fmt"
"math/rand"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/bank/types"
)
// RandomGenesisDefaultSendEnabledParam computes randomized allow all send transfers param for the bank module
func RandomGenesisDefaultSendEnabledParam(r *rand.Rand) bool {
// 90% chance of transfers being enabled or P(a) = 0.9 for success
return r.Int63n(100) < 90
}
// RandomGenesisSendEnabled creates randomized values for the SendEnabled slice.
func RandomGenesisSendEnabled(r *rand.Rand, bondDenom string) []types.SendEnabled {
rv := make([]types.SendEnabled, 0, 2)
// 60% of the time, add a denom specific record.
if r.Int63n(100) < 60 {
// 75% of the those times, set send enabled to true.
bondEnabled := r.Int63n(100) < 75
rv = append(rv, types.SendEnabled{Denom: bondDenom, Enabled: bondEnabled})
}
// Probabilities:
// P(a) = 60.0% = There's SendEnable entry for the bond denom = .600
// P(a)' = 40.0% = There is NOT a SendEnable entry for the bond denom = 1 - P(a) = 1 - .600 = .400
// P(b) = 75.0% = The SendEnable entry is true (if there is such an entry) = .750
// P(b)' = 25.0% = The SendEnable entry is false (if there is such an entry) = 1 - P(b) = 1 - .750 = .250
// P(c) = 90.0% = The default send enabled is true (defined in RandomGenesisDefaultSendEnabledParam) = .900
// P(c)' = 10.0% = The default send enabled is false = 1 - P(c) = 1 - .900 = .100
//
// P(st) = 45.0% = There's a SendEnable entry that's true = P(a)*P(b) = .600*.750 = .450
// P(sf) = 15.0% = There's a SendEnable entry that's false = P(a)*P(b)' = .600*.250 = .150
//
// P(a'c) = 36.0% = No SendEnabled entry AND default is true = P(a)'*P(c) = .400*.900 = .360
// P(a'c') = 4.0% = No SendEnabled entry AND default is false = P(a)'*P(c)' = .400*.100 = .040
// P(stc) = 40.5% = SendEnabled entry is true AND default is true = P(st)*P(c) = .450*.900 = .405
// P(stc') = 4.5% = SendEnabled entry is true AND default is false = P(st)*P(c)' = .450*.100 = .045
// P(sfc) = 13.5% = SendEnabled entry is false AND default is true = P(sf)*P(c) = .150*.900 = .135
// P(sfc') = 1.5% = SendEnabled entry is false AND default is false = P(sf)*P(c)' = .150*.100 = .015
//
// P(set) = 42.0% = SendEnabled entry that equals the default = P(stc) + P(sfc') = .405 + .015 = .420
// P(sef) = 18.0% = SendEnabled entry that does not equal the default = P(stc') + P(sfc) = .045 + .135 = .180
//
// P(t) = 81.0% = Bond denom is sendable = P(a'c) + P(st) = .360 + .450 = .810
// P(f) = 19.0% = Bond demon is NOT sendable = P(a'c') + P(sf) = .040 + .150 = .190
return rv
}
// RandomGenesisBalances returns a slice of account balances. Each account has
// a balance of simState.InitialStake for simState.BondDenom.
func RandomGenesisBalances(simState *module.SimulationState) []types.Balance {
genesisBalances := []types.Balance{}
for _, acc := range simState.Accounts {
genesisBalances = append(genesisBalances, types.Balance{
Address: acc.Address.String(),
Coins: sdk.NewCoins(sdk.NewCoin(simState.BondDenom, simState.InitialStake)),
})
}
return genesisBalances
}
// RandomizedGenState generates a random GenesisState for bank
func RandomizedGenState(simState *module.SimulationState) {
var defaultSendEnabledParam bool
simState.AppParams.GetOrGenerate(
simState.Cdc, string(types.KeyDefaultSendEnabled), &defaultSendEnabledParam, simState.Rand,
func(r *rand.Rand) { defaultSendEnabledParam = RandomGenesisDefaultSendEnabledParam(r) },
)
sendEnabled := RandomGenesisSendEnabled(simState.Rand, simState.BondDenom)
numAccs := int64(len(simState.Accounts))
totalSupply := simState.InitialStake.Mul(sdk.NewInt((numAccs + simState.NumBonded)))
supply := sdk.NewCoins(sdk.NewCoin(simState.BondDenom, totalSupply))
bankGenesis := types.GenesisState{
Params: types.NewParams(defaultSendEnabledParam),
Balances: RandomGenesisBalances(simState),
Supply: supply,
SendEnabled: sendEnabled,
}
paramsBytes, err := json.MarshalIndent(&bankGenesis.Params, "", " ")
if err != nil {
panic(err)
}
fmt.Printf("Selected randomly generated bank parameters:\n%s\n", paramsBytes)
simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(&bankGenesis)
}