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.

166 lines
4.4 KiB

  1. package evpool
  2. import (
  3. "bytes"
  4. "fmt"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. wire "github.com/tendermint/go-wire"
  8. "github.com/tendermint/tendermint/types"
  9. dbm "github.com/tendermint/tmlibs/db"
  10. )
  11. //-------------------------------------------
  12. func TestStoreAddDuplicate(t *testing.T) {
  13. assert := assert.New(t)
  14. db := dbm.NewMemDB()
  15. store := NewEvidenceStore(db)
  16. priority := 10
  17. ev := newMockGoodEvidence(2, 1, []byte("val1"))
  18. added, err := store.AddNewEvidence(ev, priority)
  19. assert.Nil(err)
  20. assert.True(added)
  21. // cant add twice
  22. added, err = store.AddNewEvidence(ev, priority)
  23. assert.Nil(err)
  24. assert.False(added)
  25. }
  26. func TestStoreMark(t *testing.T) {
  27. assert := assert.New(t)
  28. db := dbm.NewMemDB()
  29. store := NewEvidenceStore(db)
  30. // before we do anything, priority/pending are empty
  31. priorityEv := store.PriorityEvidence()
  32. pendingEv := store.PendingEvidence()
  33. assert.Equal(0, len(priorityEv))
  34. assert.Equal(0, len(pendingEv))
  35. priority := 10
  36. ev := newMockGoodEvidence(2, 1, []byte("val1"))
  37. added, err := store.AddNewEvidence(ev, priority)
  38. assert.Nil(err)
  39. assert.True(added)
  40. // get the evidence. verify. should be uncommitted
  41. ei := store.GetEvidence(ev.Height(), ev.Hash())
  42. assert.Equal(ev, ei.Evidence)
  43. assert.Equal(priority, ei.Priority)
  44. assert.False(ei.Committed)
  45. // new evidence should be returns in priority/pending
  46. priorityEv = store.PriorityEvidence()
  47. pendingEv = store.PendingEvidence()
  48. assert.Equal(1, len(priorityEv))
  49. assert.Equal(1, len(pendingEv))
  50. // priority is now empty
  51. store.MarkEvidenceAsBroadcasted(ev)
  52. priorityEv = store.PriorityEvidence()
  53. pendingEv = store.PendingEvidence()
  54. assert.Equal(0, len(priorityEv))
  55. assert.Equal(1, len(pendingEv))
  56. // priority and pending are now empty
  57. store.MarkEvidenceAsCommitted(ev)
  58. priorityEv = store.PriorityEvidence()
  59. pendingEv = store.PendingEvidence()
  60. assert.Equal(0, len(priorityEv))
  61. assert.Equal(0, len(pendingEv))
  62. // evidence should show committed
  63. ei = store.GetEvidence(ev.Height(), ev.Hash())
  64. assert.Equal(ev, ei.Evidence)
  65. assert.Equal(priority, ei.Priority)
  66. assert.True(ei.Committed)
  67. }
  68. func TestStorePriority(t *testing.T) {
  69. assert := assert.New(t)
  70. db := dbm.NewMemDB()
  71. store := NewEvidenceStore(db)
  72. // sorted by priority and then height
  73. cases := []struct {
  74. ev MockGoodEvidence
  75. priority int
  76. }{
  77. {newMockGoodEvidence(2, 1, []byte("val1")), 17},
  78. {newMockGoodEvidence(5, 2, []byte("val2")), 15},
  79. {newMockGoodEvidence(10, 2, []byte("val2")), 13},
  80. {newMockGoodEvidence(100, 2, []byte("val2")), 11},
  81. {newMockGoodEvidence(90, 2, []byte("val2")), 11},
  82. {newMockGoodEvidence(80, 2, []byte("val2")), 11},
  83. }
  84. for _, c := range cases {
  85. added, err := store.AddNewEvidence(c.ev, c.priority)
  86. assert.True(added)
  87. assert.Nil(err)
  88. }
  89. evList := store.PriorityEvidence()
  90. for i, ev := range evList {
  91. assert.Equal(ev, cases[i].ev)
  92. }
  93. }
  94. //-------------------------------------------
  95. const (
  96. evidenceTypeMock = byte(0x01)
  97. )
  98. var _ = wire.RegisterInterface(
  99. struct{ types.Evidence }{},
  100. wire.ConcreteType{MockGoodEvidence{}, evidenceTypeMock},
  101. )
  102. type MockGoodEvidence struct {
  103. Height_ int
  104. Address_ []byte
  105. Index_ int
  106. }
  107. func newMockGoodEvidence(height, index int, address []byte) MockGoodEvidence {
  108. return MockGoodEvidence{height, address, index}
  109. }
  110. func (e MockGoodEvidence) Height() int { return e.Height_ }
  111. func (e MockGoodEvidence) Address() []byte { return e.Address_ }
  112. func (e MockGoodEvidence) Index() int { return e.Index_ }
  113. func (e MockGoodEvidence) Hash() []byte { return []byte{byte(e.Index_)} }
  114. func (e MockGoodEvidence) Verify(chainID string) error { return nil }
  115. func (e MockGoodEvidence) Equal(ev types.Evidence) bool {
  116. e2 := ev.(MockGoodEvidence)
  117. return e.Height_ == e2.Height_ &&
  118. bytes.Equal(e.Address_, e2.Address_) &&
  119. e.Index_ == e2.Index_
  120. }
  121. func (e MockGoodEvidence) String() string {
  122. return fmt.Sprintf("GoodEvidence: %d/%s/%d", e.Height_, e.Address_, e.Index_)
  123. }
  124. type MockBadEvidence struct {
  125. MockGoodEvidence
  126. }
  127. func (e MockBadEvidence) Verify(chainID string) error { return fmt.Errorf("MockBadEvidence") }
  128. func (e MockBadEvidence) Equal(ev types.Evidence) bool {
  129. e2 := ev.(MockBadEvidence)
  130. return e.Height_ == e2.Height_ &&
  131. bytes.Equal(e.Address_, e2.Address_) &&
  132. e.Index_ == e2.Index_
  133. }
  134. func (e MockBadEvidence) String() string {
  135. return fmt.Sprintf("BadEvidence: %d/%s/%d", e.Height_, e.Address_, e.Index_)
  136. }