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.

109 lines
2.8 KiB

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