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
lint: Enable Golint (#4212) * Fix many golint errors * Fix golint errors in the 'lite' package * Don't export Pool.store * Fix typo * Revert unwanted changes * Fix errors in counter package * Fix linter errors in kvstore package * Fix linter error in example package * Fix error in tests package * Fix linter errors in v2 package * Fix linter errors in consensus package * Fix linter errors in evidence package * Fix linter error in fail package * Fix linter errors in query package * Fix linter errors in core package * Fix linter errors in node package * Fix linter errors in mempool package * Fix linter error in conn package * Fix linter errors in pex package * Rename PEXReactor export to Reactor * Fix linter errors in trust package * Fix linter errors in upnp package * Fix linter errors in p2p package * Fix linter errors in proxy package * Fix linter errors in mock_test package * Fix linter error in client_test package * Fix linter errors in coretypes package * Fix linter errors in coregrpc package * Fix linter errors in rpcserver package * Fix linter errors in rpctypes package * Fix linter errors in rpctest package * Fix linter error in json2wal script * Fix linter error in wal2json script * Fix linter errors in kv package * Fix linter error in state package * Fix linter error in grpc_client * Fix linter errors in types package * Fix linter error in version package * Fix remaining errors * Address review comments * Fix broken tests * Reconcile package coregrpc * Fix golangci bot error * Fix new golint errors * Fix broken reference * Enable golint linter * minor changes to bring golint into line * fix failing test * fix pex reactor naming * address PR comments
5 years ago
lint: Enable Golint (#4212) * Fix many golint errors * Fix golint errors in the 'lite' package * Don't export Pool.store * Fix typo * Revert unwanted changes * Fix errors in counter package * Fix linter errors in kvstore package * Fix linter error in example package * Fix error in tests package * Fix linter errors in v2 package * Fix linter errors in consensus package * Fix linter errors in evidence package * Fix linter error in fail package * Fix linter errors in query package * Fix linter errors in core package * Fix linter errors in node package * Fix linter errors in mempool package * Fix linter error in conn package * Fix linter errors in pex package * Rename PEXReactor export to Reactor * Fix linter errors in trust package * Fix linter errors in upnp package * Fix linter errors in p2p package * Fix linter errors in proxy package * Fix linter errors in mock_test package * Fix linter error in client_test package * Fix linter errors in coretypes package * Fix linter errors in coregrpc package * Fix linter errors in rpcserver package * Fix linter errors in rpctypes package * Fix linter errors in rpctest package * Fix linter error in json2wal script * Fix linter error in wal2json script * Fix linter errors in kv package * Fix linter error in state package * Fix linter error in grpc_client * Fix linter errors in types package * Fix linter error in version package * Fix remaining errors * Address review comments * Fix broken tests * Reconcile package coregrpc * Fix golangci bot error * Fix new golint errors * Fix broken reference * Enable golint linter * minor changes to bring golint into line * fix failing test * fix pex reactor naming * address PR comments
5 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-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 := NewPool(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 := NewPool(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. }