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.

79 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. valAddr := []byte("val1")
  40. height := int64(5)
  41. stateDB := initializeValidatorState(valAddr, height)
  42. store := NewEvidenceStore(dbm.NewMemDB())
  43. pool := NewEvidencePool(stateDB, store)
  44. goodEvidence := types.NewMockGoodEvidence(height, 0, valAddr)
  45. badEvidence := types.MockBadEvidence{goodEvidence}
  46. // bad evidence
  47. err := pool.AddEvidence(badEvidence)
  48. assert.NotNil(t, err)
  49. var wg sync.WaitGroup
  50. wg.Add(1)
  51. go func() {
  52. <-pool.EvidenceWaitChan()
  53. wg.Done()
  54. }()
  55. err = pool.AddEvidence(goodEvidence)
  56. assert.Nil(t, err)
  57. wg.Wait()
  58. assert.Equal(t, 1, pool.evidenceList.Len())
  59. // if we send it again, it shouldnt change the size
  60. err = pool.AddEvidence(goodEvidence)
  61. assert.Nil(t, err)
  62. assert.Equal(t, 1, pool.evidenceList.Len())
  63. }