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.

127 lines
3.4 KiB

  1. package evidence
  2. import (
  3. "fmt"
  4. "sync"
  5. "testing"
  6. "time"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/go-kit/kit/log/term"
  9. dbm "github.com/tendermint/tmlibs/db"
  10. "github.com/tendermint/tmlibs/log"
  11. cfg "github.com/tendermint/tendermint/config"
  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, N int) []*EvidenceReactor {
  29. reactors := make([]*EvidenceReactor, N)
  30. logger := evidenceLogger()
  31. for i := 0; i < N; i++ {
  32. params := types.EvidenceParams{}
  33. store := NewEvidenceStore(dbm.NewMemDB())
  34. state := mockState{}
  35. pool := NewEvidencePool(params, store, state)
  36. reactors[i] = NewEvidenceReactor(pool)
  37. reactors[i].SetLogger(logger.With("validator", i))
  38. }
  39. p2p.MakeConnectedSwitches(config.P2P, N, func(i int, s *p2p.Switch) *p2p.Switch {
  40. s.AddReactor("EVIDENCE", reactors[i])
  41. return s
  42. }, p2p.Connect2Switches)
  43. return reactors
  44. }
  45. // wait for all evidence on all reactors
  46. func waitForEvidence(t *testing.T, evs types.EvidenceList, reactors []*EvidenceReactor) {
  47. // wait for the evidence in all evpools
  48. wg := new(sync.WaitGroup)
  49. for i := 0; i < len(reactors); i++ {
  50. wg.Add(1)
  51. go _waitForEvidence(t, wg, evs, i, reactors)
  52. }
  53. done := make(chan struct{})
  54. go func() {
  55. wg.Wait()
  56. close(done)
  57. }()
  58. timer := time.After(TIMEOUT)
  59. select {
  60. case <-timer:
  61. t.Fatal("Timed out waiting for evidence")
  62. case <-done:
  63. }
  64. }
  65. // wait for all evidence on a single evpool
  66. func _waitForEvidence(t *testing.T, wg *sync.WaitGroup, evs types.EvidenceList, reactorIdx int, reactors []*EvidenceReactor) {
  67. evpool := reactors[reactorIdx].evpool
  68. for len(evpool.PendingEvidence()) != len(evs) {
  69. time.Sleep(time.Millisecond * 100)
  70. }
  71. reapedEv := evpool.PendingEvidence()
  72. // put the reaped evidence is a map so we can quickly check we got everything
  73. evMap := make(map[string]types.Evidence)
  74. for _, e := range reapedEv {
  75. evMap[string(e.Hash())] = e
  76. }
  77. for i, expectedEv := range evs {
  78. gotEv := evMap[string(expectedEv.Hash())]
  79. assert.Equal(t, expectedEv, gotEv,
  80. fmt.Sprintf("evidence at index %d on reactor %d don't match: %v vs %v",
  81. i, reactorIdx, expectedEv, gotEv))
  82. }
  83. wg.Done()
  84. }
  85. func sendEvidence(t *testing.T, evpool *EvidencePool, n int) types.EvidenceList {
  86. evList := make([]types.Evidence, n)
  87. for i := 0; i < n; i++ {
  88. ev := newMockGoodEvidence(int64(i), 2, []byte("val"))
  89. err := evpool.AddEvidence(ev)
  90. assert.Nil(t, err)
  91. evList[i] = ev
  92. }
  93. return evList
  94. }
  95. var (
  96. NUM_EVIDENCE = 1000
  97. TIMEOUT = 120 * time.Second // ridiculously high because CircleCI is slow
  98. )
  99. func TestReactorBroadcastEvidence(t *testing.T) {
  100. config := cfg.TestConfig()
  101. N := 7
  102. reactors := makeAndConnectEvidenceReactors(config, N)
  103. // send a bunch of evidence to the first reactor's evpool
  104. // and wait for them all to be received in the others
  105. evList := sendEvidence(t, reactors[0].evpool, NUM_EVIDENCE)
  106. waitForEvidence(t, evList, reactors)
  107. }