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.

150 lines
3.8 KiB

7 years ago
  1. package evidence
  2. import (
  3. "os"
  4. "sync"
  5. "testing"
  6. "time"
  7. "github.com/stretchr/testify/assert"
  8. sm "github.com/tendermint/tendermint/state"
  9. "github.com/tendermint/tendermint/types"
  10. tmtime "github.com/tendermint/tendermint/types/time"
  11. dbm "github.com/tendermint/tm-db"
  12. )
  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.CopyIncrementProposerPriority(1),
  31. LastHeightValidatorsChanged: 1,
  32. ConsensusParams: types.ConsensusParams{
  33. Evidence: types.EvidenceParams{
  34. MaxAgeNumBlocks: 10000,
  35. MaxAgeDuration: 48 * time.Hour,
  36. },
  37. },
  38. }
  39. // save all states up to height
  40. for i := int64(0); i < height; i++ {
  41. state.LastBlockHeight = i
  42. sm.SaveState(stateDB, state)
  43. }
  44. return stateDB
  45. }
  46. func TestEvidencePool(t *testing.T) {
  47. var (
  48. valAddr = []byte("val1")
  49. height = int64(5)
  50. stateDB = initializeValidatorState(valAddr, height)
  51. evidenceDB = dbm.NewMemDB()
  52. pool = NewPool(stateDB, evidenceDB)
  53. evidenceTime = time.Date(2019, 1, 1, 0, 0, 0, 0, time.UTC)
  54. )
  55. goodEvidence := types.NewMockEvidence(height, time.Now(), 0, valAddr)
  56. badEvidence := types.NewMockEvidence(height, evidenceTime, 0, valAddr)
  57. // bad evidence
  58. err := pool.AddEvidence(badEvidence)
  59. assert.NotNil(t, err)
  60. // err: evidence created at 2019-01-01 00:00:00 +0000 UTC has expired. Evidence can not be older than: ...
  61. var wg sync.WaitGroup
  62. wg.Add(1)
  63. go func() {
  64. <-pool.EvidenceWaitChan()
  65. wg.Done()
  66. }()
  67. err = pool.AddEvidence(goodEvidence)
  68. assert.Nil(t, err)
  69. wg.Wait()
  70. assert.Equal(t, 1, pool.evidenceList.Len())
  71. // if we send it again, it shouldnt change the size
  72. err = pool.AddEvidence(goodEvidence)
  73. assert.Nil(t, err)
  74. assert.Equal(t, 1, pool.evidenceList.Len())
  75. }
  76. func TestEvidencePoolIsCommitted(t *testing.T) {
  77. // Initialization:
  78. var (
  79. valAddr = []byte("validator_address")
  80. height = int64(42)
  81. lastBlockTime = time.Now()
  82. stateDB = initializeValidatorState(valAddr, height)
  83. evidenceDB = dbm.NewMemDB()
  84. pool = NewPool(stateDB, evidenceDB)
  85. )
  86. // evidence not seen yet:
  87. evidence := types.NewMockEvidence(height, time.Now(), 0, valAddr)
  88. assert.False(t, pool.IsCommitted(evidence))
  89. // evidence seen but not yet committed:
  90. assert.NoError(t, pool.AddEvidence(evidence))
  91. assert.False(t, pool.IsCommitted(evidence))
  92. // evidence seen and committed:
  93. pool.MarkEvidenceAsCommitted(height, lastBlockTime, []types.Evidence{evidence})
  94. assert.True(t, pool.IsCommitted(evidence))
  95. }
  96. func TestAddEvidence(t *testing.T) {
  97. var (
  98. valAddr = []byte("val1")
  99. height = int64(100002)
  100. stateDB = initializeValidatorState(valAddr, height)
  101. evidenceDB = dbm.NewMemDB()
  102. pool = NewPool(stateDB, evidenceDB)
  103. evidenceTime = time.Date(2019, 1, 1, 0, 0, 0, 0, time.UTC)
  104. )
  105. testCases := []struct {
  106. evHeight int64
  107. evTime time.Time
  108. expErr bool
  109. evDescription string
  110. }{
  111. {height, time.Now(), false, "valid evidence"},
  112. {height, evidenceTime, true, "evidence created at 2019-01-01 00:00:00 +0000 UTC has expired"},
  113. {int64(1), time.Now(), true, "evidence from height 1 is too old"},
  114. {int64(1), evidenceTime, true,
  115. "evidence from height 1 is too old & evidence created at 2019-01-01 00:00:00 +0000 UTC has expired"},
  116. }
  117. for _, tc := range testCases {
  118. tc := tc
  119. ev := types.NewMockEvidence(tc.evHeight, tc.evTime, 0, valAddr)
  120. err := pool.AddEvidence(ev)
  121. if tc.expErr {
  122. assert.Error(t, err)
  123. }
  124. }
  125. }