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.

125 lines
3.1 KiB

  1. package evidence
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. "github.com/tendermint/tendermint/types"
  6. dbm "github.com/tendermint/tm-db"
  7. )
  8. //-------------------------------------------
  9. func TestStoreAddDuplicate(t *testing.T) {
  10. assert := assert.New(t)
  11. db := dbm.NewMemDB()
  12. store := NewEvidenceStore(db)
  13. priority := int64(10)
  14. ev := types.NewMockGoodEvidence(2, 1, []byte("val1"))
  15. added := store.AddNewEvidence(ev, priority)
  16. assert.True(added)
  17. // cant add twice
  18. added = store.AddNewEvidence(ev, priority)
  19. assert.False(added)
  20. }
  21. func TestStoreCommitDuplicate(t *testing.T) {
  22. assert := assert.New(t)
  23. db := dbm.NewMemDB()
  24. store := NewEvidenceStore(db)
  25. priority := int64(10)
  26. ev := types.NewMockGoodEvidence(2, 1, []byte("val1"))
  27. store.MarkEvidenceAsCommitted(ev)
  28. added := store.AddNewEvidence(ev, priority)
  29. assert.False(added)
  30. }
  31. func TestStoreMark(t *testing.T) {
  32. assert := assert.New(t)
  33. db := dbm.NewMemDB()
  34. store := NewEvidenceStore(db)
  35. // before we do anything, priority/pending are empty
  36. priorityEv := store.PriorityEvidence()
  37. pendingEv := store.PendingEvidence(-1)
  38. assert.Equal(0, len(priorityEv))
  39. assert.Equal(0, len(pendingEv))
  40. priority := int64(10)
  41. ev := types.NewMockGoodEvidence(2, 1, []byte("val1"))
  42. added := store.AddNewEvidence(ev, priority)
  43. assert.True(added)
  44. // get the evidence. verify. should be uncommitted
  45. ei := store.GetEvidenceInfo(ev.Height(), ev.Hash())
  46. assert.Equal(ev, ei.Evidence)
  47. assert.Equal(priority, ei.Priority)
  48. assert.False(ei.Committed)
  49. // new evidence should be returns in priority/pending
  50. priorityEv = store.PriorityEvidence()
  51. pendingEv = store.PendingEvidence(-1)
  52. assert.Equal(1, len(priorityEv))
  53. assert.Equal(1, len(pendingEv))
  54. // priority is now empty
  55. store.MarkEvidenceAsBroadcasted(ev)
  56. priorityEv = store.PriorityEvidence()
  57. pendingEv = store.PendingEvidence(-1)
  58. assert.Equal(0, len(priorityEv))
  59. assert.Equal(1, len(pendingEv))
  60. // priority and pending are now empty
  61. store.MarkEvidenceAsCommitted(ev)
  62. priorityEv = store.PriorityEvidence()
  63. pendingEv = store.PendingEvidence(-1)
  64. assert.Equal(0, len(priorityEv))
  65. assert.Equal(0, len(pendingEv))
  66. // evidence should show committed
  67. newPriority := int64(0)
  68. ei = store.GetEvidenceInfo(ev.Height(), ev.Hash())
  69. assert.Equal(ev, ei.Evidence)
  70. assert.Equal(newPriority, ei.Priority)
  71. assert.True(ei.Committed)
  72. }
  73. func TestStorePriority(t *testing.T) {
  74. assert := assert.New(t)
  75. db := dbm.NewMemDB()
  76. store := NewEvidenceStore(db)
  77. // sorted by priority and then height
  78. cases := []struct {
  79. ev types.MockGoodEvidence
  80. priority int64
  81. }{
  82. {types.NewMockGoodEvidence(2, 1, []byte("val1")), 17},
  83. {types.NewMockGoodEvidence(5, 2, []byte("val2")), 15},
  84. {types.NewMockGoodEvidence(10, 2, []byte("val2")), 13},
  85. {types.NewMockGoodEvidence(100, 2, []byte("val2")), 11},
  86. {types.NewMockGoodEvidence(90, 2, []byte("val2")), 11},
  87. {types.NewMockGoodEvidence(80, 2, []byte("val2")), 11},
  88. }
  89. for _, c := range cases {
  90. added := store.AddNewEvidence(c.ev, c.priority)
  91. assert.True(added)
  92. }
  93. evList := store.PriorityEvidence()
  94. for i, ev := range evList {
  95. assert.Equal(ev, cases[i].ev)
  96. }
  97. }