/
operations.go
140 lines (118 loc) · 3.88 KB
/
operations.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package simulation
// DONTCOVER
import (
"math/big"
"math/rand"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/simulation"
"github.com/furyanprotocol/v4-chain/protocol/lib"
"github.com/furyanprotocol/v4-chain/protocol/testutil/sim_helpers"
"github.com/furyanprotocol/v4-chain/protocol/x/prices/keeper"
"github.com/furyanprotocol/v4-chain/protocol/x/prices/types"
)
// Simulation operation weights constants
const (
opWeightMsgUpdateMarketPrices = "op_weight_msg_update_market_prices"
defaultWeightMsgUpdateMarketPrices int = 100
)
var (
maxPriceChangePpm = int(0.5 * 1_000_000) // 50% in ppm
typeMsgUpdateMarketPrices = sdk.MsgTypeURL(&types.MsgUpdateMarketPrices{})
)
// WeightedOperations returns all the operations from the module with their respective weights
func WeightedOperations(
appParams simtypes.AppParams,
jsonCdc codec.JSONCodec,
k keeper.Keeper,
ak types.AccountKeeper,
bk types.BankKeeper,
) simulation.WeightedOperations {
protoCdc := codec.NewProtoCodec(codectypes.NewInterfaceRegistry())
operations := make([]simtypes.WeightedOperation, 0)
// MsgUpdateMarketPrices
var weightMsgUpdateMarketPrices int
appParams.GetOrGenerate(jsonCdc, opWeightMsgUpdateMarketPrices, &weightMsgUpdateMarketPrices, nil,
func(_ *rand.Rand) {
weightMsgUpdateMarketPrices = defaultWeightMsgUpdateMarketPrices
},
)
operations = append(operations, simulation.NewWeightedOperation(
weightMsgUpdateMarketPrices,
SimulateMsgUpdateMarketPrices(protoCdc, k, ak, bk),
))
return operations
}
// SimulateMsgUpdateMarketPrices generates a random MsgUpdateMarketPrices.
func SimulateMsgUpdateMarketPrices(
cdc *codec.ProtoCodec,
k keeper.Keeper,
ak types.AccountKeeper,
bk types.BankKeeper,
) simtypes.Operation {
return func(
r *rand.Rand,
app *baseapp.BaseApp,
ctx sdk.Context,
accs []simtypes.Account,
chainId string,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
proposer, _ := simtypes.RandomAcc(r, accs)
allMarketParamPrices, _ := k.GetAllMarketParamPrices(ctx)
priceUpdates := make([]*types.MsgUpdateMarketPrices_MarketPrice, 0)
for _, marketParamPrice := range allMarketParamPrices {
// 50% chance of updating the price.
if sim_helpers.RandBool(r) {
newPrice := getRandomlyUpdatedPrice(r, marketParamPrice)
// only update if the new price is not 0
if newPrice != 0 {
priceUpdates = append(
priceUpdates,
types.NewMarketPriceUpdate(marketParamPrice.Param.Id, newPrice),
)
}
}
}
if len(priceUpdates) == 0 {
return simtypes.NoOpMsg(types.ModuleName, typeMsgUpdateMarketPrices, "empty: market price updates"), nil, nil
}
msg := &types.MsgUpdateMarketPrices{
MarketPriceUpdates: priceUpdates,
}
opMsg, err := sim_helpers.GenerateAndDeliverTx(
r,
app,
ctx,
chainId,
cdc,
ak,
bk,
proposer,
types.ModuleName,
msg,
typeMsgUpdateMarketPrices,
true, // fee does not apply when updating market prices.
)
if err != nil {
panic(err) // panic to halt/fail simulation.
}
return opMsg, nil, nil
}
}
// getRandomlyUpdatedPrice returns a valid, random new market price.
func getRandomlyUpdatedPrice(r *rand.Rand, marketParamPrice types.MarketParamPrice) uint64 {
randomValidChangePpm := uint32(
simtypes.RandIntBetween(r, int(marketParamPrice.Param.MinPriceChangePpm), maxPriceChangePpm+1),
)
bigPrice := new(big.Int).SetUint64(marketParamPrice.Price.Price)
bigPriceChange := lib.BigIntMulPpm(bigPrice, randomValidChangePpm)
// 50% chance that the change is in the negative direction.
if sim_helpers.RandBool(r) {
bigPriceChange = new(big.Int).Neg(bigPriceChange)
}
newBigPrice := new(big.Int).Add(bigPrice, bigPriceChange)
return newBigPrice.Uint64()
}