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.

80 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/tendermint/libs/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. NextValidators: valSet.CopyIncrementAccum(1),
  25. LastHeightValidatorsChanged: 1,
  26. ConsensusParams: types.ConsensusParams{
  27. EvidenceParams: types.EvidenceParams{
  28. MaxAge: 1000000,
  29. },
  30. },
  31. }
  32. // save all states up to height
  33. for i := int64(0); i < height; i++ {
  34. state.LastBlockHeight = i
  35. sm.SaveState(stateDB, state)
  36. }
  37. return stateDB
  38. }
  39. func TestEvidencePool(t *testing.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 := types.NewMockGoodEvidence(height, 0, valAddr)
  46. badEvidence := types.MockBadEvidence{goodEvidence}
  47. // bad evidence
  48. err := pool.AddEvidence(badEvidence)
  49. assert.NotNil(t, err)
  50. var wg sync.WaitGroup
  51. wg.Add(1)
  52. go func() {
  53. <-pool.EvidenceWaitChan()
  54. wg.Done()
  55. }()
  56. err = pool.AddEvidence(goodEvidence)
  57. assert.Nil(t, err)
  58. wg.Wait()
  59. assert.Equal(t, 1, pool.evidenceList.Len())
  60. // if we send it again, it shouldnt change the size
  61. err = pool.AddEvidence(goodEvidence)
  62. assert.Nil(t, err)
  63. assert.Equal(t, 1, pool.evidenceList.Len())
  64. }