cosmos-sdk/x/simulation/operation.go

159 lines
4.4 KiB
Go
Raw Normal View History

2019-03-14 11:13:15 -07:00
package simulation
import (
"encoding/json"
"math/rand"
"sort"
"github.com/cosmos/cosmos-sdk/types/simulation"
2019-03-14 11:13:15 -07:00
)
// entry kinds for use within OperationEntry
const (
BeginBlockEntryKind = "begin_block"
EndBlockEntryKind = "end_block"
MsgEntryKind = "msg"
QueuedMsgEntryKind = "queued_msg"
2019-03-14 11:13:15 -07:00
)
// OperationEntry - an operation entry for logging (ex. BeginBlock, EndBlock, XxxMsg, etc)
type OperationEntry struct {
2019-07-05 16:25:56 -07:00
EntryKind string `json:"entry_kind" yaml:"entry_kind"`
Height int64 `json:"height" yaml:"height"`
Order int64 `json:"order" yaml:"order"`
Operation json.RawMessage `json:"operation" yaml:"operation"`
2019-03-14 11:13:15 -07:00
}
// NewOperationEntry creates a new OperationEntry instance
func NewOperationEntry(entry string, height, order int64, op json.RawMessage) OperationEntry {
2019-03-14 11:13:15 -07:00
return OperationEntry{
EntryKind: entry,
2019-03-14 11:13:15 -07:00
Height: height,
Order: order,
Operation: op,
2019-03-14 11:13:15 -07:00
}
}
// BeginBlockEntry - operation entry for begin block
func BeginBlockEntry(height int64) OperationEntry {
return NewOperationEntry(BeginBlockEntryKind, height, -1, nil)
}
2019-03-14 11:13:15 -07:00
// EndBlockEntry - operation entry for end block
func EndBlockEntry(height int64) OperationEntry {
return NewOperationEntry(EndBlockEntryKind, height, -1, nil)
2019-03-14 11:13:15 -07:00
}
// MsgEntry - operation entry for standard msg
func MsgEntry(height, order int64, opMsg simulation.OperationMsg) OperationEntry {
return NewOperationEntry(MsgEntryKind, height, order, opMsg.MustMarshal())
2019-03-14 11:13:15 -07:00
}
// QueuedMsgEntry creates an operation entry for a given queued message.
func QueuedMsgEntry(height int64, opMsg simulation.OperationMsg) OperationEntry {
return NewOperationEntry(QueuedMsgEntryKind, height, -1, opMsg.MustMarshal())
2019-03-14 11:13:15 -07:00
}
// MustMarshal marshals the operation entry, panic on error.
2019-03-14 11:13:15 -07:00
func (oe OperationEntry) MustMarshal() json.RawMessage {
out, err := json.Marshal(oe)
if err != nil {
panic(err)
}
return out
}
//_____________________________________________________________________
// OperationQueue defines an object for a queue of operations
type OperationQueue map[int][]simulation.Operation
2019-03-14 11:13:15 -07:00
// NewOperationQueue creates a new OperationQueue instance.
func NewOperationQueue() OperationQueue {
return make(OperationQueue)
2019-03-14 11:13:15 -07:00
}
// queueOperations adds all future operations into the operation queue.
2019-03-14 11:13:15 -07:00
func queueOperations(queuedOps OperationQueue,
queuedTimeOps []simulation.FutureOperation, futureOps []simulation.FutureOperation) {
2019-03-14 11:13:15 -07:00
if futureOps == nil {
return
}
for _, futureOp := range futureOps {
2019-10-17 06:47:35 -07:00
futureOp := futureOp
2019-03-14 11:13:15 -07:00
if futureOp.BlockHeight != 0 {
if val, ok := queuedOps[futureOp.BlockHeight]; ok {
queuedOps[futureOp.BlockHeight] = append(val, futureOp.Op)
} else {
queuedOps[futureOp.BlockHeight] = []simulation.Operation{futureOp.Op}
2019-03-14 11:13:15 -07:00
}
continue
}
// TODO: Replace with proper sorted data structure, so don't have the
// copy entire slice
index := sort.Search(
len(queuedTimeOps),
func(i int) bool {
return queuedTimeOps[i].BlockTime.After(futureOp.BlockTime)
},
)
queuedTimeOps = append(queuedTimeOps, simulation.FutureOperation{})
2019-03-14 11:13:15 -07:00
copy(queuedTimeOps[index+1:], queuedTimeOps[index:])
queuedTimeOps[index] = futureOp
}
}
//________________________________________________________________________
// WeightedOperation is an operation with associated weight.
// This is used to bias the selection operation within the simulator.
type WeightedOperation struct {
weight int
op simulation.Operation
}
func (w WeightedOperation) Weight() int {
return w.weight
}
func (w WeightedOperation) Op() simulation.Operation {
return w.op
2019-03-14 11:13:15 -07:00
}
Sim refactor 3: move weighted operations to modules (#4869) * move GenesisState generators to modules * minor change on slashing genState generator * move simulation params back to modules (#4839) move simulation params back to modules (#4839) * cleanup params * various fixes * move store decoders to modules * fix * module pattern * split generators for param change * param changes * revert util pkg * banksim * compile * update Decoders params * fix * address @colin-axner comments * move weighted operations to modules * cleanup * cleanup * Update cmd_test.go * simulation manager * mino fixes * cleanup * add GenerateGenesisState to simulation manager * Apply suggestions from code review Co-Authored-By: frog power 4000 <rigel.rozanski@gmail.com> * address @rigelrozanski comments * changelog * Apply suggestions from code review Co-Authored-By: colin axner <colinaxner@berkeley.edu> * restructure modules simulation pkgs * remove cycle deps * rename funcs and add missing params * modularize simulator param changes * build * fix params keys * make format * various fixes * fix tests * minor updates to sim_test * cleanup * more cleanup * modularize genesis generators * minor cleanup * remove cdc from generators * remove cdc * add get or generate * fix non-determinism in simulation * changelog and x/simulation godoc * cleanup operations * update operations to use baseapp * updates and cleanup operations * update operations * restructure sim ops params * rename sim /operations/msg.go to /operations.go * move GenTx to a helper pkg to avoid circle deps * rm msg.ValidateBasic * changelog * random fees; delete auth's DeductFees sim operation * add chain-id for sig verification * Update x/simulation/account.go Co-Authored-By: colin axner <colinaxner@berkeley.edu> * fix bank, gov and distr errors * fix staking and slashing errors; increase prob for send enabled * increase gas x10 * make format * fix some distr and staking edge cases * fix all edge cases * golang ci * rename acc vars; default no fees to 0stake * cleanup; check for exchange rate and skip invalid ops * fixes * check for max entries * add pubkey to genaccounts * fix gov bug * update staking sim ops * fix small redelegation error * fix small self delegation on unjail * rm inf loop on random val/accs * copy array * add ok boolean to RandomValidator return values * format * build * add WeightedOperations to AppModuleSimulation * define each module proposals content as part of the module pattern * Update x/bank/simulation/operations.go Co-Authored-By: colin axner <colinaxner@berkeley.edu> * Update simapp/helpers/test_helpers.go Co-Authored-By: colin axner <colinaxner@berkeley.edu> * address @colin-axner comments * add genaccount pubkey validation * fix test * update operations and move RandomFees to x/simulation * update gov ops * address @alexanderbez comments * avoid modifications to config * reorder params * modularized sim operations working * changelog * Update types/module/simulation.go Co-Authored-By: frog power 4000 <rigel.rozanski@gmail.com> * Update x/simulation/params.go Co-Authored-By: frog power 4000 <rigel.rozanski@gmail.com> * Update x/simulation/params.go Co-Authored-By: frog power 4000 <rigel.rozanski@gmail.com> * update /types/module * Update x/distribution/simulation/genesis.go Co-Authored-By: Alexander Bezobchuk <alexanderbez@users.noreply.github.com> * remove named return values * ensure all operations are simulated * golangci * add nolint * disable whitespace and funlen linter * disable godox * add TODO on unjail * update ops weights * remove dup * update godoc * remove unused func * build fixes * move weights to the same file * scopelint * changelog * address @AdityaSripal comments * address @alexanderbez comments
2019-12-05 01:29:54 -08:00
// NewWeightedOperation creates a new WeightedOperation instance
func NewWeightedOperation(weight int, op simulation.Operation) WeightedOperation {
Sim refactor 3: move weighted operations to modules (#4869) * move GenesisState generators to modules * minor change on slashing genState generator * move simulation params back to modules (#4839) move simulation params back to modules (#4839) * cleanup params * various fixes * move store decoders to modules * fix * module pattern * split generators for param change * param changes * revert util pkg * banksim * compile * update Decoders params * fix * address @colin-axner comments * move weighted operations to modules * cleanup * cleanup * Update cmd_test.go * simulation manager * mino fixes * cleanup * add GenerateGenesisState to simulation manager * Apply suggestions from code review Co-Authored-By: frog power 4000 <rigel.rozanski@gmail.com> * address @rigelrozanski comments * changelog * Apply suggestions from code review Co-Authored-By: colin axner <colinaxner@berkeley.edu> * restructure modules simulation pkgs * remove cycle deps * rename funcs and add missing params * modularize simulator param changes * build * fix params keys * make format * various fixes * fix tests * minor updates to sim_test * cleanup * more cleanup * modularize genesis generators * minor cleanup * remove cdc from generators * remove cdc * add get or generate * fix non-determinism in simulation * changelog and x/simulation godoc * cleanup operations * update operations to use baseapp * updates and cleanup operations * update operations * restructure sim ops params * rename sim /operations/msg.go to /operations.go * move GenTx to a helper pkg to avoid circle deps * rm msg.ValidateBasic * changelog * random fees; delete auth's DeductFees sim operation * add chain-id for sig verification * Update x/simulation/account.go Co-Authored-By: colin axner <colinaxner@berkeley.edu> * fix bank, gov and distr errors * fix staking and slashing errors; increase prob for send enabled * increase gas x10 * make format * fix some distr and staking edge cases * fix all edge cases * golang ci * rename acc vars; default no fees to 0stake * cleanup; check for exchange rate and skip invalid ops * fixes * check for max entries * add pubkey to genaccounts * fix gov bug * update staking sim ops * fix small redelegation error * fix small self delegation on unjail * rm inf loop on random val/accs * copy array * add ok boolean to RandomValidator return values * format * build * add WeightedOperations to AppModuleSimulation * define each module proposals content as part of the module pattern * Update x/bank/simulation/operations.go Co-Authored-By: colin axner <colinaxner@berkeley.edu> * Update simapp/helpers/test_helpers.go Co-Authored-By: colin axner <colinaxner@berkeley.edu> * address @colin-axner comments * add genaccount pubkey validation * fix test * update operations and move RandomFees to x/simulation * update gov ops * address @alexanderbez comments * avoid modifications to config * reorder params * modularized sim operations working * changelog * Update types/module/simulation.go Co-Authored-By: frog power 4000 <rigel.rozanski@gmail.com> * Update x/simulation/params.go Co-Authored-By: frog power 4000 <rigel.rozanski@gmail.com> * Update x/simulation/params.go Co-Authored-By: frog power 4000 <rigel.rozanski@gmail.com> * update /types/module * Update x/distribution/simulation/genesis.go Co-Authored-By: Alexander Bezobchuk <alexanderbez@users.noreply.github.com> * remove named return values * ensure all operations are simulated * golangci * add nolint * disable whitespace and funlen linter * disable godox * add TODO on unjail * update ops weights * remove dup * update godoc * remove unused func * build fixes * move weights to the same file * scopelint * changelog * address @AdityaSripal comments * address @alexanderbez comments
2019-12-05 01:29:54 -08:00
return WeightedOperation{
weight: weight,
op: op,
Sim refactor 3: move weighted operations to modules (#4869) * move GenesisState generators to modules * minor change on slashing genState generator * move simulation params back to modules (#4839) move simulation params back to modules (#4839) * cleanup params * various fixes * move store decoders to modules * fix * module pattern * split generators for param change * param changes * revert util pkg * banksim * compile * update Decoders params * fix * address @colin-axner comments * move weighted operations to modules * cleanup * cleanup * Update cmd_test.go * simulation manager * mino fixes * cleanup * add GenerateGenesisState to simulation manager * Apply suggestions from code review Co-Authored-By: frog power 4000 <rigel.rozanski@gmail.com> * address @rigelrozanski comments * changelog * Apply suggestions from code review Co-Authored-By: colin axner <colinaxner@berkeley.edu> * restructure modules simulation pkgs * remove cycle deps * rename funcs and add missing params * modularize simulator param changes * build * fix params keys * make format * various fixes * fix tests * minor updates to sim_test * cleanup * more cleanup * modularize genesis generators * minor cleanup * remove cdc from generators * remove cdc * add get or generate * fix non-determinism in simulation * changelog and x/simulation godoc * cleanup operations * update operations to use baseapp * updates and cleanup operations * update operations * restructure sim ops params * rename sim /operations/msg.go to /operations.go * move GenTx to a helper pkg to avoid circle deps * rm msg.ValidateBasic * changelog * random fees; delete auth's DeductFees sim operation * add chain-id for sig verification * Update x/simulation/account.go Co-Authored-By: colin axner <colinaxner@berkeley.edu> * fix bank, gov and distr errors * fix staking and slashing errors; increase prob for send enabled * increase gas x10 * make format * fix some distr and staking edge cases * fix all edge cases * golang ci * rename acc vars; default no fees to 0stake * cleanup; check for exchange rate and skip invalid ops * fixes * check for max entries * add pubkey to genaccounts * fix gov bug * update staking sim ops * fix small redelegation error * fix small self delegation on unjail * rm inf loop on random val/accs * copy array * add ok boolean to RandomValidator return values * format * build * add WeightedOperations to AppModuleSimulation * define each module proposals content as part of the module pattern * Update x/bank/simulation/operations.go Co-Authored-By: colin axner <colinaxner@berkeley.edu> * Update simapp/helpers/test_helpers.go Co-Authored-By: colin axner <colinaxner@berkeley.edu> * address @colin-axner comments * add genaccount pubkey validation * fix test * update operations and move RandomFees to x/simulation * update gov ops * address @alexanderbez comments * avoid modifications to config * reorder params * modularized sim operations working * changelog * Update types/module/simulation.go Co-Authored-By: frog power 4000 <rigel.rozanski@gmail.com> * Update x/simulation/params.go Co-Authored-By: frog power 4000 <rigel.rozanski@gmail.com> * Update x/simulation/params.go Co-Authored-By: frog power 4000 <rigel.rozanski@gmail.com> * update /types/module * Update x/distribution/simulation/genesis.go Co-Authored-By: Alexander Bezobchuk <alexanderbez@users.noreply.github.com> * remove named return values * ensure all operations are simulated * golangci * add nolint * disable whitespace and funlen linter * disable godox * add TODO on unjail * update ops weights * remove dup * update godoc * remove unused func * build fixes * move weights to the same file * scopelint * changelog * address @AdityaSripal comments * address @alexanderbez comments
2019-12-05 01:29:54 -08:00
}
}
2019-03-14 11:13:15 -07:00
// WeightedOperations is the group of all weighted operations to simulate.
type WeightedOperations []simulation.WeightedOperation
2019-03-14 11:13:15 -07:00
func (ops WeightedOperations) totalWeight() int {
totalOpWeight := 0
for _, op := range ops {
totalOpWeight += op.Weight()
2019-03-14 11:13:15 -07:00
}
return totalOpWeight
}
func (ops WeightedOperations) getSelectOpFn() simulation.SelectOpFn {
2019-03-14 11:13:15 -07:00
totalOpWeight := ops.totalWeight()
return func(r *rand.Rand) simulation.Operation {
2019-03-14 11:13:15 -07:00
x := r.Intn(totalOpWeight)
for i := 0; i < len(ops); i++ {
if x <= ops[i].Weight() {
return ops[i].Op()
2019-03-14 11:13:15 -07:00
}
x -= ops[i].Weight()
2019-03-14 11:13:15 -07:00
}
// shouldn't happen
return ops[0].Op()
2019-03-14 11:13:15 -07:00
}
}