factory: simplify validator and genesis factory functions

This commit is contained in:
William Banfield
2021-11-22 14:21:16 -05:00
parent 0d2a5de203
commit ff5cea0cff
22 changed files with 138 additions and 119 deletions
+3 -1
View File
@@ -7,6 +7,7 @@ import (
"path"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@@ -37,7 +38,8 @@ func TestByzantinePrevoteEquivocation(t *testing.T) {
tickerFunc := newMockTickerFunc(true)
appFunc := newKVStore
genDoc, privVals := factory.RandGenesisDoc(config, nValidators, false, 30)
valSet, privVals := factory.ValidatorSet(nValidators, 30)
genDoc := factory.GenesisDoc(config, time.Now(), valSet.Validators, nil)
states := make([]*State, nValidators)
for i := 0; i < nValidators; i++ {
+22 -9
View File
@@ -464,9 +464,9 @@ func loadPrivValidator(t *testing.T, config *cfg.Config) *privval.FilePV {
return privValidator
}
func randState(config *cfg.Config, nValidators int) (*State, []*validatorStub) {
func makeState(config *cfg.Config, nValidators int) (*State, []*validatorStub) {
// Get State
state, privVals := randGenesisState(config, nValidators, false, 10)
state, privVals := makeGenesisState(config, nValidators, 10, types.DefaultConsensusParams())
vss := make([]*validatorStub, nValidators)
@@ -728,7 +728,7 @@ func consensusLogger() log.Logger {
return log.TestingLogger().With("module", "consensus")
}
func randConsensusState(
func makeConsensusState(
t *testing.T,
config *cfg.Config,
nValidators int,
@@ -739,7 +739,8 @@ func randConsensusState(
) ([]*State, cleanupFunc) {
t.Helper()
genDoc, privVals := factory.RandGenesisDoc(config, nValidators, false, 30)
valSet, privVals := factory.ValidatorSet(nValidators, 30)
genDoc := factory.GenesisDoc(config, time.Now(), valSet.Validators, nil)
css := make([]*State, nValidators)
logger := consensusLogger()
@@ -794,7 +795,8 @@ func randConsensusNetWithPeers(
tickerFunc func() TimeoutTicker,
appFunc func(string) abci.Application,
) ([]*State, *types.GenesisDoc, *cfg.Config, cleanupFunc) {
genDoc, privVals := factory.RandGenesisDoc(config, nValidators, false, testMinPower)
valSet, privVals := factory.ValidatorSet(nValidators, testMinPower)
genDoc := factory.GenesisDoc(config, time.Now(), valSet.Validators, nil)
css := make([]*State, nPeers)
t.Helper()
logger := consensusLogger()
@@ -848,13 +850,24 @@ func randConsensusNetWithPeers(
}
}
func randGenesisState(
func makeGenesisState(
config *cfg.Config,
numValidators int,
randPower bool,
minPower int64) (sm.State, []types.PrivValidator) {
votingPower int64,
consensusParams *types.ConsensusParams) (sm.State, []types.PrivValidator) {
return makeGenesisStateWithGenesisTime(config, numValidators, votingPower, consensusParams, time.Now())
}
genDoc, privValidators := factory.RandGenesisDoc(config, numValidators, randPower, minPower)
func makeGenesisStateWithGenesisTime(
config *cfg.Config,
numValidators int,
votingPower int64,
consensusParams *types.ConsensusParams,
genesisTime time.Time,
) (sm.State, []types.PrivValidator) {
valSet, privValidators := factory.ValidatorSet(numValidators, votingPower)
genDoc := factory.GenesisDoc(config, genesisTime, valSet.Validators, consensusParams)
s0, _ := sm.MakeGenesisState(genDoc)
return s0, privValidators
}
+1 -1
View File
@@ -18,7 +18,7 @@ func TestReactorInvalidPrecommit(t *testing.T) {
config := configSetup(t)
n := 4
states, cleanup := randConsensusState(t,
states, cleanup := makeConsensusState(t,
config, n, "consensus_reactor_test",
newMockTickerFunc(true), newKVStore)
t.Cleanup(cleanup)
+5 -5
View File
@@ -33,7 +33,7 @@ func TestMempoolNoProgressUntilTxsAvailable(t *testing.T) {
t.Cleanup(func() { _ = os.RemoveAll(config.RootDir) })
config.Consensus.CreateEmptyBlocks = false
state, privVals := randGenesisState(baseConfig, 1, false, 10)
state, privVals := makeGenesisState(baseConfig, 1, 10, nil)
cs := newStateWithConfig(config, state, privVals[0], NewCounterApplication())
assertMempool(cs.txNotifier).EnableTxsAvailable()
height, round := cs.Height, cs.Round
@@ -55,7 +55,7 @@ func TestMempoolProgressAfterCreateEmptyBlocksInterval(t *testing.T) {
t.Cleanup(func() { _ = os.RemoveAll(config.RootDir) })
config.Consensus.CreateEmptyBlocksInterval = ensureTimeout
state, privVals := randGenesisState(baseConfig, 1, false, 10)
state, privVals := makeGenesisState(baseConfig, 1, 10, nil)
cs := newStateWithConfig(config, state, privVals[0], NewCounterApplication())
assertMempool(cs.txNotifier).EnableTxsAvailable()
@@ -75,7 +75,7 @@ func TestMempoolProgressInHigherRound(t *testing.T) {
t.Cleanup(func() { _ = os.RemoveAll(config.RootDir) })
config.Consensus.CreateEmptyBlocks = false
state, privVals := randGenesisState(baseConfig, 1, false, 10)
state, privVals := makeGenesisState(baseConfig, 1, 10, nil)
cs := newStateWithConfig(config, state, privVals[0], NewCounterApplication())
assertMempool(cs.txNotifier).EnableTxsAvailable()
height, round := cs.Height, cs.Round
@@ -123,7 +123,7 @@ func deliverTxsRange(cs *State, start, end int) {
func TestMempoolTxConcurrentWithCommit(t *testing.T) {
config := configSetup(t)
state, privVals := randGenesisState(config, 1, false, 10)
state, privVals := makeGenesisState(config, 1, 10, nil)
stateStore := sm.NewStore(dbm.NewMemDB())
blockStore := store.NewBlockStore(dbm.NewMemDB())
cs := newStateWithConfigAndBlockStore(config, state, privVals[0], NewCounterApplication(), blockStore)
@@ -149,7 +149,7 @@ func TestMempoolTxConcurrentWithCommit(t *testing.T) {
func TestMempoolRmBadTx(t *testing.T) {
config := configSetup(t)
state, privVals := randGenesisState(config, 1, false, 10)
state, privVals := makeGenesisState(config, 1, 10, nil)
app := NewCounterApplication()
stateStore := sm.NewStore(dbm.NewMemDB())
blockStore := store.NewBlockStore(dbm.NewMemDB())
+6 -5
View File
@@ -276,7 +276,7 @@ func TestReactorBasic(t *testing.T) {
config := configSetup(t)
n := 4
states, cleanup := randConsensusState(t,
states, cleanup := makeConsensusState(t,
config, n, "consensus_reactor_test",
newMockTickerFunc(true), newKVStore)
t.Cleanup(cleanup)
@@ -323,7 +323,8 @@ func TestReactorWithEvidence(t *testing.T) {
tickerFunc := newMockTickerFunc(true)
appFunc := newKVStore
genDoc, privVals := factory.RandGenesisDoc(config, n, false, 30)
valSet, privVals := factory.ValidatorSet(n, 30)
genDoc := factory.GenesisDoc(config, time.Now(), valSet.Validators, nil)
states := make([]*State, n)
logger := consensusLogger()
@@ -415,7 +416,7 @@ func TestReactorCreatesBlockWhenEmptyBlocksFalse(t *testing.T) {
config := configSetup(t)
n := 4
states, cleanup := randConsensusState(
states, cleanup := makeConsensusState(
t,
config,
n,
@@ -465,7 +466,7 @@ func TestReactorRecordsVotesAndBlockParts(t *testing.T) {
config := configSetup(t)
n := 4
states, cleanup := randConsensusState(t,
states, cleanup := makeConsensusState(t,
config, n, "consensus_reactor_test",
newMockTickerFunc(true), newKVStore)
t.Cleanup(cleanup)
@@ -524,7 +525,7 @@ func TestReactorVotingPowerChange(t *testing.T) {
config := configSetup(t)
n := 4
states, cleanup := randConsensusState(
states, cleanup := makeConsensusState(
t,
config,
n,
+2 -1
View File
@@ -1223,7 +1223,8 @@ func (bs *mockBlockStore) PruneBlocks(height int64) (uint64, error) {
// Test handshake/init chain
func TestHandshakeUpdatesValidators(t *testing.T) {
val, _ := factory.RandValidator(true, 10)
votePower := 10 + int64(rand.Uint32())
val, _ := factory.Validator(votePower)
vals := types.NewValidatorSet([]*types.Validator{val})
app := &initChainApp{vals: types.TM2PB.ValidatorUpdates(vals)}
clientCreator := proxy.NewLocalClientCreator(app)
+36 -36
View File
@@ -65,7 +65,7 @@ x * TestHalt1 - if we see +2/3 precommits after timing out into new round, we sh
func TestStateProposerSelection0(t *testing.T) {
config := configSetup(t)
cs1, vss := randState(config, 4)
cs1, vss := makeState(config, 4)
height, round := cs1.Height, cs1.Round
newRoundCh := subscribe(cs1.eventBus, types.EventQueryNewRound)
@@ -107,7 +107,7 @@ func TestStateProposerSelection0(t *testing.T) {
func TestStateProposerSelection2(t *testing.T) {
config := configSetup(t)
cs1, vss := randState(config, 4) // test needs more work for more than 3 validators
cs1, vss := makeState(config, 4) // test needs more work for more than 3 validators
height := cs1.Height
newRoundCh := subscribe(cs1.eventBus, types.EventQueryNewRound)
@@ -145,7 +145,7 @@ func TestStateProposerSelection2(t *testing.T) {
func TestStateEnterProposeNoPrivValidator(t *testing.T) {
config := configSetup(t)
cs, _ := randState(config, 1)
cs, _ := makeState(config, 1)
cs.SetPrivValidator(nil)
height, round := cs.Height, cs.Round
@@ -166,7 +166,7 @@ func TestStateEnterProposeNoPrivValidator(t *testing.T) {
func TestStateEnterProposeYesPrivValidator(t *testing.T) {
config := configSetup(t)
cs, _ := randState(config, 1)
cs, _ := makeState(config, 1)
height, round := cs.Height, cs.Round
// Listen for propose timeout event
@@ -198,7 +198,7 @@ func TestStateEnterProposeYesPrivValidator(t *testing.T) {
func TestStateBadProposal(t *testing.T) {
config := configSetup(t)
cs1, vss := randState(config, 2)
cs1, vss := makeState(config, 2)
height, round := cs1.Height, cs1.Round
vs2 := vss[1]
@@ -258,7 +258,7 @@ func TestStateBadProposal(t *testing.T) {
func TestStateOversizedBlock(t *testing.T) {
config := configSetup(t)
cs1, vss := randState(config, 2)
cs1, vss := makeState(config, 2)
cs1.state.ConsensusParams.Block.MaxBytes = 2000
height, round := cs1.Height, cs1.Round
vs2 := vss[1]
@@ -322,7 +322,7 @@ func TestStateOversizedBlock(t *testing.T) {
func TestStateFullRound1(t *testing.T) {
config := configSetup(t)
cs, vss := randState(config, 1)
cs, vss := makeState(config, 1)
height, round := cs.Height, cs.Round
// NOTE: buffer capacity of 0 ensures we can validate prevote and last commit
@@ -364,7 +364,7 @@ func TestStateFullRound1(t *testing.T) {
func TestStateFullRoundNil(t *testing.T) {
config := configSetup(t)
cs, vss := randState(config, 1)
cs, vss := makeState(config, 1)
height, round := cs.Height, cs.Round
voteCh := subscribeUnBuffered(cs.eventBus, types.EventQueryVote)
@@ -384,7 +384,7 @@ func TestStateFullRoundNil(t *testing.T) {
func TestStateFullRound2(t *testing.T) {
config := configSetup(t)
cs1, vss := randState(config, 2)
cs1, vss := makeState(config, 2)
vs2 := vss[1]
height, round := cs1.Height, cs1.Round
@@ -426,7 +426,7 @@ func TestStateFullRound2(t *testing.T) {
func TestStateLock_NoPOL(t *testing.T) {
config := configSetup(t)
cs1, vss := randState(config, 2)
cs1, vss := makeState(config, 2)
vs2 := vss[1]
height, round := cs1.Height, cs1.Round
@@ -563,7 +563,7 @@ func TestStateLock_NoPOL(t *testing.T) {
ensureNewTimeout(t, timeoutWaitCh, height, round, cs1.config.Precommit(round).Nanoseconds())
cs2, _ := randState(config, 2) // needed so generated block is different than locked block
cs2, _ := makeState(config, 2) // needed so generated block is different than locked block
// before we time out into new round, set next proposal block
prop, propBlock := decideProposal(t, cs2, vs2, vs2.Height, vs2.Round+1)
if prop == nil || propBlock == nil {
@@ -617,7 +617,7 @@ func TestStateLock_NoPOL(t *testing.T) {
func TestStateLock_POLUpdateLock(t *testing.T) {
config := configSetup(t)
cs1, vss := randState(config, 4)
cs1, vss := makeState(config, 4)
vs2, vs3, vs4 := vss[1], vss[2], vss[3]
height, round := cs1.Height, cs1.Round
@@ -717,7 +717,7 @@ func TestStateLock_POLUpdateLock(t *testing.T) {
func TestStateLock_POLRelock(t *testing.T) {
config := configSetup(t)
cs1, vss := randState(config, 4)
cs1, vss := makeState(config, 4)
vs2, vs3, vs4 := vss[1], vss[2], vss[3]
height, round := cs1.Height, cs1.Round
@@ -815,7 +815,7 @@ func TestStateLock_POLRelock(t *testing.T) {
func TestStateLock_PrevoteNilWhenLockedAndMissProposal(t *testing.T) {
config := configSetup(t)
cs1, vss := randState(config, 4)
cs1, vss := makeState(config, 4)
vs2, vs3, vs4 := vss[1], vss[2], vss[3]
height, round := cs1.Height, cs1.Round
@@ -899,7 +899,7 @@ func TestStateLock_PrevoteNilWhenLockedAndDifferentProposal(t *testing.T) {
state.
*/
cs1, vss := randState(config, 4)
cs1, vss := makeState(config, 4)
vs2, vs3, vs4 := vss[1], vss[2], vss[3]
height, round := cs1.Height, cs1.Round
@@ -996,7 +996,7 @@ func TestStateLock_POLDoesNotUnlock(t *testing.T) {
state.
*/
cs1, vss := randState(config, 4)
cs1, vss := makeState(config, 4)
vs2, vs3, vs4 := vss[1], vss[2], vss[3]
height, round := cs1.Height, cs1.Round
@@ -1127,7 +1127,7 @@ func TestStateLock_POLDoesNotUnlock(t *testing.T) {
func TestStateLock_MissingProposalWhenPOLSeenDoesNotUpdateLock(t *testing.T) {
config := configSetup(t)
cs1, vss := randState(config, 4)
cs1, vss := makeState(config, 4)
vs2, vs3, vs4 := vss[1], vss[2], vss[3]
height, round := cs1.Height, cs1.Round
@@ -1210,7 +1210,7 @@ func TestStateLock_MissingProposalWhenPOLSeenDoesNotUpdateLock(t *testing.T) {
func TestStateLock_DoesNotLockOnOldProposal(t *testing.T) {
config := configSetup(t)
cs1, vss := randState(config, 4)
cs1, vss := makeState(config, 4)
vs2, vs3, vs4 := vss[1], vss[2], vss[3]
height, round := cs1.Height, cs1.Round
@@ -1284,7 +1284,7 @@ func TestStateLock_DoesNotLockOnOldProposal(t *testing.T) {
func TestStateLock_POLSafety1(t *testing.T) {
config := configSetup(t)
cs1, vss := randState(config, 4)
cs1, vss := makeState(config, 4)
vs2, vs3, vs4 := vss[1], vss[2], vss[3]
height, round := cs1.Height, cs1.Round
@@ -1401,7 +1401,7 @@ func TestStateLock_POLSafety1(t *testing.T) {
func TestStateLock_POLSafety2(t *testing.T) {
config := configSetup(t)
cs1, vss := randState(config, 4)
cs1, vss := makeState(config, 4)
vs2, vs3, vs4 := vss[1], vss[2], vss[3]
height, round := cs1.Height, cs1.Round
@@ -1496,7 +1496,7 @@ func TestStateLock_POLSafety2(t *testing.T) {
func TestState_PrevotePOLFromPreviousRound(t *testing.T) {
config := configSetup(t)
cs1, vss := randState(config, 4)
cs1, vss := makeState(config, 4)
vs2, vs3, vs4 := vss[1], vss[2], vss[3]
height, round := cs1.Height, cs1.Round
@@ -1634,7 +1634,7 @@ func TestState_PrevotePOLFromPreviousRound(t *testing.T) {
func TestProposeValidBlock(t *testing.T) {
config := configSetup(t)
cs1, vss := randState(config, 4)
cs1, vss := makeState(config, 4)
vs2, vs3, vs4 := vss[1], vss[2], vss[3]
height, round := cs1.Height, cs1.Round
@@ -1723,7 +1723,7 @@ func TestProposeValidBlock(t *testing.T) {
func TestSetValidBlockOnDelayedPrevote(t *testing.T) {
config := configSetup(t)
cs1, vss := randState(config, 4)
cs1, vss := makeState(config, 4)
vs2, vs3, vs4 := vss[1], vss[2], vss[3]
height, round := cs1.Height, cs1.Round
@@ -1787,7 +1787,7 @@ func TestSetValidBlockOnDelayedPrevote(t *testing.T) {
func TestSetValidBlockOnDelayedProposal(t *testing.T) {
config := configSetup(t)
cs1, vss := randState(config, 4)
cs1, vss := makeState(config, 4)
vs2, vs3, vs4 := vss[1], vss[2], vss[3]
height, round := cs1.Height, cs1.Round
@@ -1845,7 +1845,7 @@ func TestSetValidBlockOnDelayedProposal(t *testing.T) {
func TestWaitingTimeoutOnNilPolka(t *testing.T) {
config := configSetup(t)
cs1, vss := randState(config, 4)
cs1, vss := makeState(config, 4)
vs2, vs3, vs4 := vss[1], vss[2], vss[3]
height, round := cs1.Height, cs1.Round
@@ -1868,7 +1868,7 @@ func TestWaitingTimeoutOnNilPolka(t *testing.T) {
func TestWaitingTimeoutProposeOnNewRound(t *testing.T) {
config := configSetup(t)
cs1, vss := randState(config, 4)
cs1, vss := makeState(config, 4)
vs2, vs3, vs4 := vss[1], vss[2], vss[3]
height, round := cs1.Height, cs1.Round
@@ -1906,7 +1906,7 @@ func TestWaitingTimeoutProposeOnNewRound(t *testing.T) {
func TestRoundSkipOnNilPolkaFromHigherRound(t *testing.T) {
config := configSetup(t)
cs1, vss := randState(config, 4)
cs1, vss := makeState(config, 4)
vs2, vs3, vs4 := vss[1], vss[2], vss[3]
height, round := cs1.Height, cs1.Round
@@ -1944,7 +1944,7 @@ func TestRoundSkipOnNilPolkaFromHigherRound(t *testing.T) {
func TestWaitTimeoutProposeOnNilPolkaForTheCurrentRound(t *testing.T) {
config := configSetup(t)
cs1, vss := randState(config, 4)
cs1, vss := makeState(config, 4)
vs2, vs3, vs4 := vss[1], vss[2], vss[3]
height, round := cs1.Height, int32(1)
@@ -1973,7 +1973,7 @@ func TestWaitTimeoutProposeOnNilPolkaForTheCurrentRound(t *testing.T) {
func TestEmitNewValidBlockEventOnCommitWithoutBlock(t *testing.T) {
config := configSetup(t)
cs1, vss := randState(config, 4)
cs1, vss := makeState(config, 4)
vs2, vs3, vs4 := vss[1], vss[2], vss[3]
height, round := cs1.Height, int32(1)
@@ -2009,7 +2009,7 @@ func TestEmitNewValidBlockEventOnCommitWithoutBlock(t *testing.T) {
func TestCommitFromPreviousRound(t *testing.T) {
config := configSetup(t)
cs1, vss := randState(config, 4)
cs1, vss := makeState(config, 4)
vs2, vs3, vs4 := vss[1], vss[2], vss[3]
height, round := cs1.Height, int32(1)
@@ -2065,7 +2065,7 @@ func TestStartNextHeightCorrectlyAfterTimeout(t *testing.T) {
config := configSetup(t)
config.Consensus.SkipTimeoutCommit = false
cs1, vss := randState(config, 4)
cs1, vss := makeState(config, 4)
cs1.txNotifier = &fakeTxNotifier{ch: make(chan struct{})}
vs2, vs3, vs4 := vss[1], vss[2], vss[3]
@@ -2128,7 +2128,7 @@ func TestResetTimeoutPrecommitUponNewHeight(t *testing.T) {
config := configSetup(t)
config.Consensus.SkipTimeoutCommit = false
cs1, vss := randState(config, 4)
cs1, vss := makeState(config, 4)
vs2, vs3, vs4 := vss[1], vss[2], vss[3]
height, round := cs1.Height, cs1.Round
@@ -2271,7 +2271,7 @@ func TestStateSlashing_Precommits(t *testing.T) {
func TestStateHalt1(t *testing.T) {
config := configSetup(t)
cs1, vss := randState(config, 4)
cs1, vss := makeState(config, 4)
vs2, vs3, vs4 := vss[1], vss[2], vss[3]
height, round := cs1.Height, cs1.Round
partSize := types.BlockPartSizeBytes
@@ -2340,7 +2340,7 @@ func TestStateOutputsBlockPartsStats(t *testing.T) {
config := configSetup(t)
// create dummy peer
cs, _ := randState(config, 1)
cs, _ := makeState(config, 1)
peer := p2pmock.NewPeer(nil)
// 1) new block part
@@ -2384,7 +2384,7 @@ func TestStateOutputsBlockPartsStats(t *testing.T) {
func TestStateOutputVoteStats(t *testing.T) {
config := configSetup(t)
cs, vss := randState(config, 2)
cs, vss := makeState(config, 2)
// create dummy peer
peer := p2pmock.NewPeer(nil)
@@ -2419,7 +2419,7 @@ func TestStateOutputVoteStats(t *testing.T) {
func TestSignSameVoteTwice(t *testing.T) {
config := configSetup(t)
_, vss := randState(config, 2)
_, vss := makeState(config, 2)
randBytes := tmrand.Bytes(tmhash.Size)
@@ -25,7 +25,7 @@ func TestMain(m *testing.M) {
}
func TestPeerCatchupRounds(t *testing.T) {
valSet, privVals := factory.RandValidatorSet(10, 1)
valSet, privVals := factory.ValidatorSet(10, 1)
hvs := NewHeightVoteSet(config.ChainID(), 1, valSet)