cosmos-sdk/simsx/v2/msg_factory.go
Alexander Peters cf721a6540
feat(sims): Integration with app v2 (#23013)
Co-authored-by: Alex | Interchain Labs <alex@skip.money>
2025-01-09 08:19:35 +00:00

32 lines
848 B
Go

package v2
import (
"math/rand"
"github.com/cosmos/cosmos-sdk/simsx"
)
// NextFactoryFn shuffles and processes a list of weighted factories, returning a selection function for factory objects.
func NextFactoryFn(factories []simsx.WeightedFactory, r *rand.Rand) func() simsx.SimMsgFactoryX {
factCount := len(factories)
r.Shuffle(factCount, func(i, j int) {
factories[i], factories[j] = factories[j], factories[i]
})
var totalWeight int
for _, f := range factories {
totalWeight += int(f.Weight)
}
return func() simsx.SimMsgFactoryX {
// this is copied from old sims WeightedOperations.getSelectOpFn
x := r.Intn(totalWeight)
for i := 0; i < factCount; i++ {
if x <= int(factories[i].Weight) {
return factories[i].Factory
}
x -= int(factories[i].Weight)
}
// shouldn't happen
return factories[0].Factory
}
}