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.

180 lines
4.9 KiB

7 years ago
7 years ago
7 years ago
7 years ago
  1. package evidence
  2. import (
  3. "fmt"
  4. "sync"
  5. "testing"
  6. "time"
  7. "github.com/go-kit/kit/log/term"
  8. "github.com/stretchr/testify/assert"
  9. cfg "github.com/tendermint/tendermint/config"
  10. dbm "github.com/tendermint/tendermint/libs/db"
  11. "github.com/tendermint/tendermint/libs/log"
  12. "github.com/tendermint/tendermint/p2p"
  13. "github.com/tendermint/tendermint/types"
  14. )
  15. // evidenceLogger is a TestingLogger which uses a different
  16. // color for each validator ("validator" key must exist).
  17. func evidenceLogger() log.Logger {
  18. return log.TestingLoggerWithColorFn(func(keyvals ...interface{}) term.FgBgColor {
  19. for i := 0; i < len(keyvals)-1; i += 2 {
  20. if keyvals[i] == "validator" {
  21. return term.FgBgColor{Fg: term.Color(uint8(keyvals[i+1].(int) + 1))}
  22. }
  23. }
  24. return term.FgBgColor{}
  25. })
  26. }
  27. // connect N evidence reactors through N switches
  28. func makeAndConnectEvidenceReactors(config *cfg.Config, stateDBs []dbm.DB) []*EvidenceReactor {
  29. N := len(stateDBs)
  30. reactors := make([]*EvidenceReactor, N)
  31. logger := evidenceLogger()
  32. for i := 0; i < N; i++ {
  33. store := NewEvidenceStore(dbm.NewMemDB())
  34. pool := NewEvidencePool(stateDBs[i], store)
  35. reactors[i] = NewEvidenceReactor(pool)
  36. reactors[i].SetLogger(logger.With("validator", i))
  37. }
  38. p2p.MakeConnectedSwitches(config.P2P, N, func(i int, s *p2p.Switch) *p2p.Switch {
  39. s.AddReactor("EVIDENCE", reactors[i])
  40. return s
  41. }, p2p.Connect2Switches)
  42. return reactors
  43. }
  44. // wait for all evidence on all reactors
  45. func waitForEvidence(t *testing.T, evs types.EvidenceList, reactors []*EvidenceReactor) {
  46. // wait for the evidence in all evpools
  47. wg := new(sync.WaitGroup)
  48. for i := 0; i < len(reactors); i++ {
  49. wg.Add(1)
  50. go _waitForEvidence(t, wg, evs, i, reactors)
  51. }
  52. done := make(chan struct{})
  53. go func() {
  54. wg.Wait()
  55. close(done)
  56. }()
  57. timer := time.After(TIMEOUT)
  58. select {
  59. case <-timer:
  60. t.Fatal("Timed out waiting for evidence")
  61. case <-done:
  62. }
  63. }
  64. // wait for all evidence on a single evpool
  65. func _waitForEvidence(t *testing.T, wg *sync.WaitGroup, evs types.EvidenceList, reactorIdx int, reactors []*EvidenceReactor) {
  66. evpool := reactors[reactorIdx].evpool
  67. for len(evpool.PendingEvidence(-1)) != len(evs) {
  68. time.Sleep(time.Millisecond * 100)
  69. }
  70. reapedEv := evpool.PendingEvidence(-1)
  71. // put the reaped evidence in a map so we can quickly check we got everything
  72. evMap := make(map[string]types.Evidence)
  73. for _, e := range reapedEv {
  74. evMap[string(e.Hash())] = e
  75. }
  76. for i, expectedEv := range evs {
  77. gotEv := evMap[string(expectedEv.Hash())]
  78. assert.Equal(t, expectedEv, gotEv,
  79. fmt.Sprintf("evidence at index %d on reactor %d don't match: %v vs %v",
  80. i, reactorIdx, expectedEv, gotEv))
  81. }
  82. wg.Done()
  83. }
  84. func sendEvidence(t *testing.T, evpool *EvidencePool, valAddr []byte, n int) types.EvidenceList {
  85. evList := make([]types.Evidence, n)
  86. for i := 0; i < n; i++ {
  87. ev := types.NewMockGoodEvidence(int64(i+1), 0, valAddr)
  88. err := evpool.AddEvidence(ev)
  89. assert.Nil(t, err)
  90. evList[i] = ev
  91. }
  92. return evList
  93. }
  94. var (
  95. NUM_EVIDENCE = 10
  96. TIMEOUT = 120 * time.Second // ridiculously high because CircleCI is slow
  97. )
  98. func TestReactorBroadcastEvidence(t *testing.T) {
  99. config := cfg.TestConfig()
  100. N := 7
  101. // create statedb for everyone
  102. stateDBs := make([]dbm.DB, N)
  103. valAddr := []byte("myval")
  104. // we need validators saved for heights at least as high as we have evidence for
  105. height := int64(NUM_EVIDENCE) + 10
  106. for i := 0; i < N; i++ {
  107. stateDBs[i] = initializeValidatorState(valAddr, height)
  108. }
  109. // make reactors from statedb
  110. reactors := makeAndConnectEvidenceReactors(config, stateDBs)
  111. // set the peer height on each reactor
  112. for _, r := range reactors {
  113. for _, peer := range r.Switch.Peers().List() {
  114. ps := peerState{height}
  115. peer.Set(types.PeerStateKey, ps)
  116. }
  117. }
  118. // send a bunch of valid evidence to the first reactor's evpool
  119. // and wait for them all to be received in the others
  120. evList := sendEvidence(t, reactors[0].evpool, valAddr, NUM_EVIDENCE)
  121. waitForEvidence(t, evList, reactors)
  122. }
  123. type peerState struct {
  124. height int64
  125. }
  126. func (ps peerState) GetHeight() int64 {
  127. return ps.height
  128. }
  129. func TestReactorSelectiveBroadcast(t *testing.T) {
  130. config := cfg.TestConfig()
  131. valAddr := []byte("myval")
  132. height1 := int64(NUM_EVIDENCE) + 10
  133. height2 := int64(NUM_EVIDENCE) / 2
  134. // DB1 is ahead of DB2
  135. stateDB1 := initializeValidatorState(valAddr, height1)
  136. stateDB2 := initializeValidatorState(valAddr, height2)
  137. // make reactors from statedb
  138. reactors := makeAndConnectEvidenceReactors(config, []dbm.DB{stateDB1, stateDB2})
  139. peer := reactors[0].Switch.Peers().List()[0]
  140. ps := peerState{height2}
  141. peer.Set(types.PeerStateKey, ps)
  142. // send a bunch of valid evidence to the first reactor's evpool
  143. evList := sendEvidence(t, reactors[0].evpool, valAddr, NUM_EVIDENCE)
  144. // only ones less than the peers height should make it through
  145. waitForEvidence(t, evList[:NUM_EVIDENCE/2], reactors[1:2])
  146. // peers should still be connected
  147. peers := reactors[1].Switch.Peers().List()
  148. assert.Equal(t, 1, len(peers))
  149. }