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.

81 lines
1.7 KiB

7 years ago
7 years ago
  1. package evidence
  2. import (
  3. "sync"
  4. "testing"
  5. "time"
  6. "github.com/stretchr/testify/assert"
  7. sm "github.com/tendermint/tendermint/state"
  8. "github.com/tendermint/tendermint/types"
  9. dbm "github.com/tendermint/tmlibs/db"
  10. )
  11. var mockState = sm.State{}
  12. func initializeValidatorState(valAddr []byte, height int64) dbm.DB {
  13. stateDB := dbm.NewMemDB()
  14. // create validator set and state
  15. valSet := &types.ValidatorSet{
  16. Validators: []*types.Validator{
  17. {Address: valAddr},
  18. },
  19. }
  20. state := sm.State{
  21. LastBlockHeight: 0,
  22. LastBlockTime: time.Now(),
  23. Validators: valSet,
  24. LastHeightValidatorsChanged: 1,
  25. ConsensusParams: types.ConsensusParams{
  26. EvidenceParams: types.EvidenceParams{
  27. MaxAge: 1000000,
  28. },
  29. },
  30. }
  31. // save all states up to height
  32. for i := int64(0); i < height; i++ {
  33. state.LastBlockHeight = i
  34. sm.SaveState(stateDB, state)
  35. }
  36. return stateDB
  37. }
  38. func TestEvidencePool(t *testing.T) {
  39. assert := assert.New(t)
  40. valAddr := []byte("val1")
  41. height := int64(5)
  42. stateDB := initializeValidatorState(valAddr, height)
  43. store := NewEvidenceStore(dbm.NewMemDB())
  44. pool := NewEvidencePool(stateDB, store)
  45. goodEvidence := newMockGoodEvidence(height, 0, valAddr)
  46. badEvidence := MockBadEvidence{goodEvidence}
  47. err := pool.AddEvidence(badEvidence)
  48. assert.NotNil(err)
  49. var wg sync.WaitGroup
  50. wg.Add(1)
  51. go func() {
  52. <-pool.EvidenceChan()
  53. wg.Done()
  54. }()
  55. err = pool.AddEvidence(goodEvidence)
  56. assert.Nil(err)
  57. wg.Wait()
  58. // if we send it again it wont fire on the chan
  59. err = pool.AddEvidence(goodEvidence)
  60. assert.Nil(err)
  61. select {
  62. case <-pool.EvidenceChan():
  63. t.Fatal("unexpected read on EvidenceChan")
  64. default:
  65. }
  66. }