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.

137 lines
3.7 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, 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()) != len(evs) {
  68. time.Sleep(time.Millisecond * 100)
  69. }
  70. reapedEv := evpool.PendingEvidence()
  71. // put the reaped evidence is 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 = 1
  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. // send a bunch of valid evidence to the first reactor's evpool
  112. // and wait for them all to be received in the others
  113. evList := sendEvidence(t, reactors[0].evpool, valAddr, NUM_EVIDENCE)
  114. waitForEvidence(t, evList, reactors)
  115. }