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.

47 lines
1.2 KiB

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