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.

170 lines
4.9 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)
  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)
  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. newBlock := <-eventChans[j]
  100. err := validateBlock(newBlock.(types.EventDataNewBlock).Block, activeVals)
  101. if err != nil {
  102. t.Fatal(err)
  103. }
  104. for _, tx := range txs {
  105. css[j].mempool.CheckTx(tx, nil)
  106. }
  107. eventChans[j] <- struct{}{}
  108. wg.Done()
  109. })
  110. }
  111. // expects high synchrony!
  112. func validateBlock(block *types.Block, activeVals map[string]struct{}) error {
  113. if block.LastCommit.Size() != len(activeVals) {
  114. return fmt.Errorf("Commit size doesn't match number of active validators. Got %d, expected %d", block.LastCommit.Size(), len(activeVals))
  115. }
  116. for _, vote := range block.LastCommit.Precommits {
  117. if _, ok := activeVals[string(vote.ValidatorAddress)]; !ok {
  118. return fmt.Errorf("Found vote for unactive validator %X", vote.ValidatorAddress)
  119. }
  120. }
  121. return nil
  122. }
  123. func timeoutWaitGroup(t *testing.T, n int, f func(*sync.WaitGroup, int)) {
  124. wg := new(sync.WaitGroup)
  125. wg.Add(n)
  126. for i := 0; i < n; i++ {
  127. go f(wg, i)
  128. }
  129. // Make wait into a channel
  130. done := make(chan struct{})
  131. go func() {
  132. wg.Wait()
  133. close(done)
  134. }()
  135. tick := time.NewTicker(time.Second * 3)
  136. select {
  137. case <-done:
  138. case <-tick.C:
  139. t.Fatalf("Timed out waiting for all validators to commit a block")
  140. }
  141. }