/
actions.go
68 lines (55 loc) · 2.06 KB
/
actions.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
package executortypes
import (
"fmt"
"math/rand"
"github.com/cosmos/cosmos-sdk/types/simulation"
legacysimexec "github.com/cosmos/cosmos-sdk/x/simulation"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/osmosis-labs/osmosis/v24/simulation/simtypes"
)
type selectActionFn func(r *rand.Rand) simtypes.ActionsWithMetadata
func GetSelectActionFn(actions []simtypes.ActionsWithMetadata) selectActionFn {
totalOpFrequency := totalFrequency(actions)
return func(r *rand.Rand) simtypes.ActionsWithMetadata {
x := r.Intn(totalOpFrequency)
// TODO: Change to an accum list approach
for i := 0; i < len(actions); i++ {
if x <= mapFrequencyFromString(actions[i].Frequency()) {
return actions[i]
}
x -= mapFrequencyFromString(actions[i].Frequency())
}
// shouldn't happen
return actions[0]
}
}
func ActionsFromWeightedOperations(ops legacysimexec.WeightedOperations) []simtypes.Action {
return actionsFromWeightedOperations("no module name", ops)
}
func actionsFromWeightedOperations(moduleName string, ops legacysimexec.WeightedOperations) []simtypes.Action {
actions := make([]simtypes.Action, 0, len(ops))
for _, op := range ops {
actions = append(actions, weightedOperationAction{moduleName: moduleName, op: op})
}
return actions
}
type weightedOperationAction struct {
moduleName string
frequency simtypes.Frequency
op simulation.WeightedOperation
}
func (a weightedOperationAction) WithFrequency(w simtypes.Frequency) simtypes.Action {
a.frequency = w
return a
}
func (a weightedOperationAction) Name() string { return fmt.Sprintf("%s: weighted_op", a.moduleName) }
func (a weightedOperationAction) Frequency() simtypes.Frequency {
return mapFrequencyFromInt(a.op.Weight())
}
func (a weightedOperationAction) Execute(sim *simtypes.SimCtx, ctx sdk.Context) (
simulation.OperationMsg, []simulation.FutureOperation, []byte, error,
) {
op, futureOp, err := a.op.Op()(sim.GetRand(), sim.BaseApp(), ctx, sim.Accounts, sim.ChainID())
// pass in an empty byte array for response data for all weighted operations
return op, futureOp, []byte{}, err
}