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.

172 lines
5.1 KiB

  1. package consensus
  2. import (
  3. "fmt"
  4. "sync"
  5. "testing"
  6. "time"
  7. "github.com/tendermint/tendermint/config/tendermint_test"
  8. "github.com/tendermint/go-events"
  9. "github.com/tendermint/go-p2p"
  10. "github.com/tendermint/tendermint/types"
  11. "github.com/tendermint/tmsp/example/dummy"
  12. )
  13. func init() {
  14. config = tendermint_test.ResetConfig("consensus_reactor_test")
  15. }
  16. //----------------------------------------------
  17. // in-process testnets
  18. // Ensure a testnet makes blocks
  19. func TestReactor(t *testing.T) {
  20. N := 4
  21. css := randConsensusNet(N, "consensus_reactor_test", crankTimeoutPropose)
  22. reactors := make([]*ConsensusReactor, N)
  23. eventChans := make([]chan interface{}, N)
  24. for i := 0; i < N; i++ {
  25. reactors[i] = NewConsensusReactor(css[i], false)
  26. eventSwitch := events.NewEventSwitch()
  27. _, err := eventSwitch.Start()
  28. if err != nil {
  29. t.Fatalf("Failed to start switch: %v", err)
  30. }
  31. reactors[i].SetEventSwitch(eventSwitch)
  32. eventChans[i] = subscribeToEvent(eventSwitch, "tester", types.EventStringNewBlock(), 1)
  33. }
  34. // make connected switches and start all reactors
  35. p2p.MakeConnectedSwitches(N, func(i int, s *p2p.Switch) *p2p.Switch {
  36. s.AddReactor("CONSENSUS", reactors[i])
  37. return s
  38. }, p2p.Connect2Switches)
  39. // wait till everyone makes the first new block
  40. timeoutWaitGroup(t, N, func(wg *sync.WaitGroup, j int) {
  41. <-eventChans[j]
  42. wg.Done()
  43. })
  44. }
  45. //-------------------------------------------------------------
  46. // ensure we can make blocks despite cycling a validator set
  47. func TestValidatorSetChanges(t *testing.T) {
  48. nPeers := 8
  49. nVals := 4
  50. css := randConsensusNetWithPeers(nVals, nPeers, "consensus_val_set_changes_test", crankTimeoutPropose)
  51. reactors := make([]*ConsensusReactor, nPeers)
  52. eventChans := make([]chan interface{}, nPeers)
  53. for i := 0; i < nPeers; i++ {
  54. reactors[i] = NewConsensusReactor(css[i], false)
  55. eventSwitch := events.NewEventSwitch()
  56. _, err := eventSwitch.Start()
  57. if err != nil {
  58. t.Fatalf("Failed to start switch: %v", err)
  59. }
  60. reactors[i].SetEventSwitch(eventSwitch)
  61. eventChans[i] = subscribeToEventRespond(eventSwitch, "tester", types.EventStringNewBlock())
  62. }
  63. p2p.MakeConnectedSwitches(nPeers, func(i int, s *p2p.Switch) *p2p.Switch {
  64. s.AddReactor("CONSENSUS", reactors[i])
  65. return s
  66. }, p2p.Connect2Switches)
  67. // map of active validators
  68. activeVals := make(map[string]struct{})
  69. for i := 0; i < nVals; i++ {
  70. activeVals[string(css[i].privValidator.GetAddress())] = struct{}{}
  71. }
  72. // wait till everyone makes block 1
  73. timeoutWaitGroup(t, nPeers, func(wg *sync.WaitGroup, j int) {
  74. <-eventChans[j]
  75. eventChans[j] <- struct{}{}
  76. wg.Done()
  77. })
  78. newValidatorPubKey := css[nVals].privValidator.(*types.PrivValidator).PubKey
  79. newValidatorTx := dummy.MakeValSetChangeTx(newValidatorPubKey.Bytes(), uint64(testMinPower))
  80. // wait till everyone makes block 2
  81. // ensure the commit includes all validators
  82. // send newValTx to change vals in block 3
  83. waitForAndValidateBlock(t, nPeers, activeVals, eventChans, css, newValidatorTx)
  84. // wait till everyone makes block 3.
  85. // it includes the commit for block 2, which is by the original validator set
  86. waitForAndValidateBlock(t, nPeers, activeVals, eventChans, css)
  87. // wait till everyone makes block 4.
  88. // it includes the commit for block 3, which is by the original validator set
  89. waitForAndValidateBlock(t, nPeers, activeVals, eventChans, css)
  90. // the commits for block 4 should be with the updated validator set
  91. activeVals[string(newValidatorPubKey.Address())] = struct{}{}
  92. // wait till everyone makes block 5
  93. // it includes the commit for block 4, which should have the updated validator set
  94. waitForAndValidateBlock(t, nPeers, activeVals, eventChans, css)
  95. // TODO: test more changes!
  96. }
  97. func waitForAndValidateBlock(t *testing.T, n int, activeVals map[string]struct{}, eventChans []chan interface{}, css []*ConsensusState, txs ...[]byte) {
  98. timeoutWaitGroup(t, n, func(wg *sync.WaitGroup, j int) {
  99. newBlockI := <-eventChans[j]
  100. newBlock := newBlockI.(types.EventDataNewBlock).Block
  101. log.Info("Got block", "height", newBlock.Height, "validator", j)
  102. err := validateBlock(newBlock, activeVals)
  103. if err != nil {
  104. t.Fatal(err)
  105. }
  106. for _, tx := range txs {
  107. css[j].mempool.CheckTx(tx, nil)
  108. }
  109. eventChans[j] <- struct{}{}
  110. wg.Done()
  111. })
  112. }
  113. // expects high synchrony!
  114. func validateBlock(block *types.Block, activeVals map[string]struct{}) error {
  115. if block.LastCommit.Size() != len(activeVals) {
  116. return fmt.Errorf("Commit size doesn't match number of active validators. Got %d, expected %d", block.LastCommit.Size(), len(activeVals))
  117. }
  118. for _, vote := range block.LastCommit.Precommits {
  119. if _, ok := activeVals[string(vote.ValidatorAddress)]; !ok {
  120. return fmt.Errorf("Found vote for unactive validator %X", vote.ValidatorAddress)
  121. }
  122. }
  123. return nil
  124. }
  125. func timeoutWaitGroup(t *testing.T, n int, f func(*sync.WaitGroup, int)) {
  126. wg := new(sync.WaitGroup)
  127. wg.Add(n)
  128. for i := 0; i < n; i++ {
  129. go f(wg, i)
  130. }
  131. // Make wait into a channel
  132. done := make(chan struct{})
  133. go func() {
  134. wg.Wait()
  135. close(done)
  136. }()
  137. tick := time.NewTicker(time.Second * 3)
  138. select {
  139. case <-done:
  140. case <-tick.C:
  141. t.Fatalf("Timed out waiting for all validators to commit a block")
  142. }
  143. }