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.

88 lines
1.9 KiB

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