You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.1 KiB

  1. package factory
  2. import (
  3. "context"
  4. "fmt"
  5. "math/rand"
  6. "sort"
  7. "github.com/tendermint/tendermint/types"
  8. )
  9. func RandValidator(randPower bool, minPower int64) (*types.Validator, types.PrivValidator) {
  10. privVal := types.NewMockPV()
  11. votePower := minPower
  12. if randPower {
  13. // nolint:gosec // G404: Use of weak random number generator
  14. votePower += int64(rand.Uint32())
  15. }
  16. pubKey, err := privVal.GetPubKey(context.Background())
  17. if err != nil {
  18. panic(fmt.Errorf("could not retrieve pubkey %w", err))
  19. }
  20. val := types.NewValidator(pubKey, votePower)
  21. return val, privVal
  22. }
  23. func RandValidatorSet(numValidators int, votingPower int64) (*types.ValidatorSet, []types.PrivValidator) {
  24. var (
  25. valz = make([]*types.Validator, numValidators)
  26. privValidators = make([]types.PrivValidator, numValidators)
  27. )
  28. for i := 0; i < numValidators; i++ {
  29. val, privValidator := RandValidator(false, votingPower)
  30. valz[i] = val
  31. privValidators[i] = privValidator
  32. }
  33. sort.Sort(types.PrivValidatorsByAddress(privValidators))
  34. return types.NewValidatorSet(valz), privValidators
  35. }