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.

206 lines
5.4 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
new pubsub package comment out failing consensus tests for now rewrite rpc httpclient to use new pubsub package import pubsub as tmpubsub, query as tmquery make event IDs constants EventKey -> EventTypeKey rename EventsPubsub to PubSub mempool does not use pubsub rename eventsSub to pubsub new subscribe API fix channel size issues and consensus tests bugs refactor rpc client add missing discardFromChan method add mutex rename pubsub to eventBus remove IsRunning from WSRPCConnection interface (not needed) add a comment in broadcastNewRoundStepsAndVotes rename registerEventCallbacks to broadcastNewRoundStepsAndVotes See https://dave.cheney.net/2014/03/19/channel-axioms stop eventBuses after reactor tests remove unnecessary Unsubscribe return subscribe helper function move discardFromChan to where it is used subscribe now returns an err this gives us ability to refuse to subscribe if pubsub is at its max capacity. use context for control overflow cache queries handle err when subscribing in replay_test rename testClientID to testSubscriber extract var set channel buffer capacity to 1 in replay_file fix byzantine_test unsubscribe from single event, not all events refactor httpclient to return events to appropriate channels return failing testReplayCrashBeforeWriteVote test fix TestValidatorSetChanges refactor code a bit fix testReplayCrashBeforeWriteVote add comment fix TestValidatorSetChanges fixes from Bucky's review update comment [ci skip] test TxEventBuffer update changelog fix TestValidatorSetChanges (2nd attempt) only do wg.Done when no errors benchmark event bus create pubsub server inside NewEventBus only expose config params (later if needed) set buffer capacity to 0 so we are not testing cache new tx event format: key = "Tx" plus a tag {"tx.hash": XYZ} This should allow to subscribe to all transactions! or a specific one using a query: "tm.events.type = Tx and tx.hash = '013ABF99434...'" use TimeoutCommit instead of afterPublishEventNewBlockTimeout TimeoutCommit is the time a node waits after committing a block, before it goes into the next height. So it will finish everything from the last block, but then wait a bit. The idea is this gives it time to hear more votes from other validators, to strengthen the commit it includes in the next block. But it also gives it time to hear about new transactions. waitForBlockWithUpdatedVals rewrite WAL crash tests Task: test that we can recover from any WAL crash. Solution: the old tests were relying on event hub being run in the same thread (we were injecting the private validator's last signature). when considering a rewrite, we considered two possible solutions: write a "fuzzy" testing system where WAL is crashing upon receiving a new message, or inject failures and trigger them in tests using something like https://github.com/coreos/gofail. remove sleep no cs.Lock around wal.Save test different cases (empty block, non-empty block, ...) comments add comments test 4 cases: empty block, non-empty block, non-empty block with smaller part size, many blocks fixes as per Bucky's last review reset subscriptions on UnsubscribeAll use a simple counter to track message for which we panicked also, set a smaller part size for all test cases
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. package state
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/stretchr/testify/require"
  7. "github.com/tendermint/abci/example/dummy"
  8. abci "github.com/tendermint/abci/types"
  9. crypto "github.com/tendermint/go-crypto"
  10. "github.com/tendermint/tendermint/proxy"
  11. "github.com/tendermint/tendermint/types"
  12. dbm "github.com/tendermint/tmlibs/db"
  13. "github.com/tendermint/tmlibs/log"
  14. )
  15. var (
  16. privKey = crypto.GenPrivKeyEd25519FromSecret([]byte("execution_test"))
  17. chainID = "execution_chain"
  18. testPartSize = 65536
  19. nTxsPerBlock = 10
  20. )
  21. func TestValidateBlock(t *testing.T) {
  22. state := state()
  23. state.SetLogger(log.TestingLogger())
  24. // proper block must pass
  25. block := makeBlock(state, 1)
  26. err := state.ValidateBlock(block)
  27. require.NoError(t, err)
  28. // wrong chain fails
  29. block = makeBlock(state, 1)
  30. block.ChainID = "not-the-real-one"
  31. err = state.ValidateBlock(block)
  32. require.Error(t, err)
  33. // wrong height fails
  34. block = makeBlock(state, 1)
  35. block.Height += 10
  36. err = state.ValidateBlock(block)
  37. require.Error(t, err)
  38. // wrong total tx fails
  39. block = makeBlock(state, 1)
  40. block.TotalTxs += 10
  41. err = state.ValidateBlock(block)
  42. require.Error(t, err)
  43. // wrong blockid fails
  44. block = makeBlock(state, 1)
  45. block.LastBlockID.PartsHeader.Total += 10
  46. err = state.ValidateBlock(block)
  47. require.Error(t, err)
  48. // wrong app hash fails
  49. block = makeBlock(state, 1)
  50. block.AppHash = []byte("wrong app hash")
  51. err = state.ValidateBlock(block)
  52. require.Error(t, err)
  53. // wrong consensus hash fails
  54. block = makeBlock(state, 1)
  55. block.ConsensusHash = []byte("wrong consensus hash")
  56. err = state.ValidateBlock(block)
  57. require.Error(t, err)
  58. // wrong results hash fails
  59. block = makeBlock(state, 1)
  60. block.LastResultsHash = []byte("wrong results hash")
  61. err = state.ValidateBlock(block)
  62. require.Error(t, err)
  63. }
  64. func TestApplyBlock(t *testing.T) {
  65. cc := proxy.NewLocalClientCreator(dummy.NewDummyApplication())
  66. proxyApp := proxy.NewAppConns(cc, nil)
  67. err := proxyApp.Start()
  68. require.Nil(t, err)
  69. defer proxyApp.Stop()
  70. state := state()
  71. state.SetLogger(log.TestingLogger())
  72. block := makeBlock(state, 1)
  73. err = state.ApplyBlock(types.NopEventBus{}, proxyApp.Consensus(), block, block.MakePartSet(testPartSize).Header(), types.MockMempool{})
  74. require.Nil(t, err)
  75. // TODO check state and mempool
  76. }
  77. // TestBeginBlockAbsentValidators ensures we send absent validators list.
  78. func TestBeginBlockAbsentValidators(t *testing.T) {
  79. app := &testApp{}
  80. cc := proxy.NewLocalClientCreator(app)
  81. proxyApp := proxy.NewAppConns(cc, nil)
  82. err := proxyApp.Start()
  83. require.Nil(t, err)
  84. defer proxyApp.Stop()
  85. state := state()
  86. state.SetLogger(log.TestingLogger())
  87. // there were 2 validators
  88. val1PrivKey := crypto.GenPrivKeyEd25519()
  89. val2PrivKey := crypto.GenPrivKeyEd25519()
  90. lastValidators := types.NewValidatorSet([]*types.Validator{
  91. types.NewValidator(val1PrivKey.PubKey(), 10),
  92. types.NewValidator(val2PrivKey.PubKey(), 5),
  93. })
  94. prevHash := state.LastBlockID.Hash
  95. prevParts := types.PartSetHeader{}
  96. prevBlockID := types.BlockID{prevHash, prevParts}
  97. now := time.Now().UTC()
  98. testCases := []struct {
  99. desc string
  100. lastCommitPrecommits []*types.Vote
  101. expectedAbsentValidators []int32
  102. }{
  103. {"none absent", []*types.Vote{{ValidatorIndex: 0, Timestamp: now}, {ValidatorIndex: 1, Timestamp: now}}, []int32{}},
  104. {"one absent", []*types.Vote{{ValidatorIndex: 0, Timestamp: now}, nil}, []int32{1}},
  105. {"multiple absent", []*types.Vote{nil, nil}, []int32{0, 1}},
  106. }
  107. for _, tc := range testCases {
  108. lastCommit := &types.Commit{BlockID: prevBlockID, Precommits: tc.lastCommitPrecommits}
  109. block, _ := state.MakeBlock(2, makeTxs(2), lastCommit)
  110. _, err = ExecCommitBlock(proxyApp.Consensus(), block, log.TestingLogger(), lastValidators)
  111. require.Nil(t, err, tc.desc)
  112. // -> app must receive an index of the absent validator
  113. assert.Equal(t, tc.expectedAbsentValidators, app.AbsentValidators, tc.desc)
  114. }
  115. }
  116. //----------------------------------------------------------------------------
  117. // make some bogus txs
  118. func makeTxs(height int64) (txs []types.Tx) {
  119. for i := 0; i < nTxsPerBlock; i++ {
  120. txs = append(txs, types.Tx([]byte{byte(height), byte(i)}))
  121. }
  122. return txs
  123. }
  124. func state() *State {
  125. s, _ := MakeGenesisState(dbm.NewMemDB(), &types.GenesisDoc{
  126. ChainID: chainID,
  127. Validators: []types.GenesisValidator{
  128. {privKey.PubKey(), 10000, "test"},
  129. },
  130. AppHash: nil,
  131. })
  132. return s
  133. }
  134. func makeBlock(state *State, height int64) *types.Block {
  135. block, _ := state.MakeBlock(height, makeTxs(state.LastBlockHeight), new(types.Commit))
  136. return block
  137. }
  138. //----------------------------------------------------------------------------
  139. var _ abci.Application = (*testApp)(nil)
  140. type testApp struct {
  141. abci.BaseApplication
  142. AbsentValidators []int32
  143. }
  144. func NewDummyApplication() *testApp {
  145. return &testApp{}
  146. }
  147. func (app *testApp) Info(req abci.RequestInfo) (resInfo abci.ResponseInfo) {
  148. return abci.ResponseInfo{}
  149. }
  150. func (app *testApp) BeginBlock(req abci.RequestBeginBlock) abci.ResponseBeginBlock {
  151. app.AbsentValidators = req.AbsentValidators
  152. return abci.ResponseBeginBlock{}
  153. }
  154. func (app *testApp) DeliverTx(tx []byte) abci.ResponseDeliverTx {
  155. return abci.ResponseDeliverTx{Tags: []*abci.KVPair{}}
  156. }
  157. func (app *testApp) CheckTx(tx []byte) abci.ResponseCheckTx {
  158. return abci.ResponseCheckTx{}
  159. }
  160. func (app *testApp) Commit() abci.ResponseCommit {
  161. return abci.ResponseCommit{}
  162. }
  163. func (app *testApp) Query(reqQuery abci.RequestQuery) (resQuery abci.ResponseQuery) {
  164. return
  165. }