-
Notifications
You must be signed in to change notification settings - Fork 36
/
proposals.go
234 lines (216 loc) · 8.34 KB
/
proposals.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package simulation
import (
"math/rand"
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/cosmos/cosmos-sdk/x/simulation"
simappparams "github.com/provenance-io/provenance/app/params"
"github.com/provenance-io/provenance/x/marker/keeper"
"github.com/provenance-io/provenance/x/marker/types"
)
const (
// OpWeightSupplyIncreaseProposal app params key for supply increase proposal
OpWeightSupplyIncreaseProposal = "op_weight_supply_increase_proposal"
// OpWeightSupplyDecreaseProposal app params key for supply decrease proposal
OpWeightSupplyDecreaseProposal = "op_weight_supply_decrease_proposal"
// OpWeightSetAdministratorProposal app params key for set administrator proposal
//nolint:gosec // not credentials
OpWeightSetAdministratorProposal = "op_weight_set_administrator_proposal"
// OpWeightRemoveAdministratorProposal app params key for remove administrator proposal
OpWeightRemoveAdministratorProposal = "op_weight_remove_administrator_proposal"
// OpWeightChangeStatusProposal app params key for change status proposal
OpWeightChangeStatusProposal = "op_weight_change_status_proposal"
// OpWeightSetDenomMetadataProposal app params key for change status proposal
//nolint:gosec // not credentials
OpWeightSetDenomMetadataProposal = "op_weight_set_denom_metadata"
)
// ProposalContents defines the module weighted proposals' contents
func ProposalContents(k keeper.Keeper) []simtypes.WeightedProposalContent {
return []simtypes.WeightedProposalContent{
simulation.NewWeightedProposalContent(
OpWeightSupplyIncreaseProposal,
simappparams.DefaultWeightSupplyIncreaseProposalContent,
SimulateCreateSupplyIncreaseProposalContent(k),
),
simulation.NewWeightedProposalContent(
OpWeightSupplyDecreaseProposal,
simappparams.DefaultWeightSupplyDecreaseProposalContent,
SimulateCreateSupplyDecreaseProposalContent(k),
),
simulation.NewWeightedProposalContent(
OpWeightSetAdministratorProposal,
simappparams.DefaultWeightSetAdministratorProposalContent,
SimulateCreateSetAdministratorProposalContent(k),
),
simulation.NewWeightedProposalContent(
OpWeightRemoveAdministratorProposal,
simappparams.DefaultWeightRemoveAdministratorProposalContent,
SimulateCreateRemoveAdministratorProposalContent(k),
),
simulation.NewWeightedProposalContent(
OpWeightChangeStatusProposal,
simappparams.DefaultWeightChangeStatusProposalContent,
SimulateCreateChangeStatusProposalContent(k),
),
simulation.NewWeightedProposalContent(
OpWeightSetDenomMetadataProposal,
simappparams.DefaultWeightSetDenomMetadataProposalContent,
SimulateSetDenomMetadataProposalContent(k),
),
}
}
// SimulateCreateSupplyIncreaseProposalContent generates random increase marker supply proposal content
func SimulateCreateSupplyIncreaseProposalContent(k keeper.Keeper) simtypes.ContentSimulatorFn {
return func(r *rand.Rand, ctx sdk.Context, accs []simtypes.Account) simtypes.Content {
dest := ""
if r.Intn(100) < 40 {
acc, _ := simtypes.RandomAcc(r, accs)
dest = acc.Address.String()
}
m := randomGovMarkerWithMaxStatus(r, ctx, k, types.StatusActive)
if m == nil {
return nil
}
maxSupply := k.GetMaxSupply(ctx).Sub(k.CurrentCirculation(ctx, m))
newSupply := math.NewIntFromBigInt(math.ZeroInt().BigInt().Rand(r, maxSupply.BigInt()))
// TODO: When the simulation tests are fixed to stop breaking supply invariants through incorrect minting, the following check should be removed.
if newSupply.GT(k.GetMaxSupply(ctx)) {
println("!!!! WARNING, TOKEN SUPPLY IS INVALID, ABORTING NEW PROPOSAL !!!!")
return nil
}
return types.NewSupplyIncreaseProposal(
simtypes.RandStringOfLength(r, 10),
simtypes.RandStringOfLength(r, 100),
sdk.NewCoin(m.GetDenom(), newSupply),
dest,
)
}
}
// SimulateCreateSupplyDecreaseProposalContent generates random create-root-name proposal content
func SimulateCreateSupplyDecreaseProposalContent(k keeper.Keeper) simtypes.ContentSimulatorFn {
return func(r *rand.Rand, ctx sdk.Context, accs []simtypes.Account) simtypes.Content {
m := randomGovMarkerWithMaxStatus(r, ctx, k, types.StatusActive)
if m == nil {
return nil
}
currentSupply := k.CurrentEscrow(ctx, m).AmountOf(m.GetDenom())
if currentSupply.LT(sdk.OneInt()) {
return nil
}
burn := sdk.NewCoin(m.GetDenom(), simtypes.RandomAmount(r, currentSupply))
return types.NewSupplyDecreaseProposal(
simtypes.RandStringOfLength(r, 10),
simtypes.RandStringOfLength(r, 100),
burn,
)
}
}
// SimulateCreateSetAdministratorProposalContent generates random create-root-name proposal content
func SimulateCreateSetAdministratorProposalContent(k keeper.Keeper) simtypes.ContentSimulatorFn {
return func(r *rand.Rand, ctx sdk.Context, accs []simtypes.Account) simtypes.Content {
m := randomGovMarkerWithMaxStatus(r, ctx, k, types.StatusDestroyed)
if m == nil {
return nil
}
return types.NewSetAdministratorProposal(
simtypes.RandStringOfLength(r, 10),
simtypes.RandStringOfLength(r, 100),
m.GetDenom(),
randomAccessGrants(r, accs, 2, m.GetMarkerType()),
)
}
}
// SimulateCreateRemoveAdministratorProposalContent generates random create-root-name proposal content
func SimulateCreateRemoveAdministratorProposalContent(k keeper.Keeper) simtypes.ContentSimulatorFn {
return func(r *rand.Rand, ctx sdk.Context, accs []simtypes.Account) simtypes.Content {
simAccount, _ := simtypes.RandomAcc(r, accs)
m := randomGovMarkerWithMaxStatus(r, ctx, k, types.StatusDestroyed)
if m == nil {
return nil
}
return types.NewRemoveAdministratorProposal(
simtypes.RandStringOfLength(r, 10),
simtypes.RandStringOfLength(r, 100),
m.GetDenom(),
[]string{simAccount.Address.String()},
)
}
}
// SimulateCreateChangeStatusProposalContent generates random create-root-name proposal content
func SimulateCreateChangeStatusProposalContent(k keeper.Keeper) simtypes.ContentSimulatorFn {
return func(r *rand.Rand, ctx sdk.Context, accs []simtypes.Account) simtypes.Content {
m := randomGovMarkerWithMaxStatus(r, ctx, k, types.StatusCancelled)
if m == nil {
return nil
}
return types.NewChangeStatusProposal(
simtypes.RandStringOfLength(r, 10),
simtypes.RandStringOfLength(r, 100),
m.GetDenom(),
m.GetStatus()+1,
)
}
}
// SimulateSetDenomMetadataProposalContent generates random set denom metadata proposal content
func SimulateSetDenomMetadataProposalContent(k keeper.Keeper) simtypes.ContentSimulatorFn {
return func(r *rand.Rand, ctx sdk.Context, accs []simtypes.Account) simtypes.Content {
m := randomGovMarkerWithMaxStatus(r, ctx, k, types.StatusDestroyed)
if m == nil {
return nil
}
// Create 1 to 4 denom unit entries with 0 to 2 aliases.
display := m.GetDenom()
denomUnits := make([]*banktypes.DenomUnit, r.Intn(4)+1)
for di := range denomUnits {
du := banktypes.DenomUnit{
Denom: simtypes.RandStringOfLength(r, 20),
Exponent: 0,
Aliases: make([]string, r.Intn(3)),
}
if di == 0 {
// First entry needs to have the same denom as the metadata base.
du.Denom = m.GetDenom()
} else {
// First entry needs exponent 0, then they need to go up from there.
du.Exponent = denomUnits[di-1].Exponent + uint32(r.Intn(4)) + 1
}
// Actually set the aliases to something.
for ai := range du.Aliases {
du.Aliases[ai] = simtypes.RandStringOfLength(r, 20)
}
// Randomly decide to use this one as the display
if r.Intn(4) == 0 {
display = du.Denom
}
denomUnits[di] = &du
}
return types.NewSetDenomMetadataProposal(
simtypes.RandStringOfLength(r, 10),
simtypes.RandStringOfLength(r, 100),
banktypes.Metadata{
Description: simtypes.RandStringOfLength(r, 100),
Base: m.GetDenom(),
Display: display,
Name: simtypes.RandStringOfLength(r, 20),
Symbol: simtypes.RandStringOfLength(r, 5),
DenomUnits: denomUnits,
},
)
}
}
func randomGovMarkerWithMaxStatus(r *rand.Rand, ctx sdk.Context, k keeper.Keeper, status types.MarkerStatus) types.MarkerAccountI {
var markers []types.MarkerAccountI
k.IterateMarkers(ctx, func(marker types.MarkerAccountI) (stop bool) {
if marker.HasGovernanceEnabled() && marker.GetStatus() <= status {
markers = append(markers, marker)
}
return false
})
if len(markers) == 0 {
return nil
}
idx := r.Intn(len(markers))
return markers[idx]
}