mirror of
https://github.com/tendermint/tendermint.git
synced 2026-01-03 03:35:19 +00:00
test/factory: pass testing.T around rather than errors for test fixtures (#7518)
This commit is contained in:
@@ -144,7 +144,6 @@ func (rts *reactorTestSuite) addNode(
|
||||
time.Now(),
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
lastCommit = types.NewCommit(
|
||||
vote.Height,
|
||||
vote.Round,
|
||||
@@ -206,7 +205,7 @@ func TestReactor_AbruptDisconnect(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
defer os.RemoveAll(cfg.RootDir)
|
||||
|
||||
genDoc, privVals := factory.RandGenesisDoc(ctx, cfg, 1, false, 30)
|
||||
genDoc, privVals := factory.RandGenesisDoc(ctx, t, cfg, 1, false, 30)
|
||||
maxBlockHeight := int64(64)
|
||||
|
||||
rts := setup(ctx, t, genDoc, privVals[0], []int64{maxBlockHeight, 0}, 0)
|
||||
@@ -245,7 +244,7 @@ func TestReactor_SyncTime(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
defer os.RemoveAll(cfg.RootDir)
|
||||
|
||||
genDoc, privVals := factory.RandGenesisDoc(ctx, cfg, 1, false, 30)
|
||||
genDoc, privVals := factory.RandGenesisDoc(ctx, t, cfg, 1, false, 30)
|
||||
maxBlockHeight := int64(101)
|
||||
|
||||
rts := setup(ctx, t, genDoc, privVals[0], []int64{maxBlockHeight, 0}, 0)
|
||||
@@ -273,7 +272,7 @@ func TestReactor_NoBlockResponse(t *testing.T) {
|
||||
|
||||
defer os.RemoveAll(cfg.RootDir)
|
||||
|
||||
genDoc, privVals := factory.RandGenesisDoc(ctx, cfg, 1, false, 30)
|
||||
genDoc, privVals := factory.RandGenesisDoc(ctx, t, cfg, 1, false, 30)
|
||||
maxBlockHeight := int64(65)
|
||||
|
||||
rts := setup(ctx, t, genDoc, privVals[0], []int64{maxBlockHeight, 0}, 0)
|
||||
@@ -325,7 +324,7 @@ func TestReactor_BadBlockStopsPeer(t *testing.T) {
|
||||
defer os.RemoveAll(cfg.RootDir)
|
||||
|
||||
maxBlockHeight := int64(48)
|
||||
genDoc, privVals := factory.RandGenesisDoc(ctx, cfg, 1, false, 30)
|
||||
genDoc, privVals := factory.RandGenesisDoc(ctx, t, cfg, 1, false, 30)
|
||||
|
||||
rts := setup(ctx, t, genDoc, privVals[0], []int64{maxBlockHeight, 0, 0, 0, 0}, 1000)
|
||||
|
||||
@@ -359,7 +358,7 @@ func TestReactor_BadBlockStopsPeer(t *testing.T) {
|
||||
//
|
||||
// XXX: This causes a potential race condition.
|
||||
// See: https://github.com/tendermint/tendermint/issues/6005
|
||||
otherGenDoc, otherPrivVals := factory.RandGenesisDoc(ctx, cfg, 1, false, 30)
|
||||
otherGenDoc, otherPrivVals := factory.RandGenesisDoc(ctx, t, cfg, 1, false, 30)
|
||||
newNode := rts.network.MakeNode(ctx, t, p2ptest.NodeOptions{
|
||||
MaxPeers: uint16(len(rts.nodes) + 1),
|
||||
MaxConnected: uint16(len(rts.nodes) + 1),
|
||||
|
||||
@@ -41,7 +41,7 @@ func TestByzantinePrevoteEquivocation(t *testing.T) {
|
||||
tickerFunc := newMockTickerFunc(true)
|
||||
appFunc := newKVStore
|
||||
|
||||
genDoc, privVals := factory.RandGenesisDoc(ctx, config, nValidators, false, 30)
|
||||
genDoc, privVals := factory.RandGenesisDoc(ctx, t, config, nValidators, false, 30)
|
||||
states := make([]*State, nValidators)
|
||||
|
||||
for i := 0; i < nValidators; i++ {
|
||||
|
||||
@@ -741,7 +741,7 @@ func randConsensusState(
|
||||
configOpts ...func(*config.Config),
|
||||
) ([]*State, cleanupFunc) {
|
||||
|
||||
genDoc, privVals := factory.RandGenesisDoc(ctx, cfg, nValidators, false, 30)
|
||||
genDoc, privVals := factory.RandGenesisDoc(ctx, t, cfg, nValidators, false, 30)
|
||||
css := make([]*State, nValidators)
|
||||
logger := consensusLogger()
|
||||
|
||||
@@ -801,7 +801,7 @@ func randConsensusNetWithPeers(
|
||||
) ([]*State, *types.GenesisDoc, *config.Config, cleanupFunc) {
|
||||
t.Helper()
|
||||
|
||||
genDoc, privVals := factory.RandGenesisDoc(ctx, cfg, nValidators, false, testMinPower)
|
||||
genDoc, privVals := factory.RandGenesisDoc(ctx, t, cfg, nValidators, false, testMinPower)
|
||||
css := make([]*State, nPeers)
|
||||
logger := consensusLogger()
|
||||
|
||||
@@ -859,7 +859,7 @@ func randGenesisState(
|
||||
minPower int64,
|
||||
) (sm.State, []types.PrivValidator) {
|
||||
|
||||
genDoc, privValidators := factory.RandGenesisDoc(ctx, cfg, numValidators, randPower, minPower)
|
||||
genDoc, privValidators := factory.RandGenesisDoc(ctx, t, cfg, numValidators, randPower, minPower)
|
||||
s0, err := sm.MakeGenesisState(genDoc)
|
||||
require.NoError(t, err)
|
||||
return s0, privValidators
|
||||
|
||||
@@ -383,7 +383,7 @@ func TestReactorWithEvidence(t *testing.T) {
|
||||
tickerFunc := newMockTickerFunc(true)
|
||||
appFunc := newKVStore
|
||||
|
||||
genDoc, privVals := factory.RandGenesisDoc(ctx, cfg, n, false, 30)
|
||||
genDoc, privVals := factory.RandGenesisDoc(ctx, t, cfg, n, false, 30)
|
||||
states := make([]*State, n)
|
||||
logger := consensusLogger()
|
||||
|
||||
|
||||
@@ -1023,8 +1023,7 @@ func TestHandshakePanicsIfAppReturnsWrongAppHash(t *testing.T) {
|
||||
genDoc, _ := sm.MakeGenesisDocFromFile(cfg.GenesisFile())
|
||||
state.LastValidators = state.Validators.Copy()
|
||||
// mode = 0 for committing all the blocks
|
||||
blocks, err := sf.MakeBlocks(ctx, 3, &state, privVal)
|
||||
require.NoError(t, err)
|
||||
blocks := sf.MakeBlocks(ctx, t, 3, &state, privVal)
|
||||
|
||||
store.chain = blocks
|
||||
|
||||
@@ -1287,7 +1286,8 @@ func TestHandshakeUpdatesValidators(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
val, _ := factory.RandValidator(ctx, true, 10)
|
||||
val, _, err := factory.RandValidator(ctx, true, 10)
|
||||
require.NoError(t, err)
|
||||
vals := types.NewValidatorSet([]*types.Validator{val})
|
||||
app := &initChainApp{vals: types.TM2PB.ValidatorUpdates(vals)}
|
||||
clientCreator := abciclient.NewLocalCreator(app)
|
||||
|
||||
@@ -33,7 +33,7 @@ func TestPeerCatchupRounds(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
valSet, privVals := factory.RandValidatorSet(ctx, 10, 1)
|
||||
valSet, privVals := factory.RandValidatorSet(ctx, t, 10, 1)
|
||||
|
||||
hvs := NewHeightVoteSet(cfg.ChainID(), 1, valSet)
|
||||
|
||||
|
||||
@@ -41,7 +41,7 @@ func TestEvidencePoolBasic(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
valSet, privVals := factory.RandValidatorSet(ctx, 1, 10)
|
||||
valSet, privVals := factory.RandValidatorSet(ctx, t, 1, 10)
|
||||
|
||||
blockStore.On("LoadBlockMeta", mock.AnythingOfType("int64")).Return(
|
||||
&types.BlockMeta{Header: types.Header{Time: defaultEvidenceTime}},
|
||||
|
||||
@@ -201,17 +201,16 @@ func TestVerifyLightClientAttack_Equivocation(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
conflictingVals, conflictingPrivVals := factory.RandValidatorSet(ctx, 5, 10)
|
||||
conflictingVals, conflictingPrivVals := factory.RandValidatorSet(ctx, t, 5, 10)
|
||||
|
||||
conflictingHeader, err := factory.MakeHeader(&types.Header{
|
||||
conflictingHeader := factory.MakeHeader(t, &types.Header{
|
||||
ChainID: evidenceChainID,
|
||||
Height: 10,
|
||||
Time: defaultEvidenceTime,
|
||||
ValidatorsHash: conflictingVals.Hash(),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
trustedHeader, _ := factory.MakeHeader(&types.Header{
|
||||
trustedHeader := factory.MakeHeader(t, &types.Header{
|
||||
ChainID: evidenceChainID,
|
||||
Height: 10,
|
||||
Time: defaultEvidenceTime,
|
||||
@@ -228,6 +227,7 @@ func TestVerifyLightClientAttack_Equivocation(t *testing.T) {
|
||||
voteSet := types.NewVoteSet(evidenceChainID, 10, 1, tmproto.SignedMsgType(2), conflictingVals)
|
||||
commit, err := factory.MakeCommit(ctx, blockID, 10, 1, voteSet, conflictingPrivVals[:4], defaultEvidenceTime)
|
||||
require.NoError(t, err)
|
||||
|
||||
ev := &types.LightClientAttackEvidence{
|
||||
ConflictingBlock: &types.LightBlock{
|
||||
SignedHeader: &types.SignedHeader{
|
||||
@@ -247,27 +247,26 @@ func TestVerifyLightClientAttack_Equivocation(t *testing.T) {
|
||||
trustedCommit, err := factory.MakeCommit(ctx, trustedBlockID, 10, 1,
|
||||
trustedVoteSet, conflictingPrivVals, defaultEvidenceTime)
|
||||
require.NoError(t, err)
|
||||
|
||||
trustedSignedHeader := &types.SignedHeader{
|
||||
Header: trustedHeader,
|
||||
Commit: trustedCommit,
|
||||
}
|
||||
|
||||
// good pass -> no error
|
||||
err = evidence.VerifyLightClientAttack(ev, trustedSignedHeader, trustedSignedHeader, conflictingVals,
|
||||
defaultEvidenceTime.Add(1*time.Minute), 2*time.Hour)
|
||||
assert.NoError(t, err)
|
||||
require.NoError(t, evidence.VerifyLightClientAttack(ev, trustedSignedHeader, trustedSignedHeader, conflictingVals,
|
||||
defaultEvidenceTime.Add(1*time.Minute), 2*time.Hour))
|
||||
|
||||
// trusted and conflicting hashes are the same -> an error should be returned
|
||||
err = evidence.VerifyLightClientAttack(ev, trustedSignedHeader, ev.ConflictingBlock.SignedHeader, conflictingVals,
|
||||
defaultEvidenceTime.Add(1*time.Minute), 2*time.Hour)
|
||||
assert.Error(t, err)
|
||||
assert.Error(t, evidence.VerifyLightClientAttack(ev, trustedSignedHeader, ev.ConflictingBlock.SignedHeader, conflictingVals,
|
||||
defaultEvidenceTime.Add(1*time.Minute), 2*time.Hour))
|
||||
|
||||
// conflicting header has different next validators hash which should have been correctly derived from
|
||||
// the previous round
|
||||
ev.ConflictingBlock.Header.NextValidatorsHash = crypto.CRandBytes(tmhash.Size)
|
||||
err = evidence.VerifyLightClientAttack(ev, trustedSignedHeader, trustedSignedHeader, nil,
|
||||
defaultEvidenceTime.Add(1*time.Minute), 2*time.Hour)
|
||||
assert.Error(t, err)
|
||||
assert.Error(t, evidence.VerifyLightClientAttack(ev, trustedSignedHeader, trustedSignedHeader, nil,
|
||||
defaultEvidenceTime.Add(1*time.Minute), 2*time.Hour))
|
||||
|
||||
// revert next validators hash
|
||||
ev.ConflictingBlock.Header.NextValidatorsHash = trustedHeader.NextValidatorsHash
|
||||
|
||||
@@ -300,17 +299,16 @@ func TestVerifyLightClientAttack_Amnesia(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
conflictingVals, conflictingPrivVals := factory.RandValidatorSet(ctx, 5, 10)
|
||||
conflictingVals, conflictingPrivVals := factory.RandValidatorSet(ctx, t, 5, 10)
|
||||
|
||||
conflictingHeader, err := factory.MakeHeader(&types.Header{
|
||||
conflictingHeader := factory.MakeHeader(t, &types.Header{
|
||||
ChainID: evidenceChainID,
|
||||
Height: height,
|
||||
Time: defaultEvidenceTime,
|
||||
ValidatorsHash: conflictingVals.Hash(),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
trustedHeader, _ := factory.MakeHeader(&types.Header{
|
||||
trustedHeader := factory.MakeHeader(t, &types.Header{
|
||||
ChainID: evidenceChainID,
|
||||
Height: height,
|
||||
Time: defaultEvidenceTime,
|
||||
@@ -327,6 +325,7 @@ func TestVerifyLightClientAttack_Amnesia(t *testing.T) {
|
||||
voteSet := types.NewVoteSet(evidenceChainID, height, 0, tmproto.SignedMsgType(2), conflictingVals)
|
||||
commit, err := factory.MakeCommit(ctx, blockID, height, 0, voteSet, conflictingPrivVals, defaultEvidenceTime)
|
||||
require.NoError(t, err)
|
||||
|
||||
ev := &types.LightClientAttackEvidence{
|
||||
ConflictingBlock: &types.LightBlock{
|
||||
SignedHeader: &types.SignedHeader{
|
||||
@@ -346,20 +345,19 @@ func TestVerifyLightClientAttack_Amnesia(t *testing.T) {
|
||||
trustedCommit, err := factory.MakeCommit(ctx, trustedBlockID, height, 1,
|
||||
trustedVoteSet, conflictingPrivVals, defaultEvidenceTime)
|
||||
require.NoError(t, err)
|
||||
|
||||
trustedSignedHeader := &types.SignedHeader{
|
||||
Header: trustedHeader,
|
||||
Commit: trustedCommit,
|
||||
}
|
||||
|
||||
// good pass -> no error
|
||||
err = evidence.VerifyLightClientAttack(ev, trustedSignedHeader, trustedSignedHeader, conflictingVals,
|
||||
defaultEvidenceTime.Add(1*time.Minute), 2*time.Hour)
|
||||
assert.NoError(t, err)
|
||||
require.NoError(t, evidence.VerifyLightClientAttack(ev, trustedSignedHeader, trustedSignedHeader, conflictingVals,
|
||||
defaultEvidenceTime.Add(1*time.Minute), 2*time.Hour))
|
||||
|
||||
// trusted and conflicting hashes are the same -> an error should be returned
|
||||
err = evidence.VerifyLightClientAttack(ev, trustedSignedHeader, ev.ConflictingBlock.SignedHeader, conflictingVals,
|
||||
defaultEvidenceTime.Add(1*time.Minute), 2*time.Hour)
|
||||
assert.Error(t, err)
|
||||
assert.Error(t, evidence.VerifyLightClientAttack(ev, trustedSignedHeader, ev.ConflictingBlock.SignedHeader, conflictingVals,
|
||||
defaultEvidenceTime.Add(1*time.Minute), 2*time.Hour))
|
||||
|
||||
state := sm.State{
|
||||
LastBlockTime: defaultEvidenceTime.Add(1 * time.Minute),
|
||||
@@ -496,14 +494,16 @@ func makeLunaticEvidence(
|
||||
totalVals, byzVals, phantomVals int,
|
||||
commonTime, attackTime time.Time,
|
||||
) (ev *types.LightClientAttackEvidence, trusted *types.LightBlock, common *types.LightBlock) {
|
||||
commonValSet, commonPrivVals := factory.RandValidatorSet(ctx, totalVals, defaultVotingPower)
|
||||
t.Helper()
|
||||
|
||||
commonValSet, commonPrivVals := factory.RandValidatorSet(ctx, t, totalVals, defaultVotingPower)
|
||||
|
||||
require.Greater(t, totalVals, byzVals)
|
||||
|
||||
// extract out the subset of byzantine validators in the common validator set
|
||||
byzValSet, byzPrivVals := commonValSet.Validators[:byzVals], commonPrivVals[:byzVals]
|
||||
|
||||
phantomValSet, phantomPrivVals := factory.RandValidatorSet(ctx, phantomVals, defaultVotingPower)
|
||||
phantomValSet, phantomPrivVals := factory.RandValidatorSet(ctx, t, phantomVals, defaultVotingPower)
|
||||
|
||||
conflictingVals := phantomValSet.Copy()
|
||||
require.NoError(t, conflictingVals.UpdateWithChangeSet(byzValSet))
|
||||
@@ -511,31 +511,30 @@ func makeLunaticEvidence(
|
||||
|
||||
conflictingPrivVals = orderPrivValsByValSet(ctx, t, conflictingVals, conflictingPrivVals)
|
||||
|
||||
commonHeader, err := factory.MakeHeader(&types.Header{
|
||||
commonHeader := factory.MakeHeader(t, &types.Header{
|
||||
ChainID: evidenceChainID,
|
||||
Height: commonHeight,
|
||||
Time: commonTime,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
trustedHeader, err := factory.MakeHeader(&types.Header{
|
||||
|
||||
trustedHeader := factory.MakeHeader(t, &types.Header{
|
||||
ChainID: evidenceChainID,
|
||||
Height: height,
|
||||
Time: defaultEvidenceTime,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
conflictingHeader, err := factory.MakeHeader(&types.Header{
|
||||
conflictingHeader := factory.MakeHeader(t, &types.Header{
|
||||
ChainID: evidenceChainID,
|
||||
Height: height,
|
||||
Time: attackTime,
|
||||
ValidatorsHash: conflictingVals.Hash(),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
blockID := factory.MakeBlockIDWithHash(conflictingHeader.Hash())
|
||||
voteSet := types.NewVoteSet(evidenceChainID, height, 1, tmproto.SignedMsgType(2), conflictingVals)
|
||||
commit, err := factory.MakeCommit(ctx, blockID, height, 1, voteSet, conflictingPrivVals, defaultEvidenceTime)
|
||||
require.NoError(t, err)
|
||||
|
||||
ev = &types.LightClientAttackEvidence{
|
||||
ConflictingBlock: &types.LightBlock{
|
||||
SignedHeader: &types.SignedHeader{
|
||||
@@ -559,10 +558,11 @@ func makeLunaticEvidence(
|
||||
ValidatorSet: commonValSet,
|
||||
}
|
||||
trustedBlockID := factory.MakeBlockIDWithHash(trustedHeader.Hash())
|
||||
trustedVals, privVals := factory.RandValidatorSet(ctx, totalVals, defaultVotingPower)
|
||||
trustedVals, privVals := factory.RandValidatorSet(ctx, t, totalVals, defaultVotingPower)
|
||||
trustedVoteSet := types.NewVoteSet(evidenceChainID, height, 1, tmproto.SignedMsgType(2), trustedVals)
|
||||
trustedCommit, err := factory.MakeCommit(ctx, trustedBlockID, height, 1, trustedVoteSet, privVals, defaultEvidenceTime)
|
||||
require.NoError(t, err)
|
||||
|
||||
trusted = &types.LightBlock{
|
||||
SignedHeader: &types.SignedHeader{
|
||||
Header: trustedHeader,
|
||||
|
||||
@@ -40,6 +40,7 @@ func newTestApp() proxy.AppConns {
|
||||
|
||||
func makeAndCommitGoodBlock(
|
||||
ctx context.Context,
|
||||
t *testing.T,
|
||||
state sm.State,
|
||||
height int64,
|
||||
lastCommit *types.Commit,
|
||||
@@ -47,64 +48,59 @@ func makeAndCommitGoodBlock(
|
||||
blockExec *sm.BlockExecutor,
|
||||
privVals map[string]types.PrivValidator,
|
||||
evidence []types.Evidence,
|
||||
) (sm.State, types.BlockID, *types.Commit, error) {
|
||||
) (sm.State, types.BlockID, *types.Commit) {
|
||||
t.Helper()
|
||||
|
||||
// A good block passes
|
||||
state, blockID, err := makeAndApplyGoodBlock(ctx, state, height, lastCommit, proposerAddr, blockExec, evidence)
|
||||
if err != nil {
|
||||
return state, types.BlockID{}, nil, err
|
||||
}
|
||||
state, blockID := makeAndApplyGoodBlock(ctx, t, state, height, lastCommit, proposerAddr, blockExec, evidence)
|
||||
|
||||
// Simulate a lastCommit for this block from all validators for the next height
|
||||
commit, err := makeValidCommit(ctx, height, blockID, state.Validators, privVals)
|
||||
if err != nil {
|
||||
return state, types.BlockID{}, nil, err
|
||||
}
|
||||
return state, blockID, commit, nil
|
||||
commit := makeValidCommit(ctx, t, height, blockID, state.Validators, privVals)
|
||||
|
||||
return state, blockID, commit
|
||||
}
|
||||
|
||||
func makeAndApplyGoodBlock(
|
||||
ctx context.Context,
|
||||
t *testing.T,
|
||||
state sm.State,
|
||||
height int64,
|
||||
lastCommit *types.Commit,
|
||||
proposerAddr []byte,
|
||||
blockExec *sm.BlockExecutor,
|
||||
evidence []types.Evidence,
|
||||
) (sm.State, types.BlockID, error) {
|
||||
) (sm.State, types.BlockID) {
|
||||
t.Helper()
|
||||
block, _, err := state.MakeBlock(height, factory.MakeTenTxs(height), lastCommit, evidence, proposerAddr)
|
||||
if err != nil {
|
||||
return state, types.BlockID{}, err
|
||||
}
|
||||
require.NoError(t, err)
|
||||
|
||||
if err := blockExec.ValidateBlock(state, block); err != nil {
|
||||
return state, types.BlockID{}, err
|
||||
}
|
||||
require.NoError(t, blockExec.ValidateBlock(state, block))
|
||||
blockID := types.BlockID{Hash: block.Hash(),
|
||||
PartSetHeader: types.PartSetHeader{Total: 3, Hash: tmrand.Bytes(32)}}
|
||||
state, err = blockExec.ApplyBlock(ctx, state, blockID, block)
|
||||
if err != nil {
|
||||
return state, types.BlockID{}, err
|
||||
}
|
||||
return state, blockID, nil
|
||||
require.NoError(t, err)
|
||||
|
||||
return state, blockID
|
||||
}
|
||||
|
||||
func makeValidCommit(
|
||||
ctx context.Context,
|
||||
t *testing.T,
|
||||
height int64,
|
||||
blockID types.BlockID,
|
||||
vals *types.ValidatorSet,
|
||||
privVals map[string]types.PrivValidator,
|
||||
) (*types.Commit, error) {
|
||||
) *types.Commit {
|
||||
t.Helper()
|
||||
sigs := make([]types.CommitSig, 0)
|
||||
for i := 0; i < vals.Size(); i++ {
|
||||
_, val := vals.GetByIndex(int32(i))
|
||||
vote, err := factory.MakeVote(ctx, privVals[val.Address.String()], chainID, int32(i), height, 0, 2, blockID, time.Now())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
require.NoError(t, err)
|
||||
sigs = append(sigs, vote.CommitSig())
|
||||
}
|
||||
return types.NewCommit(height, 0, blockID, sigs), nil
|
||||
|
||||
return types.NewCommit(height, 0, blockID, sigs)
|
||||
}
|
||||
|
||||
func makeState(t *testing.T, nVals, height int) (sm.State, dbm.DB, map[string]types.PrivValidator) {
|
||||
@@ -263,9 +259,16 @@ func makeRandomStateFromValidatorSet(
|
||||
}
|
||||
}
|
||||
|
||||
func makeRandomStateFromConsensusParams(ctx context.Context, consensusParams *types.ConsensusParams,
|
||||
height, lastHeightConsensusParamsChanged int64) sm.State {
|
||||
val, _ := factory.RandValidator(ctx, true, 10)
|
||||
func makeRandomStateFromConsensusParams(
|
||||
ctx context.Context,
|
||||
t *testing.T,
|
||||
consensusParams *types.ConsensusParams,
|
||||
height,
|
||||
lastHeightConsensusParamsChanged int64,
|
||||
) sm.State {
|
||||
t.Helper()
|
||||
val, _, err := factory.RandValidator(ctx, true, 10)
|
||||
require.NoError(t, err)
|
||||
valSet := types.NewValidatorSet([]*types.Validator{val})
|
||||
return sm.State{
|
||||
LastBlockHeight: height - 1,
|
||||
|
||||
@@ -113,7 +113,7 @@ func TestRollbackDifferentStateHeight(t *testing.T) {
|
||||
|
||||
func setupStateStore(ctx context.Context, t *testing.T, height int64) state.Store {
|
||||
stateStore := state.NewStore(dbm.NewMemDB())
|
||||
valSet, _ := factory.RandValidatorSet(ctx, 5, 10)
|
||||
valSet, _ := factory.RandValidatorSet(ctx, t, 5, 10)
|
||||
|
||||
params := types.DefaultConsensusParams()
|
||||
params.Version.AppVersion = 10
|
||||
|
||||
@@ -32,13 +32,15 @@ func TestStoreBootstrap(t *testing.T) {
|
||||
|
||||
stateDB := dbm.NewMemDB()
|
||||
stateStore := sm.NewStore(stateDB)
|
||||
val, _ := factory.RandValidator(ctx, true, 10)
|
||||
val2, _ := factory.RandValidator(ctx, true, 10)
|
||||
val3, _ := factory.RandValidator(ctx, true, 10)
|
||||
val, _, err := factory.RandValidator(ctx, true, 10)
|
||||
require.NoError(t, err)
|
||||
val2, _, err := factory.RandValidator(ctx, true, 10)
|
||||
require.NoError(t, err)
|
||||
val3, _, err := factory.RandValidator(ctx, true, 10)
|
||||
require.NoError(t, err)
|
||||
vals := types.NewValidatorSet([]*types.Validator{val, val2, val3})
|
||||
bootstrapState := makeRandomStateFromValidatorSet(vals, 100, 100)
|
||||
err := stateStore.Bootstrap(bootstrapState)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, stateStore.Bootstrap(bootstrapState))
|
||||
|
||||
// bootstrap should also save the previous validator
|
||||
_, err = stateStore.LoadValidators(99)
|
||||
@@ -61,17 +63,18 @@ func TestStoreLoadValidators(t *testing.T) {
|
||||
|
||||
stateDB := dbm.NewMemDB()
|
||||
stateStore := sm.NewStore(stateDB)
|
||||
val, _ := factory.RandValidator(ctx, true, 10)
|
||||
val2, _ := factory.RandValidator(ctx, true, 10)
|
||||
val3, _ := factory.RandValidator(ctx, true, 10)
|
||||
val, _, err := factory.RandValidator(ctx, true, 10)
|
||||
require.NoError(t, err)
|
||||
val2, _, err := factory.RandValidator(ctx, true, 10)
|
||||
require.NoError(t, err)
|
||||
val3, _, err := factory.RandValidator(ctx, true, 10)
|
||||
require.NoError(t, err)
|
||||
vals := types.NewValidatorSet([]*types.Validator{val, val2, val3})
|
||||
|
||||
// 1) LoadValidators loads validators using a height where they were last changed
|
||||
// Note that only the next validators at height h + 1 are saved
|
||||
err := stateStore.Save(makeRandomStateFromValidatorSet(vals, 1, 1))
|
||||
require.NoError(t, err)
|
||||
err = stateStore.Save(makeRandomStateFromValidatorSet(vals.CopyIncrementProposerPriority(1), 2, 1))
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, stateStore.Save(makeRandomStateFromValidatorSet(vals, 1, 1)))
|
||||
require.NoError(t, stateStore.Save(makeRandomStateFromValidatorSet(vals.CopyIncrementProposerPriority(1), 2, 1)))
|
||||
loadedVals, err := stateStore.LoadValidators(3)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, vals.CopyIncrementProposerPriority(3), loadedVals)
|
||||
@@ -153,7 +156,7 @@ func TestStoreLoadConsensusParams(t *testing.T) {
|
||||
|
||||
stateDB := dbm.NewMemDB()
|
||||
stateStore := sm.NewStore(stateDB)
|
||||
err := stateStore.Save(makeRandomStateFromConsensusParams(ctx, types.DefaultConsensusParams(), 1, 1))
|
||||
err := stateStore.Save(makeRandomStateFromConsensusParams(ctx, t, types.DefaultConsensusParams(), 1, 1))
|
||||
require.NoError(t, err)
|
||||
params, err := stateStore.LoadConsensusParams(1)
|
||||
require.NoError(t, err)
|
||||
@@ -163,7 +166,7 @@ func TestStoreLoadConsensusParams(t *testing.T) {
|
||||
// it should save a pointer to the params at height 1
|
||||
differentParams := types.DefaultConsensusParams()
|
||||
differentParams.Block.MaxBytes = 20000
|
||||
err = stateStore.Save(makeRandomStateFromConsensusParams(ctx, differentParams, 10, 1))
|
||||
err = stateStore.Save(makeRandomStateFromConsensusParams(ctx, t, differentParams, 10, 1))
|
||||
require.NoError(t, err)
|
||||
res, err := stateStore.LoadConsensusParams(10)
|
||||
require.NoError(t, err)
|
||||
|
||||
@@ -2,14 +2,18 @@ package factory
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
sm "github.com/tendermint/tendermint/internal/state"
|
||||
"github.com/tendermint/tendermint/internal/test/factory"
|
||||
"github.com/tendermint/tendermint/types"
|
||||
)
|
||||
|
||||
func MakeBlocks(ctx context.Context, n int, state *sm.State, privVal types.PrivValidator) ([]*types.Block, error) {
|
||||
func MakeBlocks(ctx context.Context, t *testing.T, n int, state *sm.State, privVal types.PrivValidator) []*types.Block {
|
||||
t.Helper()
|
||||
|
||||
blocks := make([]*types.Block, n)
|
||||
|
||||
var (
|
||||
@@ -21,10 +25,7 @@ func MakeBlocks(ctx context.Context, n int, state *sm.State, privVal types.PrivV
|
||||
for i := 0; i < n; i++ {
|
||||
height := int64(i + 1)
|
||||
|
||||
block, parts, err := makeBlockAndPartSet(ctx, *state, prevBlock, prevBlockMeta, privVal, height)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
block, parts := makeBlockAndPartSet(ctx, t, *state, prevBlock, prevBlockMeta, privVal, height)
|
||||
|
||||
blocks[i] = block
|
||||
|
||||
@@ -37,7 +38,7 @@ func MakeBlocks(ctx context.Context, n int, state *sm.State, privVal types.PrivV
|
||||
state.LastBlockHeight = height
|
||||
}
|
||||
|
||||
return blocks, nil
|
||||
return blocks
|
||||
}
|
||||
|
||||
func MakeBlock(state sm.State, height int64, c *types.Commit) (*types.Block, error) {
|
||||
@@ -57,24 +58,31 @@ func MakeBlock(state sm.State, height int64, c *types.Commit) (*types.Block, err
|
||||
|
||||
func makeBlockAndPartSet(
|
||||
ctx context.Context,
|
||||
t *testing.T,
|
||||
state sm.State,
|
||||
lastBlock *types.Block,
|
||||
lastBlockMeta *types.BlockMeta,
|
||||
privVal types.PrivValidator,
|
||||
height int64,
|
||||
) (*types.Block, *types.PartSet, error) {
|
||||
) (*types.Block, *types.PartSet) {
|
||||
t.Helper()
|
||||
|
||||
lastCommit := types.NewCommit(height-1, 0, types.BlockID{}, nil)
|
||||
if height > 1 {
|
||||
vote, _ := factory.MakeVote(
|
||||
vote, err := factory.MakeVote(
|
||||
ctx,
|
||||
privVal,
|
||||
lastBlock.Header.ChainID,
|
||||
1, lastBlock.Header.Height, 0, 2,
|
||||
lastBlockMeta.BlockID,
|
||||
time.Now())
|
||||
require.NoError(t, err)
|
||||
lastCommit = types.NewCommit(vote.Height, vote.Round,
|
||||
lastBlockMeta.BlockID, []types.CommitSig{vote.CommitSig()})
|
||||
}
|
||||
|
||||
return state.MakeBlock(height, []types.Tx{}, lastCommit, nil, state.Validators.GetProposer().Address)
|
||||
block, partSet, err := state.MakeBlock(height, []types.Tx{}, lastCommit, nil, state.Validators.GetProposer().Address)
|
||||
require.NoError(t, err)
|
||||
|
||||
return block, partSet
|
||||
}
|
||||
|
||||
@@ -103,10 +103,8 @@ func TestValidateBlockHeader(t *testing.T) {
|
||||
/*
|
||||
A good block passes
|
||||
*/
|
||||
var err error
|
||||
state, _, lastCommit, err = makeAndCommitGoodBlock(ctx,
|
||||
state, _, lastCommit = makeAndCommitGoodBlock(ctx, t,
|
||||
state, height, lastCommit, state.Validators.GetProposer().Address, blockExec, privVals, nil)
|
||||
require.NoError(t, err, "height %d", height)
|
||||
}
|
||||
|
||||
nextHeight := validationTestsStopHeight
|
||||
@@ -158,7 +156,7 @@ func TestValidateBlockCommit(t *testing.T) {
|
||||
state.LastBlockID,
|
||||
time.Now(),
|
||||
)
|
||||
require.NoError(t, err, "height %d", height)
|
||||
require.NoError(t, err)
|
||||
wrongHeightCommit := types.NewCommit(
|
||||
wrongHeightVote.Height,
|
||||
wrongHeightVote.Round,
|
||||
@@ -188,10 +186,10 @@ func TestValidateBlockCommit(t *testing.T) {
|
||||
/*
|
||||
A good block passes
|
||||
*/
|
||||
var err error
|
||||
var blockID types.BlockID
|
||||
state, blockID, lastCommit, err = makeAndCommitGoodBlock(
|
||||
state, blockID, lastCommit = makeAndCommitGoodBlock(
|
||||
ctx,
|
||||
t,
|
||||
state,
|
||||
height,
|
||||
lastCommit,
|
||||
@@ -200,7 +198,6 @@ func TestValidateBlockCommit(t *testing.T) {
|
||||
privVals,
|
||||
nil,
|
||||
)
|
||||
require.NoError(t, err, "height %d", height)
|
||||
|
||||
/*
|
||||
wrongSigsCommit is fine except for the extra bad precommit
|
||||
@@ -216,8 +213,7 @@ func TestValidateBlockCommit(t *testing.T) {
|
||||
blockID,
|
||||
time.Now(),
|
||||
)
|
||||
require.NoError(t, err, "height %d", height)
|
||||
|
||||
require.NoError(t, err)
|
||||
bpvPubKey, err := badPrivVal.GetPubKey(ctx)
|
||||
require.NoError(t, err)
|
||||
|
||||
@@ -319,9 +315,9 @@ func TestValidateBlockEvidence(t *testing.T) {
|
||||
evidence = append(evidence, newEv)
|
||||
}
|
||||
|
||||
var err error
|
||||
state, _, lastCommit, err = makeAndCommitGoodBlock(
|
||||
state, _, lastCommit = makeAndCommitGoodBlock(
|
||||
ctx,
|
||||
t,
|
||||
state,
|
||||
height,
|
||||
lastCommit,
|
||||
@@ -330,6 +326,6 @@ func TestValidateBlockEvidence(t *testing.T) {
|
||||
privVals,
|
||||
evidence,
|
||||
)
|
||||
require.NoError(t, err, "height %d", height)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -294,7 +294,7 @@ loop:
|
||||
|
||||
func mockLBResp(ctx context.Context, t *testing.T, peer types.NodeID, height int64, time time.Time) lightBlockResponse {
|
||||
t.Helper()
|
||||
vals, pv := factory.RandValidatorSet(ctx, 3, 10)
|
||||
vals, pv := factory.RandValidatorSet(ctx, t, 3, 10)
|
||||
_, _, lb := mockLB(ctx, t, height, time, factory.MakeBlockID(), vals, pv)
|
||||
return lightBlockResponse{
|
||||
block: lb,
|
||||
|
||||
@@ -76,7 +76,7 @@ func TestDispatcherReturnsNoBlock(t *testing.T) {
|
||||
|
||||
d := NewDispatcher(ch)
|
||||
|
||||
peer := factory.NodeID("a")
|
||||
peer := factory.NodeID(t, "a")
|
||||
|
||||
go func() {
|
||||
<-chans.Out
|
||||
@@ -99,7 +99,7 @@ func TestDispatcherTimeOutWaitingOnLightBlock(t *testing.T) {
|
||||
|
||||
_, ch := testChannel(100)
|
||||
d := NewDispatcher(ch)
|
||||
peer := factory.NodeID("a")
|
||||
peer := factory.NodeID(t, "a")
|
||||
|
||||
ctx, cancelFunc := context.WithTimeout(ctx, 10*time.Millisecond)
|
||||
defer cancelFunc()
|
||||
|
||||
@@ -233,7 +233,7 @@ func TestReactor_Sync(t *testing.T) {
|
||||
defer close(closeCh)
|
||||
go handleLightBlockRequests(ctx, t, chain, rts.blockOutCh,
|
||||
rts.blockInCh, closeCh, 0)
|
||||
go graduallyAddPeers(rts.peerUpdateCh, closeCh, 1*time.Second)
|
||||
go graduallyAddPeers(t, rts.peerUpdateCh, closeCh, 1*time.Second)
|
||||
go handleSnapshotRequests(t, rts.snapshotOutCh, rts.snapshotInCh, closeCh, []snapshot{
|
||||
{
|
||||
Height: uint64(snapshotHeight),
|
||||
@@ -434,10 +434,11 @@ func TestReactor_LightBlockResponse(t *testing.T) {
|
||||
rts := setup(ctx, t, nil, nil, nil, 2)
|
||||
|
||||
var height int64 = 10
|
||||
h := factory.MakeRandomHeader()
|
||||
// generates a random header
|
||||
h := factory.MakeHeader(t, &types.Header{})
|
||||
h.Height = height
|
||||
blockID := factory.MakeBlockIDWithHash(h.Hash())
|
||||
vals, pv := factory.RandValidatorSet(ctx, 1, 10)
|
||||
vals, pv := factory.RandValidatorSet(ctx, t, 1, 10)
|
||||
vote, err := factory.MakeVote(ctx, pv[0], h.ChainID, 0, h.Height, 0, 2,
|
||||
blockID, factory.DefaultTestTime)
|
||||
require.NoError(t, err)
|
||||
@@ -728,7 +729,7 @@ func handleLightBlockRequests(
|
||||
} else {
|
||||
switch errorCount % 3 {
|
||||
case 0: // send a different block
|
||||
vals, pv := factory.RandValidatorSet(ctx, 3, 10)
|
||||
vals, pv := factory.RandValidatorSet(ctx, t, 3, 10)
|
||||
_, _, lb := mockLB(ctx, t, int64(msg.Height), factory.DefaultTestTime, factory.MakeBlockID(), vals, pv)
|
||||
differntLB, err := lb.ToProto()
|
||||
require.NoError(t, err)
|
||||
@@ -797,7 +798,7 @@ func buildLightBlockChain(ctx context.Context, t *testing.T, fromHeight, toHeigh
|
||||
chain := make(map[int64]*types.LightBlock, toHeight-fromHeight)
|
||||
lastBlockID := factory.MakeBlockID()
|
||||
blockTime := startTime.Add(time.Duration(fromHeight-toHeight) * time.Minute)
|
||||
vals, pv := factory.RandValidatorSet(ctx, 3, 10)
|
||||
vals, pv := factory.RandValidatorSet(ctx, t, 3, 10)
|
||||
for height := fromHeight; height < toHeight; height++ {
|
||||
vals, pv, chain[height] = mockLB(ctx, t, height, blockTime, lastBlockID, vals, pv)
|
||||
lastBlockID = factory.MakeBlockIDWithHash(chain[height].Header.Hash())
|
||||
@@ -810,14 +811,14 @@ func mockLB(ctx context.Context, t *testing.T, height int64, time time.Time, las
|
||||
currentVals *types.ValidatorSet, currentPrivVals []types.PrivValidator,
|
||||
) (*types.ValidatorSet, []types.PrivValidator, *types.LightBlock) {
|
||||
t.Helper()
|
||||
header, err := factory.MakeHeader(&types.Header{
|
||||
header := factory.MakeHeader(t, &types.Header{
|
||||
Height: height,
|
||||
LastBlockID: lastBlockID,
|
||||
Time: time,
|
||||
})
|
||||
header.Version.App = testAppVersion
|
||||
require.NoError(t, err)
|
||||
nextVals, nextPrivVals := factory.RandValidatorSet(ctx, 3, 10)
|
||||
|
||||
nextVals, nextPrivVals := factory.RandValidatorSet(ctx, t, 3, 10)
|
||||
header.ValidatorsHash = currentVals.Hash()
|
||||
header.NextValidatorsHash = nextVals.Hash()
|
||||
header.ConsensusHash = types.DefaultConsensusParams().HashConsensusParams()
|
||||
@@ -837,6 +838,7 @@ func mockLB(ctx context.Context, t *testing.T, height int64, time time.Time, las
|
||||
// graduallyAddPeers delivers a new randomly-generated peer update on peerUpdateCh once
|
||||
// per interval, until closeCh is closed. Each peer update is assigned a random node ID.
|
||||
func graduallyAddPeers(
|
||||
t *testing.T,
|
||||
peerUpdateCh chan p2p.PeerUpdate,
|
||||
closeCh chan struct{},
|
||||
interval time.Duration,
|
||||
@@ -846,7 +848,7 @@ func graduallyAddPeers(
|
||||
select {
|
||||
case <-ticker.C:
|
||||
peerUpdateCh <- p2p.PeerUpdate{
|
||||
NodeID: factory.RandomNodeID(),
|
||||
NodeID: factory.RandomNodeID(t),
|
||||
Status: p2p.PeerStatusUp,
|
||||
}
|
||||
case <-closeCh:
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -114,7 +114,7 @@ func TestClient_SequentialVerification(t *testing.T) {
|
||||
|
||||
newKeys := genPrivKeys(4)
|
||||
newVals := newKeys.ToValidators(10, 1)
|
||||
differentVals, _ := factory.RandValidatorSet(ctx, 10, 100)
|
||||
differentVals, _ := factory.RandValidatorSet(ctx, t, 10, 100)
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
@@ -942,7 +942,7 @@ func TestClient_TrustedValidatorSet(t *testing.T) {
|
||||
|
||||
logger := log.NewTestingLogger(t)
|
||||
|
||||
differentVals, _ := factory.RandValidatorSet(ctx, 10, 100)
|
||||
differentVals, _ := factory.RandValidatorSet(ctx, t, 10, 100)
|
||||
mockBadValSetNode := mockNodeFromHeadersAndVals(
|
||||
map[int64]*types.SignedHeader{
|
||||
1: h1,
|
||||
|
||||
@@ -34,7 +34,7 @@ func TestLast_FirstLightBlockHeight(t *testing.T) {
|
||||
assert.EqualValues(t, -1, height)
|
||||
|
||||
// 1 key
|
||||
err = dbStore.SaveLightBlock(randLightBlock(ctx, int64(1)))
|
||||
err = dbStore.SaveLightBlock(randLightBlock(ctx, t, int64(1)))
|
||||
require.NoError(t, err)
|
||||
|
||||
height, err = dbStore.LastLightBlockHeight()
|
||||
@@ -58,7 +58,7 @@ func Test_SaveLightBlock(t *testing.T) {
|
||||
assert.Nil(t, h)
|
||||
|
||||
// 1 key
|
||||
err = dbStore.SaveLightBlock(randLightBlock(ctx, 1))
|
||||
err = dbStore.SaveLightBlock(randLightBlock(ctx, t, 1))
|
||||
require.NoError(t, err)
|
||||
|
||||
size := dbStore.Size()
|
||||
@@ -90,7 +90,7 @@ func Test_LightBlockBefore(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
err := dbStore.SaveLightBlock(randLightBlock(ctx, int64(2)))
|
||||
err := dbStore.SaveLightBlock(randLightBlock(ctx, t, int64(2)))
|
||||
require.NoError(t, err)
|
||||
|
||||
h, err := dbStore.LightBlockBefore(3)
|
||||
@@ -115,7 +115,7 @@ func Test_Prune(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
// One header
|
||||
err = dbStore.SaveLightBlock(randLightBlock(ctx, 2))
|
||||
err = dbStore.SaveLightBlock(randLightBlock(ctx, t, 2))
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.EqualValues(t, 1, dbStore.Size())
|
||||
@@ -130,7 +130,7 @@ func Test_Prune(t *testing.T) {
|
||||
|
||||
// Multiple headers
|
||||
for i := 1; i <= 10; i++ {
|
||||
err = dbStore.SaveLightBlock(randLightBlock(ctx, int64(i)))
|
||||
err = dbStore.SaveLightBlock(randLightBlock(ctx, t, int64(i)))
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
@@ -155,7 +155,7 @@ func Test_Concurrency(t *testing.T) {
|
||||
go func(i int64) {
|
||||
defer wg.Done()
|
||||
|
||||
err := dbStore.SaveLightBlock(randLightBlock(ctx, i))
|
||||
err := dbStore.SaveLightBlock(randLightBlock(ctx, t, i))
|
||||
require.NoError(t, err)
|
||||
|
||||
_, err = dbStore.LightBlock(i)
|
||||
@@ -198,8 +198,9 @@ func Test_Concurrency(t *testing.T) {
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
func randLightBlock(ctx context.Context, height int64) *types.LightBlock {
|
||||
vals, _ := factory.RandValidatorSet(ctx, 2, 1)
|
||||
func randLightBlock(ctx context.Context, t *testing.T, height int64) *types.LightBlock {
|
||||
t.Helper()
|
||||
vals, _ := factory.RandValidatorSet(ctx, t, 2, 1)
|
||||
return &types.LightBlock{
|
||||
SignedHeader: &types.SignedHeader{
|
||||
Header: &types.Header{
|
||||
|
||||
@@ -717,7 +717,7 @@ func loadStatefromGenesis(ctx context.Context, t *testing.T) sm.State {
|
||||
require.NoError(t, err)
|
||||
require.True(t, loadedState.IsEmpty())
|
||||
|
||||
genDoc, _ := factory.RandGenesisDoc(ctx, cfg, 0, false, 10)
|
||||
genDoc, _ := factory.RandGenesisDoc(ctx, t, cfg, 0, false, 10)
|
||||
|
||||
state, err := loadStateFromDBOrGenesisDocProvider(
|
||||
stateStore,
|
||||
|
||||
@@ -167,6 +167,7 @@ func generateLightClientAttackEvidence(
|
||||
// create a commit for the forged header
|
||||
blockID := makeBlockID(header.Hash(), 1000, []byte("partshash"))
|
||||
voteSet := types.NewVoteSet(chainID, forgedHeight, 0, tmproto.SignedMsgType(2), conflictingVals)
|
||||
|
||||
commit, err := factory.MakeCommit(ctx, blockID, forgedHeight, 0, voteSet, pv, forgedTime)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -204,6 +205,7 @@ func generateDuplicateVoteEvidence(
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
voteA, err := factory.MakeVote(ctx, privVal, chainID, valIdx, height, 0, 2, makeRandomBlockID(), time)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -287,7 +289,10 @@ func makeBlockID(hash []byte, partSetSize uint32, partSetHash []byte) types.Bloc
|
||||
}
|
||||
|
||||
func mutateValidatorSet(ctx context.Context, privVals []types.MockPV, vals *types.ValidatorSet) ([]types.PrivValidator, *types.ValidatorSet, error) {
|
||||
newVal, newPrivVal := factory.RandValidator(ctx, false, 10)
|
||||
newVal, newPrivVal, err := factory.RandValidator(ctx, false, 10)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
var newVals *types.ValidatorSet
|
||||
if vals.Size() > 2 {
|
||||
|
||||
Reference in New Issue
Block a user