test/factory: pass testing.T around rather than errors for test fixtures (#7518)

This commit is contained in:
Sam Kleinman
2022-01-07 15:51:39 -05:00
committed by GitHub
parent 90cf742065
commit d5c39f907d
28 changed files with 205 additions and 205 deletions
+5 -15
View File
@@ -1,8 +1,10 @@
package factory
import (
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/crypto/tmhash"
"github.com/tendermint/tendermint/types"
@@ -17,13 +19,6 @@ var (
DefaultTestTime = time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)
)
func MakeVersion() version.Consensus {
return version.Consensus{
Block: version.BlockProtocol,
App: 1,
}
}
func RandomAddress() []byte {
return crypto.CRandBytes(crypto.AddressSize)
}
@@ -48,7 +43,8 @@ func MakeBlockIDWithHash(hash []byte) types.BlockID {
// MakeHeader fills the rest of the contents of the header such that it passes
// validate basic
func MakeHeader(h *types.Header) (*types.Header, error) {
func MakeHeader(t *testing.T, h *types.Header) *types.Header {
t.Helper()
if h.Version.Block == 0 {
h.Version.Block = version.BlockProtocol
}
@@ -89,13 +85,7 @@ func MakeHeader(h *types.Header) (*types.Header, error) {
h.ProposerAddress = RandomAddress()
}
return h, h.ValidateBasic()
}
require.NoError(t, h.ValidateBasic())
func MakeRandomHeader() *types.Header {
h, err := MakeHeader(&types.Header{})
if err != nil {
panic(err)
}
return h
}
+8 -15
View File
@@ -2,7 +2,6 @@ package factory
import (
"context"
"fmt"
"time"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
@@ -10,12 +9,11 @@ import (
)
func MakeCommit(ctx context.Context, blockID types.BlockID, height int64, round int32, voteSet *types.VoteSet, validators []types.PrivValidator, now time.Time) (*types.Commit, error) {
// all sign
for i := 0; i < len(validators); i++ {
pubKey, err := validators[i].GetPubKey(ctx)
if err != nil {
return nil, fmt.Errorf("can't get pubkey: %w", err)
return nil, err
}
vote := &types.Vote{
ValidatorAddress: pubKey.Address(),
@@ -27,21 +25,16 @@ func MakeCommit(ctx context.Context, blockID types.BlockID, height int64, round
Timestamp: now,
}
_, err = signAddVote(ctx, validators[i], vote, voteSet)
if err != nil {
v := vote.ToProto()
if err := validators[i].SignVote(ctx, voteSet.ChainID(), v); err != nil {
return nil, err
}
vote.Signature = v.Signature
if _, err := voteSet.AddVote(vote); err != nil {
return nil, err
}
}
return voteSet.MakeCommit(), nil
}
func signAddVote(ctx context.Context, privVal types.PrivValidator, vote *types.Vote, voteSet *types.VoteSet) (signed bool, err error) {
v := vote.ToProto()
err = privVal.SignVote(ctx, voteSet.ChainID(), v)
if err != nil {
return false, err
}
vote.Signature = v.Signature
return voteSet.AddVote(vote)
}
+2 -5
View File
@@ -3,16 +3,13 @@ package factory
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/tendermint/tendermint/types"
)
func TestMakeHeader(t *testing.T) {
_, err := MakeHeader(&types.Header{})
assert.NoError(t, err)
MakeHeader(t, &types.Header{})
}
func TestRandomNodeID(t *testing.T) {
assert.NotPanics(t, func() { RandomNodeID() })
RandomNodeID(t)
}
+6 -8
View File
@@ -3,24 +3,22 @@ package factory
import (
"context"
"sort"
"testing"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/config"
tmtime "github.com/tendermint/tendermint/libs/time"
"github.com/tendermint/tendermint/types"
)
func RandGenesisDoc(
ctx context.Context,
cfg *config.Config,
numValidators int,
randPower bool,
minPower int64,
) (*types.GenesisDoc, []types.PrivValidator) {
func RandGenesisDoc(ctx context.Context, t *testing.T, cfg *config.Config, numValidators int, randPower bool, minPower int64) (*types.GenesisDoc, []types.PrivValidator) {
t.Helper()
validators := make([]types.GenesisValidator, numValidators)
privValidators := make([]types.PrivValidator, numValidators)
for i := 0; i < numValidators; i++ {
val, privVal := RandValidator(ctx, randPower, minPower)
val, privVal, err := RandValidator(ctx, randPower, minPower)
require.NoError(t, err)
validators[i] = types.GenesisValidator{
PubKey: val.PubKey,
Power: val.VotingPower,
+12 -8
View File
@@ -3,25 +3,29 @@ package factory
import (
"encoding/hex"
"strings"
"testing"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/libs/rand"
"github.com/tendermint/tendermint/types"
)
// NodeID returns a valid NodeID based on an inputted string
func NodeID(str string) types.NodeID {
func NodeID(t *testing.T, str string) types.NodeID {
t.Helper()
id, err := types.NewNodeID(strings.Repeat(str, 2*types.NodeIDByteLength))
if err != nil {
panic(err)
}
require.NoError(t, err)
return id
}
// RandomNodeID returns a randomly generated valid NodeID
func RandomNodeID() types.NodeID {
func RandomNodeID(t *testing.T) types.NodeID {
t.Helper()
id, err := types.NewNodeID(hex.EncodeToString(rand.Bytes(types.NodeIDByteLength)))
if err != nil {
panic(err)
}
require.NoError(t, err)
return id
}
+4 -9
View File
@@ -2,15 +2,10 @@ package factory
import "github.com/tendermint/tendermint/types"
// MakeTxs is a helper function to generate mock transactions by given the block height
// and the transaction numbers.
func MakeTxs(height int64, num int) (txs []types.Tx) {
for i := 0; i < num; i++ {
txs = append(txs, types.Tx([]byte{byte(height), byte(i)}))
func MakeTenTxs(height int64) []types.Tx {
txs := make([]types.Tx, 10)
for i := range txs {
txs[i] = types.Tx([]byte{byte(height), byte(i)})
}
return txs
}
func MakeTenTxs(height int64) (txs []types.Tx) {
return MakeTxs(height, 10)
}
+10 -5
View File
@@ -5,11 +5,13 @@ import (
"fmt"
"math/rand"
"sort"
"testing"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/types"
)
func RandValidator(ctx context.Context, randPower bool, minPower int64) (*types.Validator, types.PrivValidator) {
func RandValidator(ctx context.Context, randPower bool, minPower int64) (*types.Validator, types.PrivValidator, error) {
privVal := types.NewMockPV()
votePower := minPower
if randPower {
@@ -18,20 +20,23 @@ func RandValidator(ctx context.Context, randPower bool, minPower int64) (*types.
}
pubKey, err := privVal.GetPubKey(ctx)
if err != nil {
panic(fmt.Errorf("could not retrieve pubkey %w", err))
return nil, nil, fmt.Errorf("could not retrieve public key: %w", err)
}
val := types.NewValidator(pubKey, votePower)
return val, privVal
return val, privVal, err
}
func RandValidatorSet(ctx context.Context, numValidators int, votingPower int64) (*types.ValidatorSet, []types.PrivValidator) {
func RandValidatorSet(ctx context.Context, t *testing.T, numValidators int, votingPower int64) (*types.ValidatorSet, []types.PrivValidator) {
var (
valz = make([]*types.Validator, numValidators)
privValidators = make([]types.PrivValidator, numValidators)
)
t.Helper()
for i := 0; i < numValidators; i++ {
val, privValidator := RandValidator(ctx, false, votingPower)
val, privValidator, err := RandValidator(ctx, false, votingPower)
require.NoError(t, err)
valz[i] = val
privValidators[i] = privValidator
}
+4 -3
View File
@@ -23,6 +23,7 @@ func MakeVote(
if err != nil {
return nil, err
}
v := &types.Vote{
ValidatorAddress: pubKey.Address(),
ValidatorIndex: valIndex,
@@ -34,10 +35,10 @@ func MakeVote(
}
vpb := v.ToProto()
err = val.SignVote(ctx, chainID, vpb)
if err != nil {
panic(err)
if err := val.SignVote(ctx, chainID, vpb); err != nil {
return nil, err
}
v.Signature = vpb.Signature
return v, nil
}