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.

107 lines
2.5 KiB

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