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.

208 lines
5.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. "github.com/tendermint/tendermint/crypto/secp256k1"
  11. dbm "github.com/tendermint/tendermint/libs/db"
  12. "github.com/tendermint/tendermint/libs/log"
  13. "github.com/tendermint/tendermint/p2p"
  14. "github.com/tendermint/tendermint/types"
  15. )
  16. // evidenceLogger is a TestingLogger which uses a different
  17. // color for each validator ("validator" key must exist).
  18. func evidenceLogger() log.Logger {
  19. return log.TestingLoggerWithColorFn(func(keyvals ...interface{}) term.FgBgColor {
  20. for i := 0; i < len(keyvals)-1; i += 2 {
  21. if keyvals[i] == "validator" {
  22. return term.FgBgColor{Fg: term.Color(uint8(keyvals[i+1].(int) + 1))}
  23. }
  24. }
  25. return term.FgBgColor{}
  26. })
  27. }
  28. // connect N evidence reactors through N switches
  29. func makeAndConnectEvidenceReactors(config *cfg.Config, stateDBs []dbm.DB) []*EvidenceReactor {
  30. N := len(stateDBs)
  31. reactors := make([]*EvidenceReactor, N)
  32. logger := evidenceLogger()
  33. for i := 0; i < N; i++ {
  34. store := NewEvidenceStore(dbm.NewMemDB())
  35. pool := NewEvidencePool(stateDBs[i], store)
  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(-1)) != len(evs) {
  69. time.Sleep(time.Millisecond * 100)
  70. }
  71. reapedEv := evpool.PendingEvidence(-1)
  72. // put the reaped evidence in 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, valAddr []byte, n int) types.EvidenceList {
  86. evList := make([]types.Evidence, n)
  87. for i := 0; i < n; i++ {
  88. ev := types.NewMockGoodEvidence(int64(i+1), 0, valAddr)
  89. err := evpool.AddEvidence(ev)
  90. assert.Nil(t, err)
  91. evList[i] = ev
  92. }
  93. return evList
  94. }
  95. var (
  96. NUM_EVIDENCE = 10
  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. // create statedb for everyone
  103. stateDBs := make([]dbm.DB, N)
  104. valAddr := []byte("myval")
  105. // we need validators saved for heights at least as high as we have evidence for
  106. height := int64(NUM_EVIDENCE) + 10
  107. for i := 0; i < N; i++ {
  108. stateDBs[i] = initializeValidatorState(valAddr, height)
  109. }
  110. // make reactors from statedb
  111. reactors := makeAndConnectEvidenceReactors(config, stateDBs)
  112. // set the peer height on each reactor
  113. for _, r := range reactors {
  114. for _, peer := range r.Switch.Peers().List() {
  115. ps := peerState{height}
  116. peer.Set(types.PeerStateKey, ps)
  117. }
  118. }
  119. // send a bunch of valid evidence to the first reactor's evpool
  120. // and wait for them all to be received in the others
  121. evList := sendEvidence(t, reactors[0].evpool, valAddr, NUM_EVIDENCE)
  122. waitForEvidence(t, evList, reactors)
  123. }
  124. type peerState struct {
  125. height int64
  126. }
  127. func (ps peerState) GetHeight() int64 {
  128. return ps.height
  129. }
  130. func TestReactorSelectiveBroadcast(t *testing.T) {
  131. config := cfg.TestConfig()
  132. valAddr := []byte("myval")
  133. height1 := int64(NUM_EVIDENCE) + 10
  134. height2 := int64(NUM_EVIDENCE) / 2
  135. // DB1 is ahead of DB2
  136. stateDB1 := initializeValidatorState(valAddr, height1)
  137. stateDB2 := initializeValidatorState(valAddr, height2)
  138. // make reactors from statedb
  139. reactors := makeAndConnectEvidenceReactors(config, []dbm.DB{stateDB1, stateDB2})
  140. peer := reactors[0].Switch.Peers().List()[0]
  141. ps := peerState{height2}
  142. peer.Set(types.PeerStateKey, ps)
  143. // send a bunch of valid evidence to the first reactor's evpool
  144. evList := sendEvidence(t, reactors[0].evpool, valAddr, NUM_EVIDENCE)
  145. // only ones less than the peers height should make it through
  146. waitForEvidence(t, evList[:NUM_EVIDENCE/2], reactors[1:2])
  147. // peers should still be connected
  148. peers := reactors[1].Switch.Peers().List()
  149. assert.Equal(t, 1, len(peers))
  150. }
  151. func TestEvidenceListMessageValidationBasic(t *testing.T) {
  152. testCases := []struct {
  153. testName string
  154. malleateEvListMsg func(*EvidenceListMessage)
  155. expectErr bool
  156. }{
  157. {"Good EvidenceListMessage", func(evList *EvidenceListMessage) {}, false},
  158. {"Invalid EvidenceListMessage", func(evList *EvidenceListMessage) {
  159. evList.Evidence = append(evList.Evidence,
  160. &types.DuplicateVoteEvidence{PubKey: secp256k1.GenPrivKey().PubKey()})
  161. }, true},
  162. }
  163. for _, tc := range testCases {
  164. t.Run(tc.testName, func(t *testing.T) {
  165. evListMsg := &EvidenceListMessage{}
  166. n := 3
  167. valAddr := []byte("myval")
  168. evListMsg.Evidence = make([]types.Evidence, n)
  169. for i := 0; i < n; i++ {
  170. evListMsg.Evidence[i] = types.NewMockGoodEvidence(int64(i+1), 0, valAddr)
  171. }
  172. tc.malleateEvListMsg(evListMsg)
  173. assert.Equal(t, tc.expectErr, evListMsg.ValidateBasic() != nil, "Validate Basic had an unexpected result")
  174. })
  175. }
  176. }