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.

122 lines
3.1 KiB

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