2020-03-30 15:02:43 +00:00
|
|
|
package simulation
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-04-12 03:54:45 +00:00
|
|
|
"math/rand"
|
|
|
|
"strings"
|
2020-08-27 02:05:27 +00:00
|
|
|
"time"
|
2020-03-30 15:02:43 +00:00
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2020-04-12 03:54:45 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-03-30 15:02:43 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/types/module"
|
2020-04-12 03:54:45 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth"
|
|
|
|
authexported "github.com/cosmos/cosmos-sdk/x/auth/exported"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/simulation"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/supply"
|
2020-04-30 14:13:31 +00:00
|
|
|
|
2020-03-30 15:02:43 +00:00
|
|
|
"github.com/kava-labs/kava/x/bep3/types"
|
|
|
|
)
|
|
|
|
|
2020-04-12 03:54:45 +00:00
|
|
|
var (
|
2020-08-17 15:06:59 +00:00
|
|
|
MaxSupplyLimit = 1000000000000
|
|
|
|
MinSupplyLimit = 100000000
|
|
|
|
MinSwapAmountLimit = 999
|
|
|
|
accs []simulation.Account
|
|
|
|
ConsistentDenoms = [3]string{"bnb", "xrp", "btc"}
|
|
|
|
MinBlockLock = uint64(5)
|
2020-04-12 03:54:45 +00:00
|
|
|
)
|
|
|
|
|
2020-04-23 16:35:58 +00:00
|
|
|
// GenRandBnbDeputy randomized BnbDeputyAddress
|
|
|
|
func GenRandBnbDeputy(r *rand.Rand) simulation.Account {
|
|
|
|
acc, _ := simulation.RandomAcc(r, accs)
|
|
|
|
return acc
|
2020-04-12 03:54:45 +00:00
|
|
|
}
|
|
|
|
|
2020-08-17 15:06:59 +00:00
|
|
|
// GenRandFixedFee randomized FixedFee in range [1, 10000]
|
|
|
|
func GenRandFixedFee(r *rand.Rand) sdk.Int {
|
2020-06-05 01:27:54 +00:00
|
|
|
min := int(1)
|
2020-06-04 23:03:14 +00:00
|
|
|
max := types.DefaultBnbDeputyFixedFee.Int64()
|
|
|
|
return sdk.NewInt(int64(r.Intn(int(max)-min) + min))
|
|
|
|
}
|
|
|
|
|
2020-08-17 15:06:59 +00:00
|
|
|
// GenMinSwapAmount randomized MinAmount in range [1, 1000]
|
|
|
|
func GenMinSwapAmount(r *rand.Rand) sdk.Int {
|
|
|
|
return sdk.OneInt().Add(simulation.RandomAmount(r, sdk.NewInt(int64(MinSwapAmountLimit))))
|
2020-06-04 23:03:14 +00:00
|
|
|
}
|
|
|
|
|
2020-08-17 15:06:59 +00:00
|
|
|
// GenMaxSwapAmount randomized MaxAmount
|
|
|
|
func GenMaxSwapAmount(r *rand.Rand, minAmount sdk.Int, supplyMax sdk.Int) sdk.Int {
|
2020-06-04 23:03:14 +00:00
|
|
|
min := minAmount.Int64()
|
2020-08-17 15:06:59 +00:00
|
|
|
max := supplyMax.Quo(sdk.NewInt(100)).Int64()
|
|
|
|
|
2020-06-04 23:03:14 +00:00
|
|
|
return sdk.NewInt((int64(r.Intn(int(max-min))) + min))
|
2020-05-13 23:39:29 +00:00
|
|
|
}
|
|
|
|
|
2020-08-17 15:06:59 +00:00
|
|
|
// GenSupplyLimit generates a random SupplyLimit
|
|
|
|
func GenSupplyLimit(r *rand.Rand, max int) sdk.Int {
|
|
|
|
max = simulation.RandIntBetween(r, MinSupplyLimit, max)
|
|
|
|
return sdk.NewInt(int64(max))
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenSupplyLimit generates a random SupplyLimit
|
|
|
|
func GenAssetSupply(r *rand.Rand, denom string) types.AssetSupply {
|
|
|
|
return types.NewAssetSupply(
|
|
|
|
sdk.NewCoin(denom, sdk.ZeroInt()), sdk.NewCoin(denom, sdk.ZeroInt()),
|
2020-08-27 02:05:27 +00:00
|
|
|
sdk.NewCoin(denom, sdk.ZeroInt()), sdk.NewCoin(denom, sdk.ZeroInt()), time.Duration(0))
|
2020-08-17 15:06:59 +00:00
|
|
|
}
|
|
|
|
|
2020-04-12 03:54:45 +00:00
|
|
|
// GenMinBlockLock randomized MinBlockLock
|
2020-05-12 20:15:38 +00:00
|
|
|
func GenMinBlockLock(r *rand.Rand) uint64 {
|
2020-08-17 15:06:59 +00:00
|
|
|
return MinBlockLock
|
2020-04-12 03:54:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GenMaxBlockLock randomized MaxBlockLock
|
2020-05-12 20:15:38 +00:00
|
|
|
func GenMaxBlockLock(r *rand.Rand, minBlockLock uint64) uint64 {
|
2020-08-17 15:06:59 +00:00
|
|
|
max := int(50)
|
|
|
|
return uint64(r.Intn(max-int(MinBlockLock)) + int(MinBlockLock+1))
|
2020-04-12 03:54:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GenSupportedAssets gets randomized SupportedAssets
|
|
|
|
func GenSupportedAssets(r *rand.Rand) types.AssetParams {
|
2020-05-13 12:41:54 +00:00
|
|
|
|
2020-04-13 17:06:59 +00:00
|
|
|
numAssets := (r.Intn(10) + 1)
|
2020-05-13 12:41:54 +00:00
|
|
|
assets := make(types.AssetParams, numAssets+1)
|
2020-04-13 17:06:59 +00:00
|
|
|
for i := 0; i < numAssets; i++ {
|
2020-04-12 03:54:45 +00:00
|
|
|
denom := strings.ToLower(simulation.RandStringOfLength(r, (r.Intn(3) + 3)))
|
|
|
|
asset := genSupportedAsset(r, denom)
|
2020-05-13 12:41:54 +00:00
|
|
|
assets[i] = asset
|
2020-04-12 03:54:45 +00:00
|
|
|
}
|
|
|
|
// Add bnb, btc, or xrp as a supported asset for interactions with other modules
|
2020-05-13 12:41:54 +00:00
|
|
|
assets[len(assets)-1] = genSupportedAsset(r, ConsistentDenoms[r.Intn(3)])
|
2020-04-12 03:54:45 +00:00
|
|
|
|
|
|
|
return assets
|
|
|
|
}
|
|
|
|
|
|
|
|
func genSupportedAsset(r *rand.Rand, denom string) types.AssetParam {
|
|
|
|
coinID, _ := simulation.RandPositiveInt(r, sdk.NewInt(100000))
|
2020-08-17 15:06:59 +00:00
|
|
|
limit := GenSupplyLimit(r, MaxSupplyLimit)
|
|
|
|
|
|
|
|
minSwapAmount := GenMinSwapAmount(r)
|
|
|
|
minBlockLock := GenMinBlockLock(r)
|
2020-08-27 02:05:27 +00:00
|
|
|
timeLimited := r.Float32() < 0.5
|
|
|
|
timeBasedLimit := sdk.ZeroInt()
|
|
|
|
if timeLimited {
|
|
|
|
// set time-based limit to between 10 and 25% of the total limit
|
|
|
|
min := int(limit.Quo(sdk.NewInt(10)).Int64())
|
|
|
|
max := int(limit.Quo(sdk.NewInt(4)).Int64())
|
|
|
|
timeBasedLimit = sdk.NewInt(int64(simulation.RandIntBetween(r, min, max)))
|
|
|
|
}
|
2020-04-12 03:54:45 +00:00
|
|
|
return types.AssetParam{
|
2020-08-27 02:05:27 +00:00
|
|
|
Denom: denom,
|
|
|
|
CoinID: int(coinID.Int64()),
|
|
|
|
SupplyLimit: types.SupplyLimit{
|
|
|
|
Limit: limit,
|
|
|
|
TimeLimited: timeLimited,
|
|
|
|
TimePeriod: time.Hour * 24,
|
|
|
|
TimeBasedLimit: timeBasedLimit},
|
2020-08-17 15:06:59 +00:00
|
|
|
Active: true,
|
|
|
|
DeputyAddress: GenRandBnbDeputy(r).Address,
|
|
|
|
FixedFee: GenRandFixedFee(r),
|
|
|
|
MinSwapAmount: minSwapAmount,
|
|
|
|
MaxSwapAmount: GenMaxSwapAmount(r, minSwapAmount, limit),
|
|
|
|
MinBlockLock: minBlockLock,
|
|
|
|
MaxBlockLock: GenMaxBlockLock(r, minBlockLock),
|
2020-04-12 03:54:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-30 15:02:43 +00:00
|
|
|
// RandomizedGenState generates a random GenesisState
|
|
|
|
func RandomizedGenState(simState *module.SimulationState) {
|
2020-04-23 16:35:58 +00:00
|
|
|
accs = simState.Accounts
|
2020-03-30 15:02:43 +00:00
|
|
|
|
2020-04-12 03:54:45 +00:00
|
|
|
bep3Genesis := loadRandomBep3GenState(simState)
|
|
|
|
fmt.Printf("Selected randomly generated %s parameters:\n%s\n", types.ModuleName, codec.MustMarshalJSONIndent(simState.Cdc, bep3Genesis))
|
|
|
|
simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(bep3Genesis)
|
|
|
|
|
|
|
|
authGenesis, totalCoins := loadAuthGenState(simState, bep3Genesis)
|
|
|
|
simState.GenState[auth.ModuleName] = simState.Cdc.MustMarshalJSON(authGenesis)
|
|
|
|
|
|
|
|
// Update supply to match amount of coins in auth
|
|
|
|
var supplyGenesis supply.GenesisState
|
|
|
|
simState.Cdc.MustUnmarshalJSON(simState.GenState[supply.ModuleName], &supplyGenesis)
|
2020-05-13 12:41:54 +00:00
|
|
|
|
2020-04-12 03:54:45 +00:00
|
|
|
for _, deputyCoin := range totalCoins {
|
2020-04-23 16:35:58 +00:00
|
|
|
supplyGenesis.Supply = supplyGenesis.Supply.Add(deputyCoin...)
|
2020-04-12 03:54:45 +00:00
|
|
|
}
|
|
|
|
simState.GenState[supply.ModuleName] = simState.Cdc.MustMarshalJSON(supplyGenesis)
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadRandomBep3GenState(simState *module.SimulationState) types.GenesisState {
|
|
|
|
|
2020-08-17 15:06:59 +00:00
|
|
|
supportedAssets := GenSupportedAssets(simState.Rand)
|
|
|
|
supplies := types.AssetSupplies{}
|
|
|
|
for _, asset := range supportedAssets {
|
|
|
|
supply := GenAssetSupply(simState.Rand, asset.Denom)
|
|
|
|
supplies = append(supplies, supply)
|
|
|
|
}
|
2020-04-12 03:54:45 +00:00
|
|
|
|
|
|
|
bep3Genesis := types.GenesisState{
|
|
|
|
Params: types.Params{
|
2020-08-17 15:06:59 +00:00
|
|
|
AssetParams: supportedAssets,
|
2020-04-12 03:54:45 +00:00
|
|
|
},
|
2020-08-27 02:05:27 +00:00
|
|
|
Supplies: supplies,
|
|
|
|
PreviousBlockTime: types.DefaultPreviousBlockTime,
|
2020-04-12 03:54:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return bep3Genesis
|
|
|
|
}
|
|
|
|
|
2020-05-13 12:41:54 +00:00
|
|
|
func loadAuthGenState(simState *module.SimulationState, bep3Genesis types.GenesisState) (auth.GenesisState, []sdk.Coins) {
|
2020-04-12 03:54:45 +00:00
|
|
|
var authGenesis auth.GenesisState
|
|
|
|
simState.Cdc.MustUnmarshalJSON(simState.GenState[auth.ModuleName], &authGenesis)
|
|
|
|
// Load total limit of each supported asset to deputy's account
|
|
|
|
var totalCoins []sdk.Coins
|
2020-08-17 15:06:59 +00:00
|
|
|
for _, asset := range bep3Genesis.Params.AssetParams {
|
|
|
|
deputy, found := getAccount(authGenesis.Accounts, asset.DeputyAddress)
|
|
|
|
if !found {
|
|
|
|
panic("deputy address not found in available accounts")
|
|
|
|
}
|
2020-08-27 02:05:27 +00:00
|
|
|
assetCoin := sdk.NewCoins(sdk.NewCoin(asset.Denom, asset.SupplyLimit.Limit))
|
2020-04-23 16:35:58 +00:00
|
|
|
if err := deputy.SetCoins(deputy.GetCoins().Add(assetCoin...)); err != nil {
|
2020-04-12 03:54:45 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
totalCoins = append(totalCoins, assetCoin)
|
2020-08-17 15:06:59 +00:00
|
|
|
authGenesis.Accounts = replaceOrAppendAccount(authGenesis.Accounts, deputy)
|
2020-04-12 03:54:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return authGenesis, totalCoins
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return an account from a list of accounts that matches an address.
|
|
|
|
func getAccount(accounts []authexported.GenesisAccount, addr sdk.AccAddress) (authexported.GenesisAccount, bool) {
|
|
|
|
for _, a := range accounts {
|
|
|
|
if a.GetAddress().Equals(addr) {
|
|
|
|
return a, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|
2020-03-30 15:02:43 +00:00
|
|
|
|
2020-04-12 03:54:45 +00:00
|
|
|
// In a list of accounts, replace the first account found with the same address. If not found, append the account.
|
|
|
|
func replaceOrAppendAccount(accounts []authexported.GenesisAccount, acc authexported.GenesisAccount) []authexported.GenesisAccount {
|
|
|
|
newAccounts := accounts
|
|
|
|
for i, a := range accounts {
|
|
|
|
if a.GetAddress().Equals(acc.GetAddress()) {
|
|
|
|
newAccounts[i] = acc
|
|
|
|
return newAccounts
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return append(newAccounts, acc)
|
2020-03-30 15:02:43 +00:00
|
|
|
}
|